使用 Perl 获取 WMI 内存值 [英] Get WMI Memory Values using Perl

查看:30
本文介绍了使用 Perl 获取 WMI 内存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如果你不是一个perlish,这就是我要检查的:

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

这是我的脚本:

#!/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 更新了这个脚本,因为当时没有与发布的评论发生冲突.

Note: At 2011-01-19 I've updated this script, since at that time there were no conflicts with posted comments.

推荐答案

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

I suggest you to take a look at this little Perl project:

检查 WMI Plus

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

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.

这篇关于使用 Perl 获取 WMI 内存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆