26 June, 2008

Spy on Your Web Host: Checking Uptime

Your host promises 99.999% uptime, but are they really delivering? Unless you're either checking every couple of seconds, or dishing out on some monitoring service, you really don't have much of an idea.

ASP.NET Web pages served up through IIS are delivered through an ASP.NET "worker process" (aspnet_wp.exe). It's this file that executes your code and puts all the ASP.NET pieces together. If we could access this worker process, we may be able to look at how long it had been running, its current state—and perhaps even a history, showing previous instantiations of the worker process.

Dim strReport As String
Dim objInfo As ProcessInfo = _
' Get time information
strReport = "ASP.NET was started at " & objInfo.StartTime.ToString & ". " & "It has been running for " & objInfo.Age.Days & " days, " & objInfo.Age.Hours & " hours and " & objInfo.Age.Minutes & " minutes. "

' Get other info
strReport = "The process ID is " & objInfo.ProcessID & ". " & "Current status is " & objInfo.Status.ToString & ". " & "Peak memory used was " & objInfo.PeakMemoryUsed & ". " & "Request count is currently " & objInfo.RequestCount & "."


What we're doing here is retrieving a ProcessInfo object containing information about the current worker process. This is then formulated into a string using key properties and spurted out to the user, through Response.Write. Try it out—you'll be given an instant server status report.

The ProcessModelInfo class also has a GetHistory method, allowing you to retrieve information about previous instances of the aspnet_wp.exe process. It returns an array of ProcessInfo objects. Either use a For...Next loop to retrieve this information, or bind direct to a Grid.

There's also a technique for retrieving the amount of time your actual server has been running (not just the ASP.NET worker process). Simply nab the TickCount from the Environment class, which lists the number of milliseconds the machine has been running. After that, either perform a few simple calculations—or, slightly easier, convert it into a period of time (a TimeSpan object), then retrieve the appropriate properties. Here's my snippet of code that does exactly that:

Dim tsAge As TimeSpan = TimeSpan.FromMilliseconds(Environment.TickCount)
Dim intDaysUp As Integer = tsAge.Days
Dim intHoursUp As Integer = tsAge.Hours
Dim intMinsUp As Integer = tsAge.Minutes
Response.Write("The server has been up " & intDaysUp & " days, " & intHoursUp & " hours and " & intMinsUp & " minutes.")

Caption: ASP.NET only been running for 13 minutes? Sounds dodgy. Change your host.

No comments: