NetAdminTools.com
 
SignalQ Sites:
L1G3R Information Systems - Coprolite - SpotBridge - NAW
RoboCoop - AreWeDown - SolarPower - SysAdminTools
Xfig - Gold Loaf - GeekPapa - FixGMC - FixRambler
Solar Energy - Energy Efficiency - Solar Panels
Home Energy Savings
Categories:
GNU/Linux | Homebrew designs | Perl | Ruby | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | ERP | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Ruby
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·ERP
·REALbasic
·All Categories


Monitoring Windows CPU Performance With Perl
Topic:Monitoring   Date: 2003-12-28
Printer Friendly: Print   

spacerspacer
<<  <   >  >>

Subject

The Win32::PerfLib module is a glorious way to extract performance data from Windows boxen. Check out this site for more information. We used perl 5.6.1. Here is a script that shows how to use this module:

use Win32::PerfLib;
$processor = 238;
$proctime = 6;
while (6 ne 9){
if(my $perflib = new Win32::PerfLib('srv-48')){
$proc_ref0 = {};
$proc_ref1 = {};
$perflib->GetObjectList($processor, $proc_ref0);
sleep 3;
$perflib->GetObjectList($processor, $proc_ref1);
sleep 3;
$perflib->Close($proc_ref0);
$perflib->Close($proc_ref1);
$instance_ref0 = $proc_ref0->{Objects}->{$processor}->{Instances};
$instance_ref1 = $proc_ref1->{Objects}->{$processor}->{Instances};
for $p (keys %{$instance_ref0}) {
$counter_ref0 = $instance_ref0->{$p}->{Counters};
$counter_ref1 = $instance_ref1->{$p}->{Counters};
for $i (keys %{$counter_ref0}) {
next if ($instance_ref0->{$p}->{Name} eq "_Total");
if($counter_ref0->{$i}->{CounterNameTitleIndex} == $proctime){
$Numerator0 = $counter_ref0->{$i}->{Counter};
$Denominator0 = $proc_ref0->{PerfTime100nSec};
$Numerator1 = $counter_ref1->{$i}->{Counter};
$Denominator1 = $proc_ref1->{PerfTime100nSec};
$proc_time{$p} = (1- (($Numerator1 - $Numerator0) /
($Denominator1 - $Denominator0 ))) * 100;
if ($highproc < $proc_time{$p}){
$highproc = $proc_time{$p};
}
}
}
}
print $highproc."\n";
$highproc=0;
}
}

Let's run the script:

C:\tm>\Perl\bin\perl cpu.pl
19.5
62.5
41.4507577867798
39.7905638112966
52.6041666666667
45.595836881521
25
41.8848050700341
4.14509679185638
5.12821135568664
^C
C:\tm>

Here is the task manager view on srv-48:



The if clause around the initial call to Perflib is necessary to keep the program from crashing when the host is unavailable. Set up an else clause to deal with this more completely.


People:
Places:
Things:
Times:





Please read our Terms of Use and our Privacy Policy
Microsoft, Windows, Windows XP, Windows 2003, Windows 2000, and NT are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2011 NetAdminTools.com