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.
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!
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");
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
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();
mObject_Disk = new ManagementObject(mgmtScope,mPath_Disk,null);
PerfRow class contains the
GetDisk() and the
GetMemory() methods to return the respective performance counters.
public string GetCPU()
ulong u_newCPU =
ulong u_newNano =
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 !
(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;
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.
First cut on November 4th 2003