使用 Perl 获取 WMI CPU 值
我正在尝试使用 WMI 类获取 CPU 值,但运气不佳。
- 对于
Win32_PerfFormattedData_PerfOS_Processor
类,我总是得到相同的值,它们不会改变... - 对于 WMI 类
Win32_PerfRawData_PerfOS_Processor
,PercentProcessorTime
的值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.
- With the class
Win32_PerfFormattedData_PerfOS_Processor
, I'm always getting the same values they don't change... - With the WMI class
Win32_PerfRawData_PerfOS_Processor
, the value ofPercentProcessorTime
is equal toPercentProcessorTime
, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我用来收集 CPU 信息的脚本:
输出示例:
This is the script that i've done to collect CPU info:
Output sample:
编辑以使答案更精确:
检索这些类的实例Win32::OLE->GetObject(...)
为您提供处理器当前状态的快照。要查看处理器状态如何随时间变化,您需要在不同时间获取单独的实例分别调用Win32::OLE->GetObject
。Edit to make the answer more precise:
Retrieving instances of these classesWin32::OLE->GetObject(...)
gets you a snapshot of the current state of the processors. To see how processor states change over time, you will need toget separate instances atmake separate calls toWin32::OLE->GetObject
at separate times.