1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > linux 循环 cpu使用率脚本 监控CPU使用率的三种方法(shell脚本)

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

时间:2021-08-17 11:12:23

相关推荐

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

监控CPU使用率的三种方法(shell脚本),供大家学习参考。

方法一:

复制代码 代码如下:

#!/bin/sh

#监控CPU使用率

#Written by Chameleon

#

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

#

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-

fi

#sleep 5

rm -fr $logdir/cpu.txt

方法三:

复制代码 代码如下:

#!/bin/sh

#监控CPU使用率

#-gt "60"是平均值,因为服务器有两个CPU,所以CPU总值是120。

#Written by Chameleon

#

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-

fi

#sleep 5

#rm -fr $logdir/cpu.txt

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。