mikedopp
mikedopp

Reputation: 199

Web Server Monitoring via asp.net web page

I would like to monitor the following on a web page:

I host a small cluster of servers for web hosting. I need to create a hardware view within ASP.NET to get as close to a real-time snapshot as possible of what's going on.

I have heard of Spiceworks or other means for accomplishing this task. I agree that these are great tools, but I would like to code this and just keep it simple.

Here is some existing code I have come up with/found:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] logicalDrives = System.Environment.GetLogicalDrives();
            //do stuff to put it in the view.
        }
        protected static string ToSizeString(double bytes)
        {
            var culture = CultureInfo.CurrentUICulture;
            const string format = "#,0.0";

            if (bytes < 1024)
                return bytes.ToString("#,0", culture);
            bytes /= 1024;
            if (bytes < 1024)
                return bytes.ToString(format, culture) + " KB";
            bytes /= 1024;
            if (bytes < 1024)
                return bytes.ToString(format, culture) + " MB";
            bytes /= 1024;
            if (bytes < 1024)
                return bytes.ToString(format, culture) + " GB";
            bytes /= 1024;
            return bytes.ToString(format, culture) + " TB";
        }
        public static string ToApproximateString(this TimeSpan time)
        {
            if (time.TotalDays > 14)
                return ((int)(time.TotalDays / 7)).ToString("#,0.0") + " weeks";
            if (14 - time.TotalDays < .75)
                return "two weeks";
            if (time.TotalDays > 1)
                return time.TotalDays.ToString("#,0.0") + " days";
            else if (time.TotalHours > 1)
                return time.TotalHours.ToString("#,0.0") + " hours";
            else if (time.TotalMinutes > 1)
                return time.TotalMinutes.ToString("#,0.0") + " minutes";
            else
                return time.TotalSeconds.ToString("#,0.0") + " seconds";
        }
    }
} 

Upvotes: 4

Views: 2185

Answers (3)

Alan McBee
Alan McBee

Reputation: 4320

Similar to what @Sumo said, you need to use Windows Performance Counters (PC), from the System.Diagnostics namespace.

Part of the problem with your question is that you are a little vague about what you want to measure from the perspective of PCs. PCs are very specific and very narrow; they measure one highly detailed metric. You will have to translate your requirements to the specific Windows PC that you want.

You said you want to measure:

  • Total response time
  • Total bytes
  • Throughput (reqs/sec)
  • Ram used
  • Hard Drives space
  • IO issues
  • Server CPU overhead
  • Errors (by error code)
  • MSSQL load

You should also consult the Windows Technet reference at http://technet.microsoft.com/en-us/library/cc776490(WS.10).aspx (it's W2K3, but it still applies to W2K8/R2). This will provide you with a wide overview and explanation of all the performance counters that you are looking for.

Running down each one:

  • Total response time

To my knowledge, there are no ASP.NET PCs that list this. And, it probably wouldn't be meaningful to you, anyway, as ASP.NET will also be responding to a wide variety of requests that you probably don't care how long it takes (i.e. anything ending with .axd). What I do in my projects is create a custom PC, but there are other techniques available (like using a custom trace listener).

  • Total bytes
  • Throughput (reqs/sec)

I believe there are PCs for both of these, although I think Total bytes might be listed under the Web Service category, whereas Throughput is probably an ASP.NET category.

  • RAM used

There is a Memory category, but you need to decide whether you are looking for working set size, physical RAM used, etc.

  • Hard drive free space

Check the LogicalDisk category

  • IO issues

What does this mean? Again, review the available PCs to see what seems most relevant.

  • Server CPU overhead

You will find this under the Processor category

  • Errors (by error code)

You can get the total number of errors thrown, or the rate at which exceptions get thrown, but if you want to collect the entries in the EventLog, you will need to use the EventLog classes in the System.Diagnostics namespace.

  • MSSQL load

I didn't find the reference overview of SQL Server PCs, but Brent Ozar is an expert, and he has a list of PCs to check here: http://www.brentozar.com/archive/2006/12/dba-101-using-perfmon-for-sql-performance-tuning/. This list is not likely to have changed much for SQL Server 2008/R2.

NOTES:

  • You may need to make sure that the identity for the application pool running your web application has been added to the computer's user group called Windows Performance Monitor Users.
  • You only need to open your counters for read-only access.
  • Performance Counters are components, and therefore implement IDisposable. Be sure you .Dispose() them (or, better still, use using() statements).
  • Use the .NextValue() method to get your values; there is almost never any need to use .RawValue or .NextSample().

I'm not giving you exact names for each counter, because it's very important that you really understand what each one measures and how useful it is to you, and only you can answer that. Experiment.

Upvotes: 1

Sumo
Sumo

Reputation: 4112

Performance counters are exposed via the System.Diagnostics.PerformanceCounter class. Here are some Performance Counters for ASP.NET. And, another how-to.

Upvotes: 1

martin308
martin308

Reputation: 716

I would suggest using an analytic service such as New Relic. Page for .Net usage is here New Relic for .Net.

Upvotes: 0

Related Questions