Core metrics (CPU, IO, Memory, Disk)

May 24, 2011 at 12:04 PM

Not sure if MW is the right infrastructure for this, but as part of our instrumentation suite we really need to get the core environment metrics exposed in some simple format. (i.e. bypassing existing hosting provider monthly reports) - ideally a 'live' chart similar to that provided by ServerDensity.

Requirements are pretty simple - CPU usage, network IO, memory usage, and disk space. The metrics themselves are simple to gather:

System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total");
There would need to be two separate processes - one that gathers the raw data and publishes it so that it can graphed, with a second process overlaying that that would use a threshold to raise alerts if any measure starts going awry.
Jun 4, 2011 at 5:55 PM

I would recommend that you do not use the PerformanceCounter.NextValue() function.  Not all counters are created equally.  Some counters that are duration, average count, or inverse timers require 2 samples. There is a very handy class that can compute the value based on the collected samples for you.

PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor Time", "_Total");
CounterSample sample = pc.NextSample();
float value = CounterSampleCalculator.ComputeCounterValue(sample);
double sample example,
PerformanceCounter pc1 = new PerformanceCounter("category", "average count", "_Total");
CounterSample sample1 = pc1.NextSample();
// wait some time ( counters are updated every 400ms, so need to wait longer than that)
CounterSample sample2 = pc2.NextSample(); float pcValue = CounterSampleCalculator.ComputeCounterValue(sample1, sample2); Debug.WriteLine(pcValue);