我试图使用WMI类去拿CPU值,但不幸运的。
- 与类
Win32_PerfFormattedData_PerfOS_Processor
,我一直都想与他们不改变的相同值... - 随着WMI类
Win32_PerfRawData_PerfOS_Processor
,价值PercentProcessorTime
等于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;
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'
}
};