Page 1 of 4

Event Handler Script not working.

Posted: Mon Jan 28, 2019 9:54 am
by Figment
Hi All,

I'm trying to get an event handler enabled on some of our "non-critical" hosts that automatically acknowledges them if they go down. I.e hosts that we want to monitor but don't need to be alerted on our maps and notifications when they go down.

Currently I have the host defined as below;

Code: Select all

define host{
use             	         B-Hosts,host-pnp
host_name       	         TEST_PRINTER
alias           	         TEST_PRINTER
address          	         172.21.141.29
check_command    	 check-host-alive
hostgroups      	         printers
icon_image      	         printer.png
event_handler		 auto-acknowledge
}
The command in "commands.cfg" defined as;

Code: Select all

define command{
        command_name    auto-acknowledge
        command_line    $USER1$/auto-acknowledge.sh $HOSTSTATE$ $HOSTSTATETYPE$ $HOSTATTEMPT$ $HOSTNAME$
        }
And finally the "auto-acknowledge.sh" as

Code: Select all

#!/bin/sh
#
# Event handler script for acknowledging non critical hosts. 
#
#

# What state is the Host  in?
case "$1" in
UP)
	;;
UNREACHABLE)
	;;
DOWN)
	case "$2" in

	SOFT)

		case "$3" in

		1)
			now=`date +%s`
			commandfile='/usr/local/nagios/var/rw/nagios.cmd'
			/bin/printf "[%lu] ACKNOWLEDGE_HOST_PROBLEM;$4;2;1;1;Autobot;Non-critical host, outage acknowledged\n" $now > $commandfile    
			;;
			esac
		;;

	HARD)
		now=`date +%s`
		commandfile='/usr/local/nagios/var/rw/nagios.cmd'
		/bin/printf "[%lu] ACKNOWLEDGE_HOST_PROBLEM;$4;2;0;1;Autobot;Non-critical host, outage acknowledged\n" $now > $commandfile    
		;;
	esac
	;;
esac
exit 0
The "enable_event_handlers=1" option in nagios.cfg is enabled so I don't understand why it wont work? Does anyone have any ideas? I know my script could be wrong as I'm still very new to bash scripts however it appears to work fine when entered into the terminal.

Thanks

Re: Event Handler Script not working.

Posted: Tue Jan 29, 2019 8:36 am
by scottwilkerson
Hmm, you are referencing $now & $commandfile variables in the script but do not have them defined.

Also, your host does not have the following which would be required for it to trigger

Code: Select all

event_handler_enabled    1

Re: Event Handler Script not working.

Posted: Tue Jan 29, 2019 11:13 am
by Figment
Hi Scott,

They are defined using the below before calling the command.

Code: Select all

now=`date +%s`
commandfile='/usr/local/nagios/var/rw/nagios.cmd'
/bin/printf "[%lu] ACKNOWLEDGE_HOST_PROBLEM;$4;2;1;1;Autobot;Non-critical host, outage acknowledged\n" $now > $commandfile 
I'll try enabling event_handler_enabled 1 now however I thought if that was defined globally in nagios that it wasn't required.

Thanks!

Re: Event Handler Script not working.

Posted: Tue Jan 29, 2019 11:22 am
by scottwilkerson
Figment wrote:They are defined using the below before calling the command.

Code: Select all

now=`date +%s`
    commandfile='/usr/local/nagios/var/rw/nagios.cmd'
    /bin/printf "[%lu] ACKNOWLEDGE_HOST_PROBLEM;$4;2;1;1;Autobot;Non-critical host, outage acknowledged\n" $now > $commandfile 
Not sure how they missed that.
Figment wrote: I'll try enabling event_handler_enabled 1 now however I thought if that was defined globally in nagios that it wasn't required.
You cannot enable event handlers to run per host globally, you can only turn on or off the ability for any to run globally, but they still need to be defined in the host/service object.

Re: Event Handler Script not working.

Posted: Tue Jan 29, 2019 12:11 pm
by Figment
Hi Scott,

Did not realise it needed enabling per host, thanks for that. I added that to the host however it still isn't running the script. Heres the snipit from nagios.log
[1548781032] Warning: Check of host 'TEST_PRINTER' timed out after 30.01 seconds
[1548781032] HOST EVENT HANDLER: TEST_PRINTER;DOWN;SOFT;3;auto-acknowledge
[1548781032] wproc: HOST EVENTHANDLER job 2641 from worker Core Worker 32546 is a non-check helper but exited with return code 13
[1548781032] wproc: early_timeout=0; exited_ok=1; wait_status=3328; error_code=0;
[1548781032] wproc: stderr line 01: execvp(/usr/local/nagios/libexec/auto-acknowledge.sh, ...) failed. errno is 13: Permission denied
[1548781032] wproc: Core Worker 32545: job 2589 (pid=16791): Dormant child reaped
[1548781039] wproc: Core Worker 32543: job 2604 (pid=16899) timed out. Killing it
My issue looks to be this "Permission Denied", the file "auto-acknowledge" has the owner "nagios" (as does the "libexec" directory) however all other files in the directory have the owner set to "root". The folder "/usr/nagios" is also set to owner root.

Any ideas as to what this should be set too in terms of permissions to allow access?

Re: Event Handler Script not working.

Posted: Tue Jan 29, 2019 12:32 pm
by scottwilkerson
Can you show the permissions of the file

Code: Select all

ls -l /usr/local/nagios/libexec/auto-acknowledge.sh
this needs to be executable by the nagios user

Re: Event Handler Script not working.

Posted: Tue Jan 29, 2019 12:39 pm
by Figment
The permisions list is as below
-rw-r--r--. 1 nagios nagios 693 Jan 28 14:42 /usr/local/nagios/libexec/auto-acknowledge.sh
Looks okay, I think?

Re: Event Handler Script not working.

Posted: Tue Jan 29, 2019 12:41 pm
by scottwilkerson
Figment wrote:Looks okay, I think?
no, please run the following

Code: Select all

chmod +x /usr/local/nagios/libexec/auto-acknowledge.sh
Then is should work

Re: Event Handler Script not working.

Posted: Tue Jan 29, 2019 12:55 pm
by Figment
Hi Scott,

Just to check should this be ran as "root" or as "nagios".

Thanks

Re: Event Handler Script not working.

Posted: Tue Jan 29, 2019 1:14 pm
by scottwilkerson
the chmod command should be run as root