简体   繁体   English

在Linux上从WMI客户端获取CPU利用率

[英]Getting CPU utilisation from WMI client on Linux

I am using the wmi client for linux. 我正在为Linux使用wmi客户端。 Through it you can execute WQL to query windows servers remotely. 通过它,您可以执行WQL来远程查询Windows服务器。

For Example; 例如;

 Select * from Win32_ComputerSystem

What I am trying to do is calculate the CPU percentage used. 我想做的是计算所用的CPU百分比。

I've been getting the value of 'PercentProcessorTime' from 'Win32_PerfFormattedData_Counters_ProcessorInformation'. 我一直在从'Win32_PerfFormattedData_Counters_ProcessorInformation'获取'PercentProcessorTime'的值。 I think this is working fine for computers with a single CPU, but I'm lost as to how to calculate the CPU usage for computers with multiple CPU's. 我认为这对于具有单个CPU的计算机可以很好地工作,但是我对于如何计算具有多个CPU的计算机的CPU使用率一无所知。

any help is very much appreciated. 很感谢任何形式的帮助。

cheers. 干杯。

If you SELECT * FROM Win32_PerfFormattedData_Counters_ProcessorInformation then you will get a list of result sets. 如果SELECT * FROM Win32_PerfFormattedData_Counters_ProcessorInformation ,则将获得结果集列表。 The first set is the overall values for all CPUs, and the rest are values for the individual CPUs. 第一组是所有CPU的总体值,其余是各个CPU的值。

This code shows the idea. 这段代码说明了这个想法。 Note that you should replace SYSTEM with the name of the computer you want to query. 请注意,您应该用要查询的计算机的名称替换SYSTEM

use strict;
use warnings;

use Win32::OLE;
use Win32::OLE::Variant;

STDOUT->autoflush;

my $wmi = Win32::OLE->GetObject('winmgmts:\\\\SYSTEM\root\cimv2') or die Win32::OLE->LastError;
my $list = $wmi->ExecQuery('SELECT * FROM Win32_PerfFormattedData_Counters_ProcessorInformation');
my $n = 0;
for my $cpu (in $list) {
  printf "%s: %d%%\n", $n ? "CPU$n" : ' ALL', $cpu->PercentProcessorTime;
  $n++;
}

output 输出

 ALL: 8%
CPU1: 8%
CPU2: 12%
CPU3: 6%
CPU4: 12%
CPU5: 6%
CPU6: 6%
CPU7: 6%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM