Hi there

Often is useful having access system informations like processor, memory and network usage, for audit needs or simply because addicted by all those great gauges and indicators giving realtime informations regarding our system!

In .NET we have the ability to read system health status by using PerformanceCounter class. This class is a bit tricky to use because needs lot of configuration like counter name, system name, performance group name and eventually instance name… Moreover, the class itself is a bit legacy within the .NET release history, this means that does not support event based notifications or neither the INotifyPropertyChanged interface greatly useful for any WPF application.

 

I made a simple proxy (available on NuGet – TNX.SystemMonitor) for accessing all basic system information data with support for IDisposable and INotifyPropertyChanged interfaces, with added support for event notifications, letting the class easy to use in conjunction with any event notification collector like the Azure Event Hub.

The new helper proxy names PerformanceCollector. For accessing real HyperV host processor usage, instead, use the HyperVisorPerformanceCollector class.

 

Both classes share the same configuration and engine. The configurable properties are:

  • DecimalDigits: setup the precision in the rouding function executed per each data retrieval (valid range: 0-4)
  • SamplingDelayMs: the delay in Ms used between a performance collector data polling (valid range: 10-10,000)
  • NotifyOnlyOnDataChange: if set to True, the class will fire a Notify only on data value change, instead, the notify will fire at any data polling

 

The usage is definitely easy:

[WPF]

var collector = new HyperVisorPerformanceCollector()
{
    DecimalDigits = 1,
    SamplingDelayMs = 100,
    NotifyOnlyOnDataChange = true,
};
DataContext = collector;

<Grid>
    <Viewbox VerticalAlignment=”Center” >
        <UniformGrid Columns=”2″>
            <Label Content=”CPU: ” />
            <Label Content=”{Binding ProcessorTimePercentValue}” HorizontalContentAlignment=”Right” />
            <Label Content=”RAM: ” />
            <Label Content=”{Binding MemoryUsagePercentValue}” HorizontalContentAlignment=”Right” />
            <Label Content=”LAN IN: ” />
            <Label Content=”{Binding NetworkInMBitValue}” HorizontalContentAlignment=”Right” />
            <Label Content=”LAN OUT: ” />
            <Label Content=”{Binding NetworkOutMBitValue}” HorizontalContentAlignment=”Right” />
        </UniformGrid>
    </Viewbox>
</Grid>

 

For any event based usage, there are typed CLR events that helps avoiding the string-based design of the PropertyChanged event:

collector.ProcessorTimePercentValueChanged += collector_ProcessorTimePercentValueChanged;
collector.MemoryUsagePercentValueChanged += collector_MemoryUsagePercentValueChanged;
collector.NetworkInMBitValueChanged += collector_NetworkInMBitValueChanged;
collector.NetworkOutMBitValueChanged += collector_NetworkOutMBitValueChanged;

 

void collector_NetworkOutMBitValueChanged(object sender, EventArgs e)
{
    Debug.WriteLine(string.Format(“OUT: {0}”, (sender as PerformanceCollector).NetworkOutMBitValue));
}

void collector_NetworkInMBitValueChanged(object sender, EventArgs e)
{
    Debug.WriteLine(string.Format(“IN: {0}”, (sender as PerformanceCollector).NetworkInMBitValue));
}

void collector_MemoryUsagePercentValueChanged(object sender, EventArgs e)
{
    Debug.WriteLine(string.Format(“RAM: {0}”, (sender as PerformanceCollector).MemoryUsagePercentValue));
}

void collector_ProcessorTimePercentValueChanged(object sender, EventArgs e)
{
    Debug.WriteLine(string.Format(“CPU: {0}”, (sender as PerformanceCollector).ProcessorTimePercentValue));
}

 

Any question is welcome

Bye

Lascia un commento