Page 1 of 2

Passing Failed Service Name To Event Handler

Posted: Wed Feb 24, 2021 9:44 am
by jimmlegs
I have a Windows service check that checks on all services set to start automatically and I am unable to figure out how to pass the failed service name back to the event handler to restart it. I am only interested in restarting a handful of services and I have tried adding them each as a free variable but the handler only starts the first one listed.
What is the best way to go about handling what I would like to achieve?
What is the easiest way to go about handling what I would like to achieve?

The event handler itself is working and my hopes are to keep as much of this in the NagiosXI interface as possible.

Thanks

Re: Passing Failed Service Name To Event Handler

Posted: Wed Feb 24, 2021 6:12 pm
by benjaminsmith
Hi,

Since it's working for the first variable, there must be an error passing the free variables to the event handler to re-start the correct service.

What plugin are you using to check these services? Can you upload the event handler and we can take a look? Please note, we do not provide custom development in support but we can provide guidance and suggested solutions.

Regards, Benjamin

References
Restarting Windows Services With NCPA
Introduction to Event Handlers
Standard Macros in Nagios

Re: Passing Failed Service Name To Event Handler

Posted: Thu Feb 25, 2021 4:03 pm
by jimmlegs
Event Handler:
#!/bin/sh
case "$1" in
OK)
;;
WARNING)
;;
UNKNOWN)
;;
CRITICAL)
/usr/local/nagios/libexec/check_nrpe -H "$2" -p 5666 -c restart_service -a "$3"
;;
esac
exit 0



Service Definition:
define service {
service_description NRPE Check Service States - No Alert
use an-windows-non-production-standard-no-alert
display_name NRPE Check Service States - No Alert
check_command check_nrpe!check_service "top-syntax=%(list)" "detail-syntax=%(name):%(state)"!-a "exclude=gupdate" "exclude=edgeupdate" "exclude=clr_optimization_v4.0.30319_32" "exclude=clr_optimization_v4.0.30319_64" "exclude=MMCSS" "exclude=sppsvc" "exclude=RemoteRegistry" "exclude=stisvc" "exclude=CDPSvc" "exclude=tiledatamodelsvc" "exclude=ShellHWDetection" "exclude=WbioSrvc" "exclude=MapsBroker" "exclude=wuauserv" "exclude=TBS" "exclude='NetBackup SAN Client Fibre Transport Service'" "exclude=TrustedInstaller" "exclude=NetBackup Vault Manager" "exclude=NetBackup Key Management Service" "exclude=BMR Master Service" "exclude=SolarwindsSyslogService" "exclude=SolarWindsTrapService" "exclude=BMR Boot Service" "exclude=iQLManagementAgentJava" "exclude=QLManagementAgentJava" "exclude=QLogicManagementSuitenQLRemote" "exclude=Audiosrv" "exclude=SNARE" "exclude=BITS" "exclude=MSExchangeNotificationsBroker" exclude="BOEXI40Agent" exclude="BOEXI40AutomationFramework" exclude="SVNSubversion" exclude="VMwareIDMService" exclude="MSComplianceAudit" "filter=start_type = 'auto' and name not like 'OneSyncSvc_' and name not like 'CDPUserSvc_'"!!!!!!
event_handler Handler Windows - Service Restart
event_handler_enabled 0
register 1
}

From nsclient log, script tries to restart "jamsagent" and not the "w32time" service:
2021-02-25 15:59:03: debug:c:\source\master\modules\CheckExternalScripts\CheckExternalScripts.cpp:607: Command line: scripts\restart_service.bat "jamsagent"

Thank you

Re: Passing Failed Service Name To Event Handler

Posted: Mon Mar 01, 2021 11:28 am
by benjaminsmith
Hi,

I'm assuming you are passing the output from the plugin to the restart service script. What macro are you using to pass the information to the event handler? Have you confirmed that the list of stopped services is being passed to the remote system? It looks like either the correct data is not getting passed or it's not getting parsed correctly on the remote system.

--Benjamin

Re: Passing Failed Service Name To Event Handler

Posted: Mon Mar 01, 2021 3:34 pm
by jimmlegs
This is the command syntax:
$USER1$/restart_service.sh $SERVICESTATE$ $HOSTADDRESS$ $_SERVICESERVICE$

Re: Passing Failed Service Name To Event Handler

Posted: Tue Mar 02, 2021 11:09 am
by benjaminsmith
Hi @jimmlegs,

Thanks, that makes sense. Have you tried to print out the contents of $_SERVICESERVICE$ in the restart_service script to make sure it's getting passed over correctly? It's not uncommon for special characters to cause parsing issues.

Re: Passing Failed Service Name To Event Handler

Posted: Thu Mar 04, 2021 12:28 pm
by jimmlegs
I have not, let me figure out how to do that and I will update with the results.

Re: Passing Failed Service Name To Event Handler

Posted: Thu Mar 04, 2021 3:26 pm
by jimmlegs
The data is being passed properly but it only works on the first service name configured in the misc settings, I can flip serviceA with serviceB but if serviceA stops it only attempts to start serviceB

define service {
host_name BOS-vBPIT2,SRV-vINFRAOPS
service_description TEST NRPE Check Service States - No Alert
display_name TEST NRPE Check Service States - No Alert
check_command check_nrpe!check_service "top-syntax=%(list)" "detail-syntax=%(name):%(state)"!-a "exclude=gupdate"
max_check_attempts 2
check_interval 1
retry_interval 2
check_period 24x7
event_handler Handler Windows - Service Restart
event_handler_enabled 1
notification_period 24x7
notification_options c,r,
contacts dgoldstein
_service jamsagent
_service w32time
register 1
}

Re: Passing Failed Service Name To Event Handler

Posted: Fri Mar 05, 2021 12:38 pm
by benjaminsmith
Hi @jimmlegs,

Looking over the service configuration, there are two free variables with the same name, that look like the issue. If you have a different service for each free variable, then it will be necessary to pass each of those to the event handler in the command definition.

Also, since the script won't necessarily know which service was stopped, are you passing in the output of the plugin to the event handler so this can be determined?

Hope that helps. Also, the following guide has some good information on creating event handlers in Nagios XI.

Introduction to Event Handlers

Re: Passing Failed Service Name To Event Handler

Posted: Wed Mar 10, 2021 5:27 pm
by jimmlegs
So how can I pass the output of the plugin to the event handler? I've been reviewing documentation but nothing has helped.

Thanks