Monitoring Windows Disk Space with Scripting Host
Did you ever run out of disk space on an important machine? Never? Well, I’ve done so and experienced all the headaches that come along with this situation. In this article, I describe how I implemented a basic automatic monitor system with the help of a little scripting.
Why monitor Disk space?
Today’s systems seem to offer near endless disk space. However, user tend to fill up even the largest disks. And keep in mind that system administrators are users, too ;-). There are several reasons for monitoring disk space. The most obvious one is to learn on disk space shortage before it will post a problem. But there are also less obvious reasons. Many IT managers like to have a statistical chart of the disk space usage over time – so that they have something for their senior management when it comes to buying new disks (or memory to feed the cache).
As you see, you’ll probably want to get an idea of how many space is used on your systems. Ideally, you will monitor capacity installed as well as free space.
What does the system provide?
Windows NT, 2000, XP and 2003 have build in support for monitoring disk space. However, only free space is monitored and only once it reaches a specific threshold an event is written to the system event log. But that’s it – no admin alert and even the threshold can not be configured. The Windows 9x variants do not even provide that basic notification.
So what does this mean? Do we need to purchase a full blown monitoring solution just to keep track of the disk space. Definitely not! Fortunately, Microsoft provides a component called Windows Scripting Host. It is a free component for all flavors of Windows.
What is Scripting Host?
Basically, it is an engine that allows execution of VB script (and even Java Script) outside of a web browser. For those programming script on the web or using Visual Basic, there is not much new. All others need to know at least a bit about scripting to create their own scripts. Not a complex task. And those we don’t like scripting at all: relax – a fully functional sample script can be found at the end of this page – ready to run.
With scripting, Microsoft has provided an easy way to automate administrative tasks – like checking the disk space. Scripting host is installed by default on all current Microsoft Windows platforms (like Windows 2000). It is a free download off Microsoft’s web site for the older platforms (like Windows NT 4).
How does the script work?
My sample script uses the so-called “FileSystemObject”. It is provided by Scripting Host and always present if Scripting Host is installed. It allows to access the file system and has the necessary functionality to
I take that object and query all drivers present in the system. If they are hard disk type (e. g. no CD-ROM or diskette), the script evaluates the total and remaining space. That information is then formatted in comma-delimited form and added to a string. There is one line for each disk present in the system.
Finally, that string is send via email to a specific recipient. To send it, I use a small tool – SimpleMail – developed by us. It works with any smtp mail server and is easy to use – ideal for scripting.
Please note that once you have the string, you are not limited to sending it via email. For centralized administration, you can also forward it to a syslog server (e. g. run on Unix) or whatever else you can think of.
The string generated typically looks like
All fields are comma-delimited. The first one is the computer name, the second the drive id (C, D, E, …) the comes the total space in bytes and the remaining free space. That format can easily be imported into an Excel sheet to create one of these nice charts senior management loves.
Can I automatically schedule it?
Now that we have this nice script, we will make sure that it is run without our help. This is especially true if we would like to monitor several systems. Once again Microsoft has provided the right tool:
You can use the “Scheduled Task” application that comes with the operating system. It is found in the Start menu’s “Programs/Accessories/System Tools” folder. Simply start it and click on “Add Scheduled Task” and follow the wizard. When it comes to “Click the program you want to run”, click browse and find and select the VB script file (yes, this is a program!). Then follow the wizard and select a schedule, etc. That’s all you need to do. Oh, well, one last thing: make sure it runs under an account with sufficient privileges to access the disk statistics!
If your platform does not support the “Scheduled Task” application, you might want to visit the Microsoft site for a free update. Under NT 4, you can alternately use the task scheduler together with it’s “at” command line tool. But that is neither cool nor intuitive ;-).
Ready to go?
I mentioned a number of resources in my article. If you would like to follow my route, please visit these sites to obtain a free trial of the components I used to create my script:
Then, copy and paste the following script into your favorite text editor (e.g. Windows Notepad). Save that file with a .vbs extension in order to make it executable. Be sure to change the config settings right at the top of the file.
Sample code for monitoring windows disk space
This script reports the drive usage of all fixed drives on the system it is run. The report will be sent via plain text email to a specified recipient address (see last line in file).
This sample can be used in a production environment to set up an unattended disk utilization report system.
For more information contact Adiscon at http://www.adiscon.com/
Support for SimpleMail is available at http://www.simplemail.adiscon.com/
Constants for drive types
Const Unknown = 0
Const Removable = 1
Const Fixed = 2
Const Remote = 3
Const CDROM = 4
Const RAMDisk = 5
Const MailServer = "127.0.0.1" ' Mail Server to use for SimpleMail
Const MailServerPort = "25" ' SMTP Port used at Mail server (25 is default)
Send a mail message
Sub SendMail(Sender, Recipient, Subject, Message)
set o = WScript.CreateObject("Adiscon.SimpleMail.1")
o.MailServer = MailServer
o.Port = MailServerPort
o.Sender = Sender
o.Recipient = Recipient
o.Subject = Subject
o.MessageText = Message
a = o.SendEx
we do not check the return state here – it wouldn’t help if we detect an error! This script is intended to run in the background, so there is little we can do.
get current computer name (from system environment variables)
set oWsh = WScript.CreateObject("WScript.Shell")
set oWshSysEnv = oWsh.Environment("PROCESS")
GetCurrentComputerName = oWshSysEnv("COMPUTERNAME")
' Begin main cod
str = ""
set oFs = WScript.CreateObject("Scripting.FileSystemObject")
set oDrives = oFs.Drives
strComputerName = GetCurrentComputerName ' get name only once for performance reasons
for each oDrive in oDrives
Select case oDrive.DriveType
str = str & strComputerName & "," & oDrive.DriveLetter & "," & oDrive.TotalSize & "," & oDrive.FreeSpace & vbcrlf
SendMail "bounce@nowhere", "YourEMail@nowhere.dotcom", "Drive Space Report", str
Rainer Gerhards works for Adiscon, who offers software for server monitoring. Visit http://www.monitorware.com for more information and free downloads.