天天看點

nagios自定義監控系統磁盤腳本

用戶端

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" ]

       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`

   if [ "${sta[$j]}" -gt $n ]

        n=${sta[$j]}

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 $?

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