ArtinSoft's Blogs

Software Migration Experts
Welcome to ArtinSoft's Blogs Sign in | Join | Help
in Search

Jose Aguilar's Blog

All things migrations, software and technology

Reading Virtual Server's performance counters from .NET

This code allows you to retrieve the information for Virtual Server’s performance counters from C#. It reads both the Virtual Machines and Virtual Processors counters.

 

   1:  PerformanceCounterCategory vmCat =
new PerformanceCounterCategory("Virtual Machines");
   2:              PerformanceCounterCategory vpCat = 
new PerformanceCounterCategory("Virtual Processors");
   3:  string[] vmInstances = vmCat.GetInstanceNames();
   4:  string[] vpInstances = vpCat.GetInstanceNames();
   5:   
   6:  List<PerformanceCounter> countersList = 
new List<PerformanceCounter>();
   7:  
   8:  foreach (string instance in vmInstances){
   9:  PerformanceCounter[] counters =
vmCat.GetCounters(instance);
  10:  foreach (PerformanceCounter pc in counters)
  11:  {
  12:              countersList.Add(pc);
  13:  }
  14:  
  15:  foreach (string instance in vpInstances)
  16:  {
  17:              PerformanceCounter[] counters =
vpCat.GetCounters(instance);
  18:              foreach (PerformanceCounter pc in counters)
  19:              {
  20:                          countersList.Add(pc);
  21:              }
  22:  }
  23:   
  24:  while (true)
  25:  {
  26:              System.Threading.Thread.Sleep(1000);
  27:              foreach (PerformanceCounter pc in countersList)
  28:              {
  29:                          Console.WriteLine(
                                  "{0}:\"{1}\" Counter:{2} Value:{3}",
                                  pc.CategoryName, pc.InstanceName,
                                  pc.CounterName, pc.NextValue());
  30:              }
  31:  }
Published Aug 29 2006, 02:55 PM by Jaguilar
Filed under:

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Non-Commercial Edition), by Telligent Systems