自定义nagios监控脚本---磁盘检测

1. 在客户端上创建脚本/usr/local/nagios/libexec/check_disk.sh
vim /usr/local/nagios/libexec/check_disk.sh 写入如下内容:(client
上)

#!/bin/bash
row=`df -h |wc -l`
for i in `seq 2 $row`
do
ava=`df -h |sed -n "$i"p|awk '{print $4}'`
u_per=`df -h |sed -n "$i"p|sed -n "s/\%//"p|awk '{print
$5}'`
p_p=`df -h -P|sed -n "$i"p|awk '{print $6}'`
if [ "$u_per" -gt "97" ];then
echo -n "$p_p CRITICAL $u_per% $ava "
sta[$i]=2
elif [ "$u_per" -gt "95" ];then
echo -n "$p_p WARNING! $u_per% $ava "
sta[$i]=1
else
echo -n "$p_p OK $u_per% $ava "
sta[$i]=0
fi
done
n=0
for j in `seq 2 $row`
do
if [ "${sta[$j]}" -gt $n ];then
n=${sta[$j]}
fi
done
exit $n

2. 保存后,修改该脚本的权限
chmod +x /usr/lib/nagios/plugins//check_disk.sh (client上)
3. 然后编辑/etc/nagios/nrpe.cfg文件
vim /etc/nagios/nrpe.cfg # 加入一行:(client上)
command[check_disk]=/usr/lib/nagios/plugins/check_disk.sh
保存,重启nrpe服务
/etc/init.d/nrpe restart (client上)
4. 检测刚才的脚本是否正常运行的方法是,到server端执行如下命令:
check_nrpe -H 192.168.0.12 -c check_disk (server上)
如果正常的话,会输出一行磁盘检测的数据,否则可能会报错。
5. 到server上添加相应的service
cd /etc/nagios/conf.d/
vim 192.168.0.12.cfg # 加入如下内容:
define service{
use generic-service
host_name 192.168.0.12
service_description check_disk
check_command check_nrpe!check_disk
max_check_attempts 5
normal_check_interval 1
}
6. 重启nagios服务
/etc/init.d/nagios restart (server上)