His original solution would parse all volumes on a remote server, and check that they don't fall below a critical value. That was great, but I needed the threshold to be a variable. One thing led to another, and I ended up with this:
This script includes:
- The obligatory elements of the default AHM VBScript template (i.e. script response constants)
- Flexible argument parsing, type conversion/checking, error checking & syntax notification
- Flexible threshold checking - physical and percentage
- Percentage can be entered as a literal (15%% - you must double the % symbol) - or as a decimal (.15)
- Defaults for Computer ("." - local) and threshold (20%) if argument(s) not supplied
- Display of total freespace (in GB) AND percentage free.
Format (literal threshold provided =freespace(freepercent)
Format (percentage threshold provided =freepercent(freespace)
Code: Select all
' ===========HDFree.vbs===========
' Purpose: Query a local or remote computer's hard disk volumes for available disk space.
' The percentage of free space is compared against a user supplied critical threshold.
'
' Syntax: CSCRIPT HDFree.VBS {computer_name} {threshold}
' Where: {computer_name} =name of a WMI enabled computer (local or remote)
' Default ="." (local computer)
' {threshold} =minimum freespace
' If entered as an integer physical size is checked (in GB)
' If a decimal amount or an integer followed by "%%"
' percentage free is checked.
' Examples: HDFree.vbs Comp1 20 -->Checks all volumes on Comp1 for 20GB freespace
' HDFree.vbs Comp2 10%% -->Checks all volumes on Comp2 for 10% freespace
' HDFree.vbs Comp3 .15 -->Checks all volumes on Comp3 for 15% freespace
' Defaults: Local Computer (".") and 20%
'
' Output: [status]:{reply}
' [status] - Required. Format "scriptRes:{statusmsg}
' (see constants defined below)
' {reply} - Optional (on error this script always supplies one).
' May include simple description or system error codes
'
' NOTES: -This script has been modified to comply with (KS-Soft) Advanced Host Monitor's
' "shell script" method of monitoring.
' -Inspired by Glynn Seymours adaptation of Rob van der Woude's freespace.vbs
' http://www.robvanderwoude.com
' ==========Begin Script==========
Option Explicit
DIM sCompStatus, sType, sResult, sSyntaxDesc
DIM iThreshold
' *** Default Values & constants
iThreshold =.2
const statusAlive ="scriptRes:Host is alive:"
const statusDead ="scriptRes:No answer:"
const statusUnknown ="scriptRes:Unknown:"
const statusUnknownHost ="scriptRes:Unknown host:"
const statusOk ="scriptRes:Ok:"
const statusBad ="scriptRes:Bad:"
const statusBadContents ="scriptRes:Bad contents:"
const ShowSyntax ="/?"
' *** Parse the command-line arguments, check syntax, & perform necessary type conversions.
Select Case WScript.Arguments.Count
Case 0 ' *** No command line arguments
sCompStatus =ParseArg(".",0)
Case 1 ' *** Argument1 =computer name
sCompStatus =ParseArg(Wscript.Arguments(0),0)
Case 2 ' *** Argument2 =Critical threshold
sCompStatus =ParseArg(Wscript.Arguments(0),0)
iThreshold =ParseArg(Wscript.Arguments(1),1)
Case Else ' *** Maximum is 2 command line parameters - Show syntax
sCompStatus =ParseArg(ShowSyntax,99)
End Select
' *** If parsed arguments are 'bad' pass the results to StdOut
If InStr(sCompStatus,"scriptRes:") Then
WScript.StdOut.Write sCompStatus
ElseIf InStr(iThreshold,"scriptRes:") Then
WScript.StdOut.Write iThreshold
Else
' *** Otherwise call HDFreeTest Function & pass results to StdOut
WScript.StdOut.Write HDFreeTest(sCompStatus, iThreshold)
End if
' ===========End Script===========
'
' --------Functions & Subs--------
Function ParseArg(Arg, ArgNum)
' *** Purpose: Parse command-line arguments & display Syntax rules if necessary
DIM sReply, sSyntaxDesc
const Syntax ="Syntax: CSCRIPT HDFree.VBS {computer_name} {threshold}"
sSyntaxDesc = vbCrLf & "Where: {computer_name} =" & _
"name of a WMI enabled computer (local or remote)" & vbCrLf & _
vbTab & "{threshold} =minimum freespace" & vbCrLf & _
vbTab & vbTab & "An integer value is interpreted as physical size (in GB)" & vbCrLF & _
vbTab & vbTab & "A decimal value or an integer followed by '%%'" & vbCrLF & _
vbTab & vbTab & " is interpreted as a percentage" & vbCrLF & _
vbTab & vbTab & "Defaults: Computer: " & Chr(34) &"." & Chr(34) & _
" (local computer)" & vbCrLF & _
vbTab & vbTab & vbTab & " Threshold: 20%" & vbCrLF & _
"Inspired by Glynn Seymours adaptation of Rob van der Woude's freespace.vbs" & vbCrLf & _
vbTab & "http://www.robvanderwoude.com"
If InStr(Arg, "?") Then ArgNum =99
Select Case ArgNum
Case 0 ' *** sComputer Argument
ParseArg =CStr(Arg)
Case 1 ' *** iThreshold Argument
On Error Resume Next
If IsNumeric(Arg) Then
ParseArg =CSng(Arg)
Elseif InStrRev(Arg,"%")<>0 Then ParseArg =CSng(Trim(Left(Arg,InStrRev(Arg,"%")-1))/100)
Else
ParseArg =statusUnknown & "Syntax: Argument conversion error: " & Arg
On Error Goto 0
Exit Function
End If
If ParseArg <.01 or ParseArg >99 Then _
ParseArg=statusUnknown & "Threshold value out of bounds: " & Arg
On Error Goto 0
Case Else ' *** Display script syntax
ParseArg =statusUnknown & Syntax ' & sSyntaxDesc
' *** -------------------Remove this--^ if you wish to include Syntax Description
End Select
End Function
' --------------------------------
Function HDFreeTest(ArgComp, ArgThold)
' *** Purpose: Query Windows servers (WMI) for available Hard Disk free space
DIM sStatus, sReply
DIM iFree
DIM oWMIService, oItem, aItems
const GB=1073741824
On Error Resume Next
Set oWMIService =Getobject("winmgmts://" & ArgComp & "/root/cimv2")
If Err.Number Then
HDFreeTest =statusUnknownHost & "Server not found or not responding: Error # " & _
CStr(Err.Number) & " " & Err.Description
Exit Function
End If
Set aItems =oWMIService.ExecQuery("Select * from Win32_LogicalDisk where DriveType =3",,48)
If Err.Number Then
HDFreeTest =statusUnknown & "Error # " & CStr(Err.Number) & " " & Err.Description
Exit Function
End If
On Error GoTo 0
sStatus=statusOK
For Each oItem in aItems
If sReply<>"" Then sReply =sReply & ", "
'Calculate volume's percentage free space
iFree =Int(.5+(100 * oItem.FreeSpace / oItem.Size))
' *** If ANY volume falls below the critical threshold, set the AHM status to BAD
' *** then append this volumes free space and free space percentage to the return message
If iThreshold <1 Then
If iFree<iThreshold*100 Then sStatus =statusBad
sReply =sReply & oItem.Name & iFree & "%" & "(" & Round(oItem.FreeSpace / GB,2) & ")"
Else
If Round(oItem.Freespace / GB,2)<iThreshold Then sStatus ="statusBad"
sReply =sReply & oItem.Name & Round(oItem.FreeSpace / GB,2) & "(" &iFree & "%" & ")"
End If
Next
If sReply =Empty Then
HDFreeTest =statusUnknown & "Local Hard Disk(s) not found"
Else
HDFREETest =sStatus & sReply
End If
End Function