I recently had a customer that wants to get an alert when a specific service is not Disabled and/or not Stopped. I used the following steps to accomplish this using a "Timed Script Three State Monitor". Even if you do not have this specific need, these steps can be used as a template for creating a monitor that uses a script to query WMI and change state or generate alerts based on the results. If you don't have a need for three states (Critical, Warning, Healthy), there is a Two State Monitor that can be used for this.
Create a new Monitor, select Scripting\Generic\Timed Script Three State Monitor
Give it a name, target, etc. (I targeted the Windows Computer class, but Windows Operating System may be a better choice). I try to make a habit of unchecking "Monitor is enabled" and enabling it with an override later....at least while testing it:
Set the schedule...this just depends on how quickly you want to know if the service gets changed:
Next, I used a basic VB script which accepts a service name as a parameter, queries WMI for the service, and puts the Service Name, State (Running, Stopped, etc.), and StartMode (Disabled, Manual, Automatic) into property bag values. The full text of the script is below the screenshot:
Dim oAPI, oBag,strComputerSet oAPI = CreateObject("MOM.ScriptAPI")Set oBag = oAPI.CreatePropertyBag()set oArgs=wscript.argumentsstrComputer="."ServName=oArgs(0)
Set namespace=GetObject("winmgmts:\\"& strComputer & "\root\cimv2")set servinfo=namespace.ExecQuery("select * from win32_service where name =" & """" & servname & """")
for each objservice in servinfo
Call oBag.AddValue("ServiceName",ServName)Call oBag.AddValue("State",objservice.State)Call oBag.AddValue("StartMode",objservice.StartMode)Call oAPI.Return(oBag)
For the script parameter, I just enter "ServiceName"....this will be replaced by an override later, or you can just enter your service name here:
Next, I set the "Unhealthy", "Degraded", and "Healthy" expressions for the monitor. My goal is to set the state to Warning when the service is Stopped but NOT Disabled , Critical when it is NOT Stopped, and Healthy when it is Stopped AND Disabled. I used the following expressions:
Parameter Name: Property[@Name='State']
Operator: Does not equal
Parameter Name: Property[@Name='StartMode']
Next, I used the default settings for Health State, since they already match what I want to do:
Next, I configure the alert settings. The settings in the screen shot below will generate a Warning alert when the monitor is in a Warning state (service is not Disabled), and a Critical alert when the monitor is in the Critical state (service is not Stopped). The Alert Description will have the service name (using the ServiceName property created by the script):
Now that I have the monitor created, I need to enable it and set the Override for the Service Name:
I'm using the Alerter service for my test:
To test the monitor, I first set the Alerter service to Manual Startup and leave it stopped:
Then I verify that I get the Warning alert:
Health Explorer correctly shows the "Degraded" Warning state:
Now I want to test the Critical state, so I start the Alerter Service:
Now the alert is changed to Critical:
And Health Explorer shows the "Unhealthy" Critical state:
When I stop the service and disable it, the alert is auto-resolved and the state is changed back to Healthy:
I've attached my sample MP which includes the following monitors:
Service disabled and stopped - two-state monitor:
If the specified service is not Stopped AND Disabled, the computer will be put in a Warning state and a Warning alert will be generated. When the service is stopped and disabled, the computer will be put in a Healthy state.
Service disabled and stopped - three-state monitor:
If the specified service is Stopped and is not Disabled, the computer will be put in a Warning state and a Warning alert will be generated. If the specified service is not Stopped, the computer will be put in a critical state and a Critical alert will be generated. When the service is stopped and disabled, the computer will be put in a Healthy state.
Both monitors are targeted at the Windows Computer class and roll up to the Configuration Health. Both monitors are disabled by default. They are configured to check the service every 1 minute. To enable one of the monitors, add an Override for the Computer or Group you wish to monitor and set the following Override parameters:
Script Arguments = <Service Name>
Thanks for the two/three state monitor sample. However I wonder if there is a way to add diagnostic task to automatically recover stopped service. Have yoe ever worked about it?
I haven't set this up, but you should just be able to add a diagnostic or recovery command with a command line of "net start <servicename>".
My question was not so clear. I plan to monitor several servers at a time. ex; services beginning with 'cisco' display name. Therefore I need to determine stopped service name and set service name with parameters within recovery task command lines without writing a custom script
I'm not sure if anybody is still looking at the comments here, BUT:
I'm looking to monitor the Volume Shadow Copy service, and I want OpsMgr to generate an alert when it's been in a running state for over 20 minutes (this generally will indicate that it's hung up); normally the service is started and stopped in the span of five minutes. How would I do this?
You would need to change the script to query Win32_process in root\cimv2 (instead of Win32_Service), and use the "CreationDate" property to see how long it has been running. I'm not 100% sure, but I think something like this will be included in the R2 release.
How do I monitor a specific service on a specific server without having to create a script? I feel like monitoring services in OpsMgr R2 should be rudimentary but it's unbelievably convoluted.
We do have simpler ways to monitor a service....this script was provided for a way to do more granular monitoring of the service state and startup type.
You can easily monitor a service by doing one of the following:
1. Go to Authoring\Management Pack Templates, run the "Add Monitoring" wizard and create a "Windows Service" monitor. This will monitor all agents that run the service.
2. Go to Authoring\Management Pack Objects\Monitors and create a new Unit Monitor, then select "Windows Services\Basic Service Monitor". This will allow you to target specific classes for the monitor.
Very nice, it give me a pointer how to monitoring if service hang in stopping state.
nicel,value add with respect to service Monitor creation.