Web Robot View of http://authors.aspalliance.com/brettb/ASPDiskMonitoringScript.asp

Page Item Value
Title ASP Disk Monitoring Script
Description How to monitor disk usage using ASP and VBScript
Keywords asp, disk, space, monitor, monitoring, determine, vbscript
Robots Meta Tag  
Page Content   ASP Kitchen
Search: Go Home | ASP Articles | ASP.NET Articles | Tools | Table Of Contents | What's New

ASP Kitchen : ASPWatch.com articles : ASP Disk Monitoring Script

An ASP Disk Space Monitor Introduction A previous article describes how to use the Drives collection to access information about disk drives attached to an ASP web server. This article expands on the ideas presented in that article by describing how to create a dynamic disk space monitor. The techniques used here could be used to ensure that there is enough disk space before a user uploads a large file to the server, or perhaps the code could be used to help create a web based server monitoring tool so that the server administrator can keep a track of server resources when miles from home.

The typical output from this script is shown in the image below. For each accessible drive connected to the system, the drive letter is printed, together with the drive s volume name (if it has one). The amount of used space on a disk is shown in red, free space shown in blue. This effect is created by resizing red and blue GIF images to give a representation of the total amount of disk space on a given drive. Additionally, the images ALT tags have been set up so that they give the amount of disk space (in the example below, the ALT tag for the C: drive s free space shows that there is 227.1Mb of free disk space).

If you want the images used above, they are available in a ZIP file which is linked from the bottom of this article.

Dissecting the script The first line of code creates a FileSystemObject object. This is a powerful object when used with ASP its most common use is for reading and writing text files on the server, but here the Drives property is used to find out information about the drives connected to the server:

Set FileSystemObject = Server.CreateObject( Scripting.FileSystemObject )

The following line of code establishes the Drives object as a Drives collection:

Set Drives = FileSystemObject.Drives

The Drives collection will contain information about all of the drives connected to the server, so a For Each Next loop is used to loop through each member of the Drives collection:

For Each DiskDrive in Drives

One thing to remember is that although a drive is connected to a server, it may not be accessible. The script will generate errors if it attempts to access certain drive properties if that drive is not ready. Removable media such as a floppy, CD-ROM or ZIP drives will not be accessible unless there are disks in the drives. Fortunately, the IsReady property of each drive can be used to check that the drive is ready:

If DiskDrive.IsReady Then

From the TotalSize and FreeSpace drive properties, it is possible to work out the amount of disk space in use on that drive:

totalspace = DiskDrive.TotalSize
freespace = DiskDrive.FreeSpace
usedspace = totalspace - freespace

Disk space is reported as bytes, so the following code will convert the space to Mb. It will also convert free and used space as a percentage of the total space on a drive:

freepercent = Int((freespace/totalspace)*100)
freemb = Int((freespace/1024)/1024)
usedmb = Int((usedspace/1024)/1024)
usedpercent = Int((usedspace/totalspace)*100)

The following code is used to format the text that will be used for the ALT tags that display disk space. To make the display look nicer, it shows space less than 1 Mb in size as Kb, and space greater than 1024 Mb as Gb. The FormatNumber function is used to specify the number of decimal places that the numbers should be rounded to:

If freespace = 1048576 Then
freelabel = FormatNumber(freespace/1024, 1) Kb
Elseif freespace = 1073741824 then
freelabel = FormatNumber((freespace/1024)/1024, 1) Mb
Else
freelabel = FormatNumber(((freespace/1024)/1024)/1024, 2) Gb
End If

If usedspace = 1048576 Then
usedlabel = FormatNumber(usedspace/1024, 1) Kb
Elseif usedspace = 1073741824 Then
usedlabel = FormatNumber((usedspace/1024)/1024, 1) Mb
Else
usedlabel = FormatNumber(((usedspac e/1024)/1024)/1024, 2) Gb
End If

There is also a bit of a hack for drives that have no free or used space this is reported as 0 bytes:

If Left(freelabel,3) = 0.0 Then freelabel = 0 bytes
If Left(usedlabel,3) = 0.0 Then usedlabel = 0 bytes

The ALT tags for the free and used space are then prepared:

used_alt_tag = Drive DiskDrive.DriveLetter : usedlabel in use
free_alt_tag = Drive DiskDrive.DriveLetter : freelabel free

The drive letter and (if it has one) volume name are then output using Response.Write statements:

Response.Write Drive DiskDrive.DriveLetter :

If DiskDrive.VolumeName then
Response.Write [ DiskDrive.VolumeName ] :
End If

Further Response.Write statements are then used to display the graphical representation of disk space. Note that the width of the free and used space indicators are altered according to the percentage calculated earlier. The actual image is composed of four elements: a left border, the used space indicator, the free space indicator, and finally the right border.

Response.Write img align=absmiddle src=bb-drives-diskindicator-leftborder.gif
Response.Write img align=absmiddle src=bb-drives-diskindicator-used.gif width= usedpercent height=29 alt= Chr(34) used_alt_tag Chr(34)
Response.Write img align=absmiddle src=bb-drives -diskindicator-free.gif width= (100-usedpercent) height=29 alt= Chr(34) free_alt_tag Chr(34)
Response.Write img align=absmiddle src=bb-drives-diskindicator-rightborder.gif

Response.Write br

The next two lines of code will allow the script to proceed to the next drive in the Drives collection:

End If
Next

The final lines of code release the created objects from system memory:

Set Drives = nothing
Set FileSystemObject = nothing

And that s it. Hopefully you should be able to find a use for the script don t hesitate to email me if you find an interesting use for it.

For convenience, the entire code for the script is given below.

The entire code for the disk monitoring script

%
'An ASP disk space monitoring script
'See http://www.brettb.com/ASPDiskMonitoringScript.asp

'Create a FileSystemObject object
Set FileSystemObject = Server.CreateObject( Scripting.FileSystemObject )

Set Drives = FileSystemObject.Drives

'Step through the drives collection, and extract information about any drive that is ready
For Each DiskDrive in Drives

If DiskDrive.IsReady Then

'The following lines work out the amount of used and free space on a disk. ASP reports disk space in bytes.
'Divide bytes by 1024 to get Kb, Kb by 1024 to get Mb, and so on...

totalspace = DiskDrive.TotalSize
freespace = DiskDrive.FreeSpace
usedspace = totalspace - freespace

freepercent = Int((freespace/totalspace)*100)
freemb = Int((freespace/1024)/1024)
usedmb = Int((usedspace/1024)/1024)
usedpercent = Int((usedspace/totalspace)*100)

'If the amount of disk space is below one Mb then report freespace as Kb.
'A similar thing is done for Mb and Gb of space.

If freespace = 1048576 Then
freelabel = FormatNumber(freespace/1024, 1) Kb
Elseif freespace = 1073741824 then
freelabel = FormatNumber((freespace/1024)/1024, 1) Mb
Else
freelabel = FormatNumber(((freespace/1024)/1024)/1024, 2) Gb
End If

If usedspace = 1048576 Then
usedlabel = FormatNumber(usedspace/1024, 1) Kb
Elseif usedspace = 1073741824 Then
usedlabel = FormatNumber((usedspace/1024)/1024, 1) Mb
Else
usedlabel = FormatNumber(((usedspace/1024)/1024)/1024, 2) Gb
End If

If Left(freelabel,3) = 0.0 Then freelabel = 0 bytes
If Left(usedlabel,3) = 0.0 Then usedlabel = 0 bytes

used_alt_tag = Drive DiskDrive.DriveLetter : usedlabel in use
free_alt_tag = Drive DiskDrive.DriveLetter : freelabel free

Response.Write Drive DiskDrive.DriveLetter :

If DiskDrive.VolumeName then
Response.Write [ DiskDrive.VolumeName ] :
End If

'Create the graphical representation of the freespace
Response.Write img align=absmiddle src=bb-drives-diskindicator-leftborder.gif
Response.Write img align=absmiddle src=bb-drives-diskindicator-used.gif width= usedpercent height=29 alt= Chr(34) used_alt_tag Chr(34)
Response.Write img align=absmiddle src=bb-drives-diskindicator-free.gif width= (100-usedpercent) height=29 alt= Chr(34) free_alt_tag Chr(34)
Response.Write img align=absmiddle src=bb-drives-diskindicator-rightborder.gif

Response.Write br
End If
Next

Set Drives = nothing
Set FileSystemObject = nothing
%

Images used by the disk monitoring script If you would like the four images used with this script (created using Kai's Power Tools 5), then download this ZIP file. Disk monitoring script images (2.03 Kb ZIP file). Further reading Using the VBScript Drives Collection . Sams Teach Yourself Web Development With ASP in 24 Hours . Start learning ASP with this introductory guide. Useful Development Tools ASP Documentation Tool Automatically creates developer documentation for ASP 2.0 and 3.0 web applications written in VBScript and JScript. Documentation for Microsoft Access, SQL Server 7/2000 databases and Visual Basic 6.0 components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats. View Sample Output (HTML Help format).
View Sample Output (HTML Format).
Download Trial Version (5.2Mb ZIP file).
Index Server Companion The Index Server Companion is a Windows application that extends the functionality of Microsoft Index Server so that it is able to index content from remote websites and also from ODBC databases. As such it can be used as a low cost alternative to Site Server 3.0 Search. View Product Documentation (119K ZIP file).
Try Sample Search Facility .
Download Trial Version (1.7Mb ZIP file).
ASP.NET Documentation Tool Automatically creates developer documentation for ASP.NET web applications written in C# or VB.NET. Documentation for SQL Server 7/2000 databases and C#/VB.NET components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats. View Sample Output (HTML Help format).
View Sample Output (HTML Format).
Download Trial Version (727K ZIP file).
SQL Documentation Tool The SQL Documentation Tool creates technical documentation for Microsoft SQL Server 7.0 and 2000 databases. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced. The SQL Documentation Tool documents SQL Server Tables, Views, Stored Procedures, Triggers and Table Relationships. View Sample Output (HTML Help format).
View Sample Output (HTML Format).
Download Trial Version (10.3Mb ZIP file).
The Website Utility The Website Utility examines websites for errors and areas that need to be optimised for search engines by using a built in web crawling engine. Errors checked for include broken or moved hyperlinks, missing page titles and missing meta tags. It also generates HTML for use in creating website site maps (table of contents pages - like this one ), and is able to create both client-side JavaScript Search Engines and server-side ASP Search Engines for a website. View Sample Output (HTML Format).
Download Trial Version (3Mb ZIP file).
Text Workbench Text Workbench is a file search and replacement utility for text files and Microsoft Office documents. Make rapid file replacements on multiple files and folders full of files. Advanced replacement options include regular expressions support. It even works on remote file systems via FTP. A Regular Expression Laboratory allows advanced pattern matching and replacement expressions to be built and tested. This great utility will make your everyday development tasks much easier! Download Trial Version (3Mb ZIP file; you have the option to either install directly from this link or save the file for later installation). Author details Brett Burridge spent two years working in the University of Essex Computing Service, before moving to The Internet Applications Group in the Autumn of 1999, where he developed e-Business applications for a range of corporate clients and dot-com start ups. Brett is presently employed as an Internet developer and technical writer through his own company, Winnersh Triangle Web Solutions Limited . The company produces a number of innovative products, including the popular ASP Documentation Tool , the Index Server Companion , the ASP.NET Documentation Tool , the SQL Server Documentation Tool and The Website Utility . The company is also available for web application design and development at reasonable rates, primarily using Microsoft technologies (ASP, ASP.NET, Visual Basic, SQL Server) but also using open source technologies such as PHP, MySQL and Perl. Specialist services include development of search solutions using Microsoft's Index Server and Site Server 3.0 Search. As well as the ASPAlliance, Brett has written articles for Ariadne.ac.uk and ASPToday , and has contributed recipes to the ASP.NET Developer's Cookbook . links Outside web development, Brett is interested in digital photography (here's my photo gallery ), tropical fishkeeping and collecting contemporary works of art by artists such as Doug Hyde .

Article history An ASP disk space monitor originally published on ASPWatch.com on September 22 1999. Republished on ASPAlliance.com on 28 September 2001.

ASP Kitchen : ASPWatch.com articles : ASP Disk Monitoring Script

page content copyright Brett Burridge 1998 - 2004.
Image Alt Tags ASPAlliance
Example output from the ASP disk space monitoring script
View Sample Output (HTML Help format)
View Sample Output (HTML Format)
Download Trial Version
View Product Documentation
Try Sample Search Facility
Download Trial Version
View Sample Output (HTML Help format)
View Sample Output (HTML Format)
Download Trial Version
View Sample Output (HTML Help format)
View Sample Output (HTML Format)
Download Trial Version
View Sample Output (HTML Format)
Download Trial Version
Download Trial Version of Text Workbench
Index Server Companion - allows Index Server to index content from remote websites and ODBC databases!!!
Download a Free ASP Documentation Tool Now!
Google
Search Engine Builder - Build a search engine for your website!
Internal Links http://authors.aspalliance.com/brettb/Default.asp (3 links in this page) [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/VBScriptDrivesCollection.asp (2 links in this page) [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/ASPWatchArticles.asp (2 links in this page) [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/TableOfContents.asp (2 links in this page) [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/ASP.NetArticles.aspx [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/Tools.asp [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/What'sNew.aspx [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/ClassicASPArticles.asp [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/Links.asp [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/contact.asp [ Robot View of this URL ]
http://authors.aspalliance.com/brettb/redirector.asp [ Robot View of this URL ]
http://authors.aspalliance.com/ [ Robot View of this URL ]

Reporting Main Page

Report generated by The Website Utility 2.8