Click here to Skip to main content
Click here to Skip to main content

Performance Counters Enumerator

, 11 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Enumerate all performance counters registered on a machine and save the result in an XML file

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.

Performance_Counters_Enumerator.png

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.

xml_editpro.jpg

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:

  1. Retrieve the name of the local machine where the tool runs:
    // Retrieve the machine's name. 
    txtMachine.Text = Environment.MachineName; 
  2. Retrieve the Categories of Performance Counters available on the machine:
    // Get the Categories of Performance Counters
    performanceCountersMgr.GetPerformanceCountersCategories(txtMachine.Text.ToString());
    PerformanceCounterCategory[] categories = performanceCountersMgr.Categories;
    for (int i = 0; i < categories.Length; i++)
    {
        // Show these at the UI
        UpdateViewCategories(categories[i]);
    }
  3. For any selected Category, retrieve the Counters available:
    // Collect Counters for the selected Category
    ListView.SelectedListViewItemCollection sel = listViewCategories.SelectedItems;
    if(sel != null && sel.Count > 0)
    {
        // Show Description of the selected Category
        PerformanceCounterCategory[] category = performanceCountersMgr.Categories;
        textBoxCategoryDescription.Text = category[sel[0].Index].CategoryHelp;
        // Acquire the Counters of the selected Category
        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;
        }
    }
  4. 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(); 
    }
    // End of Categories Element
    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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

marc ochsenmeier
Software Developer (Senior) winitor
Germany Germany
Marc Ochsenmeier is the author of PEStudio (www.winitor.com) and works as developer with the focus on Windows Hardening.
 
PEStudio is on twitter at: https://twitter.com/ochsenmeier

Comments and Discussions

 
GeneralMy vote of 5 PinmemberDirk Held4-Nov-10 4:19 
GeneralRe: My vote of 5 Pinmembermarc ochsenmeier16-May-11 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 11 Feb 2010
Article Copyright 2010 by marc ochsenmeier
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid