监控CPU使用率的三种方法(shell脚本)

发布时间:2019-08-06编辑:脚本学堂
监控CPU使用率的三种方法(shell脚本),供大家学习参考。方法一:
复制代码 代码如下:#!/bin/sh
#监控CPU使用率
#Written by Chameleon
#Http://chameleon.icpcn.com

监控CPU使用率的三种方法(shell/ target=_blank class=infotextkey>shell脚本),供大家学习参考。

方法一:
 

复制代码 代码如下:
#!/bin/sh
#监控CPU使用率
#Written by Chameleon
#Http://chameleon.icpcn.com
scriptdir=/root/shell/cpu_jk
logdir=$scriptdir/log
if [ -d $logdir ]
then
echo "$logdir is exist~!"
else
mkdir -p $logdir
fi
for cpu in `sar -u|grep -v "%user"|grep -v "%nice"|linuxjishu/13830.html target=_blank class=infotextkey>awk '{print $3}'|sed '1,2d'|sed "s/([0-9]*)..*/1/g" `
do
if [ "$cpu" -gt "110" ]
then
echo "该注意CPU使用情况了">$logdir/cpu.txt
fi
done
if [ -s $logdir/cpu.txt ]
then
cat $logdir/cpu.txt |mail -s "`date +%Y-%m-%d`日,省平台WEB1上CPU使用率已经达到或超过110%了~~"
fi
#sleep 5
rm -fr $logdir/cpu.txt

方法二:
 

复制代码 代码如下:
#!/bin/sh
#监控CPU使用率
#Written by Chameleon
#Http://chameleon.icpcn.com
scriptdir=/root/shell/cpu_jk
logdir=$scriptdir/log
if [ -d $logdir ]
then
echo "$logdir is exist~!"
else
mkdir -p $logdir
fi
sar -u|grep -v "%user"|grep -v "%nice"|awk '{print $3}'|sed '1,2d'|sed "s/([0-9]*)..*/1/g" > $scriptdir/cpu_config
cat $scriptdir/cpu_config | grep -v '^#' | while read line
do
cpu=`echo $line | awk -F ',' '{print $1}'`
if [ "$cpu" -gt "110" ]
then
echo "该注意CPU使用情况了">$logdir/cpu.txt
fi
done
if [ -s $logdir/cpu.txt ]
then
cat $logdir/cpu.txt |mail -s "`date +%Y-%m-%d`日,省平台WEB1上CPU使用率已经达到或超过110%了~~" chizhong@c-platform.com
fi
#sleep 5
rm -fr $logdir/cpu.txt

方法三:
 

复制代码 代码如下:
#!/bin/sh
#监控CPU使用率
#-gt "60"是平均值,因为服务器有两个CPU,所以CPU总值是120。
#Written by Chameleon
#Http://chameleon.icpcn.com
scriptdir=/root/shell/cpu_jk
logdir=$scriptdir/log
if [ -d $logdir ]
then
echo "$logdir is exist~!"
else
mkdir -p $logdir
fi
cpu=`mpstat 1 2 |grep "Average"|awk '{print $3}'|sed "s/([0-9]*)..*/1/g"`
if [ "$cpu" -gt "60" ]
then
echo "该注意CPU使用情况了">$logdir/cpu.txt
cat $logdir/cpu.txt |mail -s "`date +%Y-%m-%d`日,省平台WEB1上CPU使用率已经达到或超过120%了~~" chizhong@c-platform.com
fi
#sleep 5
#rm -fr $logdir/cpu.txt