How to Create Plugin in Nagios Using Bash Script?

29 / Mar / 2017 by Shivam Agrawal 0 comments

Nagios is an open source computer software application that monitors systems, networks, and infrastructure. With Nagios, we can monitor host and configure alerts on the services for servers, switches, applications. It sends alert messages to relevant people through emails when things go wrong and then sends another alert message when things get rectified.

In this post, we will make our custom plugin using bash script which is executed on every client machine using nrpe.

First, install the Nagios-plugin and NRPE on the client servers

apt-get install -y nagios-plugins nagios-nrpe-server

Furthermore, assume that we want to monitor the memory of the server. In order to achieve this, write a bash script memory.sh in the directory /usr/lib/Nagios/plugins/ as shown below

#!/bin/bash
free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }'

Now add the script file in the nrpe configuration file (/etc/Nagios/nrpe.cfg) on the client VPS as shown below

command[memory_bash]=/usr/lib/nagios/plugins/memory.sh -w 85 -c 90

Now on the Nagios-core server add the below command to Nagios checks in the commands.cfg file in /etc/nagios3/ directory.

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

Moving further, define a service in the configuration file to monitor the host as shown below:

define service {
        use                             generic-service
        host_name                       i-085675f30c08376a3
        service_description             Custom memory checker In Bash
        check_command                   memory_bash
        }

Now restart the nrpe server and nagios3 service as below

service nagios3 restart             ( on the nagios-core)
service nagios-nrpe-server restart  ( on the client server)

That’s it! Your Nagios UI will be able to check the results now. Hope this blog was helpful and you will yourself be able to create Plugin in Nagios using Bash Script.

FOUND THIS USEFUL? SHARE IT

Leave a comment -