Activities

January 2013
M T W T F S S
« Dec   Mar »
 123456
78910111213
14151617181920
21222324252627
28293031  

How do I create custom Nagios plugins using bash script

Sometimes people may want to monitor the value of certain output to ensure the availability of service stability. This is most easiest way to create your own script to monitor our expected output of a command. This will be useful for monitoring a numeric variable which returned as output.

This eg. I’m going to teach you, I want to monitor the email queue of my QMAIL server to identify the mail server busy hours/sending email density of particular time.

Here we go, a Sample nagios script is shown below

#!/bin/bash
#Usage : /path/to/script/name.sh 30 40
#Output : OK: Number of  mails in the queue is 15
if [ ! -n "$1" ]
   then
        echo "UNKNOWN: Missing WARNING VALUE..."
        exit 3
    fi

if [ ! -n "$2" ]
    then
        echo "UNKNOWN: Missing CRITICAL VALUE..."
        exit 3
    fi


VALUE=`/usr/local/nagios/libexec/qmailmrtg7  q /var/qmail/queue | head -1`

if [ ! -n "$VALUE" ]
    then
        echo "CRITICAL: Error Unable to read"
        exit 2
    elif [ $VALUE -le "$1" ]
    then
        SC="OK"
        EX=0
    elif [ $VALUE -le "$2" ]
    then
        SC="WARNING"
        EX=1
    else
        SC="CRITICAL"
        EX=2
   fi

echo "$SC: Number of  mails in the queue is $VALUE"
exit $EX

Pls note here we used 3 validations at the first time to verify the inputs using if conditions statements. arguments are shown as $1 and $2. $1 indicate the warning value of that service check and $2 indicate the critical values (numeric) of the service check. The next statements are comparing the output value with threshold limit values and set the status messages [ OK, WARNING, CRITICAL]

How do I apply this script in NRPE daemon.

There are few things you need to do.

a. Uncomment the line #Defaults requiretty in visudo file.

 #Defaults    requiretty

b. Provide sudo permission to nagios to execute out script.
add the line at the bottom of the visudo file. one Eg. is shown below

 nagios  ALL=(ALL) NOPASSWD: /bin/sh /usr/local/nagios/libexec/mailq.sh

c. Add the entry in nrpe config file (/usr/local/nagios/etc/nrpe.cfg)

 command[check_mailq]=/usr/bin/sudo /bin/sh  /usr/local/nagios/libexec/check_mailq.sh

Here I uses another script (mailq.sh) to execute the actual nagios’s check script. here is the content of that file

[root@web03 ~]# cat  /usr/local/nagios/libexec/check_mailq.sh
/bin/sh  /usr/local/nagios/libexec/check_qmail_queue.sh 20 30
[root@web03 ~]#

Verifying this script is working.

You may execute it directly from the command line console from Nagios client serer. sometime this will not work if the sudo permission is not set properly. I got the script execute locally fine and but did not execute it from the Nagios monitoring host.

[root@u15526863 ~]#   /usr/local/nagios/libexec/check_nrpe -H 74.28.45.13 -c check_mailq
OK: Number of  mails in the queue is 15
[root@u15526863 ~]#

-H : Nagios monitoring host
-c : nrpe command

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>