Disk space monitoring script for windows

This is the first VB script I've done in a long time - but I think it's probably pretty handy for some people so here it is - be kind :-)

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

' Constants for drive types
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.
<job>
<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.

Related Blog Entries

Comments
Hieu Truong's Gravatar I've tried to run the script on my laptop (for testing) and I'm getting an error "cannnot retrieve referenced url: lib_diskspace.vbs (both files are located on my c drive). Where the file be located?

Thanks!!
Hieu Truong
# Posted By Hieu Truong | 10/8/07 6:11 PM
Mark Lynch's Gravatar Hi Hieu,
Both files should be in the same directory.
Cheers,
Mark
# Posted By Mark Lynch | 10/8/07 8:08 PM
Hieu Truong's Gravatar G'day Mark,
Thanks for the feedback, however I did have the both files in c:\ location.

Cheers!!
Hieu Truong
# Posted By Hieu Truong | 10/15/07 7:10 PM
alvin's Gravatar Wow, fantastic code :). how to add additional server in for monitoring, so that in one email i can get all the disk space details?
# Posted By alvin | 10/24/07 5:01 PM
Hieu Truong's Gravatar I still can't get the script working. I've try to put both files on the same location and still get the above error ""cannnot retrieve referenced url: lib_diskspace.vbs"
# Posted By Hieu Truong | 10/29/07 3:34 PM
Mark Lynch's Gravatar Hi Hieu,
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
# Posted By Mark Lynch | 10/29/07 4:45 PM
Hieu Truong's Gravatar Sorry everyone !! I've made a dumpiest mistake, I when through the code and chek the spelling on the file "lib_diskspace.vbs" I have left out the "s" on "space".
The scirpt is find I will start to implement on my daily shcedule tonight.

Thanks Mark for your help!

Hieu Truong
# Posted By Hieu Truong | 10/29/07 7:20 PM
JOHN's Gravatar it is giving vbscript compliation error
# Posted By JOHN | 12/27/07 2:54 AM
Russ Mittler's Gravatar The script works well! Ive never really worked with scripts... which most likely makes my job more difficult.. I just never learned it. However I am learning it now. Great script!
# Posted By Russ Mittler | 2/29/08 8:50 PM
jared's Gravatar I have also tried to do this, and it is the same directories, however i cannot seem to get it to work right. (i am also new to WMI scripting)
# Posted By jared | 3/12/08 4:57 PM
Nagaraju's Gravatar This script works fantastic for me.
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
# Posted By Nagaraju | 5/5/08 2:49 PM
saravanan rv's Gravatar hi,

I' m filled with great joy, coz i was very successful to implement the code.
# Posted By saravanan rv | 6/17/08 7:48 PM
Tameem Khan's Gravatar Hi,

The script worked once and then for some reason stopped working
# Posted By Tameem Khan | 8/21/08 11:13 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.