#!/usr/bin/perl # # getwmi.pl - query WMI for a performance counter value # # The provenance of this script is unknown to me. # # Syntax: # getwmi.pl _ [,object] [_Total] # # Example execution and output: # c:\temp>getwmi.pl localhost query Win32_PerfRawData_TCPv4 SegmentsSentPerSec # 588211634 # c:\temp> # # c:\temp>getwmi.pl localhost query Win32_PerfRawData_PerfOS_Memory PagesInputPerSec,PagesOutputPerSec # 18840121899 # 26421020711 # # C:\Temp>getwmi.pl localhost query Win32_PerfRawData_PerfDisk_LogicalDisk DiskReadBytesPerSec _Total # 160718450688 # 160718450688 # C:\Temp> # # C:\Temp>getwmi.pl localhost get Win32_PerfRawData_PerfDisk_LogicalDisk DiskReadBytesPerSec,DiskWriteBytesPerSec _Total # 160718483456 # 109654942720 # C:\Temp> # # C:\Temp>getwmi.pl localhost query Win32_PerfFormattedData_PerfOS_PagingFile PercentUsage # 19 # C:\Temp> # # C:\Temp>getwmi.pl localhost query Win32_PerfRawData_PerfOS_Memory PageReadsPersec,PageWritesPersec # 1916108 # 3992645 # C:\Temp> # # # Also see this SO post for other examples for how to query WMI from perl: # https://stackoverflow.com/questions/4726968/get-wmi-memory-values-using-perl # # use Win32::OLE; my $output_delimeter = "\n"; my $argCount = scalar(@ARGV); my $debug = 0; #Parse through the command-line arguments and display the WMI information. WMIMain(@ARGV); sub WMIMain(\@) { my @compdata = split /\./,$_[0]; my $computer = $compdata[0]; my $action = $_[1]; my $Win32_Class = $_[2]; my $rvalue = $_[3]; my $kvalue = $_[4]; my $WMI_Key = "Name"; #my $class = "WinMgmts://$computer"; my $class = "WinMgmts:\\\\$computer\\root\\CIMV2"; my $wmi = Win32::OLE->GetObject($class); if ($debug) { print "computer = $computer\n"; print "action = $action\n"; print "win32_calls = $Win32_Class\n"; print "rvalue = $rvalue\n"; print "kvalue = $kvalue\n"; print "class = $class\n"; } my $i = 0; if ($wmi) { if ($action eq "index") { my $properties = "$WMI_Key"; my $computers = $wmi->ExecQuery("SELECT $properties FROM $Win32_Class"); if (scalar(Win32::OLE::in($computers)) lt "1") { print "\n Check the computer and class name.\n"; print " No information was found on the specified class!\n"; return; } foreach my $pc (Win32::OLE::in($computers)) { $i++; properties($pc,$properties); if ($i < scalar(Win32::OLE::in($computers))) { print "\n"; } } } # if action = index if ($action eq "query") { my $properties = "$WMI_Key,$rvalue"; my $computers = $wmi->ExecQuery("SELECT $properties FROM $Win32_Class"); if (scalar(Win32::OLE::in($computers)) lt "1") { print "\n Check the computer and class name.\n"; print " No information was found on the specified class!\n"; return; } foreach my $pc (Win32::OLE::in($computers)) { $i++; properties($pc,$properties); if ($i < scalar(Win32::OLE::in($computers))) { } } } # if action = query if ($action eq "get") { my $properties = $rvalue; my $computers = $wmi->ExecQuery("SELECT $properties FROM $Win32_Class Where $WMI_Key='$kvalue'"); if (scalar(Win32::OLE::in($computers)) lt "1") { print "\n Check the computer and class name.\n"; print " No information was found on the specified class!\n"; return; } foreach my $pc (Win32::OLE::in($computers)) { properties($pc,$properties); } } # if action = get if ($action eq "browse") { my $properties = "*"; my $computers = $wmi->ExecQuery("SELECT $properties FROM $Win32_Class"); if (scalar(Win32::OLE::in($computers)) lt "1") { print "\n Check the computer and class name.\n"; print " No information was found on the specified class!\n"; return; } foreach my $pc (Win32::OLE::in($computers)) { $i++; properties($pc,$properties); if ($i < scalar(Win32::OLE::in($computers))) { } } } # if action = browse } # if wmi else { print "Unable to talk to WMI for $computer.\n"; } } #Loop through an object's properties. #Parameters: # 0 - a reference to the object # 1 - a single property to lookup sub properties($$) { my $node = $_[0]; my $properties = $_[1]; my $i = 0; if ($properties eq '*') { foreach ( Win32::OLE::in($node->{Properties_}) ) { viewPropertyBrowse($_); } } else { my @properties = split(',', $properties); foreach (@properties) { $i++; if (scalar(@properties) eq "1") { viewProperty($node->{Properties_}->{$_}); } elsif (scalar(@properties) gt "1") { viewPropertyMulti($node->{Properties_}->{$_}); if ((scalar(@properties) gt "1") & $i lt (scalar(@properties))) { #print "$output_delimeter"; } } } } } #Display an object's property. #Parameters: # 0 - a reference to the property object sub viewProperty($$) { my $object = $_[0]; my $name = $object->{Name}; my $cimtype = $object->{CIMType}; my $isarray = $object->{IsArray}; my $islocal = $object->{IsLocal}; my $origin = $object->{Origin}; my $qualifiers = $object->{Qualifiers_}; my $value = $object->{Value}; print "$ARGV[2].$name $value"; } sub viewPropertyMulti($$) { my $object = $_[0]; next unless ($object->{Value}); #Don't print a stupid blank line next if ($object->{Value} =~ /[A-Za-z]/); #Skip stuff with alpha chars chomp ($object->{Value}); print "$object->{Value}"; } sub viewPropertyBrowse($$) { my $object = $_[0]; chomp ($object->{Value}); print "$object->{Name}:$object->{Value}"; }