Example logical diskspace monitoring script for use with SCOM 2007

I wrote this script to demonstrate some of the new scripting techniques in SCOM, and to remedy a problem I noticed when monitoring logical disk counters.

The default rule included in SCOM will perform an AND query for "% Free Space", and "Free Megabytes". There is no easy method to monitor just one counter of these values.

Install this script as a timed rule in scom. Target it to something like windows servers or windows computers.

The script will generate up to 4 events in the Operations Manager log:

10111 - Warning - Warning Threshold for MB Free has been reached

10111 - Error - Error Threshold for MB Free has been reached

10112 - Warning - Warning Threshold for % Disk Free has been reached

10112 - Error - Error Threshold for % Disk Free has been reached

The script takes up to 5 parameters:

Example: Diskspace.vbs 2000 4000 5 10 1

1st param - Error condition for MB free. This will trigger the 10111 Error Event

2nd Param - Warning Condition for MB free. This will trigger the 10111 Warning Event

3rd Param - Error condition for % free. This will trigger the 10112 Error Event

4th Param - Warning condition for % free. This will trigger the 10112 Warning Event

5th Param - Enable Debug logging. This param is optional. Log defaults to C:\temp\diskspace.log (Note: The scripts checks for the presence of the 5th parameter to enable logging. Changing it to a "0" has no effect)

Here's an example of what the event log text looks like:

Diskspace.vbs : Drive C: has 1403.79MB(11.43%) out of 12284.08MB Free [Alert on: 2000MB Free]

You can use the $Data/EventDescription$ variable in your alert rule to get this information

Also see this link for more information on scripting for SCOM

https://www.systemcenterforum.org/using-property-bags-with-custom-scripting-in-operations-manager-2007/

===============

Const HARD_DISK = 3

Dim AlertSizeinMB

Dim AlertPercent

Dim SpaceinMB

Dim SizeinMB

Dim PercentFree

Dim StrComputer

Dim Logging

Dim oAPI,oArgs

Set oAPI = CreateObject("MOM.ScriptAPI")

Set oArgs = WScript.Arguments

if oArgs.Count < 4 Then

WriteLog ("Argument Error")

DumpInfo()

Call oApi.LogScriptEvent ("Diskspace.vbs",101,1, "Diskspace.vbs script was called with less than 4 arguments")

Wscript.Quit -1

End If

Logging = 0

if oArgs.Count = 5 then

Logging = 1

End If

'1st Arg Error Size in MB

'2nd Arg Warning Size in MB

'3rd Arg Error Size in %

'4th Arg Warning Size in %

'If the 5th Argument exists, turn on logging to C:\temp\diskspace.log

AlertSizeinMB_ERR = oArgs(0)

AlertSizeinMB_WARN = oArgs(1)

AlertPercent_ERR = oArgs(2)

AlertPercent_WARN = oArgs(3)

WriteLog ("Script Startup V3.02")

WriteLog ("AlertSize in MB = " & AlertSizeinMB_ERR & " Alert Percent = " & AlertPercent_ERR)

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colDisks = objWMIService.ExecQuery _

("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")

WriteLog ("WMI Query Complete on " & strComputer )

For Each objDisk in colDisks

SpaceinMB=round (objDisk.FreeSpace/1024/1024,2)

SizeinMB=round (objDisk.Size/1024/1024,2)

WriteLog("SizeinMB: " & SizeinMB & " SPaceinMB: " & SpaceinMB)

PercentFree=round(((objDisk.FreeSpace / objDisk.Size) * 100),2)

WriteLog("Percent Free: " & PercentFree)

if (SpaceinMB < round(AlertSizeinMB_ERR) ) then

WriteLog ("SpaceinMB: " & SpaceinMB & " < " & "AlertSizeinMB: " & AlertSizeinMB_ERR & " 10111")

WriteLog ("Drive " & objDisk.DeviceID & " has " & SpaceinMB & "MB" & "("& PercentFree & "%) out of " & SizeinMB & "MB Free [Error Alert on: " & AlertSizeinMB_ERR & "MB Free]")

Call oApi.LogScriptEvent ("Diskspace.vbs",10111,1, "Drive " & objDisk.DeviceID & " has " & SpaceinMB & "MB" & "("& PercentFree & "%) out of " & SizeinMB & "MB Free [Error Alert on: " & AlertSizeinMB_ERR & "MB Free]")

elseif (SpaceinMB < round(AlertSizeinMB_WARN) ) then

WriteLog ("SpaceinMB: " & SpaceinMB & " < " & "AlertSizeinMB: " & AlertSizeinMB_WARN & " 10111")

WriteLog ("Drive " & objDisk.DeviceID & " has " & SpaceinMB & "MB" & "("& PercentFree & "%) out of " & SizeinMB & "MB Free [Warning Alert on: " & AlertSizeinMB_WARN & "MB Free]")

Call oApi.LogScriptEvent ("Diskspace.vbs",10111,2, "Drive " & objDisk.DeviceID & " has " & SpaceinMB & "MB" & "("& PercentFree & "%) out of " & SizeinMB & "MB Free [Warning Alert on: " & AlertSizeinMB_WARN & "MB Free]")

End If

if (PercentFree < round(AlertPercent_ERR) ) then

WriteLog("PercentFree < AlertPercent_ERR 10112")

WriteLog ("Drive " & objDisk.DeviceID & " has " & SpaceinMB & "MB" & "("& PercentFree & "%) out of " & SizeinMB & "MB Free [Error Alert on: " & AlertPercent_ERR & "%]")

Call oApi.LogScriptEvent ("Diskspace.vbs",10112,1, "Drive " & objDisk.DeviceID & " has " & SpaceinMB & "MB" & "("& PercentFree & "%) out of " & SizeinMB & "MB Free [Error Alert on: " & AlertPercent_ERR & "%]")

elseif (PercentFree < round(AlertPercent_WARN) ) then

WriteLog("PercentFree < AlertPercent_WARN 10112")

WriteLog ("Drive " & objDisk.DeviceID & " has " & SpaceinMB & "MB" & "("& PercentFree & "%) out of " & SizeinMB & "MB Free [Warning Alert on: " & AlertPercent_WARN & "%]")

Call oApi.LogScriptEvent ("Diskspace.vbs",10112,2, "Drive " & objDisk.DeviceID & " has " & SpaceinMB & "MB" & "("& PercentFree & "%) out of " & SizeinMB & "MB Free [Warning Alert on: " & AlertPercent_WARN & "%]")

End If

Next

WriteLog ("Script Ended")

Function WriteLog (strLogText)

Dim objfs

Dim objf

Dim strTimeStamp

Dim FileName

if ( Logging = 1 ) then

Filename = "c:\temp\diskspace.log"

On Error Resume Next

Err.Clear

Set objfs = CreateObject("Scripting.FileSystemObject")

Set objf = objfs.OpentextFile(FileName, 8, False)

' If log file doesn't exist - create it

If Err.Number <> 0 Then

Set objf = objfs.CreatetextFile(FileName, False)

Err.Clear

End If

strTimeStamp = "[" & Date & " " & Time & "] "

objf.WriteLine(strTimeStamp & strLogText)

Set objfs = Nothing

Set objf = Nothing

End If

End Function

Function DumpInfo()

On Error Resume Next

WriteLog "--- Beginning DumpInfo()"

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)

For Each objItem in colItems

WriteLog "Computer Name: " & objItem.CSName & " " & objItem.Caption & "SP "& objItem.ServicePackMajorVersion & "." & objItem.ServicePackMinorVersion & " (" & objItem.Version & ")"

next

Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)

For Each objItem in colItems

WriteLog "== PID: " & objItem.ProcessId & " " & objItem.Name & " ++ Parent PID: " & objItem.ParentProcessId

if objItem.CommandLine <> "" then WriteLog "--- CommandLine: " & objItem.CommandLine

if objItem.ExecutablePath <> "" then if objItem.ExecutablePath <> objItem.CommandLine then Writelog "--- ExecutablePath: " & objItem.ExecutablePath

next

WriteLog "--- Exiting DumpInfo()"

End Function

diskspace.vbs