Windows Management Instrumentation (WMI)

3.1 Perl Example 2
#!perl

use Win32::OLE qw(in with);

$true  = 1;
$false = 0;

$jid = $ARGV[0];
if (!$ARGV[0]) 
 {
  print "Job ID>";
  chomp($jid = ());
 }

# Search Resources for Nodes Associated with this Job ID
$found = $false;
open(NODES, "ccusage |");
while()
 {
  chop;
  ($host, $jobid) = (split())[0,3];
  if ($jobid == $jid) { push(@Nodes,$host); $found = $true; }
 }
close(NODES);

if (!$found) { print "ERROR: $jid is not currently running\n"; exit; }
 
$WMI = Win32::OLE->new('WbemScripting.SWbemLocator') ||
 die "Cannot access WMI on local machine: ", Win32::OLE->LastError; 

for(;;)
 { 
  foreach $host (@Nodes)
   {
    print "\n$host\n";
    $Services = $WMI->ConnectServer($host) ||
     die "Cannot access WMI on remote machine: ", Win32::OLE->LastError; 

    # Gather Processor Information
    $processors = $Services->InstancesOf("Win32_Processor") ||
     die "Cannot access WMI Processor Information:", Win32::OLE->LastError;
    $total = 0;
    $count = 0;
    foreach $proc (in($processors))
     {
      print "$proc->{'DeviceID'}>>$proc->{'LoadPercentage'}\n";
      $total += $proc->{'LoadPercentage'};
      $count++;
     }
    print "Utlization: ", int($total/$count);
    sleep 1;
   } # for each node to monitor
 } # for ever
The Output looks like the following...
C:\WIP\WMIExamples\Example 2 - Text CPU Monitor>cpu_watch.pl
Job ID>77643

ctc003.tc.cornell.edu
CPU0>>93
CPU1>>79
CPU2>>82
CPU3>>0
Utlization: 63
ctc004.tc.cornell.edu
CPU0>>71
CPU1>>71
CPU2>>2
CPU3>>2
Utlization: 36
ctc005.tc.cornell.edu
CPU0>>79
CPU1>>68
CPU2>>5
CPU3>>2
Utlization: 38
ctc006.tc.cornell.edu
CPU0>>71
CPU1>>71
CPU2>>5
CPU3>>2
Utlization: 37
ctc007.tc.cornell.edu
CPU0>>76
CPU1>>76
CPU2>>4
CPU3>>2
Utlization: 39
ctc003.tc.cornell.edu
CPU0>>97
CPU1>>71
CPU2>>80
CPU3>>5
Utlization: 63
ctc004.tc.cornell.edu
CPU0>>64
CPU1>>77
CPU2>>4
CPU3>>4
Utlization: 37
ctc005.tc.cornell.edu
CPU0>>74
CPU1>>67
CPU2>>5
CPU3>>4
Utlization: 37
ctc006.tc.cornell.edu
CPU0>>67
CPU1>>74
CPU2>>2
CPU3>>2
Utlization: 36
ctc007.tc.cornell.edu
CPU0>>61
CPU1>>68
CPU2>>4
CPU3>>2
Utlization: 33