harry’s memorandum

おれおれメモ

WMI client (WMIC) for Linux は超便利

WMICってのは、WindowsXP以降に標準でついてくるWMIのCLIです。便利ですよね。
これをLinuxから使えたら便利なのに...って思ってたのですが、あ、、あった!。

インストール

このWMICの大元のソースはどこかわからない。とりあえず http://www.orvant.com/packages からダウンロード。ソースを眺めていると「Zenoss Inc」って書いてあるのでZenossで作っているかな?。
まずはソースをコンパイル。

$ wget http://www.orvant.com/files/packages/source/wmi_1.3.14.orig.tar.bz2
$ bzip2 -cd wmi_1.3.14.orig.tar.bz2 | tar xvf -
$ cd wmi-1.3.14
$ make

ソースディレクトリをみるとsambaのソースを使って RPC/DCOM を解釈しているようですね。

$ ls
GNUmakefile  Samba  pyinclude.py  pysamba

コマンドを実行してみる。

$ ./Samba/source/bin/wmic
Usage: [-?] [-?] [-?] [-?NP] [-?NPV] [-?|--help] [--usage] [-d|--debuglevel DEBUGLEVEL]
        [--debug-stderr] [-s|--configfile CONFIGFILE] [--option=name=value]
        [-l|--log-basename LOGFILEBASE] [--leak-report] [--leak-report-full]
        [-R|--name-resolve NAME-RESOLVE-ORDER]
        [-O|--socket-options SOCKETOPTIONS] [-n|--netbiosname NETBIOSNAME]
        [-W|--workgroup WORKGROUP] [--realm=REALM] [-i|--scope SCOPE]
        [-m|--maxprotocol MAXPROTOCOL] [-U|--user [DOMAIN\]USERNAME[%PASSWORD]]
        [-N|--no-pass] [--password=STRING] [-A|--authentication-file FILE]
        [-S|--signing on|off|required] [-P|--machine-pass]
        [--simple-bind-dn=STRING] [-k|--kerberos STRING]
        [--use-security-mechanisms=STRING] [-V|--version] [--namespace=STRING]
        [--delimiter=STRING]
        //host query

Example: wmic -U [domain/]adminuser%password //host "select * from Win32_ComputerSystem"

例を実行。パイプ"|"区切りで表示してくれるようです。

$ ./Samba/source/bin/wmic -U dharry%password //crimson "Select * From Win32_ComputerSystem"
CLASS: Win32_ComputerSystem
AdminPasswordStatus|AutomaticManagedPagefile|AutomaticResetBootOption|AutomaticResetCapability|BootOptionOnLimit|BootOptionOnWatchDog|BootROMSupported|BootupState|Caption|ChassisBootupState|CreationClassName|CurrentTimeZone|DaylightInEffect|Description|DNSHostName|Domain|DomainRole|EnableDaylightSavingsTime|FrontPanelResetStatus|InfraredSupported|InitialLoadInfo|InstallDate|KeyboardPasswordStatus|LastLoadInfo|Manufacturer|Model|Name|NameFormat|NetworkServerModeEnabled|NumberOfLogicalProcessors|NumberOfProcessors|OEMLogoBitmap|OEMStringArray|PartOfDomain|PauseAfterReset|PCSystemType|PowerManagementCapabilities|PowerManagementSupported|PowerOnPasswordStatus|PowerState|PowerSupplyState|PrimaryOwnerContact|PrimaryOwnerName|ResetCapability|ResetCount|ResetLimit|Roles|Status|SupportContactDescription|SystemStartupDelay|SystemStartupOptions|SystemStartupSetting|SystemType|ThermalState|TotalPhysicalMemory|UserName|WakeUpType|Workgroup
3|True|True|True|0|0|True|Normal boot|CRIMSON|3|Win32_ComputerSystem|540|False|AT/AT COMPATIBLE|crimson|WORKGROUP|0|True|3|False|NULL|(null)|3|(null)|MICRO-STAR INTERNATIONAL CO.,LTD|MS-7549|CRIMSON|(null)|True|4|1|NULL|NULL|False|-1|1|NULL|False|3|0|3|(null)|kisi|1|-1|-1|(LM_Workstation,LM_Server,NT,Potential_Browser,Backup_Browser,Master_Browser)|OK|NULL|0|NULL|0|X86-based PC|3|3488800768|crimson\kishi|6|WORKGROUP

firefox.exeプロセスの情報取得。WQLを色々投げれて楽しい。

$ ./Samba/source/bin/wmic -U dharry%password //crimson "Select * From Win32_Process where Caption='firefox.exe'"
CLASS: Win32_Process
Caption|CommandLine|CreationClassName|CreationDate|CSCreationClassName|CSName|Description|ExecutablePath|ExecutionState|Handle|HandleCount|InstallDate|KernelModeTime|MaximumWorkingSetSize|MinimumWorkingSetSize|Name|OSCreationClassName|OSName|OtherOperationCount|OtherTransferCount|PageFaults|PageFileUsage|ParentProcessId|PeakPageFileUsage|PeakVirtualSize|PeakWorkingSetSize|Priority|PrivatePageCount|ProcessId|QuotaNonPagedPoolUsage|QuotaPagedPoolUsage|QuotaPeakNonPagedPoolUsage|QuotaPeakPagedPoolUsage|ReadOperationCount|ReadTransferCount|SessionId|Status|TerminationDate|ThreadCount|UserModeTime|VirtualSize|WindowsVersion|WorkingSetSize|WriteOperationCount|WriteTransferCount
firefox.exe|"C:\Program Files\Mozilla Firefox\firefox.exe" |Win32_Process|20120529024909.460137+540|Win32_ComputerSystem|CRIMSON|firefox.exe|C:\Program Files\Mozilla Firefox\firefox.exe|0|7900|409|(null)|68172437|1380|200|firefox.exe|Win32_OperatingSystem|Microsoft Windows 7 Professional |C:\Windows|\Device\Harddisk0\Partition2|25084|309249|106350|92288|2100|124888|327544832|143844|8|94502912|7900|35|326|40|334|2390|70528310|1|(null)|(null)|31|92976596|317468672|6.1.7601|115810304|4152|68639352

Nagiosプラグインとか。

NagiosPluginの Check-WMI-Plus では このWMICを使ってWindowsマシンのリソースを取得しているようです。

f:id:dharry:20120529032347p:image

これで、LinuxもWindowsも統合的に監視ができるようになりました。