Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ASP.NET Server Info page

Simple ASP.NET server info page. Shows uptime, hard drive space, and RAM.

Add this to Web.config system.web -> configuration -> assemblies:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
public static class TimeSpanExtensions
{
public static string ToNiceString(this TimeSpan timespan)
{
int days = (int)timespan.TotalDays;
int hrs = timespan.Hours;
int mins = timespan.Minutes;
StringBuilder sb = new StringBuilder();
if (days > 0)
{
sb.Append(days + (days == 1 ? " day, " : " days, "));
}
if (hrs > 0 || days > 0)
{
sb.Append(hrs + (hrs == 1 ? " hour, " : " hours, "));
}
sb.Append(mins + (mins == 1 ? " min" : " mins"));
return sb.ToString();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ServerInfo.aspx.cs" Inherits="Daniel15.Server.ServerInfo" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
</head>
<body>
<dl>
<dt>Hostname</dt>
<dd><%= Environment.MachineName %></dd>
<dt>OS</dt>
<dd><%= _computerInfo.OSFullName %></dd>
<dt>Uptime</dt>
<dd><%= Uptime.ToNiceString() %></dd>
</dl>
<h2>Memory Usage</h2>
<table>
<thead>
<tr>
<th>Type</th>
<th>Usage</th>
<th>Free</th>
<th>Used</th>
<th>Size</th>
</tr>
</thead>
<tbody>
<tr>
<td>Physical memory</td>
<td><%= ((_computerInfo.TotalPhysicalMemory - _computerInfo.AvailablePhysicalMemory) / (float)_computerInfo.TotalPhysicalMemory * 100.0).ToString("0.00") %>%</td>
<td><%= ToSizeString(_computerInfo.AvailablePhysicalMemory)%></td>
<td><%= ToSizeString(_computerInfo.TotalPhysicalMemory - _computerInfo.AvailablePhysicalMemory) %></td>
<td><%= ToSizeString(_computerInfo.TotalPhysicalMemory) %></td>
</tr>
<!--tr>
<td>Swap</td>
<td><%= ToSizeString(_computerInfo.AvailableVirtualMemory) %></td>
<td><%= ToSizeString(_computerInfo.TotalVirtualMemory - _computerInfo.AvailableVirtualMemory) %></td>
<td><%= ToSizeString(_computerInfo.TotalVirtualMemory) %></td>
</tr-->
</tbody>
</table>
<h2>Mounted Filesystems</h2>
<table>
<thead>
<tr>
<th>Mountpoint</th>
<th>Usage</th>
<th>Free</th>
<th>Used</th>
<th>Size</th>
</tr>
</thead>
<tbody>
<tr>
<td>C:</td>
<td><%= (((_driveInfo.TotalSize - _driveInfo.TotalFreeSpace) / (float)_driveInfo.TotalSize) * 100.0).ToString("0.00") %>%</td>
<td><%= ToSizeString(_driveInfo.AvailableFreeSpace) %></td>
<td><%= ToSizeString(_driveInfo.TotalSize - _driveInfo.TotalFreeSpace) %></td>
<td><%= ToSizeString(_driveInfo.TotalSize) %></td>
</tr>
</tbody>
</table>
</body>
</html>
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Security.Permissions;
using Microsoft.VisualBasic.Devices;
namespace Daniel15.Server
{
public partial class ServerInfo : System.Web.UI.Page
{
protected PermissionSetAttribute _permissionSetAttribute = new PermissionSetAttribute(SecurityAction.Assert);
protected ComputerInfo _computerInfo = new ComputerInfo();
protected PerformanceCounter _uptime = new PerformanceCounter("System", "System Up Time");
protected DriveInfo _driveInfo = new DriveInfo("c");
//protected ManagementClass _cpuManagement = new ManagementClass("Win32_Processor");
protected void Page_Init(object sender, EventArgs e)
{
_uptime.NextValue();
//var cpus = _cpuManagement.GetInstances();
//cpu.Properties["Name"].Value
}
protected TimeSpan Uptime
{
get { return TimeSpan.FromSeconds(_uptime.NextValue()); }
}
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";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.