How to Create Own Nagios Plugin using Bash Shell Scripts. This is useful for monitoring infrastructure as your own setup. This tutorial will help you to understand, how to write a script and use them with NRPE and Nagios for monitoring.
Step 1 – Understand Return Codes
Every Nagios plugin must return with a status code which is called return code. On the basis of return codes, Nagios core service takes decisions and appropriate action for the corresponding host or service.
Hosts:
Return Code / Host status
0 => UP
1 => DOWN
Other Maintains last known state
Services:
Return code / Service status
0 => OK
1 => WARNING
2 => CRITICAL
3 => UNKNOWN
Other CRITICAL : unknown return code
Step 2 – Install NRPE Client
Let’s install the NRPE client on your system using the following commands.
sudo apt-get update sudo apt-get install nagios-nrpe-server nagios-plugins
The above commands are for Debian based systems. To install NRPE on Redhat based system, visit this tutorial.
Step 3 – Write A Shell Script
Now the time to write a shell script to monitor any service on your system. For this example below script will monitor the disk space uses.
vim /usr/lib/nagios/plugins/check_disk_uses.sh
Add the below script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #!/bin/bash ### ======================================================================= ### ### A nagios plugin to check disk uses for given disk or mountpoint ### ### Uses: ./check_disk_uses.sh / ### ### ./check_disk_uses.sh /mnt ### ### ./check_disk_uses.sh /dev/sda1 ### ### ======================================================================= ### ### ======================================================================= ### ### FUNCTIONS ### ### ======================================================================= ### calculate_disk_uses(){ USED_DISK_SPACE=`df -h ${MOUNT_POINT} | grep -v Filesystem | awk '{print $5}' | sed 's/%//g'` case ${USED_DISK_SPACE} in [1-80]*) echo "OK - ${USED_DISK_SPACE}% of disk space used." exit 0 ;; [81-85]*) echo "WARNING - ${USED_DISK_SPACE}% of disk space used." exit 1 ;; [86-100]*) echo "CRITICAL - ${USED_DISK_SPACE}% of disk space used." exit 2 ;; *) echo "UNKNOWN - ${USED_DISK_SPACE}% of disk space used." exit 3 ;; esac } ### ======================================================================= ### ### SCRIPT EXECUTION START HERE ### ### ======================================================================= ### if [[ -z "$1" ]] then echo "Missing parameters! Syntax: ./`basename $0` mount_point/disk" exit 3 else MOUNT_POINT=$1 fi calculate_disk_uses ### ======================================================================= ### ### END OF SCRIPT ### ### ======================================================================= ### |
Now set the execute permission on the new script.
chmod +x /usr/lib/nagios/plugins/check_disk_uses.sh
Step 4 – Update NRPE Configuration
Now edit NRPE configuration file /etc/nagios/nrpe.cfg and add your command to monitor some disk of your system.
command[check_disk_uses]=/usr/lib/nagios/plugins/check_disk_uses.sh /dev/sda1
You can call check_disk_uses command from Nagios server using check_nrpe command and get the results back.
Step 5 – Test with Check_Nrpe Command
Now, run the below command from your Nagios server, where 192.168.1.100 is the IP of the NRPE client system.
./check_nrpe -H 192.168.1.100 -c check_disk_uses OK - 22% of disk space used.
1 Comment
Hello,
I have a problem with this tutorial. I have a version 3 for check_nrpe. I have a message “check_disk_uses not defined since the server centreon.I have add the command on the file nrpe.cfg, enable to 1 to “dont_blame_nrpe” . Since the client machine (debian) our command works proprely.
Do you have a solution ?
Thanks for our answer.