使用Perl获取WMI内存值

前端之家收集整理的这篇文章主要介绍了使用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我已经更新了这个脚本,因为当时与发布的评论没有冲突.

解决方法

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

Check WMI Plus

它的客户端较少,可以在Linux下运行,也适用于远程Windows机器.我最近一直在使用它来监控内存使用情况.

猜你在找的Perl相关文章