使用 Perl 获取 WMI CPU 值

发布于 2024-10-13 05:12:23 字数 2742 浏览 3 评论 0原文

我正在尝试使用 WMI 类获取 CPU 值,但运气不佳。

  1. 对于 Win32_PerfFormattedData_PerfOS_Processor 类,我总是得到相同的值,它们不会改变...
  2. 对于 WMI 类 Win32_PerfRawData_PerfOS_ProcessorPercentProcessorTime 的值code> 等于 PercentProcessorTime,有问题。

#!/usr/bin/perl -w
use strict;
use warnings;

use Win32::OLE;
use Data::Dumper;

my $class = "Win32_PerfFormattedData_PerfOS_Processor";
my $key = 'Name';

my @properties = qw(PercentIdleTime PercentProcessorTime PercentPrivilegedTimePercentUserTime PercentInterruptTime);
my $wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2")
    or die "Failed getobject\n";

my $list, my $v;

$list = $wmi->InstancesOf("$class")
    or die "Failed getobject\n";
my $hash;
foreach $v (in $list) {        
    $hash->{$v->{$key}}->{$_}  = $v->{$_} for @properties;
}

print Dumper $hash;

#-------------------
# Using Otehr class    
$class = 'Win32_PerfRawData_PerfOS_Processor';

$wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2")
    or die "Failed getobject\n";

$list = $wmi->InstancesOf("$class")
    or die "Failed getobject\n";
foreach $v (in $list) {        
    $hash->{$v->{$key}}->{$_}  = $v->{$_} for @properties;
}

print Dumper $hash;

输出:

$VAR1 = {
          '0' => {
                   'PercentPrivilegedTime' => '0',
                   'PercentIdleTime' => '0',
                   'PercentInterruptTime' => '0',
                   'PercentUserTime' => '0',
                   'PercentProcessorTime' => '100'
                 },
          '_Total' => {
                        'PercentPrivilegedTime' => '0',
                        'PercentIdleTime' => '0',
                        'PercentInterruptTime' => '0',
                        'PercentUserTime' => '0',
                        'PercentProcessorTime' => '100'
                      }
        };
$VAR1 = {
          '0' => {
                   'PercentPrivilegedTime' => '15442905808',
                   'PercentIdleTime' => '2505024948976',
                   'PercentInterruptTime' => '1866684160',
                   'PercentUserTime' => '682681648',
                   'PercentProcessorTime' => '2505024948976'
                 },
          '_Total' => {
                        'PercentPrivilegedTime' => '15442905808',
                        'PercentIdleTime' => '2505024948976',
                        'PercentInterruptTime' => '1866684160',
                        'PercentUserTime' => '682681648',
                        'PercentProcessorTime' => '2505024948976'
                      }
        };

I'm Trying go GET CPU values using WMI classes but with no lucky.

  1. With the class Win32_PerfFormattedData_PerfOS_Processor, I'm always getting the same values they don't change...
  2. With the WMI class Win32_PerfRawData_PerfOS_Processor, the value of PercentProcessorTime is equal to PercentProcessorTime, something is wrong.

#!/usr/bin/perl -w
use strict;
use warnings;

use Win32::OLE;
use Data::Dumper;

my $class = "Win32_PerfFormattedData_PerfOS_Processor";
my $key = 'Name';

my @properties = qw(PercentIdleTime PercentProcessorTime PercentPrivilegedTimePercentUserTime PercentInterruptTime);
my $wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2")
    or die "Failed getobject\n";

my $list, my $v;

$list = $wmi->InstancesOf("$class")
    or die "Failed getobject\n";
my $hash;
foreach $v (in $list) {        
    $hash->{$v->{$key}}->{$_}  = $v->{$_} for @properties;
}

print Dumper $hash;

#-------------------
# Using Otehr class    
$class = 'Win32_PerfRawData_PerfOS_Processor';

$wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2")
    or die "Failed getobject\n";

$list = $wmi->InstancesOf("$class")
    or die "Failed getobject\n";
foreach $v (in $list) {        
    $hash->{$v->{$key}}->{$_}  = $v->{$_} for @properties;
}

print Dumper $hash;

OUTPUT:

$VAR1 = {
          '0' => {
                   'PercentPrivilegedTime' => '0',
                   'PercentIdleTime' => '0',
                   'PercentInterruptTime' => '0',
                   'PercentUserTime' => '0',
                   'PercentProcessorTime' => '100'
                 },
          '_Total' => {
                        'PercentPrivilegedTime' => '0',
                        'PercentIdleTime' => '0',
                        'PercentInterruptTime' => '0',
                        'PercentUserTime' => '0',
                        'PercentProcessorTime' => '100'
                      }
        };
$VAR1 = {
          '0' => {
                   'PercentPrivilegedTime' => '15442905808',
                   'PercentIdleTime' => '2505024948976',
                   'PercentInterruptTime' => '1866684160',
                   'PercentUserTime' => '682681648',
                   'PercentProcessorTime' => '2505024948976'
                 },
          '_Total' => {
                        'PercentPrivilegedTime' => '15442905808',
                        'PercentIdleTime' => '2505024948976',
                        'PercentInterruptTime' => '1866684160',
                        'PercentUserTime' => '682681648',
                        'PercentProcessorTime' => '2505024948976'
                      }
        };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

腻橙味 2024-10-20 05:12:23

这是我用来收集 CPU 信息的脚本:

use strict;
use warnings;

use Win32::OLE;


my $interval = 1;
my $key = 'Name';
my @properties = qw(PercentIdleTime PercentProcessorTime PercentPrivilegedTime 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 1){

    $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_Sys100NS'}-$h1->{'TimeStamp_Sys100NS'}));
}

输出示例:

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 %

This is the script that i've done to collect CPU info:

use strict;
use warnings;

use Win32::OLE;


my $interval = 1;
my $key = 'Name';
my @properties = qw(PercentIdleTime PercentProcessorTime PercentPrivilegedTime 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 1){

    $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_Sys100NS'}-$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 %
无人接听 2024-10-20 05:12:23

编辑以使答案更精确:

检索这些类的实例 Win32::OLE->GetObject(...) 为您提供处理器当前状态的快照。要查看处理器状态如何随时间变化,您需要在不同时间获取单独的实例分别调用Win32::OLE->GetObject

Edit to make the answer more precise:

Retrieving instances of these classes Win32::OLE->GetObject(...) gets you a snapshot of the current state of the processors. To see how processor states change over time, you will need to get separate instances at make separate calls to Win32::OLE->GetObject at separate times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文