News

SCOM MPs: Process Count Monitor

In my latest project I ran into a situation where I wanted to monitor the amount of processes of a specific executable. If you have worked with the SCOM Operations Manager Console before, you might know that a template exists for this particular scenario. However, this template will only discover the object if the process is already running. This wasn’t acceptable for this project so I decided to create my own monitor for the number of processes of an executable. The discovery was actually done using a registry entry (which I find very handy; but be aware of 64Bit operating systems, as I described in one of my other blog articles).

First off I needed a class which would be the target of the monitor. When the class is created you can create the monitor with this class as a target (you need to discover the object somehow though). Create a new script monitor with two states (or all three states, if you prefer to).

Use the following script when the wizard prompts you for a script:

' Process Count Script. Usage:script.vbs
Option Explicit
' SCOM Scripting API Objects
Dim oAPI, oBag
Dim objWMIService
' Parameters
Dim sComputerName, strProcessName
Dim colProcess, objItem, PrcCount

sComputerName = WScript.Arguments(0)
strProcessName = """" & WScript.Arguments(1) & """"

Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputerName & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("SELECT * FROM CIM_Process WHERE Caption = " & strProcessName)
PrcCount = 0

if Not IsNull(colProcess) Then
For Each objItem in colProcess
PrcCount = PrcCount + 1
Next
End If

Call oBag.AddValue("Count",PrcCount)
Call oAPI.Return(oBag)

Don’t click on ‘Next’ just yet. Hit ‘Parameters’ and make the list look like in the following screenshot (Image 1). Note that there is a single blank between $Target/Host…$ and PROCESS.EXE. Replace PROCESS.EXE with whatever your binary is called.

ScriptParameters

In the next two steps you’ll have to define the unhealthy and healthy expressions. Since I used the property bag in the script these will be Property[@Name=’Count’] (see Image 2). Set the expression for the ‘Healthy Expression’ to something similar. If you have to ensure there is always exactly a single process active you need to set the unhealthy expression to ‘Not Equal’ 1 and the ‘Healthy Expression’ to ‘Equals’ 1. In my scenario I allowed process counts from none to one.

UnhealthyExpression
Last but not least you can check the configuration in the overview. In the next screenshot (Image 3) you can see that I used Integer for the @Type rows. It doesn’t really matter whether it’s Integer or String so don’t bother to change it accordingly.

Configuration
Although this is a very simple monitor, I didn’t find a ready-to-use solution somewhere. I hope this will help you if you’re trying to build something similar.