nagios中使用shell脚本监控cpu利用率

By | 2017年8月8日

今天在nagios增加CPU使用率,去网上找了一些脚本,总是不太完善,后来又自己修改了下,算是比较完善的吧,能正常使用。

#!/bin/bash
#如果某些命令找不到,可以在此定义$PATH。
# 状态定义
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

function help {
echo -e “\n\tThis plugin shows the % of used CPU, using either procinfo or sar (whichever is available)\n\n\t$0:\n\t\t-c <integer>\tIf the % of used CPU is above <integer>, returns CRITICAL state\n\t\t-w <integer>\tIf the % of used CPU is below CRITICAL and above <integer>, returns WARNING state\n”
exit -1
}
# Getting parameters:
while getopts “w:c:h” OPT; do
case $OPT in
“w”) warning=$OPTARG;;
“c”) critical=$OPTARG;;
“h”) help;;
esac
done
# Checking parameters:
( [ “$warning” == “” ] || [ “$critical” == “” ] ) && echo “ERROR: You must specify warning and critical levels” && help
[[ “$warning” -ge “$critical” ]] && echo “ERROR: critical level must be highter than warning level” && help

# 将执行6次的结果写入文件
touch /tmp/iostat.log
iostat -c 1 6 >/tmp/iostat.log
if [ $? -ne 0 ]
then
echo “Error : Can’t find the command ‘iostat’ .”
exit “$STATE_UNKNOWN”
fi
# 判断文件是否存在
if [ ! -f /tmp/iostat.log ]
then
echo “Error : iostat.log not found .”
exit “$STATE_UNKNOWN”
fi

# 赋值,取6次结果中的后5次的平均值
float_user=`cat /tmp/iostat.log |grep -v % |grep -v ^$ |sed ‘1,2d’ |awk ‘{a+=$1}END{printf “%.2f”,a/5}’`
float_system=`cat /tmp/iostat.log |grep -v % |grep -v ^$ |sed ‘1,2d’ |awk ‘{a+=$3}END{printf “%.2f\n”,a/5}’`
float_idle=`cat /tmp/iostat.log |grep -v % |grep -v ^$ |sed ‘1,2d’ |awk ‘{a+=$6}END{printf “%.2f\n”,a/5}’`
float_iowait=`cat /tmp/iostat.log |grep -v % |grep -v ^$ |sed ‘1,2d’ |awk ‘{a+=$4}END{printf “%.2f\n”,a/5}’`

# 删除iostat日志文件
if [ -f /tmp/iostat.log ]
then
rm -f /tmp/iostat.log
fi

# 计算CPU使用率
used_idle=`awk “BEGIN{print (100-$float_idle)}”`

# 判断取到的值是否在正确范围
if [ `echo “$used_idle > 0″| bc` -eq 0 -o `echo “$used_idle < 100” | bc` -eq 0 ]
then
echo “Error : used_idle out of the correct range .”
exit “$STATE_UNKNOWN”
fi

# 定义报警阈值,这里为百分数,请不要添加‘%’
#critical=80
#warning=60

# 定义报警输出
stat_ok=”OK”
stat_warning=”WARNING”
stat_critical=”CRITICAL”
stat_unknown=”UNKNOWN”
stat_info=”User=${float_user}% System=${float_system}% Iowait=${float_iowait}% Idle=${float_idle}% | CPU_Used=${used_idle}%”

# 判断并返回相应的值
if [ `echo “${used_idle} < $warning” | bc` -eq 1 ]
then
echo “${stat_ok} : ${stat_info}”
exit “$STATE_OK”

elif [ `echo “${used_idle} > $warning ” | bc` -eq 1 -a `echo “${used_idle} < $critical”|bc` -eq 1 ]
then
echo “${stat_warning} : ${stat_info}”
exit “$STATE_WARNING”

elif [ `echo “${used_idle} > $critical” |bc` -eq 1 ]
then
echo “${stat_critical} : ${stat_info}”
exit “$STATE_CRITICAL”

else
echo “${stat_unknown} : ${stat_info}”
exit “$STATE_UNKNOWN”
fi

check_cpu.zip

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注