Windows Service Check - event handler

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
warnox
Posts: 39
Joined: Thu Nov 20, 2014 5:22 am

Windows Service Check - event handler

Post 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.
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Windows Service Check - event handler

Post 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?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
warnox
Posts: 39
Joined: Thu Nov 20, 2014 5:22 am

Re: Windows Service Check - event handler

Post 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
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Windows Service Check - event handler

Post by tmcdonald »

Maybe I just need my coffee, but it looks like everything is working?

If so, are we clear to close this thread?
Former Nagios employee
warnox
Posts: 39
Joined: Thu Nov 20, 2014 5:22 am

Re: Windows Service Check - event handler

Post by warnox »

Yep all good now :)
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Windows Service Check - event handler

Post 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.
Former Nagios Employee
Locked