Introduction
Some articles have already been written about Windows Performance Counters. Nevertheless, I still experience that their potential is underestimated by administrators and often almost unknown to many software developers.
One of the main reasons for this is the fact that they are just too many Performance counters! On a plain end-user machine, they are several hundreds of them. The amount of performance counters increases vertically on a Server.
Goal
The goal of this article is to present a way to enumerate all performance counters registered on a local machine and to give the possibility to save the result of the enumeration in an XML file. Once saved in an XML file, one can transform the result using XSLT and present it in a nice «look and feel» report.
The motivation is to make a kind of snapshot of all performance counters available on a machine. This will help developers and administrators to choose the ideal performance counters based on their description when needed. Often when dealing with diagnose and the Windows built-in «Performance Monitor», I am overwhelmed with the huge amount of these counters and the lack of having a comprehensive list of the performance counters without having to click several hundred times in the MMC dialog.
Implementation
Using the available .NET Diagnostic classes, it is just a matter of a few minutes to reach this goal.
Since this project only enumerates the category of performance counters and all performance counters belonging to these categories, I only need two of the available classes that deal with performance counters.
PerformanceCounterCategory
and PerformanceCounter
both belong to the System.Diagnostics
Namespace which, as stated in the MSDN documentation, mainly provides classes that allow you to interact with:
- system processes
- event logs
- and performance counters
Having said that, this tool consists of four steps:
- Retrieve the name of the local machine where the tool runs:
txtMachine.Text = Environment.MachineName;
- Retrieve the Categories of Performance Counters available on the machine:
performanceCountersMgr.GetPerformanceCountersCategories(txtMachine.Text.ToString());
PerformanceCounterCategory[] categories = performanceCountersMgr.Categories;
for (int i = 0; i < categories.Length; i++)
{
UpdateViewCategories(categories[i]);
}
- For any selected Category, retrieve the Counters available:
ListView.SelectedListViewItemCollection sel = listViewCategories.SelectedItems;
if(sel != null && sel.Count > 0)
{
PerformanceCounterCategory[] category = performanceCountersMgr.Categories;
textBoxCategoryDescription.Text = category[sel[0].Index].CategoryHelp;
PerformanceCounter[] counters =
performanceCountersMgr.GetPerformanceCounters(category[sel[0].Index]);
if (counters != null)
{
for (int i = 0; i < counters.Length; i++)
{
PerformanceCounter counter = counters[i];
UpdateViewCounters(counter);
}
listViewCounters.Items[0].Selected = true;
}
}
- Save the result at the desired location as an XML File:
XmlTextWriter xml = new XmlTextWriter(filename, Encoding.UTF8);
xml.WriteStartDocument(true);
xml.WriteStartElement("Categories");
PerformanceCounterCategory[] category = performanceCountersMgr.Categories;
for (int i = 0; i < category.Length; i++)
{
xml.WriteStartElement("Categorie");
xml.WriteStartAttribute("Name");
xml.WriteString(category[i].CategoryName);
if (checkBoxCategoryDescription.Checked)
{
xml.WriteStartAttribute("Description");
xml.WriteString(category[i].CategoryHelp);
}
xml.WriteEndAttribute();
xml.WriteEndElement();
}
xml.WriteEndElement();
xml.Close();
Conclusion
This article shows how to enumerate the performance counters on a local machine using just a few of the available .NET Diagnostic classes and how to serialize the enumeration in an XML file using the XmlTextWriter
class. The enumeration of these on a remote machine is left as an exercise for the reader.
History
- 11.02.2010 - Enumeration of the categories of performance counters and their associated counters