Previously wrote an article about the use of Nagios in the building of http://msiyuetian.blog.51cto.com/8637744/1704346
The following article focuses on the scripting implementation of Nagios custom monitoring system disks.
Nagios can identify 4 status return messages, 0 indicates a healthy state (OK), 1 indicates a warning (WARNING), 2 indicates a very serious error (CRITICAL), and 3 indicates an unknown error (UNKNOWN). Nagios determines the state of the monitored object based on the value returned by the plugin and displays it through the web. We can use the above feature to realize the status of the monitoring disk:
A, when the disk utilization is greater than 97% indicates a very serious error (CRITICAL), the return value is 2;
b, when the disk utilization is greater than 95% indicates a warning (WARNING), the return value is 1;
C, when the disk utilization is less than 95% indicates that the status is OK, the return value is 0;
Description
Server ip:192.168.0.114
Client ip:192.168.0.115
1. Client Configuration
1) scripting
[Email protected] ~]# vim/usr/lib/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 $ p_p= ' df-h-p|sed-n "$i" P|awk ' {print $6} ' If ["$u _per"-gt "];then" Echo-n "$p _p CRITICAL $u _per% $ava" sta[$i]=2 elif ["$u _per"-gt "];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 |
Script Description :
We use the df-h command to view the status of disk usage [Email protected] ~]# df-h Filesystem Size used Avail use% mounted on /dev/sda3 18G 2.6G 15G 16%/ Tmpfs 504M 0 504M 0%/dev/shm /DEV/SDA1 97M 26M 67M 29%/boot In the script for-I in ' seq 2 $row ': Indicates that the loop starts at the second line of output from the Df-h command. Parameters Ava, U_per, p_p are defined as remaining space (Avail), usage (use%), mount point (mounted on), respectively. Array sta[$i], the output is the return value, when return 0 means OK, return 1 is warning!, return 2 means critical. Finally, we use the maximum value of the output array to judge that, as long as a mount point has a usage rate of more than 97%, critical will be displayed on the Nagios Web page. |
2) Modify Script permissions
[Email protected] ~]# chmod +x/usr/lib/nagios/plugins/check_disk.sh
3) Edit the configuration file
[[email protected] ~]# vim/etc/nagios/nrpe.cfg//Add one line
command[check_disk]=/usr/lib/nagios/plugins/check_disk.sh |
4) Restart Nrpe service
[Email protected] ~]#/etc/init.d/nrpe restart
2. Service-side configuration
1) Check that the script on the client is working correctly
[Email protected] ~]#/usr/lib/nagios/plugins/check_nrpe-h 192.168.0.115-c check_disk
/OK 16% 15g/dev/shm ok 0% 504m/boot OK 29% 67M |
Note :
If error: Check_nrpe:error-could not complete SSL handshake. Indicates that the client does not add IP that allows the server host, modifies the client Nrpe configuration file, adds the server's IP [Email protected] ~]# vim/etc/nagios/nrpe.cfg allowed_hosts=127.0.0.1,192.168.0.114 [Email protected] ~]#/etc/init.d/nrpe restart |
2) Add service
[Email protected] ~]# cd/etc/nagios/conf.d/
[Email protected] conf.d]# vim 192.168.0.115.cfg
Define Host{ Use Linux-server HOST_NAME 192.168.0.115 Alias 0.115 Address 192.168.0.115 }
Define Service{ Use Generic-service HOST_NAME 192.168.0.115 Service_description Check_disk Check_command Check_nrpe!check_disk Max_check_attempts 5 Normal_check_interval 1 } |
3) Edit the commands.cfg configuration file
[[email protected] conf.d]# vim/etc/nagios/objects/commands.cfg//End Add the following:
Define Command{ Command_name Check_nrpe Command_line $USER 1$/check_nrpe-h $HOSTADDRESS $-C $ARG 1$ } |
4) Restart the Nagios service
[[Email protected] conf.d]# service Nagios restart
3. Viewing effects
Browser input: 192.168.0.114/nagios, verify the account password entry can see the effect:
The hosts have added a single
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/76/3F/wKiom1ZNzGOgPfyiAAOOsFBcWsg032.jpg "style=" float: none; "title=" 1.jpg "alt=" Wkiom1znzgogpfyiaaoosfbcwsg032.jpg "/>
Services Display Monitoring disk status
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M02/76/3E/wKioL1ZNzL2CmHv8AAX-K6P5OnE262.jpg "style=" float: none; "title=" 2.jpg "alt=" Wkiol1znzl2cmhv8aax-k6p5one262.jpg "/>
This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1714835
"Nagios Custom Monitoring System disk script" for Shell programming