Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disk space monitoring script

Status
Not open for further replies.

erdengel

ISP
Dec 19, 2002
9
0
0
ZA
In our environment, we have a number of machines that have SAN storage, etc, which makes it impossible to use the default disk monitoring script that comes with MOM. This means we have had to write our own script. I have been staring at these lines of code for days now, because it is just not behaving the way it should. I was hoping one of you clever people could help me.

This is the script:

Const Event_Type_Error = 1
Const Event_Type_Warning = 2
Const Event_Type_Info = 4

Set objEvent = ScriptContext.Event

strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strComputer & "\root\cimv2")
set colItems = objWMIService.ExecQuery("Select DeviceID, FreeSpace, Size from Win32_LogicalDisk where DriveType = 3")


For Each objItem In colItems

TotalSize = FormatNumber((objItem.Size/1073741824), 2, -2, -1, 0)
strDrive = objItem.DeviceID
FreeSpace = FormatNumber((objItem.FreeSpace/1073741824), 2, -2, -1, 0)


if TotalSize <= 5 then
strThresholdW = 0.244
strThresholdC = 0.1
elseif TotalSize >= 5 and TotalSize < 10 then
strThresholdW = 0.488
strThresholdC = 0.244
elseif TotalSize >= 10 and TotalSize < 50 then
strThresholdW = 2
strThresholdC = 1
elseif TotalSize >= 50 and TotalSize < 100 then
strThresholdW = 10
strThresholdC = 5
elseif TotalSize >= 100 and TotalSize < 200 then
strThresholdW = 15
strThresholdC = 10
elseif TotalSize >= 200 then
strThresholdW = 50
strThresholdC = 25
end if

FreePerc = FormatNumber(100*(FreeSpace/TotalSize))


If FreeSpace < strThresholdW and FreeSpace > strThresholdC then

if strDrive = "C:" then
strMessage = "Disk volume " & strDrive & " is low on free space. Free space available " & FreeSpace & "GB, which is "& FreePerc &"% free. This is a system disk, and the overall performance of the system could be affected. This disk's total size is "& TotalSize &"GB and the threshold for this disk is " & strThresholdW &"GB."
else
strMessage = "Disk volume " & strDrive & " is low on free space. Free space available " & FreeSpace & "GB, which is "& FreePerc &"% free. This disk's total size is "& TotalSize &"GB and the threshold for this disk is " & strThresholdW &"GB."
end if

CreateEvent 1100, 1

ElseIf FreeSpace < strThresholdC then

if strDrive = "C:" then
strMessage = "Disk volume " & strDrive & " is low on free space. Free space available " & FreeSpace & "GB, which is "& FreePerc &"% free. This is a system disk, and the overall performance of the system could be affected. This disk's total size is "& TotalSize &"GB and the threshold for this disk is " & strThresholdC &"GB."
else
strMessage = "Disk volume " & strDrive & " is low on free space. Free space available " & FreeSpace & "GB, which is "& FreePerc &"% free. This disk's total size is "& TotalSize &"GB and the threshold for this disk is " & strThresholdC &"GB."
end if
CreateEvent 1101, 2

ElseIf Freespace > strThresholdC then

strMessage = "Disk volume " & strDrive & " has " & FreeSpace & "GB available as free space, which is "& FreePerc &"% free. This disk's total size is "& TotalSize &"GB and the threshold for this disk is " & strThresholdW &"GB."
CreateEvent 1102, 4

End if
Next


Sub CreateEvent (ByVal StrEventID, strEventType)
' Create new event and submit event objects
set objCE_NewEvent = ScriptContext.CreateEvent ' Set event properties
objCE_NewEvent.Message = strMessage
objCE_NewEvent.EventNumber = StrEventID
objCE_NewEvent.EventType = strEventType
objCE_NewEvent.EventSource = "Custom Disk space monitor"
' Submit the event
ScriptContext.Submit(objCE_NewEvent)
set objCE_NewEvent = Nothing
End Sub

----------------------------------------------

It is supposed to query the WMI logical disk instance, check the total partition size, check the free space, and then compare it to a threshold (which is based on the total size, due to the inconsistant configurations we have encountered).

In my lab environment, the script, as I have pasted it above, works perfectly. It logs the errors and warnings according to the thresholds, and the info events are displayed in the event log (I put those in as a sanity check).
However, when I take the script to my live environment, very few info events are logged, and the script seems to fail completely on the compare, logging errors for disks that have 90% free, etc.

The only difference between my lab and live is that the MOM server itself in the lab is running Win2k3 R2, while in live, the MOM server is Win2k3 rtm. All the other servers in the lab are Win2k3 rtm, while the live environment has a mixture of NT4, Win2k and Win2k3 servers. I am running MOM 2005 SP1.

What am I missing? I have reworked this script so many time, and each time I get the same result. Help, please, because I think I am going completely nuts :)
 
I did resolve this problem, for in case someone comes across this in future...

Here is the updated script:

'*******************************************************************************
' Script Name - Custom - Check Disk Space
'
' Version - 0.2 (22-nov-2007)
'
' Purpose - Determines the % free space available on the logical disks
' and logs an event if the threshold is breached.
'
' Events - 1100 = "Disk space utilisation has breached warning threshold" (Warning)
' - 1101 = "Disk space utilisation has breached critical threshold" (Critical)
'
'
'
'*************************************************************************

' --- Define Constants ---
' Event types
Const Event_Type_Success = 0
Const Event_Type_Error = 1
Const Event_Type_Warning = 2
Const Event_Type_Info = 4
Const Event_Type_Audit_Success = 8
Const Event_Type_Audit_Failure = 16
' Strings
Dim strComputer, intThresholdSys, intThreshold, strMessage


Dim objEvent
Dim objWMIService
Set objEvent = ScriptContext.Event

strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strComputer & "\root\cimv2")
set colItems = objWMIService.ExecQuery("Select DeviceID, FreeSpace, Size from Win32_LogicalDisk where DriveType = 3") 'Grab the name, the free space and total size for fixed drives

For Each objItem In colItems

if len(objItem.Size) <> 0 then
TotalSize = FormatNumber((objItem.Size/1073741824), 2, -2, -1, 0)
else
TotalSize = 0
end if

strDrive = objItem.DeviceID

if len(objItem.FreeSpace) <> 0 then
FreeSpace = FormatNumber((objItem.FreeSpace/1073741824), 2, -2, -1, 0)
else
FreeSpace = 0
end if

if TotalSize <= 1 then
intThresholdW = 0.244
intThresholdC = 0.1
elseif TotalSize >= 5 and TotalSize < 10 then
intThresholdW = 0.488
intThresholdC = 0.244
elseif TotalSize >= 10 and TotalSize < 50 then
intThresholdW = 2
intThresholdC = 1
elseif TotalSize >= 50 and TotalSize < 100 then
intThresholdW = 4
intThresholdC = 2
elseif TotalSize >= 100 and TotalSize < 200 then
intThresholdW = 5
intThresholdC = 3
elseif TotalSize >= 200 then
intThresholdW = 10
intThresholdC = 5
end if


if len(FreeSpace) <> 0 then
FreePerc = FormatNumber(100*(FreeSpace/TotalSize))
else
FreePerc = 0
end if



if cInt(FreeSpace) < intThresholdW and cInt(FreeSpace) > intThresholdC then

strMessage = "Disk volume " & strDrive & " is low on free space. Free space available " & FreeSpace & "GB, which is "& FreePerc &"% free. This disk's total size is "& TotalSize &"GB and the threshold for this disk is " & intThresholdW &"GB."
CreateEvent 1100, 1

ElseIf cInt(FreeSpace) < intThresholdC then

strMessage = "Disk volume " & strDrive & " is low on free space. Free space available " & FreeSpace & "GB, which is "& FreePerc &"% free. This disk's total size is "& TotalSize &"GB and the threshold for this disk is " & intThresholdC &"GB."
CreateEvent 1101, 2

ElseIf cInt(FreeSpace) > intThresholdC then

strMessage = "Disk volume " & strDrive & " has " & FreeSpace & "GB available as free space, which is "& FreePerc &"% free. This disk's total size is "& TotalSize &"GB and the threshold for this disk is " & intThresholdW &"GB."
CreateEvent 1102, 4

End if

if strDrive = "C:" then

strMessage = strMessage & " This is a system drive, and a low disk space condition may affect the overall performance of the system."

end if

next


' Sub : CreateEvent
' Parameters : None
' Description : Create and submit an event

Sub CreateEvent (ByVal StrEventID, strEventType)
' Create new event and submit event objects
set objCE_NewEvent = ScriptContext.CreateEvent ' Set event properties
objCE_NewEvent.Message = strMessage
objCE_NewEvent.EventNumber = StrEventID
objCE_NewEvent.EventType = strEventType
objCE_NewEvent.EventSource = "Custom Disk Space Monitor"
' Submit the event
ScriptContext.Submit(objCE_NewEvent)
set objCE_NewEvent = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top