Window's Service Restart

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
baron164
Posts: 7
Joined: Wed Jun 22, 2016 1:08 pm

Window's Service Restart

Post by baron164 »

I am working on having our Nagios install restart Windows Services automatically. I have it working but I've run into a minor issue. Some services have breaks in the name. For example, instead of just something like "DNSCache" or "Netlogon" the service is named "Vendor Service Host Manager". And yes, that is the actual service name and there is no shortened name. So the issue I have is that I'm unable to get the servicerestart.sh script to pass the full name through to the runcmd.bat file on the client machine. I tried using quotes, breaks to remove the spaces etc but so far nothing has worked. It looks like windows is only receiving the "Vendor" part of the service name and no the rest of it. I'm wondering if anyone has had this issue before and possibly has a fix. It seems like there should be some kind of syntax fix for this.

The command I'm running from our Nagios box is this:
./servicerestart.sh Critical 192.168.0.1 "Vendor Service Host Manager"

And it always returns the "Service Name is invalid" message.

I can run ./servicerestart.sh Critical 192.168.0.1 DnsCache or Netlogon and it works perfectly. It's only when I try to use a service name with spaces in it.


I've tried the following combinations.

"Vendor Service Host Manager"
"Vendor\ Service\ Host\ Manager"
VendorServiceHostManager
Vendor_Service_ Host_Manager
'Vendor Service Host Manager'
'"Vendor Service Host Manager"'
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Window's Service Restart

Post by ssax »

Please refer to this post for a solution:

Code: Select all

https://support.nagios.com/forum/viewtopic.php?p=146177#p146177
Let us know if you have any questions or if you run into problems.
baron164
Posts: 7
Joined: Wed Jun 22, 2016 1:08 pm

Re: Window's Service Restart

Post by baron164 »

Thanks, I found that if I change the "%1" variable in the runcmd.bat to "%*" it works. I can run the script manually and everything works.

The only issue I have now is that for some reason when the service goes critical in Nagios the service_restart event handler does not appear to run the script.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Window's Service Restart

Post by ssax »

Event handlers are executed when a service or host:

- Is in a SOFT problem state
- Initially goes into a HARD problem state
- Initially recovers from a SOFT or HARD problem state
Taken from here:

https://assets.nagios.com/downloads/nag ... dlers.html

Was it already in a hard critical state? If it was, try resetting it back to OK before testing.


Thank you
baron164
Posts: 7
Joined: Wed Jun 22, 2016 1:08 pm

Re: Window's Service Restart

Post by baron164 »

I start with everything in a good state, green across the board. I manually shut down the service I'm monitoring. In Nagios I see that the service goes into a red (Critical) state and then nothing happens. From what I can tell it never fires off the event handler. I have "Event Handler Enabled" set to "On" and the Event Handler I created is selected.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Window's Service Restart

Post by ssax »

The first thing I do to verify that it is working is to edit the servicerestart.sh script and add this to the top line:

Code: Select all

echo "I ran" >> /tmp/eventhandlerrun.log
Then force an OK to CRITICAL, if you see it in the logs then it's something with your script further down or on the other side.

Please post your service definition and your command definition from your /usr/local/nagios/var/objects.cache so that I may review it.

The usual culprit is that the command is wrong (Configure > Core Config Manager > Commands).
baron164
Posts: 7
Joined: Wed Jun 22, 2016 1:08 pm

Re: Window's Service Restart

Post by baron164 »

Ok, I modified the servicerestart.sh script and when I checked the log it showed "I ran" for each of the failed check attempts.

The service_restart command is setup with this Command line.

$USER1$/servicerestart.sh $SERVICESTATE$ $HOSTADDRESS$ $_SERVICESERVICE$

If I login as the service account or as root to the Nagios server and run (./servicerestart.sh CRITICAL 192.168.0.1 "Service Name") it works fine.

I checked the Variable Definition under the service check and confirmed that it's set to this:
Variable Name = _SERVICE
Variable Definition = Service Name

I tried both using quotes around the service name and without quotes and it didn't make a difference.

The objects.cache file is rather larger so I'll have to dig through to find that info.
User avatar
tgriep
Madmin
Posts: 9190
Joined: Thu Oct 30, 2014 9:02 am

Re: Window's Service Restart

Post by tgriep »

Can you post your servicerestart.sh script.
The command definition I use in my script is as follows

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H "$2" -p 5666 -c runcmd -a "$3"
and I used double quotes around the service name and when I tested it, it restarted for me.
Be sure to check out our Knowledgebase for helpful articles and solutions!
baron164
Posts: 7
Joined: Wed Jun 22, 2016 1:08 pm

Re: Window's Service Restart

Post by baron164 »

I'm using an unaltered script that I downloaded from Nagios's site. I'm fairly certain that the issue lies with the fact that the server name has spaces in it.

Also, if I run this command manually (./check_nrpe -H "192.168.1.41" -p 5666 -c runcmd -a "Vendor Service Manager") the service restarts properly

case "$1" in
OK)
;;
WARNING)
;;
UNKNOWN)
;;
CRITICAL)

/usr/local/nagios/libexec/check_nrpe -H "$2" -p 5666 -c runcmd -a "$3"
;;
esac

exit 0
User avatar
tgriep
Madmin
Posts: 9190
Joined: Thu Oct 30, 2014 9:02 am

Re: Window's Service Restart

Post by tgriep »

The command in the servicerestart.sh looks correct. We are going to need more information from you.
Can you post the NSClient's ini file and the runcmd.bat file from the Windows system?
Can you post how the service is defined on the Nagios system?
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked