Running check_nagios by ssh

Lehman, John LEHMANJ at us.panasonic.com
Fri Nov 3 22:42:56 CET 2006


This was exactly what I was looking for and I wanted to let you know it
works like a charm. 
I removed the rsync stuff and all is great.

Thanks.

John

-----Original Message-----
From: Donnell Lewis [mailto:donnell.lewis at icoretechnology.com] 
Sent: Tuesday, October 31, 2006 10:11 AM
To: Lehman, John
Cc: Tedman Eng; nagios-users at lists.sourceforge.net
Subject: Re: [Nagios-users] Running check_nagios by ssh

Here is a script I wrote for checking that the master is running or not
and it will ENABLE/DISABLE the service checks and notifications
accordingly on the slave depending on it's check results of the master.
You will need to edit the variables accordingly and remove the rsync
stuff as I used it to automatically turn the rsync cron ON/OFF. I run
this on a cron.  The use of sudo in this script is because I am running
it as the nagios user and it needed it for adjusting the rsync cron, but
you can remove the rsync stuff and add your own if you want it to do any
additional functions beyond ENABLING/DISABLING service checks and notifs
on the slave.  Please keep in mind this script is rudimentary and could
be streamlined alot, I just haven't gotten around to it..

-Don

check_nagios_primary.sh contents:


#!/bin/bash

# Author: Donnell C. Lewis | 2005-09-14 |NOTE:This script was written to
work with Nagios 2.x and up.
# This script is covered under the GPL and can be freely distributed,
re-distributed and edited, sliced
# diced and throw in the fire so long as you leave the original $AUTHOR
credits in place.
# Props go out to Ethan Galstad who made the awesome software for which
this script was written to
# work with.

# What is is: This script was written to be a semi-logical automatic
control interface between the
# primary Nagios host and the slave Nagios host, and should be set to
run on a crontab.  It includes all
# the functions that it requires to login/reach the primary Nagios to
make sure it's running it's Nagios
# processes.  If this script cannot ssh to the primary or should it not
be able to locate a running
# Nagios process on the primary it will send commands to the nagios.cmd
file on the slave that will
# enable "service checks" and "notifications" and disable the companion
rsync script.  Once this
# script can once again reach the primary and finds running processes it
will send commands to
# nagios.cmd that will disable "service checks" and "notifications" and
re-enable the companion
# rsync script.

# Here we set some PATH's and environment variables for script execution
PATH=$PATH:$HOME/bin:/usr:/bin:/sbin:/usr/bin:/usr/sbin:

export PATH
unset USERNAME

# Here we check if this script is already running and if so we die !
if [ -e "check_nagios_primary.lock" ]; then
echo "check_nagios_primary.sh already running..exiting!"
echo "check_nagios_primary.sh already running..exiting!" >>
check_nagios_primary.log
exit
fi

# Write a command to the Nagios command file to cause
# it to process a service check result
touch check_nagios_primary.lock
echocmd="/bin/echo"
CommandFile="/usr/local/nagios/var/rw/nagios.cmd"
# get the current date/time in seconds since UNIX epoch
datetimestamp=$(date +%s)
datetime=$(date)
# create the command line to add to the command file
cmdline="[$datetimestamp]"
SVCUP="STOP_EXECUTING_SVC_CHECKS;"
SVCDOWN="START_EXECUTING_SVC_CHECKS;"
NOTIFON="ENABLE_NOTIFICATIONS;"
NOTIFOFF="DISABLE_NOTIFICATIONS;"
PRIMARY="89.149.155.19"

#Check nagios with check_nagios plugin
/usr/local/nagios/libexec/check_by_ssh -t 90 -H $PRIMARY -C
"./check_nagios -e 5 -F /usr/local/nagios/var/nagios.log
-C /usr/local/nagios/bin/nagios >> /dev/null"

if [ "${?}" != 0 ] ; then
echo "$datetime CRITICAL:Nagios not running, enabling service checks and
notifications !"
touch /usr/local/nagios/var/check_nagios_primary.log
echo "$datetime CRITICAL:Nagios not running, enabling service checks and
notifications !" >> check_nagios_primary.log
$echocmd $cmdline $SVCDOWN >> $CommandFile
$echocmd $cmdline $NOTIFON >> $CommandFile
sudo crontab -u rsync -r
 else
echo "$datetime OK:Nagios running fine mate, disabling service checks
and notifications !"
touch /usr/local/nagios/var/check_nagios_primary.log
echo "$datetime OK:Nagios running fine mate, disabling service checks
and notifications !" >> check_nagios_primary.log
$echocmd $cmdline $SVCUP >> $CommandFile
$echocmd $cmdline $NOTIFOFF >> $CommandFile
sudo crontab -u rsync -r
echo "*/5 * * * * /home/rsync/rsync.pull-nagios.sh >/dev/null 2>&1" >
cron.tmp
sudo crontab -u rsync cron.tmp
rm -rf cron.tmp
fi
rm -rf check_nagios_primary.lock
exit



On Mon, 2006-10-30 at 18:52 -0500, Lehman, John wrote:
> Hello,
> I am trying to run the check_nagios command from one machine to
another.
> (slave to master)
> 
> I can run the following from the command prompt on the "slave"
machine:
> 
> Note: the master I am checking is 10.130.4.80
> 
> /usr/lib/nagios/plugins/check_by_ssh -H 10.130.4.80 -l smadmin-sec -i
> home/smadmin-sec/.ssh/id_dsa -C "/usr/lib/nagios/plugins/check_nagios
> /var/log/nagios/status.log 5 'nagios'"
> 
> 
> I get the following as a response:
> 
> Nagios ok: located 1 process, status log updated 3 seconds ago
> 
> 
> 
> My question is, can I use the "response" to determine whether the
master
> is up and if not I want the slave to take over?
> 
> I thought this might be a simple and easy way to set up failover????
> 
> Thanks.
> 
> 
> 
>
------------------------------------------------------------------------
-
> Using Tomcat but need to do more? Need to support web services,
security?
> Get stuff done quickly with pre-integrated technology to make your job
easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
>
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Nagios-users mailing list
> Nagios-users at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/nagios-users
> ::: Please include Nagios version, plugin version (-v) and OS when
reporting any issue. 
> ::: Messages without supporting info will risk being sent to /dev/null


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list