客户端
1、创建脚本check_disk.sh
[root@y2 ~]# vim /usr/lib64/nagios/plugins/check_disk.sh #一般都放在这个路径下
#!/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|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
######输出结果######
[root@y2 ~]# sh /usr/lib64/nagios/plugins/check_disk.sh
/ OK 26% 13G /dev/shm OK 0% 242M /boot OK 8% 426M
[root@y2 ~]# echo $?
0
2、保存后,修改该脚本的权限
[root@y2 ~]# chmod +x /usr/lib64/nagios/plugins/check_disk.sh
3、编辑/etc/nagios/nrpe.cfg,加入自定义的脚本
[root@y2 ~]# vim /etc/nagios/nrpe.cfg
command[check_disk]=/usr/lib64/nagios/plugins/check_disk.sh
4、重启nrpe服务
[root@y2 ~]# /etc/init.d/nrpe restart
服务端
5、检测刚才的脚本是否正常运行
[root@wy ~]# /usr/lib64/nagios/plugins/check_nrpe -H 192.168.219.128 -c check_disk
/ OK 26% 13G /dev/shm OK 0% 242M /boot OK 8% 426M
解释说明:
如果正常的话,会输出一行磁盘检测的数据,否则可能会报错。
#####错误#####
[root@localhost ~]# /usr/lib64/nagios/plugins/check_nrpe -H 192.168.153.129 -c check_disk
NRPE: Unable to read output
#############
注:原因是客户端的脚本没有修改权限
6、添加相应的service
[root@wy ~]# cd /etc/nagios/conf.d
[root@wy conf.d]# vim 192.168.219.128.cfg
define service{
use generic-service
host_name 192.168.219.128
service_description check_disk
check_command check_nrpe!check_disk
max_check_attempts 5
normal_check_interval 1
}
注:也要检查一下/etc/nagios/object/commands.cfg里有没有check_nrpe
7、重启nagios服务、启动http服务
[root@wy conf.d]# /etc/init.d/nagios restart
[root@wy conf.d]# /etc/init.d/httpd start
8、访问浏览器查看