How to Create Nagios Plugins With Perl On CentOS 6



Nagios is an open source monitoring software application that helps organizations to prevent IT infrastructure problems. It can be used with Linux as well as other Unix variants.

In this tutorial, you will learn how to create Nagios plugins with Perl on CentOS 6.

Initial Set Up


Before getting into the creation of Nagios plugins, you have to first install RPMForge repository and NRPE on client VPS. NRPE stands for Nagios Remote Plugin Executor.

That can be installed using the following commands:

rpm -ivh
yum -y install perl nagios-nrpe
useradd nrpe && chkconfig nrpe on


Creation of Perl Script


Perl programming language allows us to create scripts and install additional libraries. Here, we will be creating a script which will check disk usage and throw alert if it is over 85%.

The script will look like this:

use strict;
use warnings;
use feature qw(switch say); 

my $used_space = `df -h / \|awk 'FNR == 2 {print \$5}'`; 

given ($used_space) {
    when ($used_space lt '85%') { print "OK - $used_space of disk space used."; exit(0);      }
    when ($used_space eq '85%') { print "WARNING - $used_space of disk space used."; exit(1);      }
    when ($used_space gt '85%') { print "CRITICAL - $used_space of disk space used."; exit(2); }
    default { print "UNKNOWN - $used_space of disk space used."; exit(3); }

This script has to be saved in the same directory as other Nagios plugins, /usr/lib64/nagios/plugins/

Make it executable:

chmod +x /usr/lib64/nagios/plugins/

You can customize the script to apply different logic to trigger alerts and exit codes or return codes.


Here is the list of Nagios Return Codes:

Exit Codes / Return Codes Status
0 OK


Configure NRPE


Now that we are done creating the script, we have to add the script to nrpe configuration /etc/nagios/nrpe.cfg on client host. For that, we will first delete the original config file (/etc/nagios/nrpe.cfg) and update it with the following lines of code:



You have to specify the correct values for allowed_hosts, which should be the IP of monitoring server.

Restart the Nagios nrpe service:

service nrpe restart


Set Up on Nagios Monitoring Server


In this step, we will add a new command to check Nagios on monitoring server. For that, we have to  update the commands file /etc/nagios/objects/commands.cfg.

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

This will use nrpe to make TCP connections to port 5666 and will run the command ‘checkdiskspace_perl’ which we already defined in /etc/nagios/nrpe.cfg on remote host.


Now we will add this check to Nagios configuration file for client VPS. In this tutorial, we will be using ‘CentOS6Droplet’ as server for monitoring and will update /etc/nagios/servers/CentOS6Droplet.cfg with following lines of code:

define service {
        use                             generic-service
        host_name                       CentOSDroplet
        service_description             Custom Disk Checker In Perl
        check_command                   checkdiskspace_perl

Once the changes are done, you should restart the Nagios:

service nagios restart

Verify whether the new check is working or not:


That’s it!

support2 has written 111 articles

Leave a Reply