How to Create Plugin in Nagios Using Bash Script?

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.