gpt4 book ai didi

perl - 使用 Perl 获取 WMI 内存值

转载 作者:行者123 更新时间:2023-12-04 20:56:27 26 4
gpt4 key购买 nike

我需要使用 WMI 收集 Windows 操作系统的内存数据。从这个意义上说,我开发了一个 Perl 脚本来生成这样的数据。但是,我想知道我的方法是否正确以及有哪些替代方法。收集数据的方法旨在尽可能广泛地使用 Windows 操作系统。

如果您不是 Perlish,这就是我要检查的内容:

MEM_USED = Win32_OperatingSystem->TotalVisibleMemorySize - Win32_OperatingSystem->FreePhysicalMemory
SWAP_USED = (Win32_OperatingSystem->SizeStoredInPagingFiles - Win32_OperatingSystem->FreeSpaceInPagingFiles) / Win32_OperatingSystem->SizeStoredInPagingFiles

这是我的脚本:
#!/bin/env perl

use Win32::OLE;

use strict;
use warnings;

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

my $list, my $v;

$list = $wmi->InstancesOf("Win32_OperatingSystem")
or die "Failed getobject\n";

my $end_time = time;


my ($total_mem, $free_mem, $used_mem, $mem_percent, $free_percent);

foreach $v (in $list) {

$total_mem = $v->{TotalVisibleMemorySize};
$free_mem = $v->{FreePhysicalMemory};
$used_mem = $total_mem - $free_mem;
$mem_percent = sprintf("%.2f", $used_mem / $total_mem * 100);
$free_percent = sprintf("%.2f", $free_mem / $total_mem * 100);

print "Memory used: $mem_percent\%\n";
print "Memory free: $free_percent\%\n";
print "Memory total: $total_mem kb\n";
print "Memory used: $used_mem kb\n";
print "Memory free: $free_mem kb\n";

my $total_swap_mem = $v->{SizeStoredInPagingFiles};
my $free_swap_mem = $v->{FreeSpaceInPagingFiles};
my $used_swap_mem = $total_swap_mem - $free_swap_mem;
my $used_swap_mem_perc = ($total_swap_mem - $free_swap_mem) / $total_swap_mem * 100;

printf "Swap total:%d kb\n", $total_swap_mem;
printf "Swap free:%d kb\n", $free_swap_mem;
printf "Swap used:%d kb\n", $used_swap_mem;
printf "Swap used:%.2f %%\n", $used_swap_mem_perc;
}

注:在 2011-01-19 我更新了这个脚本,因为当时没有与发布的评论发生冲突。

最佳答案

我建议你看看这个小小的 Perl 项目:

Check WMI Plus

它的客户端较少,可在 Linux 下工作,也可与远程 Windows 机器一起使用。我最近一直在使用它来监控内存使用情况。

关于perl - 使用 Perl 获取 WMI 内存值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4726968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com