#!/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'} - $hash_prev->{'_Total'}->{'PercentProcessorTime'} ) / ( $hash->{'_Total'}->{'TimeStamp_Sys100NS'} - $hash_prev->{'_Total'}->{'TimeStamp_Sys100NS'} ) )* 100 ); print "CPU=$cpu\n"; $hash_prev = $hash; }