How Long has the Web Server Been Up?By Scott Mitchell
|More Web Server Information at your Finger Tips|
|One of the coolest things about ASP.NET is its ability to use all of the classes and methods of the .NET Framework. This means that tasks that required a COM component in classic ASP can be done natively with ASP.NET. This current article shows how to determine how long the Web server computer has gone without a reboot. In Displaying Information about the ASP.NET Process, we look at how to determine statistical information about the ASP.NET process itself. In Displaying Performance Monitor Information through an ASP.NET Web Page we look at how to read data from the Windows Performance Monitor (such as free memory, CPU utlization, ASP.NET information, IIS information, etc.) and display it through an ASP.NET Web page.|
Have you ever wondered how long your Web server has been running without needing a reboot? If you work at a Web host, or are physically close to your Web server, determining this information is very easy. However, if you Web site is hosted by a Web hosting company or if you don't have access to the actual box, finding out such information may be difficult. Well, it was difficult with classic ASP, but with ASP.NET finding out all sorts of performance information about your Web server is easy!
Determining How Long Your Web Server Has Been Running
In computer-speak, the uptime of a computer is the time between it's run since its last reboot. With older versions of Windows, sadly, this time interval was usually very low, especially compared to other operating systems (think UNIX and its derivatives) that boasted months or even years of continuous uptime. With Windows 2000 and Windows XP, though, Windows users have come to expect lengthy uptimes. For example, my personal computer usually has an average uptime of around 50-75 days.
If you use a Web hosting company, you may be interested to know just how often the machine that hosts your Web site is being rebooted. Remember, each time it's rebooted, there's an interval of time where your visitors will not be able to visit the site. Being able to keep tabs on this uptime number, if nothing else, serves as a metric for how well your Web hosting company is doing keeping your site up and running!
Determining the uptime and displaying it through an ASP.NET Web page is quite simple. The .NET Framework
Environment class that contains all sorts of machine-level information, such as:
the machine name, the OS version, the system directory, the machine's environment variables, and (important
for us) the
TickCount is the number of milliseconds the machine has been running. It is
represented as a 32-bit integer, meaning that, due to the size restrictions of such a sized integer,
will be "reset" every 49.7 days. That means, after 49.7 consecutive days of uptime, the
property will be reset to 0.
Displaying the number of milliseconds your Web server has been running is not that useful; rather, we'd like to know the days, hours, and minutes. We can compute these via a simple calculation:
Hopefully these calculations are clear. The bold part of the code uses the
TickCount property to retrieve the number of milliseconds the computer has been
running. Next, the number of seconds the machine has been running is simply the number of milliseconds
divided by 1,000 (since there are 1,000 milliseconds per second), the number of minutes is the number
of seconds divided by 60, and so on.
When displaying this information, we have to be a bit clever, since the value of
number of minutes the machine has been running) might be a large number, like 6,346. We are only interested
in displaying a value between 0 and 59, hence we use the
Mod operator, as shown in the code below:
A simpler, and more elegant way, to display the uptime of the Web server is to use the
TimeSpanclass. This class contains properties and methods to allow for determining differences between dates and times. (For an article on accomplishing such a task with classic ASP, be sure to read this FAQ.) By using this class, we don't need to do the division and modulo math that we performed in our above example.
Pretty straight forward, and without all that division. Furthermore, when displaying these values,
we can just display
minutesUp and not need to worry about displaying
minutesUp Mod 60. (Thanks to Ryan Trudelle-Schwarz
The final code, and a live demo, can be seen below.
Complete Source Code
Determining how long your Web server has been running without needing a reboot is an easy task to accomplish with ASP.NET. In fact, you can determine a myriad of other performance metrics, such as RAM usage, CPU usage, etc. through the .NET Framework by using classes to read the Windows performance tool readings. All this through an ASP.NET Web page! Perhaps a future article will cover this topic in more detail...