gulden has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

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.

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_virtual_mem = $v->{TotalVirtualMemorySize}; my $free_virtual_mem = $v->{FreeVirtualMemory}; my $used_virtual_mem = $total_virtual_mem - $free_virtual_mem; my $used_virtual_mem_perc = ($total_virtual_mem - $free_virtual_ +mem) / $total_virtual_mem * 100; printf "Swap total:%d kb\n", $total_virtual_mem; printf "Swap free:%d kb\n", $free_virtual_mem; printf "Swap used:%d kb\n", $used_virtual_mem; printf "Swap used:%.2f %%\n", $used_virtual_mem_perc; }

Regards,

Gulden PT

«A contentious debate is always associated with a lack of valid arguments.»

Replies are listed 'Best First'.
Re: Collect Memory Data from WMI (Win32)
by gulden (Monk) on Jan 19, 2011 at 16:02 UTC
    I've detected that my swap calculation method is wrong, since:
    Virtual Memory = PhysicalMemory(RAM) + Extended Memory (Disk)
    Therefore, i've changed the SWAP calculation method to:
    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;
    in short, i need to validade this:
    MEM_USED = Win32_OperatingSystem->TotalVisibleMemorySize - Win32_Opera +tingSystem->FreePhysicalMemory SWAP_USED = (Win32_OperatingSystem->SizeStoredInPagingFiles - Win32_Op +eratingSystem->FreeSpaceInPagingFiles) / Win32_OperatingSystem->Size +StoredInPagingFiles

    tks,

    gulden
    «A contentious debate is always associated with a lack of valid arguments.»