教程集 www.jiaochengji.com
教程集 >  脚本编程  >  shell  >  正文 监控CPU使用率的三种方法(shell脚本)

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

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

监控CPU使用率的三种方法(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"|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

您可能感兴趣的文章:
监控CPU使用率的三种方法(shell脚本)
监控linux中CPU 内存 磁盘数据的shell脚本(图文)
php中使用proc/loadavg监控CPU的平均负载
shell脚本统计多个CPU利用率
找出1小时内占用cpu最多的10个进程的shell脚本
php记录服务器负载、内存、cpu状态的代码
用C#监控并显示CPU状态信息
一个监控网站运行情况的shell脚本
监控linux服务器性能的shell脚本
监控硬盘使用率的shell脚本

[关闭]
~ ~