' Code for monitoring windows disk space
'
'Version History
'Date version Modified by Details
'06/23/2015 1.0 San Mattel initial version to check DB space
'04/23/2018 1.1 San Mattel Included configuration file to read parameters
'==================================================================
' general constants and variable – NEED TO BE MODIFIED FOR YOUR ENVIRONMENT
'==================================================================
Const MailServerPort = "25" ' SMTP Port used at Mail server (25 is default)
Dim MailServer
Const LOCAL_HARD_DISK = 3
'Only enumerate physical disks (Not Network Drives)
Const HARD_DISK = 3
strVer ="1.1"
strModifyBy =" San Mattel"
strModifyDate = "4/23/18"
'==================================================================
' get current computer name (from system environment variables)
'==================================================================
Function GetCurrentComputerName
set oWsh = WScript.CreateObject("WScript.Shell")
set oWshSysEnv = oWsh.Environment("PROCESS")
GetCurrentComputerName = oWshSysEnv("COMPUTERNAME")
End Function
'==================================================================
' Send a mail message
'==================================================================
Sub SendMail(Sender, Recipient, Subject, Message)
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = Subject
objMessage.From = Sender
objMessage.To = Recipient
objMessage.TextBody = Message
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MailServer
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = MailServerPort
objMessage.Configuration.Fields.Update
objMessage.Send
End Sub
'==================================================================
' Begin main code
'==================================================================
str = ""
'Get Parameters from Configuration file
'======================================
Dim xmlDom
Dim objNode
dim IsLogging
dim IsMailSending
dim IsEcho
dim WarningPercentage
dim ToMailIds
dim IsWarning
'dim MailServer
'dim IsLogging
Set xmlDom = CreateObject("Microsoft.XMLDOM")
'xmlDom.async = "false"
strdir="FreeSpaceChcker.Config.xml"
If (Not xmlDom.load(strdir)) Then
wscript.echo "Unable to load file '" & strdir & "'. "
WScript.Quit(1)
End If
'read from xml configuration file
Set objNode = xmlDom.selectSingleNode("/root/property[@name='IsLogging']")
'if isEmpty(objNode) Then
' wscript.echo "Unable to read property from file '" & strdir & "'. "
' WScript.Quit(1)
'end if
' wscript.echo
IsLogging = objNode.getAttribute("value")
Set objNode = xmlDom.selectSingleNode("/root/property[@name='IsMailSending']")
IsMailSending = objNode.getAttribute("value")
Set objNode = xmlDom.selectSingleNode("/root/property[@name='IsEcho']")
IsEcho = objNode.getAttribute("value")
Set objNode = xmlDom.selectSingleNode("/root/property[@name='MailServer']")
MailServer = objNode.getAttribute("value")
Set objNode = xmlDom.selectSingleNode("/root/property[@name='WarningPercentage']")
WarningPercentage = objNode.getAttribute("value")
Set objNode = xmlDom.selectSingleNode("/root/property[@name='ToMailIds']")
ToMailIds = objNode.getAttribute("value")
Set objNode=Nothing
Set xmlDom=Nothing
'====================================================================
' Get Disk details from current Server
'===================================================================
strComputer = GetCurrentComputerName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
IsWarning ="FALSE"
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
str = str & "Date :" & Date & " "& Time &" Server: " & strComputer & vbcrlf
For Each objDisk in colDisks
str = str & "Disk: "& objDisk.DeviceID & vbTab
str = str & "Disk Size: "& FormatNumber(objDisk.Size/1073741824,2) &" GB"& vbTab
'str = str & "Disk Size: "& objDisk.Size & vbTab
str = str & " Free Disk Space: "& FormatNumber(objDisk.FreeSpace/1073741824,2) &" GB"
usedSpaceGB = objDisk.Size - objDisk.FreeSpace
percentFree = FormatNumber((objDisk.FreeSpace / objDisk.Size) * 100, 2)
str = str & " Free Percentage: "& percentFree &"% "
'str = str & " Free Disk Space: "& FormatNumber(CLng(objDisk.FreeSpace / 1024 / 1024),0,,,-1) & " MB" & vbcrlf
if WarningPercentage>percentFree then
IsWarning ="TRUE"
str = str & "**Need Attention**(Warning % is " & WarningPercentage & " )"
end if
str = str &vbcrlf
Next
Vstr = str & vbcrlf
if ucase(IsLogging) = "TRUE" then ' if logging enabled in configuration file
Set objFSO=CreateObject("Scripting.FileSystemObject")
' How to write file
outFile="FreeSpaceChcker.log"
If (objFSO.FileExists(outFile)) Then
Set objFile = objFSO.OpenTextFile(outFile,8,True)
Else
Set objFile = objFSO.CreateTextFile(outFile,False)
End If
objFile.Writeline Vstr
objFile.Close
end if
if ucase(IsEcho) ="TRUE" then 'if checking from command line
'wscript.echo Vstr
'msgbox Vstr
end if
'Send the email
if UCase(IsMailSending) = "TRUE" and IsWarning ="TRUE" then ' send mail only if enabled from configuration file and reached warning level
SendMail "frommailid@domain.com",ToMailIds, "Drive Space Report: Server Name " & strComputer , str
end if