VBScript to harddisk free space monitoring

Although using SNMP based management software to monitor disk usage is the best solution, on some small site with few servers, a simple VB Script to monitor the disk usage is already good enough.

The below script uses BlatMail to generate an SMTP email.  You also need an SMTP server to accept the SMTP request.

I setup an hourly schedule to run "d:\batch\freespace.vbs" and to generate alert if the freespace is below threshold.

freespace.vbs


' Stanley 2013.05.16-
' - can auto skip non existing drive.  Great for running this script on cluster passive nodes.

Set getFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
szHost = WshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

' Syntax  checking(hostname, drive letter, free space in GB)
checking "Z:", 362
checking "Q:", 2
checking "F:", 100
checking "E:", 20
checking "D:", 17
checking "C:", 10


Function checking(szDriveLetter, nThreshold)
    getFreeSpace = -1
    On Error Resume Next

    getFreeSpace = round((getFSO.GetDrive(szDriveLetter)).AvailableSpace/1000000000,2)

    if getFreeSpace >= 0  Then
        If getFreeSpace < nThreshold Then
            ' Handling if free space is low (below threshold).
            WshShell.Run "c:\blat\blat -to support@mycompany.com -f networkmonitor@mycompany.com -server SMTPMAILSERVER -subject """ & szHost & " - " & szDriveLetter & " is below " & CStr(nThreshold) & " GB" & """ -body ""Only " &  Cstr(getFreeSpace) &" GB left.""", 1, True
         
        Else
            ' Handling if free space is normal (above threshold).
            WshShell.Run "c:\blat\blat -to networkmonitor@mycompany.com -f networkmonitor@mycompany.com -server SMTPMAILSERVER -subject ""Free Space Check: " szHost & " - " & szDriveLetter &  " has " & Cstr(getFreeSpace) & " GB free space" & """ -body ""Congratulation.  It is above the threshold level of " & CStr(nThreshold) & " GB.""", 1, True

        End If
    End IF
End Function

Comments

Popular Posts