使用 PHP SOAP 的虚拟机状态

使用 PHP SOAP 的虚拟机状态

到目前为止,我能够使用 php SOAP 从 VMWARE Esxi 检索有关 VM 的以下信息。

‘名称’、‘guest.ipAddress’、‘guest.guestState’、‘runtime.powerState’、‘config.hardware.numCPU’、‘config.hardware.memoryMB’

这是代码。

class soapclientd extends soapclient
{
        public $action = false;

    public function __construct($wsdl, $options = array())
        {
        parent::__construct($wsdl, $options);
    }

        public function __doRequest($request, $location, $action, $version, $one_way = 0)
        {
//        echo '<pre>' . htmlspecialchars(str_replace(array ('<ns', '></'), array (PHP_EOL . '<ns', '>'.PHP_EOL.'</'), $request)) . '</pre>';
        $resp = parent::__doRequest($request, $location, $action, $version, $one_way);
                return $resp;
        }

}


$client =  new SoapClient("https://172.16.3.3/sdk/vimService.wsdl", array("trace" => 1, "location"=>"https://172.16.3.3/sdk/"));
try
{
    $request = new stdClass();
    $request->_this = array ('_' => 'ServiceInstance', 'type' => 'ServiceInstance');
    $response = $client->__soapCall('RetrieveServiceContent', array((array)$request));
} catch (Exception $e)
{
    echo $e->getMessage();
    exit;
}
$ret = $response->returnval;

try
{
    $request = new stdClass();
    $request->_this = $ret->sessionManager;
    $request->userName = 'root';
    $request->password = 'xxxx';
    $response = $client->__soapCall('Login', array((array)$request));
} catch (Exception $e)
{
    echo $e->getMessage();
    exit;
}

$ss1 = new soapvar(array ('name' => 'FolderTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$ss2 = new soapvar(array ('name' => 'DataCenterVMTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$a = array ('name' => 'FolderTraversalSpec', 'type' => 'Folder', 'path' => 'childEntity', 'skip' => false, $ss1, $ss2);

$ss = new soapvar(array ('name' => 'FolderTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$b = array ('name' => 'DataCenterVMTraversalSpec', 'type' => 'Datacenter', 'path' => 'vmFolder', 'skip' => false, $ss);

$res = null;
try
{
    $request = new stdClass();
    $request->_this = $ret->propertyCollector;
    $request->specSet = array (
        'propSet' => array (
            array ('type' => 'VirtualMachine', 'all' => 0, 'pathSet' => array ('name', 'guest.ipAddress', 'guest.guestState', 'runtime.powerState', 'config.hardware.numCPU', 'config.hardware.memoryMB')),
        ),
        'objectSet' => array (
            'obj' => $ret->rootFolder,
            'skip' => false,
            'selectSet' => array (
                new soapvar($a, SOAP_ENC_OBJECT, 'TraversalSpec'),
                new soapvar($b, SOAP_ENC_OBJECT, 'TraversalSpec'),
                ),
            )
        );
    $res = $client->__soapCall('RetrieveProperties', array((array)$request));
} catch (Exception $e)
{
    echo $e->getMessage();
}

print_r($res);

对于每个虚拟机来说,代码的输出都是这样的。

    [28] => stdClass Object
        (
            [obj] => stdClass Object
                (
                    [_] => 97
                    [type] => VirtualMachine
                )

            [propSet] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => config.hardware.memoryMB
                            [val] => 15996
                        )

                    [1] => stdClass Object
                        (
                            [name] => config.hardware.numCPU
                            [val] => 4
                        )

                    [2] => stdClass Object
                        (
                            [name] => guest.guestState
                            [val] => notRunning
                        )

                    [3] => stdClass Object
                        (
                            [name] => name
                            [val] => 8796-january-Core-3.181
                        )

                    [4] => stdClass Object
                        (
                            [name] => runtime.powerState
                            [val] => poweredOff
                        )

                )

        )

现在我想将检索信息扩展到虚拟机资源消耗统计数据,如使用的内存、CPU、网络等。

我尝试在数组中添加属性,但代码本身不起作用,所以主要是属性名称错误,或者这不是检索这些内容的正确方法。

答案1

所以,

整个游戏都是基于 propSet 值。

    'propSet' => array (
        array ('type' => 'VirtualMachine', 'all' => 0, 'pathSet' => array ('name', 'guest.ipAddress', 'guest.guestState', 'runtime.powerState', 'config.hardware.numCPU', 'config.hardware.memoryMB')),
    )

这里如果我提到 all=1 那么我就可以检索所有内容。

但是,对于过滤的东西,我需要在“pathSet”数组中提及属性名称。

我第一次使用 all=1 获取了所有信息,现在我有了完整的属性列表。根据获取的数据,我可以添加更多属性。

像“overallStatus”在正常情况下应该会显示“绿色”。

像下面这样

config.guestFullName => Oracle Linux 4/5/6 (64-bit)
config.uuid => 564d2981-6b15-8a42-b7fc-4e86e7401655
config.version => vmx-08
guest.hostName => Win7-VM
guest.net.macAddress => 00:0c:29:ad:d4:f0

相关内容