Page 1 of 1

How to start multiple services using Event handlers

Posted: Fri Feb 09, 2018 12:07 am
by computerone
Hi community,

I managed to start service using nagios event handler but i need to find how to restart multiple services using one event handler.
e.g. we monitor multiple services like print spooler, Citrix Management service and others using one single nagios service cfg. See below.

define service {
service_description Windows Core Services
use COMP1_Default Settings
hostgroup_name OS-WIN-Core Monitoring
display_name SVC - Windows Services
check_command COMP1_check_services!'DNS Client',Server,eventlog,Workstation!!!!!!!
_xiwizard exchange
register 1
}

When passing arguments to eventhandler command under manage free variables. how to define other services. :mrgreen:
Any help will be greatly appreciated.

Thanks,
C1

Re: How to start multiple services using Event handlers

Posted: Fri Feb 09, 2018 11:14 am
by npolovenko
Hello, @computerone.Have you tried adding multiple services under Manage Free Variables?

Code: Select all

   name        value
_SERVICE service1
_SERVICE service2
_SERVICE service3
https://assets.nagios.com/downloads/nag ... ios-XI.pdf

What does your event handler script look like?

Re: How to start multiple services using Event handlers

Posted: Mon Feb 12, 2018 6:44 pm
by computerone
Event Handler script looks like this:

#!/bin/sh
#$1 defines service state, $2 defines hostname, $3 defines servicename to restart on windows server ,$4 defines servicestate, $5 defines hostdowntime, $6 defines servicedowntime

logfile=/usr/local/nagios/libexec/eventhandler.log
case "$1" in
OK)
;;
WARNING)
;;
UNKNOWN)
;;
CRITICAL)
if [[ "$4" == "HARD" ]] && [[ "$5" == "0" ]] && [[ "$6" == "0" ]]
then
/usr/local/nagios/libexec/check_nrpe -H "$2" -p 5666 -c restart_service -a "$3" >> $logfile
echo ServiceState $4 >> $logfile
echo HostDownTime $5 >> $logfile
echo ServiceDownTime $6 >> $logfile
fi
;;
esac
exit 0


I tried to add multiple variables but for some reason, it didnt work

Re: How to start multiple services using Event handlers

Posted: Tue Feb 13, 2018 2:51 pm
by npolovenko
@computerone, Where's the event handler reference in the service definition?

Code: Select all

define service {
service_description	Windows Core Services
use	COMP1_Default Settings
hostgroup_name	OS-WIN-Core Monitoring
display_name	SVC - Windows Services
check_command	COMP1_check_services!'DNS Client',Server,eventlog,Workstation!!!!!!!
_xiwizard	exchange
register	1
}	
Could you send in your Nagios XI System Profile so I can review it?
To send us your system profile. Login to the Nagios XI GUI using a web browser.
Click the "Admin" > "System Profile" Menu
Click the "Download Profile" button
Save the profile.zip file and upload it with your next post. Or you could upload the profile to a cloud storage of your choice and share a link with me in a pm. After that, please post something in this thread to bring it back up in the support queue.

Re: How to start multiple services using Event handlers

Posted: Tue Feb 13, 2018 5:55 pm
by computerone
Hi,

I have uploaded the profile with this post. Event handler is attached to a service called "SVC - Print Spooler Services".

define service {
host_name c1b1-backup.c1.local
service_description Print Spooler Services
use COMP1_Default Settings
check_command COMP1_check_services_test!"Print Spooler"!!!!!!!
event_handler Service Restart - Windows
event_handler_enabled 1
_SERVICE spooler
_xiwizard exchange
register 1
}

Name of Event Handler is "Service Restart - Windows".
define command {
command_name Service Restart - Windows
command_line $USER1$/restart_service.sh $SERVICESTATE$ $HOSTADDRESS$ $_SERVICESERVICE$ $SERVICESTATETYPE$ $HOSTDOWNTIME$ $SERVICEDOWNTIME$
}

Re: How to start multiple services using Event handlers

Posted: Wed Feb 14, 2018 4:09 pm
by npolovenko
@computerone, Right now you're passing all three services under the $_SERVICESERVICE$ variable. So all three service names get assigned to $3 in your event handler. Windows can not restart 3 processes at the same time using net start. So what you could do:
a) change the command to pass each service as a new argument to the event handler:

Code: Select all

define command {
command_name Service Restart - Windows
command_line $USER1$/restart_service.sh $SERVICESTATE$ $HOSTADDRESS$ $_SERVICESERVICE$ $_SERVICESERVICE$ $_SERVICESERVICE$  $SERVICESTATETYPE$ $HOSTDOWNTIME$ $SERVICEDOWNTIME$
}
Then in the event handler you run the command three times with a different argument==servicename each time. $4, $5, $6 in my example.

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H "$2" -p 5666 -c restart_service -a "$4" >> $logfile
/usr/local/nagios/libexec/check_nrpe -H "$2" -p 5666 -c restart_service -a "$5" >> $logfile
/usr/local/nagios/libexec/check_nrpe -H "$2" -p 5666 -c restart_service -a "$6" >> $logfile
But, as you can probably guess this won't work quite well because the event handler will have to restart all three services each time, there is no way for it to determine which one went down. Nagios will always pass all services to $_SERVICESERVICE$, not just the one that caused the check to go critical.

So either you can pass all of the services to the restart script on Windows and modify the windows script to dynamically determine which of three services is actually down, or just create a separate check for each service and use the same event handler.

Re: How to start multiple services using Event handlers

Posted: Wed Feb 14, 2018 7:15 pm
by computerone
That does make sense. Thanks for your help.

Re: How to start multiple services using Event handlers

Posted: Thu Feb 15, 2018 2:51 pm
by npolovenko
@computerone, No problem. I will go ahead and lock this thread as resolved.