CheckServiceState Script CheckServiceState.vbs.txt
If you ever wanted more control how to monitor windows services with Operations Manager 2007 you most likely realized, that you have to do it all by yourself with a custom script.
I had at least two major problems which forced me to use a custom script:
- It seems that you cannot monitor the status of a service which has the startup type set to “Manual”. (There is a parameter in the overrides dialog called “Alert only if service startup type is automatic” but it doesn’t have any effect, the health state simply doesn’t change)
- When using templates to monitor a windows service you cannot override the monitor to only monitor a subset of services using a group. The group just doesn’t show up in the list
So this is my approach to windows service monitoring:
First of all, let’s set up a Windows Service monitoring using the standard Management Pack Templates Wizard. In this example: “Print Spooler”. In the monitoring wizard select or create a destination management pack, here: “Windows Service Monitoring”.
I use the template wizard because I can use the discovered service class to target my own script and I get all the other goodies (performance monitors, baseline rules, etc).
After a while the service you just configured to monitor will be discovered. Now let’s setup a group:
Create a new group with a name like “Print Spooler Service Group” in the “Windows Service Monitoring” management pack. In this example I use explicit group members but you can also use dynamic membership of course. The beauty is, that your group can (and should) contain the discovered service instances:
Next, bring up the monitors the template wizard creates for us:
There you should see the (very inflexible and buggy) availability monitor:
Right-click on the “Service Running State” monitor and select “Overrides > Override the Monitor > For all objects of type: Print Spooler”. In the Override Properties, disable the monitor (check “Override” for the Parameter Name “Enabled” and change the “Override Setting” to “False”).
Next, right-click on the “Availability” node and select “Create a monitor > Unit Monitor…”. In the unit monitor dialog, select the “Scripting > Generic > Timed Script Two State Monitor” as monitor type and place it in the same Management Pack “Windows Service Monitoring”.
In the General properties page we can now target the script to the “Print Spooler” class. Also be sure to disable the monitor by default (We enable it later using a group override).
Pick a schedule which makes sense to you (I recommend every 5 minutes but depending on your environment I suggest that you don’t go below 2 minutes).
In the Script page, provide a script name, like CheckServiceState.vbs and paste the attached script into the script area.
The script takes 3 parameters (documented in the script) but for a standard scenario you can just use:
“$Target/Property[Type=”MicrosoftSystemCenterNTServiceLibrary!Microsoft.SystemCenter.NTService”]/DisplayName$” “Running” “Auto”
Because we already target the print spooler class we can use a dynamic parameter to pass on the service name. Parameter 2 and 3 (which is optional) can be used to define the desired status and startup type. Please be sure to encapsulate each parameter in double-quotes – just to be on the safe side.
Next let’s define the health expressions:
And finally the alert:
I suggest an alert description like:
The state of the monitored service ‘$Target/Property[Type=”MicrosoftSystemCenterNTServiceLibrary!Microsoft.SystemCenter.NTService”]/DisplayName$’ on ‘$Target/Host/Property[Type=”Windows!Microsoft.Windows.Computer”]/NetworkName$’ is invalid: $Data/Context/Property[@Name=’AlertText’]$
After you created your monitor, enable it using an override for the group we created earlier.
As last (optional) step I suggest to create a state view containing the Print Spooler instances and scoped to the group where we enabled the monitor.
Of course, it would be much better if that functionality and flexibility is provided by OpsMgr out of the box…
CEO of code4ward GmbH and Windows lead developer at http://www.royalapps.com where most of the time is spent on Royal TS, a multi platform, multi protocol remote management solution, for Windows, macOS and mobile supporting RDP, VNC, SSH, Telnet, and many more.
Long time Microsoft MVP supporting communities on- and offline as well as speaking at user groups and conferences about DevOps and other software development topics.