Here is the code:
#!/usr/bin/perl -w use strict; use warnings; use Win32::OLE; # Init WMI my $wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2") or die "Failed getobject\n"; # get WMI values sub get_wmi{ my $wmi = shift; my $list, my $v; my @properties = qw(PercentProcessorTime TimeStamp_Sys100NS); my $class = 'Win32_PerfRawData_PerfOS_Processor'; my $key = 'Name'; $list = $wmi->InstancesOf("$class") or die "Failed getobject\n"; my $hash; foreach $v (in $list) { $hash->{$v->{$key}}->{$_} = $v->{$_} for @properties; } $hash; } # CPU calculation my $cpu; my $hash_prev = get_wmi($wmi); while(1){ sleep 2; my $hash = get_wmi($wmi); $cpu = sprintf("%.2f", ( 1 - ( $hash->{'_Total'}->{'PercentProcessorTime'} - $has +h_prev->{'_Total'}->{'PercentProcessorTime'} ) / ( $hash->{'_Total'}->{'TimeStamp_Sys100NS'} - $hash_ +prev->{'_Total'}->{'TimeStamp_Sys100NS'} ) )* 100 ); print "CPU=$cpu\n"; $hash_prev = $hash; }
Another similar solution in:
http://stackoverflow.com/questions/4750582/get-wmi-cpu-values-using-perl
In reply to Re: Windows 7 CPU usage
by gulden
in thread Windows 7 CPU usage
by gepebril69
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |