How to enumerate performance counters of a given class?
Download
|
|
The performance class is a collection of performance counters.
For example, a class with name "System" may define several performance counters,
such as "Processor Queue Length", "Context Switches/sec", "System Up Time", etc. The Delphi function below retrieves the list of performance counters that are defined for the provided class on a local or remote computer. This example utilizes TWmiPerformanceMonitor component. The source code below creates and destroys the components on the fly, so this function may be used in console- or UI-type applications. |
// This procedure determines the list of performance counters
// of the given class on the target computer. On exit, it will populate
// CounterNames parameter with the names of the found counters.
// Parameters:
// ClassName: name of the class to obtain counters for. Required.
// CounterNames: output parameter, gets populated by the function. Required.
// TargetHost: name or IP address of the host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
procedure ListPerformanceCounters(ClassName: string; CounterNames: TStrings;
TargetHost, UserName, Password: string);
var
vMonitor: TWmiPerformanceMonitor;
vPerfClass: TWmiPerfClass;
i, count: integer;
begin
vMonitor := TWmiPerformanceMonitor.Create(nil);
try
vMonitor.MachineName := TargetHost;
vMonitor.Credentials.UserName := UserName;
vMonitor.Credentials.Password := Password;
// try to connect. It may fail if the destination computer does not have
// WMI (like windows 95, 98, NT without WMI core installed), or
// if the provided credentials are not valid
try
vMonitor.Active := true;
except
Exit;
end;
vPerfClass := vMonitor.PerfClasses.FindByClassName(ClassName);
count := vPerfClass.CounterDefs.Count;
for i := 0 to count - 1 do
CounterNames.Add(vPerfClass.CounterDefs[i].DisplayName + ': ' +
vPerfClass.CounterDefs[i].Name);
finally
vMonitor.Free;
end;
end;
|