Monitoring All Automatic Start Windows Services in SCOM 2007 R2
One of my jobs is an admin of Microsoft System Center Operations Manager 2007 R2. I had a request to monitor all Windows Services that were set to startup automatic. However, need to be able to create overrides to not alert on specified services for particular servers or groups. Also, don’t alert if the server rebooted in last 15 minutes, and only when a service is down for two consecutive iterations. To accomplish this I created a timed two state script unit monitor. Below is the script that I used in that monitor.
Note: There was an error in the script. I posted an updated version of the script on 03/24/2011. Please let me know if you find any other errors.
' Monitor if any automatic service is not running ' Excluded: Performance Logs and Alerts service is auto but never running ' Create a two state timed generic script unit monitor ' HEALTH EXPRESSIONS ' Healthy Property[@Name='State'] Contains GOOD ' Unhealthy Property[@Name='State'] Contains BAD ' ALERT DESCRIPTION ' $Data/Context/Property[@Name='Description']$ Set oAPI = CreateObject("MOM.ScriptAPI") Set oShell = CreateObject( "WScript.Shell" ) Set arArgs = WScript.Arguments.Named Set oBag = oAPI.CreatePropertyBag() dim StateRegPath : StateRegPath = "HKLM\" & oAPI.GetScriptStateKeyPath("ServicesStopped") On Error Resume Next GetState = oShell.RegRead(StateRegPath & "\ServicesList") on error Goto 0 allGood = TRUE RepeatDown = FALSE sExcludeList = arArgs.Item("Exclude") Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 'Get System Uptime, if recently rebotted, do not continue Set colOperatingSystems = oWMI.ExecQuery("Select * From Win32_PerfFormattedData_PerfOS_System") For Each objOS in colOperatingSystems intSystemUptime = Int(objOS.SystemUpTime) Next If (intSystemUptime > 900) Then Set allSvc = oWMI.ExecQuery("Select * from Win32_Service Where StartMode = 'Auto'") For Each oSvc in allSvc If (oSvc.Started = FALSE and instr(1,sExcludeList,"'" & oSvc.DisplayName & "'") < 1) Then sList = sList & oSvc.DisplayName & ", " allGood = FALSE if instr(1,GetState,oSvc.DisplayName) > 0 then RepeatDown = TRUE End If Next If allGood = TRUE then Call oBag.AddValue("State", "GOOD") call oShell.RegWrite (StateRegPath & "\ServicesList", "", "REG_SZ") Else if RepeatDown = TRUE then Call oBag.AddValue("State", "BAD") Call oBag.AddValue("Description", "Automatic service(s) not running: " & sList) end if call oShell.RegWrite (StateRegPath & "\ServicesList", sList, "REG_SZ") End if Else Call oBag.AddValue("State", "GOOD") End If Call oAPI.Return(oBag)