Aug 29, 2011

Scheduling multiple service checks in Nagios

Just like most Nagios administrators out there I am often required to add multiple hosts in Nagios monitoring which is quite simple task. But it takes a little while, sometimes longer, for Nagios to schedule checks for all the newly added hosts and services.

To make the life easier for us Nagios developers had been kind enough to provide us a command to do this programmatically.


http://old.nagios.org/developerinfo/externalcommands/commandinfo.php?command_id=29

I have created a quick shell script in BASH to automate this process.

The syntax for the script is:

$ ./schedule_service_checks.sh <hostname|ip address>

Of if you have multiple hosts to check you may run it as follows:

# define multiple hosts
$ hosts="server1 server2 server3 server4"
$ for host in ${hosts[@]}; do ./schedule_service_checks.sh $host ; done

Essentially the script would write the commands to Nagios' command file (named pipe) called nagios.cmd.

Unless you have followed the standard Nagios installation procedure with its predefined directory structure you may want to modify the following variables:

# the directory where service configuration files are kept
service_dir="/usr/local/nagios/etc/services"

and

# nagios command file (named pipe)
cmd_file="/usr/local/nagios/var/rw/nagios.cmd"

and execute the script as user "nagios" or any other user who has permission to write data to the command file.

Script:

#!/bin/sh

# This script has been released under the same license as GNU bash 3
# Please let me know if you have any suggestions

host=$1

# check whether hostname has been specified
if [[ -z $host ]]
then
        echo "Enter hostname!"
        echo "Usage: $0 hostname"
        exit 2
fi

# epoch time
NOW=$(date +%s)

# the directory where service configuration files are kept
service_dir="/usr/local/nagios/etc/services"

# nagios command file (named pipe)
cmd_file="/usr/local/nagios/var/rw/nagios.cmd"

# check for existence of service configuration directory
if [[ ! -d $service_dir ]]
then
        echo "Service config directory (set to $service_dir) not found!"
        exit 2
# check for existence of service configuration file
elif [[ ! -e "$service_dir/$host.cfg" ]]
then
        echo "$service_dir/$host.cfg does not exist!"
        exit 2
fi

# check for existence of command file, the named pipe
if [[ ! -e $cmd_file ]]
then
        echo "Command file (set to $cmd_file) not found"
        exit 2
fi

grep -i 'service_description' $service_dir/$host.cfg | \
        sed -e 's/^\s*service_description\s*//g' > /tmp/$NOW

# to handle service names containting spaces, e.g. "Disk Monitor"
OLD_IFS=$IFS
IFS=$'\n'


# schedule an immediate host check
echo "Checking host => $host"
echo "[$NOW] SCHEDULE_HOST_CHECK;$host;$NOW" >> $cmd_file
sleep 1

# schedule service checks one by one
for i in $(cat /tmp/$NOW)
do
        echo "Checking service => $i"
        echo "[$NOW] SCHEDULE_SVC_CHECK;$host;$i;$NOW" >> $cmd_file
        # be nice to nagios :)
        sleep 1
done

echo "Done!"

rm -f /tmp/$NOW

IFS=$OLD_IFS

exit 0

The output would be as follows:

$ ./schedule_service_checks.sh server1
Checking host => server1
Checking service => atd
Checking service => Cron
Checking service => Disk Monitor
Checking service => Ping
Checking service => SNMP
Checking service => SSH
Done!

And something like following would appear in Nagios logs

[1314354006] EXTERNAL COMMAND: SCHEDULE_HOST_CHECK;server1;1314354006
[1314354007] EXTERNAL COMMAND: SCHEDULE_SVC_CHECK;server1;atd;1314354006
[1314354008] EXTERNAL COMMAND: SCHEDULE_SVC_CHECK;server1;Cron;1314354006
[1314354009] EXTERNAL COMMAND: SCHEDULE_SVC_CHECK;server1;Disk Monitor;1314354006
[1314354010] EXTERNAL COMMAND: SCHEDULE_SVC_CHECK;server1;Ping;1314354006
[1314354015] EXTERNAL COMMAND: SCHEDULE_SVC_CHECK;server1;SNMP;1314354006
[1314354016] EXTERNAL COMMAND: SCHEDULE_SVC_CHECK;server1;SSH;1314354006

No comments:

Post a Comment