Event handlers problem: [SOLVED]


#1

hi all,
i’ve tested the Event handlers example of Nagios documentation but it doesn’t work.

My configuration files:

restart-httpd file:
!/bin/sh

case “$1” in
OK)
;;
WARNING)
;;
UNKNOWN)
;;
CRITICAL)
case “$2” in
SOFT)
case “$3” in
3)
echo -n “Restarting HTTP service (3rd soft critical state)…”

Call the init script to restart the HTTPD server

sudo /etc/init.d/apache2 restart
;;
esac
;;
HARD)
echo -n "Restarting HTTP service…"
sudo /etc/init.d/apache2 restart
;;
esac
;;
esac
exit 0

Service definition:
define service{
host_name NAGIOS3
service_description HTTP
check_command check_http
check_interval 1
max_check_attempts 4
event_handler restart-httpd
}

Command definition:
define command {
command_name restart-httpd
command_line $USER2$/restart-httpd $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
}

resource.cfg file:
$USER1$=/usr/local/nagios/libexec
$USER2$=/usr/local/nagios/libexec/eventhandlers

I stop the http service and this is what can i see in logs:
localhost nagios: SERVICE ALERT: NAGIOS3;HTTP;CRITICAL;SOFT;1;Connection refused
localhost nagios: SERVICE EVENT HANDLER: NAGIOS3;HTTP;(null);(null);(null);restart-httpd
localhost nagios: SERVICE ALERT: NAGIOS3;HTTP;CRITICAL;SOFT;2;Connection refused
localhost nagios: SERVICE EVENT HANDLER: NAGIOS3;HTTP;(null);(null);(null);restart-httpd
localhost nagios: SERVICE ALERT: NAGIOS3;HTTP;CRITICAL;SOFT;3;Connection refused
localhost nagios: SERVICE EVENT HANDLER: NAGIOS3;HTTP;(null);(null);(null);restart-httpd
localhost nagios: SERVICE ALERT: NAGIOS3;HTTP;CRITICAL;HARD;4;Connection refused
localhost nagios: SERVICE NOTIFICATION: nagiosadmin;NAGIOS3;HTTP;CRITICAL;notify-service-by-email;Connection refused
localhost nagios: SERVICE EVENT HANDLER: NAGIOS3;HTTP;(null);(null);(null);restart-httpd

I can’t see Nagios macros values output $SERVICESTATE$ , $SERVICESTATETYPE$ and $SERVICEATTEMPT$ !!!
Http service doesn’t restart … :x

PLEASE HELP ME !


#2

hi!

I’m not sure of this, but you might try to swap the arguments from your command definition to your event_handler def:

Service definition:
define service{
host_name NAGIOS3
service_description HTTP
check_command check_http
check_interval 1
max_check_attempts 4
event_handler restart-httpd $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
}
(you may have to replace the spaces with “!”, as it is the nagios arg separator…)

and

Command definition:
define command {
command_name restart-httpd
command_line $USER2$/restart-httpd $ARG1$ $SARG2$ $ARG3$
}

Hope this will help you or put you on the right way ^^


#3

Don’t think “event_handler restart-httpd $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$” will work as those macros are assigned ‘after the fact’ so to speak. If you can “su nagios” and “./restart-httpd CRITICAL SOFT 3” with the expected results, i.e. the thing works fine 'cept it just ain’t running in nagios, you could be having the “sudo: sorry, you must have a tty to run sudo” error, which, of course, you won’t be able to see (unless you incorporate into your script some method by which the command output is captured) as it is sent to the nagios proc, where it is duely ignorified. A ‘get-round’ for this is by running “visudo” as root, and then commenting out the requiretty line, thus:

Of course, if you can’t run it from the command-line either that’s a whole different bag of spanners, but at least you should have something echo back to your screen as to whadupwidit, and if you bring that to the forum then someone can maybe point you to a solution from there. Oh, and even though the (null);(null);(null) would seem to imply that nagios isn’t passing the parameters to your script, this is in fact a red herring.

HTH

/S


#4

I do these changes:

Service definition:
define service{
host_name NAGIOS3
service_description HTTP
check_command check_http
check_interval 1
max_check_attempts 4
event_handler restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
}
(Without “!” i have an error on Nagios restart)

and

Command definition:
define command {
command_name restart-httpd
command_line $USER2$/restart-httpd $ARG1$ $SARG2$ $ARG3$
}

I stop the http service and this is what can i see in logs:
localhost nagios: SERVICE ALERT: localhost;HTTP;CRITICAL;SOFT;1;Connection refused
localhost nagios: SERVICE EVENT HANDLER: Nagios3;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
localhost nagios: SERVICE ALERT: Nagios3;HTTP;CRITICAL;SOFT;2;Connection refused
localhost nagios: SERVICE EVENT HANDLER: Nagios3;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
localhost nagios: SERVICE ALERT: Nagios3;HTTP;CRITICAL;SOFT;3;Connection refused
localhost nagios: SERVICE EVENT HANDLER: Nagios3;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
localhost nagios: SERVICE ALERT: Nagios3;HTTP;OK;SOFT;4;HTTP OK HTTP/1.1 200 OK - 1494 bytes in 0.021 seconds
localhost nagios: SERVICE EVENT HANDLER: Nagios3;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
localhost nagios: SERVICE ALERT: Nagios3;HTTP;CRITICAL;SOFT;1;Connection refused
localhost nagios: SERVICE EVENT HANDLER: Nagios3;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
localhost nagios: SERVICE ALERT: Nagios3;HTTP;CRITICAL;SOFT;2;Connection refused
localhost nagios: SERVICE EVENT HANDLER: Nagios3;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
localhost nagios: SERVICE ALERT: Nagios3;HTTP;CRITICAL;SOFT;3;Connection refused
localhost nagios: SERVICE EVENT HANDLER: Nagios3;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
localhost nagios: SERVICE ALERT: Nagios3;HTTP;OK;SOFT;4;HTTP OK HTTP/1.1 200 OK - 1494 bytes in 0.003 seconds :o
localhost nagios: SERVICE EVENT HANDLER: Nagios3;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$

Now, it works ! Thanks !
But, i don’t understand i have a Nagios3;HTTP;(null);(null);(null).
I would have Nagios3;HTTP;(CRITICAL);(SOFT);(3)
And i just use the example of Nagios doc ! http://nagios.sourceforge.net/docs/3_0/eventhandlers.html

Now, it works but i can’t understand why …
Thanks a lot for your help.


#5

As I said before, the (null) is a red herring - it represents the value of the macro at the time the log entry was written - in the documentation it is written [blockquote]"These macros are not valid for the service they are associated with when that service is being checked (i.e. they make no sense, as they haven’t been determined yet). " [/blockquote]- at the time the event handler runs, the macro’s have been populated with the values obtained from the check. Anyway, glad it’s up and running.

/S