使用 Perl 获取 WMI 内存值
我需要使用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我更新了这个脚本,因为当时没有冲突并发表评论。
I need to collect memory data of Windows operating systems using WMI. In that sense, I developed a Perl script to generate such data. However, I wonder if my method is correct and what are the alternatives. It is intended that the method of collecting data is to be as widely as possible in terms of Windows OS's.
If you are not a perlish, this is what I what to check:
MEM_USED = Win32_OperatingSystem->TotalVisibleMemorySize - Win32_OperatingSystem->FreePhysicalMemory
SWAP_USED = (Win32_OperatingSystem->SizeStoredInPagingFiles - Win32_OperatingSystem->FreeSpaceInPagingFiles) / Win32_OperatingSystem->SizeStoredInPagingFiles
This is my script:
#!/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;
}
Note: At 2011-01-19 I've updated this script, since at that time there were no conflicts with posted comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议你看一下这个 Perl 小项目:
Check WMI Plus
它没有客户端,可以在 Linux 下运行,也可以在远程 Windows 机器上运行。我最近一直在使用它来监控内存使用情况。
I suggest you to take a look at this little Perl project:
Check WMI Plus
It's client less, works under Linux and works also with remote Windows machines. I've been using it recently in order to monitor memory usage.