ESXi上の全VMの情報を見る(手抜き)

サンプルが全てグローバル変数使いまくりで気持ち悪かったので、どうにかできないかな、とコード見ながら書いた。
無駄にセッション管理ができるようなので、それも盛り込んでみた。
ただし、クッソ手抜き。例によりPerl

use strict;
use warnings;
use VMware::VILib;
use VMware::VICommon;

my $SERVICE_URL = "https://192.168.11.128/sdk/webService";
my $USER_NAME = "ryozi";
my $PASSWORD = "XXXXXX";

my $SESSION_FILE = 'session';

my $vim = new Vim(service_url =>$SERVICE_URL);

if(-f $SESSION_FILE){
	eval{
		$vim->load_session(session_file => $SESSION_FILE);
	};
	print STDERR "$@\n" if $@;
}

if($vim->is_logout_on_disconnect){
	$vim->login( user_name => $USER_NAME, password => $PASSWORD);
}

my $vms = $vim->find_entity_views(view_type => 'VirtualMachine');

use Data::Dumper;
print Dumper($vms);

$vim->save_session(session_file => $SESSION_FILE);


単純に全部の情報を取ってきてDumpすると、
何かインストールしたVMなら、1VMあたり100KBぐらいの情報量に。
find_entity_viewsのfilterプロパティを使ってname指定してVM名を絞り込むことも可能です。

$VAR1 = [
      bless( {
(中略)
           'config' => bless( {
                    'guestId' => 'centos64Guest',
                    'files' => bless( {
                              'suspendDirectory' => '[datastore_small1] CentOS 6.2',
                              'vmPathName' => '[datastore_small1] CentOS 6.2/CentOS 6.2.vmx',
                              'logDirectory' => '[datastore_small1] CentOS 6.2',
                              'snapshotDirectory' => '[datastore_small1] CentOS 6.2'
                            }, 'VirtualMachineFileInfo' ),
                    'vAssertsEnabled' => '0',
                    'firmware' => 'bios',
                    'uuid' => '564d82b0-0ed0-3e7f-baa4-9ba3dd5c62d3',
                    'bootOptions' => bless( {
                                'enterBIOSSetup' => '0',
                                'bootRetryDelay' => '10000',
                                'bootRetryEnabled' => '0',
                                'bootDelay' => '0'
                                }, 'VirtualMachineBootOptions' ),
                    'cpuHotRemoveEnabled' => '0',
                    'hardware' => bless( {
                               'virtualSMCPresent' => '0',
                               'virtualICH7MPresent' => '0',
                               'device' => [
                                       bless( {
                                          'deviceInfo' => bless( {
                                                       'summary' => 'IDE 0',
                                                       'label' => 'IDE 0'
                                                       }, 'Description' ),
                                          'busNumber' => '0',
                                          'key' => '200'
                                          }, 'VirtualIDEController' ),
                                       bless( {
                                          'device' => [
                                                  '3002'
                                                ],
                                          'deviceInfo' => bless( {
                                                       'summary' => 'IDE 1',
                                                       'label' => 'IDE 1'
                                                       }, 'Description' ),
                                          'busNumber' => '1',
                                          'key' => '201'
                                          }, 'VirtualIDEController' ),
                                       bless( {
                                          'device' => [
                                                  '600',
                                                  '700'
                                                ],
                                          'deviceInfo' => bless( {
                                                       'summary' => 'PS2 controller 0',
                                                       'label' => 'PS2 controller 0'
                                                       }, 'Description' ),
                                          'busNumber' => '0',
                                          'key' => '300'
                                          }, 'VirtualPS2Controller' ),
                                       bless( {
                                          'device' => [
                                                  '500',
                                                  '12000',
                                                  '1000',
                                                  '4000'
                                                ],
                                          'deviceInfo' => bless( {
                                                       'summary' => 'PCI controller 0',
                                                       'label' => 'PCI controller 0'
                                                       }, 'Description' ),
                                          'busNumber' => '0',
                                          'key' => '100'
                                          }, 'VirtualPCIController' ),
                                       bless( {
                                          'device' => [
                                                  '8000'
                                                ],
                                          'deviceInfo' => bless( {
                                                       'summary' => 'SIO controller 0',
                                                       'label' => 'SIO controller 0'
                                                       }, 'Description' ),
                                          'busNumber' => '0',
                                          'key' => '400'
                                          }, 'VirtualSIOController' ),
                                       bless( {
                                          'unitNumber' => '0',
                                          'deviceInfo' => bless( {
                                                       'summary' => 'Keyboard',
                                                       'label' => 'Keyboard '
                                                       }, 'Description' ),
                                          'controllerKey' => '300',
                                          'key' => '600'
                                          }, 'VirtualKeyboard' ),
                                       bless( {
                                          'backing' => bless( {
                                                      'hostPointingDevice' => 'autodetect',
                                                      'useAutoDetect' => '0',
                                                      'deviceName' => ''
                                                    }, 'VirtualPointingDeviceDeviceBackingInfo' ),
                                          'unitNumber' => '1',
                                          'deviceInfo' => bless( {
                                                       'summary' => 'Pointing device; Device',
                                                       'label' => 'Pointing device'
                                                       }, 'Description' ),
                                          'controllerKey' => '300',
                                          'key' => '700'
                                          }, 'VirtualPointingDevice' ),
                                       bless( {
                                          'useAutoDetect' => '0',
                                          'enable3DSupport' => '0',
                                          'videoRamSizeInKB' => '8192',
                                          'unitNumber' => '0',
                                          'deviceInfo' => bless( {
                                                       'summary' => 'Video card',
                                                       'label' => 'Video card '
                                                       }, 'Description' ),
                                          'numDisplays' => '1',
                                          'controllerKey' => '100',
                                          'key' => '500'
                                          }, 'VirtualMachineVideoCard' ),
                                       bless( {
                                          'allowUnrestrictedCommunication' => '0',
                                          'id' => '-581147949',
                                          'unitNumber' => '17',
                                          'deviceInfo' => bless( {
                                                       'summary' => 'Device on the virtual machine PCI bus that provides support for the virtual machine communication interface',
                                                       'label' => 'VMCI device'
                                                       }, 'Description' ),
                                          'controllerKey' => '100',
                                          'key' => '12000'
                                          }, 'VirtualMachineVMCIDevice' ),
                                       bless( {
                                          'sharedBus' => bless( {
                                                      'val' => 'noSharing'
                                                      }, 'VirtualSCSISharing' ),
                                          'device' => [
                                                  '2000'
                                                ],
                                          'key' => '1000',
                                          'hotAddRemove' => '1',
                                          'scsiCtlrUnitNumber' => '7',
                                          'deviceInfo' => bless( {
                                                       'summary' => 'LSI Logic',
                                                       'label' => 'SCSI controller 0'
                                                       }, 'Description' ),
                                          'unitNumber' => '3',
                                          'controllerKey' => '100',
                                          'busNumber' => '0'
                                          }, 'VirtualLsiLogicController' ),
                                       bless( {
                                          'shares' => bless( {
                                                     'shares' => '1000',
                                                     'level' => bless( {
                                                               'val' => 'normal'
                                                               }, 'SharesLevel' )
                                                     }, 'SharesInfo' ),
                                          'capacityInKB' => '41943040',
                                          'storageIOAllocation' => bless( {
                                                            'shares' => bless( {
                                                                       'shares' => '1000',
                                                                       'level' => bless( {
                                                                                 'val' => 'normal'
                                                                               }, 'SharesLevel' )
                                                                     }, 'SharesInfo' ),
                                                            'limit' => '-1'
                                                          }, 'StorageIOAllocationInfo' ),
                                          'backing' => bless( {
                                                      'writeThrough' => '0',
                                                      'fileName' => '[datastore_small1] CentOS 6.2/CentOS 6.2.vmdk',
                                                      'uuid' => '6000C29d-413e-56de-1ff7-22deb6591e4d',
                                                      'digestEnabled' => '0',
                                                      'diskMode' => 'persistent',
                                                      'datastore' => bless( {
                                                                  'value' => '4f686c32-486bf4cf-b9b0-001517193f27',
                                                                  'type' => 'Datastore'
                                                                }, 'ManagedObjectReference' ),
                                                      'thinProvisioned' => '1',
                                                      'split' => '0',
                                                      'contentId' => 'ec752ee10acbe3f8ad611f3c5f214d1b'
                                                    }, 'VirtualDiskFlatVer2BackingInfo' ),
                                          'unitNumber' => '0',
                                          'deviceInfo' => bless( {
                                                       'summary' => '41,943,040 KB',
                                                       'label' => 'Hard disk 1'
                                                       }, 'Description' ),
                                          'controllerKey' => '1000',
                                          'key' => '2000'
                                          }, 'VirtualDisk' ),
                                       bless( {
                                          'connectable' => bless( {
                                                        'allowGuestControl' => '1',
                                                        'status' => 'untried',
                                                        'connected' => '0',
                                                        'startConnected' => '0'
                                                      }, 'VirtualDeviceConnectInfo' ),
                                          'backing' => bless( {
                                                      'fileName' => '[] /usr/lib/vmware/isoimages/linux.iso'
                                                    }, 'VirtualCdromIsoBackingInfo' ),
                                          'unitNumber' => '0',
                                          'deviceInfo' => bless( {
                                                       'summary' => 'ISO [] /usr/lib/vmware/isoimages/linux.iso',
                                                       'label' => 'CD/DVD drive 1'
                                                       }, 'Description' ),
                                          'controllerKey' => '201',
                                          'key' => '3002'
                                          }, 'VirtualCdrom' ),
                                       bless( {
                                          'macAddress' => '00:0c:29:5c:62:d3',
                                          'backing' => bless( {
                                                      'network' => bless( {
                                                                'value' => 'HaNetwork-VM Network',
                                                                'type' => 'Network'
                                                                }, 'ManagedObjectReference' ),
                                                      'useAutoDetect' => '0',
                                                      'deviceName' => 'VM Network'
                                                    }, 'VirtualEthernetCardNetworkBackingInfo' ),
                                          'wakeOnLanEnabled' => '1',
                                          'key' => '4000',
                                          'connectable' => bless( {
                                                        'allowGuestControl' => '1',
                                                        'status' => 'untried',
                                                        'connected' => '0',
                                                        'startConnected' => '1'
                                                      }, 'VirtualDeviceConnectInfo' ),
                                          'addressType' => 'generated',
                                          'deviceInfo' => bless( {
                                                       'summary' => 'VM Network',
                                                       'label' => 'Network adapter 1'
                                                       }, 'Description' ),
                                          'unitNumber' => '7',
                                          'controllerKey' => '100'
                                          }, 'VirtualE1000' ),
                                       bless( {
                                          'connectable' => bless( {
                                                        'allowGuestControl' => '1',
                                                        'status' => 'untried',
                                                        'connected' => '0',
                                                        'startConnected' => '0'
                                                      }, 'VirtualDeviceConnectInfo' ),
                                          'backing' => bless( {
                                                      'useAutoDetect' => '0',
                                                      'deviceName' => ''
                                                    }, 'VirtualFloppyRemoteDeviceBackingInfo' ),
                                          'unitNumber' => '0',
                                          'deviceInfo' => bless( {
                                                       'summary' => 'Remote',
                                                       'label' => 'Floppy drive 1'
                                                       }, 'Description' ),
                                          'controllerKey' => '400',
                                          'key' => '8000'
                                          }, 'VirtualFloppy' )
                                     ],
                               'numCoresPerSocket' => '1',
                               'numCPU' => '1',
                               'memoryMB' => '2048'
                               }, 'VirtualHardware' ),
                    'guestAutoLockEnabled' => '0',
                    'instanceUuid' => '52671d7e-ed70-4f2f-aaf3-4e999b1b22b2',
                    'memoryAllocation' => bless( {
                                   'shares' => bless( {
                                              'shares' => '20480',
                                              'level' => bless( {
                                                        'val' => 'normal'
                                                      }, 'SharesLevel' )
                                            }, 'SharesInfo' ),
                                   'reservation' => '0',
                                   'expandableReservation' => '0',
                                   'limit' => '-1'
                                   }, 'ResourceAllocationInfo' ),
                    'swapPlacement' => 'inherit',
                    'memoryHotAddEnabled' => '0',
                    'maxMksConnections' => '40',
                    'template' => '0',
                    'tools' => bless( {
                              'lastInstallInfo' => bless( {
                                            'counter' => '0'
                                            }, 'ToolsConfigInfoToolsLastInstallInfo' ),
                              'toolsUpgradePolicy' => 'manual',
                              'afterPowerOn' => '1',
                              'syncTimeWithHost' => '0',
                              'beforeGuestStandby' => '1',
                              'toolsVersion' => '8384',
                              'afterResume' => '1',
                              'beforeGuestShutdown' => '1'
                            }, 'ToolsConfigInfo' ),
                    'name' => 'CentOS 6.2',
                    'modified' => '1970-01-01T00:00:00Z',
                    'guestFullName' => 'CentOS 4/5/6 (64-bit)',
                    'cpuAllocation' => bless( {
                                  'shares' => bless( {
                                             'shares' => '1000',
                                             'level' => bless( {
                                                       'val' => 'normal'
                                                     }, 'SharesLevel' )
                                           }, 'SharesInfo' ),
                                  'reservation' => '0',
                                  'expandableReservation' => '0',
                                  'limit' => '-1'
                                }, 'ResourceAllocationInfo' ),
                    'changeTrackingEnabled' => '0',
                    'annotation' => '',
                    'npivTemporaryDisabled' => '1',
                    'changeVersion' => '2012-04-03T09:52:50.536192Z',
                    'alternateGuestName' => '',
                    'locationId' => '564d82b0-0ed0-3e7f-baa4-9ba3dd5c62d3',
                    'initialOverhead' => bless( {
                                  'initialMemoryReservation' => '94724096',
                                  'initialSwapReservation' => '126910464'
                                  }, 'VirtualMachineConfigInfoOverheadInfo' ),
                    'defaultPowerOps' => bless( {
                                  'suspendType' => 'hard',
                                  'powerOffType' => 'soft',
                                  'defaultPowerOffType' => 'soft',
                                  'defaultSuspendType' => 'hard',
                                  'resetType' => 'soft',
                                  'standbyAction' => 'powerOnSuspend',
                                  'defaultResetType' => 'soft'
                                  }, 'VirtualMachineDefaultPowerOpInfo' ),
                    'flags' => bless( {
                              'snapshotPowerOffBehavior' => 'powerOff',
                              'htSharing' => 'any',
                              'runWithDebugInfo' => '0',
                              'monitorType' => 'release',
                              'disableAcceleration' => '0',
                              'virtualExecUsage' => 'hvAuto',
                              'recordReplayEnabled' => '0',
                              'enableLogging' => '1',
                              'useToe' => '0',
                              'diskUuidEnabled' => '0',
                              'virtualMmuUsage' => 'automatic'
                            }, 'VirtualMachineFlagInfo' ),
                    'version' => 'vmx-08',
(中略)
         }, 'VirtualMachine' )
    ];

たくさん出たねーよかったねー


中略してしまったけど、興味深かったのが、無効なメソッドのリスト(disabledMethodプロパティ)があったこと。ほとんど無効になってた。vCenterがないとほとんどだめっぽい。
でもGuestShutdownとかはpowerOpsのサンプルで叩けたし、もしかしたら叩けるかもしれないけど、試すのめんどっちい。


あと、CloneとかTemplate、カスタマイズ構成とかはvCenterが前提だったのね。
vCenter SDKって名前にして分けてくれればいいのに。
vCenter試用できるけど60日ぐらいだし、1年20万のライセンスなんか買いたくないですし。


ゲームクリア感が出てきたけども、何とか擬似クローン的なことをやってみたい。
vmkfstoolsを使えば、仮想ディスク(vmdk)の複製が出来るので、
これをうまいこと空のVMへシステムディスクとしてマウントできれば、クローンっぽい挙動にはなりそうです。
もしかしてほかのVMMのほうが自由度高いんじゃ。Xenとかどうなんだろう。