当前位置: > shell编程 >

监控多cpu下每个cpu运行情况的shell脚本

时间:2014-10-01 09:01来源:linux.it.net.cn 作者:it
用于监控多cpu机器环境下,单个cpu运行情况的shell脚本

现在的服务器,很多都在用多个CPU来支撑业务的运行,本节分享一个用于监测多cpu情况下,单个cpu运行情况的脚本。

代码如下:
 

复制代码代码示例:

#!/bin/sh 
#fielname monitor_cpu.sh

DATE=`date +%Y%m%d%H`
DIR="/root/"
MAILX=/usr/bin/mailx
NOTIFY="monitor@it.net.cn"
FROM="*****@****.com"

cd $DIR
rm out.`date -d -1day +%Y%m%d`*
rm out.$DATE
rm report_cpu

awk '$0 ~/cpu/' /proc/stat | while read line 
do 
echo "$line" | awk '{total=$2+$3+$4+$5+$6+$7+$8;free=$5; print$1" Free "free/total*100"%", "Used " (total-free)/total*100"%"}'>>out.$DATE

done

while read line
do
cpu_usage=`echo $line | awk '{print $5}' |sed 's/\%$//g'`
#cpu_usage=`echo $line | awk '{print $5}'`
echo $cpu_usage
flag=`expr $cpu_usage \> 0.4`

if [ "$flag" == "1" ] ; then
cpu=`echo $line|awk '{print $1}'`
usage=`echo $line|awk '{print $5}'`
echo "the $cpu used $usage">>report_cpu

fi
done <out.$DATE

echo "please check below processes :" >>report_cpu
ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head >>report_cpu

$MAILX -s "the cpu of $hostname used too much , please check it" $NOTIFY<report_cpu


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容