Event handler

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
lord_hypnos
Posts: 13
Joined: Thu Aug 01, 2013 8:52 am

Event handler

Post by lord_hypnos »

Hello,
I want run my own script when service fail so i used this tutorial:
http://assets.nagios.com/downloads/nagi ... ndlers.pdf

My reset_tomcat.sh file:

Code: Select all

#!/bin/bash
SERVICESTATE=$1 
SERVICESTATETYPE=$2 

if [$SERVICESTATETYPE == 'SOFT']
then exit
fi

case "$SERVICESTATE" in
	OK)
		exit 0
		;;
	WARNING)
		net rpc service stop SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD
		net rpc service start SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD
		;;
	CRITCAL)
		net rpc service stop SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD
		net rpc service start SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD
		;;
	UNKNOWN)
		net rpc service stop SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD
		net rpc service start SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD
		;;
esac
But my problem is that this script is not running when Nagios detect failure service.

Command Management:
Image

Service Management:
Image

Log(How You can see notification is send properly but event is not trigger):
Image

Thank You for answer!
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Event handler

Post by abrist »

Lets enable event handler logging, if it is not already enabled. Edit /usr/local/nagios/etc/nagios.conf
Add or edit the log_event_handlers line so that it reads:

Code: Select all

log_event_handlers=1
Restart nagios:

Code: Select all

service nagios restart
Are you sure the script is not running? You may want to add an echo-to-file to the top of the script so that you know for sure if it ran or not:

Code: Select all

echo "event handler run" > /tmp/eventhandler.tmp
What are the permissions on your script?

Code: Select all

ls -la /usr/local/nagios/libexec/reset_tomcat.sh
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
lord_hypnos
Posts: 13
Joined: Thu Aug 01, 2013 8:52 am

Re: Event handler

Post by lord_hypnos »

abrist wrote:Lets enable event handler logging, if it is not already enabled. Edit /usr/local/nagios/etc/nagios.conf
Add or edit the log_event_handlers line so that it reads:

Code: Select all

log_event_handlers=1
Restart nagios:

Code: Select all

service nagios restart
Done.
abrist wrote: Are you sure the script is not running? You may want to add an echo-to-file to the top of the script so that you know for sure if it ran or not:

Code: Select all

echo "event handler run" > /tmp/eventhandler.tmp
I added but file not created so i think is not ran.
abrist wrote: What are the permissions on your script?

Code: Select all

ls -la /usr/local/nagios/libexec/reset_tomcat.sh
In Your guide is this code :

Code: Select all

chmod +x event_handler_test.sh
and i used it ;) Also i can run this code via ./reset_tomcat.sh

Is notification enabled is necessary to trigger event handler?
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Event handler

Post by abrist »

lord_hypnos wrote:and i used it ;) Also i can run this code via ./reset_tomcat.sh
Can you run it as the "nagios" user though?

Code: Select all

ls -la /usr/local/nagios/libexec/reset_tomcat.sh
Now that you have logging setup, check the XI Event Log for "external" commands or "events", or just tail/grep the nagios.log.

While testing, it may be advantageous to turn on "volatile" for the service so that the handler is run every check (to see if that file gets written).

EDIT: Are event handlers enabled (set to 1)?

Code: Select all

grep event /usr/local/nagios/etc/nagios.cfg
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
lord_hypnos
Posts: 13
Joined: Thu Aug 01, 2013 8:52 am

Re: Event handler

Post by lord_hypnos »

abrist wrote:
lord_hypnos wrote:and i used it ;) Also i can run this code via ./reset_tomcat.sh
Can you run it as the "nagios" user though?

Code: Select all

ls -la /usr/local/nagios/libexec/reset_tomcat.sh
I have only root account at this moment. And i ran this script as root.
abrist wrote: Now that you have logging setup, check the XI Event Log for "external" commands or "events", or just tail/grep the nagios.log.

While testing, it may be advantageous to turn on "volatile" for the service so that the handler is run every check (to see if that file gets written).
Ok it's look like now event handler work properly but still nothing write to /tmp/eventhandler.tmp file. I dont have idea why.

Code: Select all

Event Handler2013-08-13 08:44:05SERVICE EVENT HANDLER: xxx.xxx.xx;HTTP check document;CRITICAL;HARD;5;reset_tomcat
Event Handler2013-08-13 08:44:05GLOBAL SERVICE EVENT HANDLER: xxx.xxx.xx;HTTP check document;CRITICAL;HARD;5;xi_service_event_handler
Service Critical2013-08-13 08:44:05SERVICE ALERT: xxx.xxx.xx;HTTP check document;CRITICAL;HARD;5;Connection refused
Event Handler2013-08-13 08:43:15SERVICE EVENT HANDLER: xxx.xxx.xx;check tomcat session;UNKNOWN;HARD;3;reset_tomcat
Event Handler2013-08-13 08:43:15GLOBAL SERVICE EVENT HANDLER: xxx.xxx.xx;check tomcat session;UNKNOWN;HARD;3;xi_service_event_handler
Service Unknown2013-08-13 08:43:15SERVICE ALERT: xxx.xxx.xx;check tomcat session;UNKNOWN;HARD;3;Tomcat sessions edmsdirect UNKNOWN:
Event Handler2013-08-13 08:43:05SERVICE EVENT HANDLER: xxx.xxx.xx;HTTP check document;CRITICAL;SOFT;4;reset_tomcat
Event Handler2013-08-13 08:43:05GLOBAL SERVICE EVENT HANDLER: xxx.xxx.xx;HTTP check document;CRITICAL;SOFT;4;xi_service_event_handler
Service Critical2013-08-13 08:43:05SERVICE ALERT: xxx.xxx.xx;HTTP check document;CRITICAL;SOFT;4;Connection refused
abrist wrote: EDIT: Are event handlers enabled (set to 1)?

Code: Select all

grep event /usr/local/nagios/etc/nagios.cfg

Code: Select all

grep event /usr/local/nagios/etc/nagios.cfg
OUTPUT:

Code: Select all

log_event_handlers=1
global_host_event_handler=xi_host_event_handler
global_service_event_handler=xi_service_event_handler
enable_event_handlers=1
event_broker_options=-1
event_handler_timeout=30
EDIT:
All works. Script is triggering now. Thank You so much for help! Again:)
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Event handler

Post by slansing »

Awesome!
Locked