Pages

Friday, 11 March 2016

Creating a custom NAGIOS plugin to check your bespoke applications

Full credit / source: https://www.digitalocean.com/community/tutorials/how-to-create-nagios-plugins-with-bash-on-ubuntu-12-10

In my opinion writing addons for NAGIOS couldn't be easier - there are three main requirements:

- The check returns the relevent exit code (0 = OK, 1 = Warning, 2 = Critical, 3=Unknown)
- Ideally push some useful information out to STDOUT.

Since the requirements are so simple you can pretty much write the checks in anything you want - BASH scripting, C, python etc.

So on the client host we create the script:

sudo vi /usr/lib/nagios/plugins/vendorid.sh

#!/bin/bash
vendor_id=`echo /proc/cpuinfo | grep vendor_id`
case $vendor_id in
[1-84]*)
echo "OK - $vendor_id% of disk space used."
exit 0
;;
[85]*)
echo "WARNING - $vendor_id% of disk space used."
exit 1
;;
[86-100]*)
echo "CRITICAL - $vendor_id% of disk space used."
exit 2
;;
*)
echo "UNKNOWN - $vendor_id% of disk space used."
exit 3
;;
esac

and ensure it can be executed:

chmod +x /usr/lib/nagios/plugins/vendorid.sh

Now on the client host install the NRPE plugin and overwrite /etc/nagios/nrpe.cfg

sudo rm /etc/nagios/nrpe.cfg
sudo vi /etc/nagios/nrpe.cfg

and add:

log_facility=daemon
pid_file=/var/run/nagios/nrpe.pid
server_port=5666
nrpe_user=nrpe
nrpe_group=nrpe
allowed_hosts=1.2.3.4
dont_blame_nrpe=1
debug=0
command_timeout=60
connection_timeout=300
include_dir=/etc/nagios/nrpe.d/

command[vendorid_bash]=/usr/lib/nagios/plugins/vendorid.sh

and restart the NRPE service:

service nagios-nrpe-server restart

Now on the NAGIOS server we edit the commands.cfg file:

sudo vi /etc/nagios/objects/commands.cfg

and add:

define command{
        command_name    vendorid_bash
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c vendorid_bash
        }

and then add a service definition e.g.

define service {
        use                             generic-service
        host_name                       myhost
        service_description             Custom Check
        check_command                   vendorid_bash
        }

and finally restart nagios / check the config:

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
sudo service nagios restart

No comments:

Post a Comment