Win32::PerfLib - Accessing the Windows NT Performance Counter
use Win32::PerfLib; my $server = ""; Win32::PerfLib::GetCounterNames($server, \%counter); %r_counter = map { $counter{$_} => $_ } keys %counter; # retrieve the id for process object $process_obj = $r_counter{Process}; # retrieve the id for the process ID counter $process_id = $r_counter{'ID Process'}; # create connection to $server $perflib = new Win32::PerfLib($server); $proc_ref = {}; # get the performance data for the process object $perflib->GetObjectList($process_obj, $proc_ref); $perflib->Close(); $instance_ref = $proc_ref->{Objects}->{$process_obj}->{Instances}; foreach $p (sort keys %{$instance_ref}) { $counter_ref = $instance_ref->{$p}->{Counters}; foreach $i (keys %{$counter_ref}) { if($counter_ref->{$i}->{CounterNameTitleIndex} == $process_id) { printf( "% 6d %s\n", $counter_ref->{$i}->{Counter}, $instance_ref->{$p}->{Name} ); } } }
This module allows to retrieve the performance counter of any computer (running Windows NT) in the network.
All of the functions return false if they fail, unless otherwise noted. If the $server argument is undef the local machine is assumed.
Retrieves the counter names and their indices from the registry and stores them in the hash reference
Retrieves the counter help strings and their indices from the registry and stores them in the hash reference
Creates a connection to the performance counters of the given server
retrieves the object and counter list of the given performance object.
closes the connection to the performance counters
converts the counter type to readable string as referenced in calc.html so that it is easier to find the appropriate formula to calculate the raw counter data.
The performance data is returned in the following data structure:
$hashref = { 'NumObjectTypes' => VALUE 'Objects' => HASHREF 'PerfFreq' => VALUE 'PerfTime' => VALUE 'PerfTime100nSec' => VALUE 'SystemName' => STRING 'SystemTime' => VALUE }
The hash reference $hashref->{Objects} has the returned object ID(s) as keys and a hash reference to the object counter data as value. Even there is only one object requested in the call to GetObjectList there may be more than one object in the result.
$hashref->{Objects} = { <object1> => HASHREF <object2> => HASHREF ... }
Each returned object ID has object-specific performance information. If an object has instances like the process object there is also a reference to the instance information.
$hashref->{Objects}->{<object1>} = { 'DetailLevel' => VALUE 'Instances' => HASHREF 'Counters' => HASHREF 'NumCounters' => VALUE 'NumInstances' => VALUE 'ObjectHelpTitleIndex' => VALUE 'ObjectNameTitleIndex' => VALUE 'PerfFreq' => VALUE 'PerfTime' => VALUE }
If there are instance information for the object available they are stored in the 'Instances' hashref. If the object has no instances there is an 'Counters' key instead. The instances or counters are numbered.
$hashref->{Objects}->{<object1>}->{Instances} = { <1> => HASHREF <2> => HASHREF ... <n> => HASHREF } or $hashref->{Objects}->{<object1>}->{Counters} = { <1> => HASHREF <2> => HASHREF ... <n> => HASHREF }
$hashref->{Objects}->{<object1>}->{Instances}->{<1>} = { Counters => HASHREF Name => STRING ParentObjectInstance => VALUE ParentObjectTitleIndex => VALUE } or $hashref->{Objects}->{<object1>}->{Counters}->{<1>} = { Counter => VALUE CounterHelpTitleIndex => VALUE CounterNameTitleIndex => VALUE CounterSize => VALUE CounterType => VALUE DefaultScale => VALUE DetailLevel => VALUE Display => STRING }
$hashref->{Objects}->{<object1>}->{Instances}->{<1>}->{Counters} = { <1> => HASHREF <2> => HASHREF ... <n> => HASHREF }
$hashref->{Objects}->{<object1>}->{Instances}->{<1>}->{Counters}->{<1>} = { Counter => VALUE CounterHelpTitleIndex => VALUE CounterNameTitleIndex => VALUE CounterSize => VALUE CounterType => VALUE DefaultScale => VALUE DetailLevel => VALUE Display => STRING }
Depending on the CounterType there are calculations to do (see calc.html).
Jutta M. Klebe, jmk@bybyte.de
perl(1).
To install Win32::PerfLib, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Win32::PerfLib
CPAN shell
perl -MCPAN -e shell install Win32::PerfLib
For more information on module installation, please visit the detailed CPAN module installation guide.