How to Create Own Nagios Plugin using Bash Shell Script

Channel: Linux
Abstract: ./check_disk_uses.sh / ### ### ./check_disk_uses.sh /mnt ### ### ./check_disk_uses.sh /dev/sda1 ### ### ==============================================

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.

#!/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 ### ### ======================================================================= ###12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#!/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 3else        MOUNT_POINT=$1fi 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.

Ref From: tecadmin

Related articles