Skip to content

Instantly share code, notes, and snippets.

@kushin
Created March 15, 2010 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kushin/333472 to your computer and use it in GitHub Desktop.
Save kushin/333472 to your computer and use it in GitHub Desktop.
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.DirectoryServices" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script type="text/C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
populateIIS();
}
}
private void populateIIS()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Server: {0} Username: {1}<br/>", Environment.MachineName.ToString(), Environment.UserName.ToString());
try
{
TimeSpan span = TimeSpan.FromSeconds(Convert.ToDouble(getCounter("Web Service", "Service Uptime", "_Total")));
sb.Append(string.Concat(new object[] { "IIS Running - Uptime : ", span.Days, " Days, ", span.Hours, " Hours, ", span.Minutes, " Min <br/>" }));
}
catch { }
sb.AppendFormat("Current Anonymous users: {0}<br/>", getCounter("Web Service", "Current Anonymous Users", "_Total"));
sb.AppendFormat("Current Connections: {0}<br/>", getCounter("Web Service", "Current Connections", "_Total"));
sb.AppendFormat("Max Connections: {0}<br/>", getCounter("Web Service", "Maximum Connections", "_Total"));
sb.AppendFormat("Memory, Available MBytes: {0}<hr/>", getCounter("Memory", "Available MBytes", ""));
sb.AppendFormat("ASP.NET Request Wait Time: {0}<br/>", getCounter("ASP.NET", "Request Wait Time", ""));
sb.AppendFormat("ASP.NET Application Anonymous Requests: {0}<br/>", getCounter("ASP.NET Applications", "Anonymous Requests", "__Total__"));
sb.AppendFormat("ASP.NET Sessions Active: {0}<br/>", getCounter("ASP.NET Applications", "Sessions Active", "__Total__"));
sb.AppendFormat("ASP.NET Sessions Total: {0}<br/>", getCounter("ASP.NET Applications", "Sessions Total", "__Total__"));
sb.AppendFormat("ASP.NET Applications Running: {0}<hr/>", getCounter("ASP.NET v2.0.50727", "Applications Running", ""));
sb.Append("IIS Apps <br/>");
foreach (string s in getIISApps())
{ sb.AppendFormat("{0}", s);}
lblText.Text = sb.ToString();
}
public static string getCounter(string a, string b, string c)
{
string countervalue;
try
{
string str = string.Empty;
PerformanceCounter counter = new PerformanceCounter(a, b);
counter.InstanceName = c;
str = counter.NextValue().ToString();
counter.Dispose();
counter.Close();
countervalue = str;
}
catch (Exception exception)
{
throw exception;
}
return countervalue;
}
private List<string> getIISApps()
{
List<string> sites = new List<string>();
DirectoryEntry entry = new DirectoryEntry("IIS://" + Environment.MachineName + "/w3svc");
try
{
foreach (DirectoryEntry entry2 in entry.Children)
{
if (entry2.SchemaClassName == "IIsWebServer")
{
foreach (DirectoryEntry entry3 in entry2.Children)
{
string binding;
if (!(entry3.SchemaClassName == "IIsWebVirtualDir"))
{
continue;
}
if (entry2.Properties["ServerBindings"].Value.ToString() == "System.Object[]")
{
binding = "All Unassigned";
}
else
{
binding = entry2.Properties["ServerBindings"].Value.ToString();
}
sites.Add(string.Format("name: {0} - binding: {1}<br/>", entry2.Properties["ServerComment"].Value.ToString(), binding));
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return sites;
}
</script>
<asp:Label ID="lblText" runat="server" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment