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

最新文章

  1. Vue.js学习笔记(1)
  2. Oracle并发与多版本控制
  3. 三种Scriptlet总结
  4. CI框架源码阅读笔记2 一切的入口 index.php
  5. .net平台下C#socket通信(上)
  6. Linux操作系统里查看所有用户
  7. pyzmq missing when running ipython notebook
  8. java的文件流:字节流(FileInputStream、FileOutputStream)和字符流(FileReader、FileWriter)。
  9. spring3+struts2+hibernate3整合出现的问题,No mapping found for dependency [type=java.lang.String, name=&#39;struts.objectFactory.spring.enableAopSupport&#39;]
  10. win7 cmd命令行窗口 宽度无法变大 自由调整大小
  11. execlp函数使用
  12. adobe reader安装完成之前被中断,错误代码150210解决方法
  13. http学习笔记(3)
  14. CI 路由设置
  15. debug和release下PostThreadMessage的异同
  16. ArcGIS鼠标滚轮方向之代码篇
  17. MySQL长度、大小写验证问题[开发篇]
  18. java 里面耦合和解耦
  19. error: insufficient permissions for device(解决adb shell问题)
  20. React Native基础概念和基础认识

热门文章

  1. PHP处理超时方法
  2. 一些有用的HTML5 pattern属性
  3. 移动前端头部标签(HTML5 head meta)
  4. hdu 1312 Red and Black
  5. JavaScript实现存储HTML字符串
  6. 记忆用户设置-提升程序的体验VB/C#
  7. [CareerCup] 15.7 Student Grade 学生成绩
  8. Vue.js组件示例
  9. Spring 定时器
  10. service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误