Page 1 of 1

Windows Service Check - event handler

Posted: Sun Nov 08, 2015 6:39 pm
by warnox
Hi,

I want to set up an event handler to restart a stopped Windows service but I'm not sure exactly how the process works. Currently, I'm using one service definition to check the status of multiple Windows services, via https://www.itefix.net/check_winservice.

Code: Select all

define service {
        use                             generic_passive_service
        service_description             Server Services
        hostgroup_name                  servergroup1
        check_command                   winsrv_check_stopped_services!Spooler,MSSQLSERVER!auto
}
I've read over the documentation below but I cannot find anything which describes how the service name is passed to the event_handler script. From what I can see none of the arguments ($SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$) specify the service name?

https://assets.nagios.com/downloads/nag ... dlers.html
http://songwf100.blogspot.co.uk/2013/03 ... ndows.html

Thanks for any help.

Re: Windows Service Check - event handler

Posted: Sun Nov 08, 2015 8:30 pm
by Box293
I have two documents here that are written for XI, but it's all the same in relation to how it works with core.

Here's one specifically on restarting a service:
https://assets.nagios.com/downloads/nag ... ios-XI.pdf

This one is more of an introduction to event handlers:
https://assets.nagios.com/downloads/nag ... ios-XI.pdf

Does this help you get it working?

Re: Windows Service Check - event handler

Posted: Mon Nov 09, 2015 4:23 am
by warnox
Thanks for your reply. I've already read the first document on your list and that's what I based my testing on. I guess what I want to know is,

1. Does each service definition only need to be checking for one Windows service state? Currently I'm checking all Windows services with one command so I'm not sure how that would work.

EDIT: Got it to work but had to modify the command which calls the bash script. $_SERVICESERVICE$ did not work and I couldn't find that macro in any documentation, changing this to "$SERVICEDESC$" fixed the problem. Included quote marks so service names which spaces in them can also be processed.

Code: Select all

define service {
        use                             generic_passive_service
        service_description             Print Spooler
        hostgroup_name                  testsrv
        check_command                   winsrv_check_services!Print Spooler
        event_handler                   winsrv_restart_service
}

Code: Select all

define command {
        command_name winsrv_restart_service
        command_line $USER1$/restart_service.sh $SERVICESTATE$ $HOSTADDRESS$ "$SERVICEDESC$"
}
restartservice.sh

Code: Select all

/#!/bin/sh
# Event Handler for Restarting Windows Services
case "$1" in
	OK)	
		;;
	WARNING)
		;;
	UNKNOWN)
		;;
	CRITICAL)	

		/usr/local/nagios/libexec/check_nrpe -H "$2" -c restart_service -a "$3"
	;;
esac
		
exit 0

Re: Windows Service Check - event handler

Posted: Mon Nov 09, 2015 1:37 pm
by tmcdonald
Maybe I just need my coffee, but it looks like everything is working?

If so, are we clear to close this thread?

Re: Windows Service Check - event handler

Posted: Tue Nov 10, 2015 11:15 am
by warnox
Yep all good now :)

Re: Windows Service Check - event handler

Posted: Tue Nov 10, 2015 11:33 am
by rkennedy
Thanks for the update. I will now close this thread, but feel free to open another in the future if you ever need assistance.