Cacti WMI A – Example Perl WMI Memory Script

Cacti WMI A – Example Perl WMI Memory Script

Appendix A – Example Perl WMI Memory Script

This article provides a basic perl script which interrogates a remote Windows Server through WMI and retrieves the following statistics :-

  1. Total Physical Memory (Win32_PhysicalMemory Class)
  2. Available Physical Memory (Win32_PerfFormattedData_PerfOS_Memory Class)
  3. Used Physical Memory (Total minus Available Memory)
  4. Total PageFile (Win32_PageFileUsage Class)
  5. Current PageFile Usage  (Win32_PageFileUsage Class)
  6. Free PageFile (Total minus Current Usage)

The script is shown in the following section and also available as an attached text file to this article.

Wmi-Mem-Usage.Pl Script

#!/usr/bin/perl

$domain = $ARGV[0];

$user = $ARGV[1];

$password = $ARGV[2];

$server = $ARGV[3];

$perfresult = `wmic -U $domain/$user%”$password” //$server “select AvailableBytes from Win32_PerfFormattedData_PerfOS_Memory”`;

@perfresults=split(/\n/,$perfresult);

my @DiskPerfArray=split(/\|/, @perfresults[2]);

$sizeresult = `wmic -U $domain/$user%”$password” //$server “select Capacity from Win32_PhysicalMemory”`;

@sizeresults=split(/\n/,$sizeresult);

my @DiskSizeArray=split(/\|/, @sizeresults[2]);

@DiskSizeArray[0] = @DiskSizeArray[0] / 1024 ;

@DiskPerfArray[0] = @DiskPerfArray[0] / 1024;

my $UsedMem = @DiskSizeArray[0] – @DiskPerfArray[0];

$results = `wmic -U $domain/$user%”$password” //$server “select AllocatedBaseSize, CurrentUsage from Win32_PageFileUsage”`;

@results=split(/\n/,$results);

my @PFResults=split(/\|/, @results[2]);

my $PFFree = @PFResults[0] – @PFResults[1];

print “TotalMem:@DiskSizeArray[0] FreeMem:@DiskPerfArray[0] UsedMem:$UsedMem PFTotal:@PFResults[0] PFUsed:@PFResults[1] PFFree:$PFFree\n”;