I'm looking for a way, custom sensor or not, to be able to monitor a list of windows services without having to have them all created as a separate sensor and then monitored. Would also need this to be able to restart services if they fail etc. Is this possible or could it be possible in future updates?
How to monitor multiple windows services in 1 sensor?
Votes:
0
11 Replies
Votes:
3
This can be done with a batch file as Custom Sensor
Create a new .bat file in de "\Custom Sensors\EXE" folder of your PRTG installation and paste the following code:
@echo off rem **names of services to check set services="MpsSvc;eventlog;WinDefend" rem **number of services to check set amount=3 set /a count=0 :loop set /a count+=1 FOR /F "tokens=%count% delims=;" %%s IN (%services%) DO set service=%%s sc query %service% | find "RUNNING" > nul if %errorlevel% GTR 0 goto restart goto next :restart sc start %service% > nul ping 1.1.1.1 -w 3000 > nul sc query %service% | find "RUNNING" > nul if %errorlevel% GTR 0 goto error goto next :error echo 1:Could not restart service %service% goto exit :next if %count% == %amount% goto end goto loop :end echo 0:All %amount% services are running goto exit :exit
Replace the set services= line with a list of services you want to check and replace the amount= line with the number of services in the list.
The batch file assumes that the services to be checked are running on the local computer (PRTG server or remote probe). If they are running on another computer, replace
sc start
with
sc \\computer start
and
sc query
with
sc \\computer query
Note: the sensor must run with credentials that can actually restart the services.
Created on Jan 6, 2011 3:53:02 PM
Last change on Jul 28, 2011 3:12:08 PM by
Torsten Lindner [Paessler Support]
Votes:
0
Gerard,
That was exactly what I was looking for! Still in the early phases of basically playing around with the idea of using this method over others but that works perfectly. I did a little tweaking to make it log when it restarts and go into warning/error but works like a charm.
Thanks!
Votes:
0
Here are a few things I changed to the above script to help it better fit my needs:
If the servers could not be restarted I echo'd 2 so that sensor would go to a down state. echo 2:Could not restart service %service%
I wanted also to be able to log if a service needed to be restarted. So after initiating the restart I echo'd which service was restarted and a return code of 1 to put the sensor in a warning state. I also increased the ping timeout to allow the warning to go into effect. This doesn't affect the time before the service was attempted to be restarted but allowed me to get my logging info. echo 1:Attempting restart of %service%
Last but not the least, the most important part and what I completely skipped for the first half hour, was to turn on alerting/monitoring in the custom sensor if the value changes. And then adjust alerting so that values above 0 = warning, and above 1 = down.
This way the sensor would go into a warning state when a service needed to be restarted and then go into a down state if it did not restart successfully.
Votes:
0
Hi sschill,
Please feel free to change the script to your needs anyway you like. It basically was indented as a template to get you going. There are however some directions you must follow.
If I understand it correctly, with your adjustments to the script it is possible when a service is attempted to restart, but fails, there are two lines echoed to the command line:
1:Attempting restart of %service% 2:Could not restart service %service%
This will work in PRTG V7, but is not accepted in PRTG V8 anymore. In fact it was never accepted for a sensor to write more than one line as its output, but so far tolerated in V7.
Votes:
0
Hi
Can anyone tell me how I make this sensor go to an alarm state (i.e flash red and send an email)
Many thanks for any help provided
Mark
Votes:
0
The sensor returns "1" if a service could not be restarted
echo 1:Could not restart service %service%
You can use the channels tab to enable limits.
e.g. Set the sensor to an error state if it returns a value > 0
Votes:
0
Runs great on a probe device (keeps the mes down) - but cannot get it to run on a non-probe device
I can see the advice to change some of the lines of code - but there is no line "sc check " Any advice on this
Thanks
Mark
Votes:
0
Good point, that should have been "sc query"
I also changed this in the example above.
Votes:
0
This does not work with PRTG 15.3.x and a remote probe (W2k8R2). I run the script on a remote probe (W2k8R2) to check another server (W2k8R2) in the remote Network and Change the line "sc query" to "sc
IP or
HOSTNAME query", i try both but in PRTG i receive the message "Could not restart service MSSQLSERVER"
Did you have a hint for me how can i fix this?
Thanks Chris
Votes:
2
Solved by changing the Security Context of specified Sensor from "Use security context of probe service " to "Use Windows credentials of parent device"
Regards, Chris
Add comment