This is the script that i've done to collect CPU info:
use strict; use warnings; use Win32::OLE; my $interval = 1; # seconds my $key = 'Name'; my @properties = qw(PercentIdleTime PercentProcessorTime PercentPrivil +egedTime PercentUserTime PercentInterruptTime TimeStamp_Sys100NS); my $hash1 = {}; my $wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2") or die "Failed to get object\n"; my $list = $wmi->InstancesOf('Win32_PerfRawData_PerfOS_Processor') or die "Failed to get instance object\n"; my $v; foreach $v (in $list) { map{$hash1->{$v->{$key}}->{$_} = $v->{$_} }@properties; } while(sleep $interval){ $list = $wmi->InstancesOf('Win32_PerfRawData_PerfOS_Processor') or die "Failed to get instance object\n"; my $hash = {}; foreach $v (in $list) { map{$hash->{$v->{$key}}->{$_} = $v->{$_} }@properties; } my $cpu_time = sprintf("%.2f", (1 - get_value($hash1->{'_Total'}, +$hash->{'_Total'}, 'PercentProcessorTime' )) * 100); my $cpu_idle = sprintf("%.2f", 100-$cpu_time); my $cpu_user = sprintf("%.2f", get_value($hash1->{'_Total'}, $hash +->{'_Total'}, 'PercentUserTime' )* 100); my $cpu_priv = sprintf("%.2f", get_value($hash1->{'_Total'}, $hash +->{'_Total'}, 'PercentPrivilegedTime' )* 100); my $cpu_int = sprintf("%.2f", get_value($hash1->{'_Total'}, $hash- +>{'_Total'}, 'PercentInterruptTime' )* 100); printf "CPU Time %s %% , privileged %s %% , user %s %%, interrupt +%s %%\n", $cpu_time,$cpu_priv,$cpu_user,$cpu_int; $hash1 = $hash; } exit; sub get_value { my $h1 = shift; my $h2 = shift; my $property = shift; return (($h2->{$property} - $h1->{$property})/($h2->{'TimeStamp_Sy +s100NS'}-$h1->{'TimeStamp_Sys100NS'})); }
Output sample:
CPU Time 2.03 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 % CPU Time 1.87 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 % CPU Time 2.16 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 % CPU Time 1.76 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 % CPU Time 2.19 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 % CPU Time 1.77 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 % CPU Time 1.98 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 % CPU Time 1.93 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 % CPU Time 2.08 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 % CPU Time 2.84 % , privileged 2.94 % , user 0.00 %, interrupt 0.00 %
«A contentious debate is always associated with a lack of valid arguments.»

In reply to Re: How to GET CPU values from WMI by gulden
in thread How to GET CPU values from WMI by gulden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.