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.

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

The syntax for the script is:

$ ./ <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 ./ $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


# nagios command file (named pipe)

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



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


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

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

# the directory where service configuration files are kept

# nagios command file (named pipe)

# check for existence of service configuration directory
if [[ ! -d $service_dir ]]
        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" ]]
        echo "$service_dir/$host.cfg does not exist!"
        exit 2

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

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"

# 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)
        echo "Checking service => $i"
        echo "[$NOW] SCHEDULE_SVC_CHECK;$host;$i;$NOW" >> $cmd_file
        # be nice to nagios :)
        sleep 1

echo "Done!"

rm -f /tmp/$NOW


exit 0

The output would be as follows:

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

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


Post a Comment

Tu comentario será moderado la primera vez que lo hagas al igual que si incluyes enlaces. A partir de ahi no ser necesario si usas los mismos datos y mantienes la cordura. No se publicarán insultos, difamaciones o faltas de respeto hacia los lectores y comentaristas de este blog.