Click here to Skip to main content
6,635,160 members and growing! (20,804 online)
Email Password   helpLost your password?
Languages » C# » Applications     Intermediate

Performance Monitor Grid

By retZ

Tool to monitor machines on network.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:4 Nov 2003
Views:115,648
Bookmarked:80 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 5.31 Rating: 4.32 out of 5
1 vote, 5.9%
1

2
1 vote, 5.9%
3
7 votes, 41.2%
4
8 votes, 47.1%
5

PerfGrid

Introduction

The Performance Monitor Grid (PMG) displays performance counters of machines on a network in a DataGrid format. Each row in the DataGrid indicates the CPU%, Available Memory (in Megabytes) and Disk % for a particular host. A user can monitor up to 5 machines on the network at a time.

When closed, the application window remains in the systray and would "alert" the user if the performance counters on any host exceeds a certain threshold.

Background

Well, cutting to the chase, I am currently learning C# and writing some code along the way sounded good to me. If any piece of code in this app becomes useful to you any day, I would be really happy!

Using the code

The code uses .NET's WMI PerfRawData classes of the System.Management namespace. Figuring out how to make sense out of the "raw data" returned by these classes was a pain and I had to spend quite sometime in MSDN and Google for that!

The PerfRow class is the core class of the application. A separate instance of this class is created for each row in the DataGrid. The code below shows the connect method of the PerfRow class to connect to a host.

public int Connect()
{
    if (Dns.GetHostName() == strHostName)
    {
        mgmtScope = new 
          ManagementScope("\\\\" + strHostName+ "\\root\\cimv2");
    }
    else
    {
        options          = new ConnectionOptions();
        options.Username = strUserName;
        options.Password = strPassword;

        // scope object for remote machine
        mgmtScope = new 
          ManagementScope("\\\\" + strHostName + "\\root\\cimv2",options);
        
    }

    // connect to machine to monitor. Return 0 if not able to connect
    try
    {
        mgmtScope.Connect();
    }
    catch(System.UnauthorizedAccessException)
    {
        return 0;
    }

    // CPU%
    mPath_CPU                = new ManagementPath();
    mPath_CPU.RelativePath   = "Win32_PerfRawData_PerfOS_Processor.Name='0'";
    mObject_CPU  = new ManagementObject(mgmtScope,mPath_CPU,null);

    // Memory Available (in MBytes)
    mPath_Mem                = new ManagementPath();
    mPath_Mem.RelativePath   = "Win32_PerfRawData_PerfOS_Memory";
    mc  = new ManagementClass(mgmtScope,mPath_Mem,null);

    // Disk %            
    mPath_Disk               = new ManagementPath();
    mPath_Disk.RelativePath  = 
      "Win32_PerfRawData_PerfDisk_PhysicalDisk.Name='_total'";
    mObject_Disk = new ManagementObject(mgmtScope,mPath_Disk,null);

    return 1;
}

The PerfRow class contains the GetCPU(), GetDisk() and the GetMemory() methods to return the respective performance counters.

public string GetCPU()
{
    decimal PercentProcessorTime=0;
    mObject_CPU.Get();

    ulong  u_newCPU   = 
      (ulong)mObject_CPU.Properties["PercentProcessorTime"].Value;
    ulong u_newNano   = 
      (ulong)mObject_CPU.Properties["TimeStamp_Sys100NS"].Value;
    decimal d_newCPU  = Convert.ToDecimal(u_newCPU);
    decimal d_newNano = Convert.ToDecimal(u_newNano);
    decimal d_oldCPU  = Convert.ToDecimal(u_oldCPU);
    decimal d_oldNano = Convert.ToDecimal(u_oldNano);

    // Thanks to MSDN for giving me this formula !

    PercentProcessorTime = 
      (1 - ((d_newCPU-d_oldCPU)/(d_newNano - d_oldNano)))*100m;

    // Save the values for the next run

    u_oldCPU          = u_newCPU;
    u_oldNano         = u_newNano;
            
    return PercentProcessorTime.ToString("N",nfi);;
}

Points of interest

Need to add a lot of tweaks to the code. I have just about ignored cleaning up memory. Also would like to introduce an e-mailing feature in the alerts.

History

First cut on November 4th 2003

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

retZ


Member

Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 34 (Total in Forum: 34) (Refresh)FirstPrevNext
GeneralAny one know how to pass this data to a database ? Pinmemberryan.summey10:56 12 Jan '09  
Generalcan we get it for windows mobile 6.0 PinmemberMember 296291310:34 5 Aug '08  
GeneralRe: can we get it for windows mobile 6.0 Pinmemberryan.summey10:55 12 Jan '09  
QuestionProblem with Vista Pinmemberasp_guy5:54 30 Nov '07  
GeneralRe: Problem with Vista Pinmemberkameshpareek21:34 22 Apr '08  
QuestionProblem with Vista Pinmemberasp_guy5:53 30 Nov '07  
GeneralPerformance grid for each process Pinmemberchristalin4:58 27 Jul '07  
GeneralRe: Performance grid for each process PinmemberMember 47369265:14 27 Feb '09  
GeneralGood Job Pinmembervincent73722:05 23 Jul '07  
GeneralGood work buddy Pinmemberzion41718:22 12 Dec '06  
GeneralGetting the CPU% Dynamically PinmemberSidathRatnajeewa0:03 7 Jul '06  
Questionplz help me in adding 2 more columns Pinmembercabinet3691:14 12 Feb '06  
GeneralGr8 ! job but i ve a doubt Pinmembercabinet3692:54 11 Jan '06  
GeneralWhat CPU% means? PinmemberWin32nipuh1:56 11 Feb '05  
Generalit doesn't work in xp Pinmemberkimshw21:02 6 May '04  
GeneralRe: it doesn't work in xp PinmemberIneffable12:49 22 Sep '04  
Generalrstatd PinmemberretZ8:02 14 Apr '04  
Generalcan we get the usage of Win98? Pinmemberarahmad8210:39 31 Mar '04  
GeneralRe: can we get the usage of Win98? PinmemberIneffable12:50 22 Sep '04  
GeneralFormulas Pinmemberratoncete4:10 30 Jan '04  
GeneralGreat Job Pinmembermikhaelb12:35 11 Dec '03  
GeneralRe: Great Job PinmemberRetish P5:33 12 Dec '03  
GeneralRe: Great Job Pinmembermikhaelb5:48 12 Dec '03  
GeneralRe: Great Job PinmemberretZ17:11 29 Dec '03  
GeneralRe: Great Job PinmemberIneffable12:53 22 Sep '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Nov 2003
Editor: Smitha Vijayan
Copyright 2003 by retZ
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project