Disk space monitoring script for windows
It is a simple script which modified from an original one from http://www.adiscon.com/ which checks all the fixed disks on a windows machine and can email the results.
The modified script is slightly more abstracted and doesn't require any additional components to send the email. It should be useful for monitoring disk space on DB servers or web servers that have log files on busy websites.
It is split into two parts - the library which holds all the functions and the config main code.
lib_diskspace.vbs
This is the library of functions
Const Unknown = 0
Const Removable = 1
Const Fixed = 2
Const Remote = 3
Const CDROM = 4
Const RAMDisk = 5
' Send a mail message
Sub SendMail(Sender, Recipient, Subject, Message,Server,Port)
Set objEmail = CreateObject("CDO.Message")
objEmail.From = Sender
objEmail.To = Recipient
objEmail.Subject = Subject
objEmail.Textbody = Message
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = Server
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Port
objEmail.Configuration.Fields.Update()
objEmail.Send
End Sub
' 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
' get free space report
Function GetFreeSpaceReport
set oFs = WScript.CreateObject("Scripting.FileSystemObject")
set oDrives = oFs.Drives
for each oDrive in oDrives
Select case oDrive.DriveType
Case Fixed
GetFreeSpaceReport = GetFreeSpaceReport & oDrive.DriveLetter & ": " & Round(oDrive.FreeSpace/(1024*1024)) & "MB free (" & Round(100 * (oDrive.FreeSpace/oDrive.TotalSize),2) & "%)" & vbcrlf
End Select
next
End Function
diskspace.wsf
This is the main script that should be edited to your configuration, and added to your scheduled tasks.<script src="lib_diskspace.vbs" language="vbscript" />
<script language="vbscript">
'====================================================================================
' Begin main code
'====================================================================================
' general constants
Const MailServer = "192.168.166.1" ' Mail Server to use for SMTP
Const MailServerPort = "25" ' SMTP Port used at Mail server (25 is default)
Const MailTo = "mark@lynchconsulting.com.au" ' Who should be notified
Const MailFrom = "server@lynchconsulting.com.au"
str = ""
subject = ""
strComputerName = GetCurrentComputerName ' get name only once for performance reasons
subject = "Drive Space Report " & strComputerName
str = str & strComputerName & vbcrlf
str = str & GetFreeSpaceReport
'Output to screen - useful for debugging
Msgbox str
'Send mail to administrator
SendMail MailFrom , MailTo , subject , str, MailServer , MailServerPort
</script>
</job>
Hope it helps.




Thanks!!
Hieu Truong
Both files should be in the same directory.
Cheers,
Mark
Thanks for the feedback, however I did have the both files in c:\ location.
Cheers!!
Hieu Truong
Here are a couple of things to check:
1. What version of script host are you using? Type in "cscript" and it should display the version number on the top line. I'm using version 5.6 on windows XP.
2. What directory are you running the application from? From the console make sure you are in the same directory when you run it.
Let me know if either of these help.
Cheers,
Mark
The scirpt is find I will start to implement on my daily shcedule tonight.
Thanks Mark for your help!
Hieu Truong
only thing i want is, to see the space detail only when it is less than a specified number.
can you please let me know how to add this logic?
this will be a great help for me
I' m filled with great joy, coz i was very successful to implement the code.
The script worked once and then for some reason stopped working