This VBScript:' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' This script is a merge of 2 scripts. Credits go to :
' - XXXXX's , which use the performance monitors values to test the disk space, and most code for this script.
' - YYYYYY's, which retrieve the thresholds from a specified attribute
' I added the feature that, if the given attribute is not set, you can use a default value for all disks.
' Also, specifying a threshold of 0 disables the check on that disk. You must always specify the unit (%, G or M), even when disabling the check.
' Format for the thresholds are something like this : 'C:\=10%;D:\=5G;E:\=650M;G:\=0%'
' THE DRIVE NAME IN THE ATTRIBUTE MUST BE THE SAME AS THE ONE RETURNED BY THE DISK PERF MONITOR. (e.g. for Windows : C:\, D:\, and so on...)
' You can see see the name of each drive by using the Test button and scanning the result.
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Name of the attribute which will contain the specific thresholds
THRESHOLDS_ATTRIBUTE = "Disk_Space_Thresholds"
' Name of the attribute which will contain the result of the check. Automatically created.
RESULT_ATTRIBUTE = "Disk_Space_Results"
' If the threshold attribute is not found, should the monitor report as DOWN ?
' If set to False, it will use the default value for all disks.
DOWN_NO_ATTRIBUTE = true
' If there are no recent performance entries for at least one device, should the monitor report as DOWN ? - NOT IMPLEMENTED
DOWN_ON_AGED_VALUES = true
' MAX_AGE value specify the maximum age for entries, in seconds - NOT IMPLEMENTED
MAX_ENTRIES_AGE = 3600
' Default value to be used, in percentage. We monitor the FREE SPACE, which must not go below the threshold.
' 1- when the attribute doesn't specify the threshold for a given disk
' 2- for all disks if there is no attribute AND DOWN_NO_ATTRIBUTE = True
' If set to 0, not specified disks will not be checked.
DEFAULT_THRESHOLD = 10
' Unit of default threshold - THIS IS NOT A DEFAULT UNIT FOR SPECIFIED THRESHOLDS
' "%" = percentage
' "G" = gigabyte
' "M" = megabyte
DEFAULT_THRESHOLD_UNIT = "%"
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Set the result code of the check (0=Success, 1=Error)
Context.SetResult 0, "No error"
bAlarm = false
' Retrieve DB object
Set oDb = Context.GetDB
If IsNull(oDb) Then
Context.SetResult 1, "Could not create DB object"
Else
' Fetch deviceID
nDeviceID = Context.GetProperty ("DeviceID")
' Get the attribute which contains the specific value.
sqlGetDS = "select sValue from DeviceAttribute where (nDeviceID = " & nDeviceID & ") and (sName = '" & THRESHOLDS_ATTRIBUTE & "')"
Set oRs1 = oDb.Execute(sqlGetDS)
' We report a DOWN if no attribute and script is configured as such.
If DOWN_NO_ATTRIBUTE and oRs1.EOF Then
Context.SetResult 1, "This monitor needs a attribute named " & THRESHOLDS_ATTRIBUTE & " to work."
Else
drvMonitors = null
If Not oRs1.EOF Then
oRs1.moveFirst
drvMonitors = split(oRs1("sValue"),";")
End If
' SQL request. We ask SQL to compute the value we want itself : it return the remaining percentage, megabytes and gigabytes on each disk.
sSql = "SELECT SDI.sDescription, SD.dPollTime, D.sDisplayName, (1 - SD.nUsed_Avg / SD.nSize) * 100 AS perc, (SD.nSize - SD.nUsed_Avg) / 1073741824 AS giga, (SD.nSize - SD.nUsed_Avg) / 1048576 AS mega FROM Device AS D INNER JOIN PivotStatisticalMonitorTypeToDevice AS PSM ON PSM.nDeviceID = D.nDeviceID INNER JOIN StatisticalDiskIdentification AS SDI ON SDI.nPivotStatisticalMonitorTypeToDeviceID = PSM.nPivotStatisticalMonitorTypeToDeviceID INNER JOIN StatisticalDisk AS SD ON SD.nStatisticalDiskIdentificationID = SDI.nStatisticalDiskIdentificationID WHERE D.nDeviceID = '" & nDeviceID & "' AND SD.dPollTime = (SELECT top 1 SD.dPollTime FROM Device AS D INNER JOIN PivotStatisticalMonitorTypeToDevice AS PSM ON PSM.nDeviceID = D.nDeviceID INNER JOIN StatisticalDiskIdentification AS SDI ON SDI.nPivotStatisticalMonitorTypeToDeviceID = PSM.nPivotStatisticalMonitorTypeToDeviceID INNER JOIN StatisticalDisk AS SD ON SD.nStatisticalDiskIdentificationID = SDI.nStatisticalDiskIdentificationID WHERE D.nDeviceID = '" & nDeviceID & "' ORDER BY SD.dPollTime DESC) ORDER BY SDI.sDescription"
Set oRs2 = oDb.Execute(sSql)
oRs2.moveFirst
While Not oRs2.EOF
' First we check if the last polled value is a recent one, if that check is enabled
' TODO
displayName = oRs2("sDisplayName")
description = ucase(oRs2("sDescription"))
'Default value
drvMonitorLimit = DEFAULT_THRESHOLD
drvMonitorUnit = DEFAULT_THRESHOLD_UNIT
' We retrieve the threshold from the attribute, if it exists.
If Not IsNull(drvMonitors) Then
For each drvMonitor in drvMonitors
'Is it a matching threshold ?
tokens = split(drvMonitor, "=")
If tokens(0) = description Then
' We then retrieve the threshold value AND the unit. The unit MUST be specified.
' Correct format is "100M", "10%", "4G" for example.
drvMonitorUnit = ucase(Right(tokens(1),1))
drvMonitorLimit = int(Left(tokens(1),len(tokens(1))-1))
Exit For
End If
Next
End If
Select Case drvMonitorUnit
Case "%"
value = int(oRs2("perc"))
Case "G"
value = int(oRs2("giga"))
Case "M"
value = int(oRs2("mega"))
Case Else
' Invalid unit, we will report a Down.
value = -1
End Select
' If unit is invalid, it' a down.
If value = -1 Then
bAlarm = true
oChecksResult = oChecksResult & "Disk " & description & " : threshold unit is invalid (" & drvMonitorUnit & "). You MUST specify %, G or M as a threshold unit (example : C=10%;D=5G;E=650M)" & vbCrLf
Else
' If unit is valid, but threshold is zero, we don't check.
If drvMonitorLimit = 0 Then
oChecksResult = oChecksResult & "Disk " & description & " : " & Round(value) & drvMonitorUnit & _
" remaining space - not checked because threshold is zero." & vbCrLf
Else
' Else we check.
If value < drvMonitorLimit Then
bAlarm = true
oChecksResult = oChecksResult & "Disk " & description & " : " & Round(value) & drvMonitorUnit & _
" remaining space is below threshold (" & drvMonitorLimit & drvMonitorUnit & ") - DOWN !!" & vbCrLf
Else
oChecksResult = oChecksResult & "Disk " & description & " : " & Round(value) & drvMonitorUnit & _
" remaining space is above threshold (" & drvMonitorLimit & drvMonitorUnit & ") - UP" & vbCrLf
End If
End If
End If
oRs2.moveNext
Wend
' Store the result in an attribute. Allows to access details even on a Device-configured action by using the %Device.Attribute.* variable.
' Formatting it to a SQL compatible format.
if (Right(oChecksResult,1) = vbCrLf) Then
oChecksResultSQL = Replace(Left(oChecksResult,len(oChecksResult)-1), vbCrLf, "' + CHAR(10) + N'")
Else
oChecksResultSQL = Replace(oChecksResult, vbCrLf, "' + CHAR(10) + N'")
End If
' Checking if the attribute already exists. If not we create one, if yes we simply update it.
sqlCheck = "SELECT nDeviceAttributeID FROM DeviceAttribute WHERE (nDeviceID = " & nDeviceID & ") AND (sName = N'" & RESULT_ATTRIBUTE & "')"
Set oRs1 = oDb.Execute(sqlCheck)
' Storing it in DB as needed.
If oRs1.EOF Then
sqlStoreResult = "INSERT INTO DeviceAttribute (nDeviceID, sName, sValue) VALUES (" & _
nDeviceID & ", N'" & RESULT_ATTRIBUTE & "', N'" & oChecksResultSQL & "')"
Else
sqlStoreResult = "UPDATE DeviceAttribute SET sValue = N'" & oChecksResultSQL & _
"' WHERE (nDeviceID = " & nDeviceID & ") AND (sName = N'" & RESULT_ATTRIBUTE & "')"
End If
oDb.Execute(sqlStoreResult)
' Set the result of the monitor.
If bAlarm Then
oChecksResult = oChecksResult & vbCrLf & "Monitor is DOWN"
Context.SetResult 1, oChecksResult
Else
oChecksResult = oChecksResult & vbCrLf & "Monitor is UP"
Context.SetResult 0, oChecksResult
End If
End If
End If
' Cleanup code
Set oDb = Nothing
Set oRs2 = Nothing
Set oRs1 = Nothing



Plus this entry in the Attribute section (feel free to add more drive letters with corresponding values)
******************************
Attribute Name: Disk_Space_Thresholds
Attribute Value: 'C:\=10%;D:\=1g;'
******************************
It works for most of my servers. I'm just having problems with ones where there is no sample data.
Any reference to this script can be found in here: http://forums.ipswitch.com/Topic40728-14-1.aspx#bm40800