source: http://linoxide.com/linux-shell-script/shell-script-check-linux-system-health/

This article introduces a shell script to perform linux system health check.

This script collects system information and status like hostname, kernel version, uptime, cpu / memory / disk usage.
Script uses:
hostname, uptime, who, mpstat, lscpu, ps, top, df, free, bc commands to get system information
and cut, grep, awk and sed for text processing.

The output of the script is a text file which will be generated in the current directory.

A variable is set to provide email address to which script can send report file.

Apart from system status, the script will check a predefined threshold for cpu load and filesystem size.

Remember : Make sure you have all the above commands working, to output all results correctly.

 #!/bin/bash
EMAIL=''
function sysstat {
echo -e "
#####################################################################
Health Check Report (CPU,Process,Disk Usage, Memory)
##################################################################### Hostname : `hostname`
Kernel Version : `uname -r`
Uptime : `uptime | sed 's/.*up \([^,]*\), .*/\1/'`
Last Reboot Time : `who -b | awk '{print $3,$4}'` *********************************************************************
CPU Load - > Threshold < Normal > Caution , > Unhealthy
*********************************************************************
"
MPSTAT=`which mpstat`
MPSTAT=$?
if [ $MPSTAT != ]
then
echo "Please install mpstat!"
echo "On Debian based systems:"
echo "sudo apt-get install sysstat"
echo "On RHEL based systems:"
echo "yum install sysstat"
else
echo -e ""
LSCPU=`which lscpu`
LSCPU=$?
if [ $LSCPU != ]
then
RESULT=$RESULT" lscpu required to producre acqurate reults"
else
cpus=`lscpu | grep -e "^CPU(s):" | cut -f2 -d: | awk '{print $1}'`
i=
while [ $i -lt $cpus ]
do
echo "CPU$i : `mpstat -P ALL | awk -v var=$i '{ if ($3 == var ) print $4 }' `"
let i=$i+
done
fi
echo -e "
Load Average : `uptime | awk -F'load average:' '{ print $2 }' | cut -f1 -d,` Heath Status : `uptime | awk -F'load average:' '{ print $2 }' | cut -f1 -d, | awk '{if ($1 > 2) print "Unhealthy"; else if ($1 > 1) print "Caution"; else print "Normal"}'`
"
fi
echo -e "
*********************************************************************
Process
********************************************************************* => Top memory using processs/application PID %MEM RSS COMMAND
`ps aux | awk '{print $2, $4, $6, $11}' | sort -k3rn | head -n ` => Top CPU using process/application
`top b -n1 | head - | tail -` *********************************************************************
Disk Usage - > Threshold < Normal > % Caution > Unhealthy
*********************************************************************
"
df -Pkh | grep -v 'Filesystem' > /tmp/df.status
while read DISK
do
LINE=`echo $DISK | awk '{print $1,"\t",$6,"\t",$5," used","\t",$4," free space"}'`
echo -e $LINE
echo
done < /tmp/df.status
echo -e " Heath Status"
echo
while read DISK
do
USAGE=`echo $DISK | awk '{print $5}' | cut -f1 -d%`
if [ $USAGE -ge ]
then
STATUS='Unhealty'
elif [ $USAGE -ge ]
then
STATUS='Caution'
else
STATUS='Normal'
fi LINE=`echo $DISK | awk '{print $1,"\t",$6}'`
echo -ne $LINE "\t\t" $STATUS
echo
done < /tmp/df.status
rm /tmp/df.status
TOTALMEM=`free -m | head - | tail -| awk '{print $2}'`
TOTALBC=`echo "scale=2;if($TOTALMEM<1024 && $TOTALMEM > 0) print 0;$TOTALMEM/1024"| bc -l`
USEDMEM=`free -m | head - | tail -| awk '{print $3}'`
USEDBC=`echo "scale=2;if($USEDMEM<1024 && $USEDMEM > 0) print 0;$USEDMEM/1024"|bc -l`
FREEMEM=`free -m | head - | tail -| awk '{print $4}'`
FREEBC=`echo "scale=2;if($FREEMEM<1024 && $FREEMEM > 0) print 0;$FREEMEM/1024"|bc -l`
TOTALSWAP=`free -m | tail -| awk '{print $2}'`
TOTALSBC=`echo "scale=2;if($TOTALSWAP<1024 && $TOTALSWAP > 0) print 0;$TOTALSWAP/1024"| bc -l`
USEDSWAP=`free -m | tail -| awk '{print $3}'`
USEDSBC=`echo "scale=2;if($USEDSWAP<1024 && $USEDSWAP > 0) print 0;$USEDSWAP/1024"|bc -l`
FREESWAP=`free -m | tail -| awk '{print $4}'`
FREESBC=`echo "scale=2;if($FREESWAP<1024 && $FREESWAP > 0) print 0;$FREESWAP/1024"|bc -l` echo -e "
*********************************************************************
Memory
********************************************************************* => Physical Memory Total\tUsed\tFree\t%Free ${TOTALBC}GB\t${USEDBC}GB \t${FREEBC}GB\t$(($FREEMEM * / $TOTALMEM ))% => Swap Memory Total\tUsed\tFree\t%Free ${TOTALSBC}GB\t${USEDSBC}GB\t${FREESBC}GB\t$(($FREESWAP * / $TOTALSWAP ))%
"
}
FILENAME="health-`hostname`-`date +%y%m%d`-`date +%H%M`.txt"
sysstat > $FILENAME
echo -e "Reported file $FILENAME generated in current directory." $RESULT
if [ "$EMAIL" != '' ]
then
STATUS=`which mail`
if [ "$?" != ]
then
echo "The program 'mail' is currently not installed."
else
cat $FILENAME | mail -s "$FILENAME" $EMAIL
fi
fi

(copy) Shell Script to Check Linux System Health的更多相关文章

  1. linux基础之Shell Script入门介绍

    本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下. 本文转自:http://www.jbxue.com ...

  2. 一个改动配置文件的linux shell script

    不久以前,以前搜到一篇博客是读取配置文件的,http://www.cnblogs.com/bo083/archive/2012/11/19/2777076.html,用到如今,感觉十分方便.感谢作者. ...

  3. Shell script for logging cpu and memory usage of a Linux process

    Shell script for logging cpu and memory usage of a Linux process http://www.unix.com/shell-programmi ...

  4. linux C程序中获取shell脚本输出(如获取system命令输出)

    转载自 http://blog.csdn.net/hjxhjh/article/details/7909518 1. 前言 Unix 界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些 ...

  5. “windows的批处理”与“Linux的shell script”的类比学习

    从2005年开始,做了将近10年的系统维护,先是做网络接入管理,然后做网络安全与审计,然后做服务器管理等整个网络系统的运营管理:现在又兼着做一些Linux下的视频监控系统的软硬件维护.过程中遇到太多重 ...

  6. Linux的shell script

    Linux的shell script //编辑shell: vi a.sh //子进程运行shell sh a.sh //主线程运行shell source a.sh 相关例子: #!/bin/bas ...

  7. Linux基础之-shell script(变量,运算符,流程控制,函数)

    一.shell script Shell 脚本(shell script),是一种为shell编写的脚本程序.业界所说的shell通常都是指shell脚本,但读者朋友要知道,shell和shell s ...

  8. Linux 脚本运维总结之Shell script

    1. 本地变量和环境变量 变量类型 定义形式 声明位置 显示命令 作用域 本地变量 VARNAME=value 命令行或shell脚本 set (显示所有变量) 本用户,本进程 环境变量 export ...

  9. Linux shell Script初识

    shell secript: 执行方式的差异: ./ sh执行都是在创建一个子程序来执行,只会继承环境变量, 其中的变量如果export声明,子程序的子程序会继承,不会升级为环境变量 source 的 ...

随机推荐

  1. BZOJ 1059 & 二分图匹配

    题意: 判断一个黑白染色的棋盘能否通过交换行或列使对角线上都是黑色. SOL: 真是有点醉...这种问题要么很神要么很水...第一眼感觉很水但就是不造怎么做...想了10分钟怎么感觉就是判断个数够不够 ...

  2. Linux 下安装mysql 链接库

    1.mysql 客户端 开发 链接库 1.1)CentOS yum install mysql-devel

  3. Update UI from an asynchronous thread

    One of the most common tasks you need to perform in a Windows Phone application is updating the UI f ...

  4. Qt 控件随窗口缩放

    在Qt的界面设计中,我们有时候希望窗口在最大化的时候,上面的控件也跟着缩放,那么我们就需要调整控件的SizePolicy属性,关于这个属性的讲解请参见我之前的博客Qt SizePolicy 属性,由于 ...

  5. Odoo 8.0 实施开发指南 第一版 试读

    试读地址: http://share.weiyun.com/4f83964db87e022c7c210abe6b5e782f 如有错误,欢迎指正.

  6. Defining custom settings in Odoo

    Unfortunately Odoo documentation doesn’t seem to include any information about adding new configurat ...

  7. 移动端设备UA检测

    网上找到的都不全,不是漏这个就是漏那个,有的甚至还把桌面的chrome判断为移动浏览器. 于是自己动手整理,这回算是比较全了.以后发现漏掉的立马加上. if(/AppleWebKit.*Mobile/ ...

  8. Linux关闭休眠和屏保模式

    本人因为特殊需求,想让某台Linux主机始终显示某个程序,显示器不能关机或者休眠或进入屏保模式. 环境:Ubuntu 11.10 最小化模式安装并安装有轻量级桌面openbox(非gnome).因为X ...

  9. matlab中使用fuzzy工具箱

    4步教你学会使用matlab模糊控制工具箱 Matlab模糊控制工具箱为模糊控制器的设计提供了一种非常便捷的途径,通过它我们不需要进行复杂的模糊化.模糊推理及反模糊化运算,只需要设定相应参数,就可以很 ...

  10. html 前端 总结(一)

    前端 html 总结(一) 基础部分:计算机原理 a: 是由运算器 控制器 内存组成储存器包括内存.外村外存 硬盘内存 由外村调入到内存执行输入——内存——cpu 运算 cpu运算——内存——输出设备 ...