Monitoring Free Disk Space in Real-Time

Microsoft® Windows® 2000 Scripting Guide

If disk space is used at a fairly predictable rate, you do not have to constantly monitor the free space on a disk. Instead, you might check free space once or twice a day and then use that data to predict when available space might become scarce.

However, disk use might not always be predictable. On some servers, the amount of data added to the drive might vary widely from day to day. If these servers carry out critical functions in your organization, you might prefer to be notified immediately if disk space runs low. Instead of finding out tomorrow morning that the mail server is almost out of disk space, you might want to be informed the moment available space drops below a specified level.

To receive these immediate notifications, you can use a temporary event consumer to monitor available disk space. You can specify a threshold, such as 100 megabytes (MB) of free space, and receive immediate notification if available disk space on the computer falls below that threshold.

Scripting Steps

Listing 10.8 contains a script that monitors the free disk space on a computer. To carry out this task, the script must perform the following steps:

  1. Create a constant named LOCAL_HARD_DISK and set the value to 3.

    This ensures that the query responds only to changes to the hard disks installed on the computer.

  2. Create a variable to specify the computer name.

  3. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  4. Use the ExecNotificationQuery method to register for event notifications. In this case, notification is requested any time there is a modification to the Win32_LogicalDisk class. The Within 30 clause specifies that events will be checked every 30 seconds, which means you will receive a notice no later than 30 seconds after a disk runs low on disk space.

  5. Create a Do loop to allow for continuous monitoring.

  6. Each time notification is received (by means of the NextEvent method), check to see if the event involves a hard disk. If it does, proceed to step 7. If it does not, proceed to step 8.

  7. If the event involved a change to a hard disk and the available space on the disk is now less than 100,000,000 bytes, echo the message "Hard disk space has fallen below 100,000,000 bytes." In a production-level script, of course, you might send an e-mail or a network alert rather than simply echo a message to the screen.

  8. Loop around and continue monitoring.

Listing 10.8 Monitoring Free Disk Space


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredDisks = objWMIService.ExecNotificationQuery _
 ("Select * from __instancemodificationevent within 30 where " _
 & "TargetInstance isa 'Win32_LogicalDisk'")
i = 0
Do While i = 0
 Set objDiskChange = colMonitoredDisks.NextEvent
 If objDiskChange.TargetInstance.DriveType = LOCAL_HARD_DISK Then
 If objDiskChange.TargetInstance.Size < 100000000 Then
 Wscript.Echo "Hard disk space is below 100000000 bytes."
 End If
 End If