Monitoring Windows Disk Space with Scripting Host
Article created 2001-04-03 by Rainer Gerhards.
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
- detect which drives are installed in the system
- how much space they offer
- how many free space is left
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
SYSNAME,C,5109346304,136216576
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.
I hope that this article helpful. If you have any questions or remarks, please do not
hesitate to contact me at rgerhards@adiscon.com.
|