linux 循环 cpu使用率脚本,监控CPU使用率的三种方法(shell脚本)

监控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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值