我在嘗試去使用WMI類獲取CPU值,但沒有幸運。使用Perl獲取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'
}
};
`-w`和`使用warnings`在這種情況下是多餘的。 `-w`是全局的,而'use warnings`編譯指示可以在塊內啓用和禁用,或者``無警告`。 – 2011-01-20 17:48:18