You are currently browsing the tag archive for the ‘WMI’ tag.

If you followed my wmic post a while back you’ll have seen that wmic outputs a LARGE amount of data seperated by the | character, sort of like a CSV. This quickly becomes unusable as the first line is the column titles of the data and each following line is a result. So I wrote a small, very much ugly and not exactly rock solid, bit of python to dump wmic output into a Dictionary of Lists. What I mean is every column will be a key in the Dictionary and each value is a list of 1 or more values. So for instance a WMI query of HDDs will likely return 2+ (Your C Drive and a CD Drive), but say one for memory will only return one. Anyway this is pretty basic and doesn’t do much more than print the actual dictionary but it might help someone write a Cacti plugin or a Nagios plugin that can pull free disk space for example.

Here it is: pythonwmic (Yes Sorry it’s called .doc but wordpress doesn’t seem to like .py)

To use it just call the script (first modifying the win32User/Password variables) with one argument which is the IP address of the server you want to query. Bear in mind it’s fairly rough but hopefully enough to get you going. It doesn’t work for all WML queries but the Win32_PerfFormattedData_ ones seem to work nicely (Win32_OperatingSystem I’m looking at you inparticular with your extra | characters in the Name column !).

As an example of output I ran against one of my machines querying the Logical Disks:

{‘DiskBytesPersec’: [‘0’, ‘0’, ‘0’, ‘0’], ‘Frequency_Sys100NS’: [‘0’, ‘0’, ‘0’, ‘0’], ‘PercentDiskReadTime’: [‘0’, ‘0’, ‘0’, ‘0’], ‘Timestamp_Object’: [‘0’, ‘0’, ‘0’, ‘0’], ‘AvgDiskBytesPerTransfer’: [‘0’, ‘0’, ‘0’, ‘0’], ‘PercentIdleTime’: [‘100’, ‘100’, ‘100’, ‘100’], ‘AvgDiskBytesPerWrite’: [‘0’, ‘0’, ‘0’, ‘0’], ‘Description’: [‘(null)’, ‘(null)’, ‘(null)’, ‘(null)’], ‘DiskWriteBytesPersec’: [‘0’, ‘0’, ‘0’, ‘0’], ‘Timestamp_Sys100NS’: [‘0’, ‘0’, ‘0’, ‘0’], ‘DiskWritesPersec’: [‘0’, ‘0’, ‘0’, ‘0’], ‘PercentDiskTime’: [‘0’, ‘0’, ‘0’, ‘0’], ‘AvgDiskQueueLength’: [‘0’, ‘0’, ‘0’, ‘0’], ‘AvgDiskBytesPerRead’: [‘0’, ‘0’, ‘0’, ‘0’], ‘AvgDisksecPerRead’: [‘0’, ‘0’, ‘0’, ‘0’], ‘Timestamp_PerfTime’: [‘0’, ‘0’, ‘0’, ‘0’], ‘Name’: [‘C:’, ‘E:’, ‘F:’, ‘_Total’], ‘CurrentDiskQueueLength’: [‘0’, ‘0’, ‘0’, ‘0’], ‘AvgDisksecPerWrite’: [‘0’, ‘0’, ‘0’, ‘0’], ‘DiskReadBytesPersec’: [‘0’, ‘0’, ‘0’, ‘0’], ‘Caption’: [‘(null)’, ‘(null)’, ‘(null)’, ‘(null)’], ‘PercentDiskWriteTime’: [‘0’, ‘0’, ‘0’, ‘0’], ‘Frequency_Object’: [‘0’, ‘0’, ‘0’, ‘0’], ‘Frequency_PerfTime’: [‘0’, ‘0’, ‘0’, ‘0’], ‘SplitIOPerSec’: [‘0’, ‘0’, ‘0’, ‘0’], ‘AvgDiskReadQueueLength’: [‘0’, ‘0’, ‘0’, ‘0’], ‘FreeMegabytes’: [‘54155’, ‘127668’, ‘69809’, ‘251632’], ‘DiskReadsPersec’: [‘0’, ‘0’, ‘0’, ‘0’], ‘AvgDiskWriteQueueLength’: [‘0’, ‘0’, ‘0’, ‘0’], ‘AvgDisksecPerTransfer’: [‘0’, ‘0’, ‘0’, ‘0’], ‘PercentFreeSpace’: [’77’, ’91’, ’99’, ’90’], ‘DiskTransfersPersec’: [‘0’, ‘0’, ‘0’, ‘0’]}

Have fun, and please post comments, corrections or improvements ! I’m sure there’s better/more_pythonistic ways to build the Dictionary and I will at some stage build a better way to output key bits of data rather than just dump the dictionary as is possibly with a Cacti/Nagios plugin to utilise it.

Advertisements

sudo aptitude install wmi-client

Example of usage is;

wmic -U DOMAIN/administrator%password //10.99.92.9 “Select * from Win32_Service”

Lists all services, the first line it spits back is the fields which you can use this SQL like language  to filter, so to see only the names of the services installed we’d do:

wmic -U DOMAIN/administrator%password //10.99.92.9 “Select Name from Win32_Service”

Or Name and State:

wmic -U DOMAIN/administrator%password //10.99.92.9 “Select Name,State from Win32_Service”

Or for just one service in this case the UPS service:

wmic -U DOMAIN/administrator%password //10.99.92.9 “Select Name,State from Win32_Service where name=’UPS'”

The language for the queries is called WQL and a reference is available here over at MSDN.

Hope this helps, this is only in Hardy as far as I’m aware.

Advertisements