SignalR Server Monitor
Hi Guys,
This time I made a small web application which monitors the server usage real time using SignalR. Hope You like it.
1) Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Htm5Notifications.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Server Usage Dashboard</title>
<script src="../Scripts/jquery-1.6.4.js"></script>
<script src="../Scripts/jquery.signalR-2.2.0.js"></script>
<script src="http://localhost:51255/signalr/hubs/" type="text/javascript"></script>
<script src="Message.js"></script>
<link href="../Styles/bootstrap.min.css" rel="stylesheet" />
<script src="justgage.1.0.1.min.js"></script>
<script src="raphael.2.1.0.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 >Server Usage Dashboard</h1>
</div>
<div class="panel-body">
<div class="col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-bar-chart-o fa-fw"></i> Overall Summary</h3>
</div>
<div class="panel-body">
<div class="alert alert-success" role="alert"><asp:Label ID="lblMsg" runat="server" Text=""></asp:Label></div><br />
<div class="list-group">
<a href="#" class="list-group-item">
<h3 class="list-group-item-heading " id="hProcess"></h3>
<h5 class="list-group-item-text">Processes</h5>
</a>
<a href="#" class="list-group-item">
<h3 class="list-group-item-heading " id="hThread"></h3>
<h5 class="list-group-item-text">Threads</h5>
</a>
<a href="#" class="list-group-item">
<h3 class="list-group-item-heading " id="hCMemory"></h3>
<h5 class="list-group-item-text">Total Memory (MB)</h5>
</a>
<a href="#" class="list-group-item">
<h3 class="list-group-item-heading " id="hMemory"></h3>
<h5 class="list-group-item-text">Free Memory (MB)</h5>
</a>
</div>
</div>
</div>
</div>
<div class="col-lg-8">
<div class="pull-right" style="padding-bottom:10px">
<asp:Button ID="btnStart" class="btn btn-primary" runat="server" Text="Start Monitor" OnClick="btnStart_Click" />
</div>
<br />
<br />
<br />
<div class="col-lg-4">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title" ng-switch on="IsBarWeekly">
<span ng-switch-when="true">Processor Usage (%) </span>
</h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<div id="cpuG"></div>
</div>
</div>
</div>
</div><div class="col-lg-4">
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title" ng-switch on="IsBarWeekly">
<span ng-switch-when="true">Memory Usage (%) </span>
</h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<div id="memG"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
2) Default.aspx.cs
using Htm5Notifications.Message;
using Htm5Notifications.MonitorService;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading.Tasks;
using System.Threading;
namespace Htm5Notifications
{
public partial class Default : System.Web.UI.Page
{
//Token To Cancel Task
public CancellationTokenSource RunningTaskToken
{
get
{
if (Session["__RunningTask"] != null)
return Session["__RunningTask"] as CancellationTokenSource;
else
return null;
} set { Session["__RunningTask"] = value; } }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btnStart_Click(object sender, EventArgs e)
{
RunningTaskToken = new CancellationTokenSource();
var token = RunningTaskToken.Token;
Task.Factory.StartNew(() =>
{
try
{
//Performance Counters
PerformanceCounter cpuUsage =
new PerformanceCounter("Processor", "% Processor Time", "_Total"),
ramCounter = new PerformanceCounter("Memory", "Available MBytes"),
ramCacheCounter = new PerformanceCounter("Memory", "System Driver Resident Bytes"),
processCounter = new PerformanceCounter("System", "Processes"),
threadCounter = new PerformanceCounter("System", "Threads");
float totalMemory,availMemory;
Microsoft.VisualBasic.Devices.ComputerInfo myCompInfo
= new Microsoft.VisualBasic.Devices.ComputerInfo();
while (true)
{
availMemory = ramCounter.NextValue();
totalMemory = Convert.ToInt32((myCompInfo.TotalPhysicalMemory / 1024) / 1024);
MessageHub.SendMessages(cpuUsage.NextValue().ToString(), availMemory.ToString(),
processCounter.NextValue().ToString(), threadCounter.NextValue().ToString(),
totalMemory.ToString(),
(((totalMemory - Convert.ToInt32(availMemory)) / totalMemory) * 100).ToString());
System.Threading.Thread.Sleep(1000);
}
}
catch (Exception ex)
{
}
},token);
lblMsg.Text = "Monitoring Started On : " + DateTime.Now;
}
}
}
3) MessageHub.cs
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace Htm5Notifications.Message
{
[HubName("messageHub")]
public class MessageHub : Hub
{
[HubMethodName("sendMessages")]
public static void SendMessages(string cpu,string mem,string process,string thread,
string cacheMemory,string memUsage)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessageHub>();
context.Clients.All.showMessage(cpu, mem, process, thread, cacheMemory, memUsage);
}
}
}
4) Message.js
/// <reference path="../Scripts/jquery-1.6.4.js" />
/// <reference path="../Scripts/jquery.signalR-2.2.0.js" />
$(function () {
//Instance of the Hub
//var hub = $.connection.messageHub;
var cpuG = null,memG = null;
var Notification = window.Notification || window.mozNotification || window.webkitNotification;
Notification.requestPermission(function (permission) {
// console.log(permission);
});
function show(cpu, mem, proc, thread, cmem, memUse) {
if (cpuG == null)
cpuG = new JustGage({
id: "cpuG",
value: parseInt(cpu),
min: 0,
max: 100,
title: " ",
label: "",
levelColorsGradient: false
});
cpuG.refresh(parseInt(cpu));
if (memG == null)
memG = new JustGage({
id: "memG",
value: parseInt(memUse),
min: 0,
max: 100,
title: " ",
label: "",
levelColorsGradient: false
});
memG.refresh(parseInt(memUse));
$('#hProcess').html(proc);
$('#hThread').html(thread);
$('#hMemory').html(mem);
$('#hCMemory').html(cmem);
return false;
}
var messageHub = $.connection.messageHub;
messageHub.client.showMessage = function (cpu, mem, proc, thread, cmem, memUse) {
show(cpu, mem,proc, thread,cmem,memUse);
}
$.connection.hub.url = "http://localhost:51255/signalr";
$.connection.hub.start().done(function () {
}).fail(function (error) {
console.error(error);
});;
});
Solution
This time I made a small web application which monitors the server usage real time using SignalR. Hope You like it.
1) Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Htm5Notifications.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Server Usage Dashboard</title>
<script src="../Scripts/jquery-1.6.4.js"></script>
<script src="../Scripts/jquery.signalR-2.2.0.js"></script>
<script src="http://localhost:51255/signalr/hubs/" type="text/javascript"></script>
<script src="Message.js"></script>
<link href="../Styles/bootstrap.min.css" rel="stylesheet" />
<script src="justgage.1.0.1.min.js"></script>
<script src="raphael.2.1.0.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 >Server Usage Dashboard</h1>
</div>
<div class="panel-body">
<div class="col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-bar-chart-o fa-fw"></i> Overall Summary</h3>
</div>
<div class="panel-body">
<div class="alert alert-success" role="alert"><asp:Label ID="lblMsg" runat="server" Text=""></asp:Label></div><br />
<div class="list-group">
<a href="#" class="list-group-item">
<h3 class="list-group-item-heading " id="hProcess"></h3>
<h5 class="list-group-item-text">Processes</h5>
</a>
<a href="#" class="list-group-item">
<h3 class="list-group-item-heading " id="hThread"></h3>
<h5 class="list-group-item-text">Threads</h5>
</a>
<a href="#" class="list-group-item">
<h3 class="list-group-item-heading " id="hCMemory"></h3>
<h5 class="list-group-item-text">Total Memory (MB)</h5>
</a>
<a href="#" class="list-group-item">
<h3 class="list-group-item-heading " id="hMemory"></h3>
<h5 class="list-group-item-text">Free Memory (MB)</h5>
</a>
</div>
</div>
</div>
</div>
<div class="col-lg-8">
<div class="pull-right" style="padding-bottom:10px">
<asp:Button ID="btnStart" class="btn btn-primary" runat="server" Text="Start Monitor" OnClick="btnStart_Click" />
</div>
<br />
<br />
<br />
<div class="col-lg-4">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title" ng-switch on="IsBarWeekly">
<span ng-switch-when="true">Processor Usage (%) </span>
</h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<div id="cpuG"></div>
</div>
</div>
</div>
</div><div class="col-lg-4">
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title" ng-switch on="IsBarWeekly">
<span ng-switch-when="true">Memory Usage (%) </span>
</h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<div id="memG"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
2) Default.aspx.cs
using Htm5Notifications.Message;
using Htm5Notifications.MonitorService;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading.Tasks;
using System.Threading;
namespace Htm5Notifications
{
public partial class Default : System.Web.UI.Page
{
//Token To Cancel Task
public CancellationTokenSource RunningTaskToken
{
get
{
if (Session["__RunningTask"] != null)
return Session["__RunningTask"] as CancellationTokenSource;
else
return null;
} set { Session["__RunningTask"] = value; } }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btnStart_Click(object sender, EventArgs e)
{
RunningTaskToken = new CancellationTokenSource();
var token = RunningTaskToken.Token;
Task.Factory.StartNew(() =>
{
try
{
//Performance Counters
PerformanceCounter cpuUsage =
new PerformanceCounter("Processor", "% Processor Time", "_Total"),
ramCounter = new PerformanceCounter("Memory", "Available MBytes"),
ramCacheCounter = new PerformanceCounter("Memory", "System Driver Resident Bytes"),
processCounter = new PerformanceCounter("System", "Processes"),
threadCounter = new PerformanceCounter("System", "Threads");
float totalMemory,availMemory;
Microsoft.VisualBasic.Devices.ComputerInfo myCompInfo
= new Microsoft.VisualBasic.Devices.ComputerInfo();
while (true)
{
availMemory = ramCounter.NextValue();
totalMemory = Convert.ToInt32((myCompInfo.TotalPhysicalMemory / 1024) / 1024);
MessageHub.SendMessages(cpuUsage.NextValue().ToString(), availMemory.ToString(),
processCounter.NextValue().ToString(), threadCounter.NextValue().ToString(),
totalMemory.ToString(),
(((totalMemory - Convert.ToInt32(availMemory)) / totalMemory) * 100).ToString());
System.Threading.Thread.Sleep(1000);
}
}
catch (Exception ex)
{
}
},token);
lblMsg.Text = "Monitoring Started On : " + DateTime.Now;
}
}
}
3) MessageHub.cs
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace Htm5Notifications.Message
{
[HubName("messageHub")]
public class MessageHub : Hub
{
[HubMethodName("sendMessages")]
public static void SendMessages(string cpu,string mem,string process,string thread,
string cacheMemory,string memUsage)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessageHub>();
context.Clients.All.showMessage(cpu, mem, process, thread, cacheMemory, memUsage);
}
}
}
4) Message.js
/// <reference path="../Scripts/jquery-1.6.4.js" />
/// <reference path="../Scripts/jquery.signalR-2.2.0.js" />
$(function () {
//Instance of the Hub
//var hub = $.connection.messageHub;
var cpuG = null,memG = null;
var Notification = window.Notification || window.mozNotification || window.webkitNotification;
Notification.requestPermission(function (permission) {
// console.log(permission);
});
function show(cpu, mem, proc, thread, cmem, memUse) {
if (cpuG == null)
cpuG = new JustGage({
id: "cpuG",
value: parseInt(cpu),
min: 0,
max: 100,
title: " ",
label: "",
levelColorsGradient: false
});
cpuG.refresh(parseInt(cpu));
if (memG == null)
memG = new JustGage({
id: "memG",
value: parseInt(memUse),
min: 0,
max: 100,
title: " ",
label: "",
levelColorsGradient: false
});
memG.refresh(parseInt(memUse));
$('#hProcess').html(proc);
$('#hThread').html(thread);
$('#hMemory').html(mem);
$('#hCMemory').html(cmem);
return false;
}
var messageHub = $.connection.messageHub;
messageHub.client.showMessage = function (cpu, mem, proc, thread, cmem, memUse) {
show(cpu, mem,proc, thread,cmem,memUse);
}
$.connection.hub.url = "http://localhost:51255/signalr";
$.connection.hub.start().done(function () {
}).fail(function (error) {
console.error(error);
});;
});
5) Startup.cs
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Htm5Notifications
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
Solution
Comments
Post a Comment