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

发布时间:2020-11-16编辑:脚本学堂
本文介绍下,用于监控多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@jb200.com"
FROM="*****@****.com"

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

linuxjishu/13830.html target=_blank class=infotextkey>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