System Information and the PerformanceCounter Class

The other day I discussed retrieving system information using the .NET framework. I guess the main focus of that post was to discuss using PInvoke to call unmanaged WIN32 DLL exports, more so than actually retrieving different pieces of system information. The objective of that post was retrieving memory information (I also retrieve disk information in my ‘AboutBox’ discussed in that post). In the 'managed codeworld' there is another way to get different information values. Using the System.Diagnostics.PerformanceCounter Class you can retrieve ‘counter’ information from a local or remote machine. Many of you may be familiar with some of the information accessible through PerformanceCounters if you run the
performance monitor control panel applet.
The information accessible via the System.Diagnostics.PerformanceCounter Class is not limited to just system information. Many services and applications also make ‘counter’ information available for retrieval. Performance Counters are broken out by category (System.Diagnostics.PerformanceCounterCategory). Within each category there many be multiple instances, for example the Logical Disk category will have an instance for each drive, as well as an instance for the _Total of all drives. Each instance will have its own set of counters that can be retrieved. In keeping with the same example, the _Total instance of the Logical Disk category has counters such as ‘% Free Space’, ‘Free Megabytes’ and ‘% Disk Write Time’ to name a few.
Reading counter information is a lot easier than one would expect (the .NET Framework almost made things too easy). I had created a basic-sample PerformanceCounter application that allows for the selection of one of the available (on the local machine) performance counter categories. Once the category is selected, each instance is listed (if there is more than one). Once the counter to be retrieved is selected the value is displayed in a System.Windows.Forms.ListBox (using a System.Timers.Timer for interval). Note: When I use Delphi for development I use the TChart component to display graphs (as I had in BPSNMPMon). If anyone knows of a decent charting control that works with Visual Studio 2005 let me know.

System.Diagnostics.PerformanceCounter perfcounter;

        private void cbxPerfCategories_SelectedIndexChanged( object sender, EventArgs e )
        {
            cbxInstances.Enabled = true;
            cbxInstances.Items.Clear ();
            cbxCounters.Items.Clear ();
            lstOutput.Items.Clear ();
            timer1.Enabled = false;

            if ( cbxPerfCategories.SelectedIndex != -1 )
            {
                if ( PerformanceCounterCategory.Exists ( cbxPerfCategories.Text ) )
                {
                    PerformanceCounterCategory perfcategory =
                        new PerformanceCounterCategory ( cbxPerfCategories.Text );

                    cbxInstances.Items.AddRange ( perfcategory.GetInstanceNames () );
                    if ( cbxInstances.Items.Count == 0 )
                    {
                        cbxInstances.Enabled = false;
                        // Get the counters for a category that has only one
                        // instance
                        GetCounters ( perfcategory, "", cbxCounters );
                    }
                }
            }
        }

        private void GetCounters( PerformanceCounterCategory perfcategory,
            string instancename,
            ComboBox combobox )
        {
            System.Diagnostics.PerformanceCounter[] counters;
            if ( instancename.Equals ( "" ) )
            {
                counters = perfcategory.GetCounters ();
            }
            else
            {
                counters = perfcategory.GetCounters ( instancename );
            }
            for ( int i = 0; i < counters.Length; i++ )
            {
                combobox.Items.Add ( counters[i].CounterName );
            }
        }

        private void frmMain_Load( object sender, EventArgs e )
        {
            PerformanceCounterCategory[] perfcategories;
            try
            {
                perfcategories = PerformanceCounterCategory.GetCategories ();
                for ( int i = 0; i < perfcategories.Length; i++ )
                {
                    cbxPerfCategories.Items.Add ( perfcategories[i].CategoryName );
                }
            }
            catch ( Exception ex )
            {
                MessageBox.Show ( ex.Message, ex.Source, MessageBoxButtons.OK,
                    MessageBoxIcon.Error );
            }
        }

        private void cbxInstances_SelectedIndexChanged( object sender, EventArgs e )
        {
            cbxCounters.Items.Clear ();
            lstOutput.Items.Clear ();
            timer1.Enabled = false;

            if ( cbxInstances.SelectedIndex != -1 )
            {
                if ( PerformanceCounterCategory.InstanceExists ( cbxInstances.Text, cbxPerfCategories.Text ) )
                {
                    PerformanceCounterCategory perfcategory =
                      new PerformanceCounterCategory ( cbxPerfCategories.Text );
                    GetCounters ( perfcategory, cbxInstances.Text, cbxCounters );
                }
            }
        }

        private void cbxCounters_SelectedIndexChanged( object sender, EventArgs e )
        {
            lstOutput.Items.Clear ();
            if ( cbxInstances.Text.Equals ( "" ) )
            {
                perfcounter = new System.Diagnostics.PerformanceCounter ( cbxPerfCategories.Text,
                    cbxCounters.Text, true );
            }
            else
            {
                perfcounter = new System.Diagnostics.PerformanceCounter ( cbxPerfCategories.Text,
                    cbxCounters.Text, cbxInstances.Text, true );
            }
            timer1.Enabled = true;
        }

        private void timer1_Tick( object sender, EventArgs e )
        {
            float value;
            value = perfcounter.NextValue ();
            lstOutput.Items.Insert ( 0, value.ToString ( "#,##0.00" ) );
        }



   

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading