How to start multiple services using Event handlers

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
computerone
Posts: 21
Joined: Thu Jan 03, 2013 9:04 pm

How to start multiple services using Event handlers

Post 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
npolovenko
Support Tech
Posts: 3457
Joined: Mon May 15, 2017 5:00 pm

Re: How to start multiple services using Event handlers

Post 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?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
computerone
Posts: 21
Joined: Thu Jan 03, 2013 9:04 pm

Re: How to start multiple services using Event handlers

Post 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
npolovenko
Support Tech
Posts: 3457
Joined: Mon May 15, 2017 5:00 pm

Re: How to start multiple services using Event handlers

Post 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.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
computerone
Posts: 21
Joined: Thu Jan 03, 2013 9:04 pm

Re: How to start multiple services using Event handlers

Post 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$
}
You do not have the required permissions to view the files attached to this post.
npolovenko
Support Tech
Posts: 3457
Joined: Mon May 15, 2017 5:00 pm

Re: How to start multiple services using Event handlers

Post 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.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
computerone
Posts: 21
Joined: Thu Jan 03, 2013 9:04 pm

Re: How to start multiple services using Event handlers

Post by computerone »

That does make sense. Thanks for your help.
npolovenko
Support Tech
Posts: 3457
Joined: Mon May 15, 2017 5:00 pm

Re: How to start multiple services using Event handlers

Post by npolovenko »

@computerone, No problem. I will go ahead and lock this thread as resolved.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked