Building IIS 7 Request monitor with ServerManager and WorkerProcess.GetRequests

by nolovelust 31. March 2011 13:19

Microsoft.Web.Administration Namespace is wonderful! With couple of hours work I have managed to build a request monitor to watch who requesting what and how long it takes to serve. If you aren't bothered with the code below just download this requests.zip (38.69 kb) file and put it on a classic .net 4 application pool with and id that has administrator privileges.  You won't be able to read system data if you use IUSR or apppool identity.

web.config  

<?xml version="1.0"?>
<configuration>
  <system.web>
    <customErrors mode="Off"/>
    <compilation debug="false" targetFramework="4.0">
      <assemblies>
        <add assembly="Microsoft.Web.Administration, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="Microsoft.Web.Management, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
  </system.web>
</configuration>

requests.aspx.cs


using System;
using Microsoft.Web.Administration;
using System.Text;
using System.Linq;

public partial class _requests : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        manager = new ServerManager();
        int filtered = Convert.ToInt32(Request.QueryString["filter"]);
        StringBuilder sb = new StringBuilder();
        foreach (WorkerProcess proc in manager.WorkerProcesses)
        {
            RequestCollection rc = proc.GetRequests(0);

            var selected = from r in rc
                           select r;
             if (filtered>0)
             {
                 selected  = from r in rc
                                      where r.SiteId == filtered
                                      select r;
             }


            foreach (Request r in selected)
            {
                sb.AppendFormat("<tr><td><a href=\"?filter={8}\">{8}</a></td><td>{0}</td><td>{1}</td><td>{2}</td><td><img src=\"http://mobilust.net/onlines/iptoflag.aspx?ip={3}\" alt=\"{3}\" /> {3}</td><td>{4} ({5}s)</td><td>{6}</td><td>{7}</td></tr>", r.HostName, Server.HtmlEncode(r.Url), r.Verb, r.ClientIPAddr, r.PipelineState, TimeSpan.FromMilliseconds(r.TimeInState).TotalSeconds, r.CurrentModule, TimeSpan.FromMilliseconds(r.TimeElapsed).TotalSeconds, r.SiteId);
                RequestCount++;
            }
        }

        RequestList = sb.ToString();
    }

    protected int RequestCount
    {
        get;
        set;
    }
    protected string RequestList
    {
        get;
        set;
    }
    protected ServerManager manager
    {
        get;
        set;
    }
}



requests.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="requests.aspx.cs" Inherits="_requests" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Requests</title>
    <script type="text/javascript" src="assets/jquery-latest.js"></script>
    <script type="text/javascript" src="assets/jquery.tablesorter.min.js"></script>
    <link href="assets/style.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        $(document).ready(function () {
            sortTable();
        });
        function sortTable()
        {
         $("table").tablesorter({
                sortList: [[0, 0]]
            });
        }
    </script>
</head>
<body id="requestpage">
<div class="header">
<div class="left"><h1>Total <%=manager.WorkerProcesses.Count%> Worker Processes, <%=RequestCount %> requests</h1></div>
<div class="right"><h1><a href="requests.aspx">Reset</a> <a href="">Refresh</a></h1></div>
<div class="clear"></div>
</div>
<table cellspacing="1" class="tablesorter" id="requestlist">
        <thead><tr><th> </th><th>Host Name&nbsp;</th><th>Url&nbsp;</th><th>Method&nbsp;</th><th>Client&nbsp;</th><th>State&nbsp;</th><th>Module Name&nbsp;</th><th>Time (s)&nbsp;</th></tr></thead>
        <tbody><%=RequestList%></tbody>
    </table>
</body>
</html>




You can download zip file above if you want javascript enabled sorting of the requests table.

Have look at the end result below. Keepin mind that Country flags is not part of this sample but you can get the code for that from here



 

Tags: , , , , ,

Open Source | Useful

Unable to disable Asp.Net 4 Request Validation Errors?

by nolovelust 25. November 2010 11:50

Since I started to develop with .Net 4 something was bugging me. I was unable to disable  Request Validation like on 2. There is option on .Net 4 that you can set it .Net 2 model with

   requestValidationMode="2.0" 

So that you can enable/disable validation on page level. But, if you are using .Net 4 WebForms Routing sometimes you end up with errors like

   A potentially dangerous Request.Path value was detected from the client

Although it is good thing to filter out dangerous requests in my case i need to have them present in the url.

There is a simple solution for it. You just need to set HttpRuntimeSection.RequestPathInvalidCharacters Property as you like. Default illegal chars are  "<,>,*,%,&,:,\,?".

You can change them in your web.config. Here is my perefered settings

<httpRuntime requestValidationMode="2.0" requestPathInvalidCharacters="*,:" /> 

For more info read

HttpRuntimeSection.RequestValidationMode Property
HttpRuntimeSection.RequestPathInvalidCharacters Property
http://blog.tjitjing.com/index.php/2010/10/how-to-remove-characters-to-avoid-net-request-validation-error-a-potentially-dangerous-request-path-value-was-detected-from-the-client.html

Tags: , , ,

Tag cloud

Month List