start a script automatically

tomvo at absi.be tomvo at absi.be
Wed Apr 5 10:20:38 CEST 2006


Hi,

I just did this a few days ago using eventhandlers, I'll post my config 
for your convenience, actually, it's dead easy:

this is the config of the service check:

define service{
        use                             generic-service         ; Name of 
service template to use
        host_name                       www.xxxx.be
        service_description HTTP_check_www.xxxxx.be_.5_interface
        contact_groups                  solaris-admins
        retry_check_interval            3
        event_handler                   restart_xxxweb
        check_command check_webtest!xxx-5.scr!http://www.xxx.be interface 
.5 via internet
        }


the key here is the event_handler line. This 'function' is executed 
whenever a state change occurs.

in checkcommands.cfg you must define this function:

define command{
        command_name    restart_xxxweb
        command_line 
/usr/lib/nagios/plugins/eventhandlers/restart_xxxxxweb $SERVICESTATE$ 
$SERVICESTATETYPE$ $SERVICEATTEMPT$
        }


This provides the link to the script that will be executed each time a 
state change occurs.


below the contents of this script (I found it somewhere on a nagios 
support site) and modified it slightly for my environment. As you will 
see, I'm using the nagios client mechanism to restart the service on the 
remote host.



#!/bin/sh
#
# Event handler script for restarting the web server on the local machine
#
# Note: This script will only restart the web server if the service is
#       retried 3 times (in a "soft" state) or if the web service somehow
#       manages to fall into a "hard" error state.
#


# What state is the HTTP service in?
case "$1" in
OK)
        # The service just came back up, so don't do anything...
        ;;
WARNING)
        # We don't really care about warning states, since the service is 
probably still running...
        ;;
UNKNOWN)
        # We don't know what might be causing an unknown error, so don't 
do anything...
        ;;
CRITICAL)
        # Aha!  The HTTP service appears to have a problem - perhaps we 
should restart the server...

        # Is this a "soft" or a "hard" state?
        case "$2" in
        # We're in a "soft" state, meaning that Nagios is in the middle of 
retrying the
        # check before it turns into a "hard" state and contacts get 
notified...
        SOFT)

                # What check attempt are we on?  We don't want to restart 
the web server on the first
                # check, because it may just be a fluke!
                case "$3" in

                # Wait until the check has been tried 3 times before 
restarting the web server.
                # If the check fails on the 4th time (after we restart the 
web server), the state
                # type will turn to "hard" and contacts will be notified 
of the problem.
                # Hopefully this will restart the web server successfully, 
so the 4th check will
                # result in a "soft" recovery.  If that happens no one 
gets notified because we
                # fixed the problem!
                1)
                        echo "$(date): Dit is de eerste soft state voor 
xxxxweb, we herstarten de gateway" >> /tmp/eventhandler_test
                        /usr/lib/nagios/plugins/check_nrpe -H 
193.110.158.5  -c restart_xxxweb
                        ;;

                2)
                        echo "$(date): Dit is de tweede soft state voor 
xxxweb, we herstarten de gateway voor de 2e keer" >> 
/tmp/eventhandler_test
                        /usr/lib/nagios/plugins/check_nrpe -H 
193.110.158.5  -c restart_xxxweb
                        ;;
                3)
                        echo "$(date): Dit is de derde soft state voor 
xxxweb, we herstarten de gateway voor de laatste maal, indien het nog niet 
werkt is er iets goed fout" >> /tmp/eventhandler_test
                        /usr/lib/nagios/plugins/check_nrpe -H 
193.110.158.5  -c restart_xxxweb
                        ;;

                esac
                ;;

        # The HTTP service somehow managed to turn into a hard error 
without getting fixed.
        # It should have been restarted by the code above, but for some 
reason it didn't.
        # Let's give it one last try, shall we?
        # Note: Contacts have already been notified of a problem with the 
service at this
        # point (unless you disabled notifications for this service)

        HARD)
                echo "$(date): na 3x proberen wil de xxxweb gateway nog 
altijd niet opstarte, misschien eens proberen met de server te rebooten ?" 
>> /tmp/eventhandler_test
                ;;
        esac
esac
exit 0









---------------------------------------------------------------------------
Tom Van Overbeke - ABSI Service Delivery Coordinator
email: tomvo at absi.be
Tel: +32 2 333 40 00 - Fax: +32 2 332 34 69
website: http://www.absi.be
---------------------------------------------------------------------------





"Marco Borsani" <m.borsani at it.net>
Sent by: nagios-users-admin at lists.sourceforge.net
03/04/2006 13:57
 
        To:     "'NAGIOS'" <nagios-users at lists.sourceforge.net>
        cc: 
        Subject:        [Nagios-users] start a script automatically


Hi all!

Is it possible to start a script automatically on a remote host in case of 
a
particolar event?

For example, if nagios detects that process "named" on a DNS server is 
down,
will Nagios can run a remote command on that server to restart the 
process?

Regards

Marco Borsani 
Unix & Monitoring System Administrator
Technical Operation
Tel.    +39 010 4310115
Fax     +39 010 4327454
E-mail: m.borsani at IT.net

ITnet S.r.l. - Direzione e Coordinamento di WIND Telecomunicazioni S.p.A.
Internet Service Provider
Sede legale:                     Via C.G.Viola, 48 - 00148 Roma
Dir. Centrale e Amministrativa:                  Via Pacinotti, 39
                                              16151 Genova (Italy)
 
http://www.it.net 
mailto:info at IT.net 
_______________________________________________________________
Altre sedi ITnet:
MILANO tel.: +39 02 30114900    info-milano at IT.net
ROMA    tel.: +39 06 83116707    info-roma at IT.net
_______________________________________________________________

ITnet is associated to CIX (Commercial IP eXchange) and RIPE
ITnet is associated to AIIP (Associazione Italiana Internet Providers)




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting 
language
that extends applications into web and mobile media. Attend the live 
webcast
and join the prime developer group breaking into this new coding 
territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&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




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&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