PHP vSphere (5.x) programming

It has been a while I updated my PHP examples, the previous code was designed for VI3 (a long time back).

Not too much has changed, but somethings you should be aware of. PHP5 has a build in soap library, which I do not use, I like to use nusoap, so make sure you use the nusoap_client, instead of soap_client. I also have a small library that contains 2 functions, that I found where easier done by creating raw soap messages, instead of creating them with variables. One is for the most imports Retrieve Properties. I build it with a very extensive traversal spec, so it should be easily be able to find anything.


require_once("lib/nusoap.php");
require_once("vc_lib.php");

After adding the libraries to your code and of course having the nusoap files available you can easily create the connection:

$myconnection = new nusoap_client("https://".$ServerIP."/sdk"); // Change to your own server!
$namespace = "urn:vim25";
$soapmsg[data] = new soapval('_this','ServiceInstance','ServiceInstance');
$result = $myconnection->call("RetrieveServiceContent",$soapmsg,$namespace);

This first call “RetrieveServiceContent” can be done without providing any login details. It should return the verion of the vSphere or ESX host and most importantly the “root folder object”. You can easily display the result information by using this code:
print_r($result);

 

The next step of course is to login to the VC server, this can be done as followed:

unset($soapmsg);
$soapmsg[this] = new soapval('_this','SessionManager','SessionManager');
$soapmsg[userName] = $username;
$soapmsg[password] = $password;
$result = $myconnection->call("Login",$soapmsg,$namespace);

The login request should return some basic info like the time (from the vSphere server) you are logged in. It also contains the session key that need to be used for all other following calls to use these credentials. You do not have to worry about this, as the nusoap library takes care of this, as long as you keep using the same connection variable.

One of the first things after login you probably want to do is find out information about certain objects like virtual machines, hosts, cluster, etc. All these objects have an unique Managed Object ID and you can retrieve this with probably the most complicated call of the entire API “RetrieveProperties”.  This API call allows you to search objects, but requires some extreme complex filters to find objects. I wrote a simple wrapper around this and my vc_lib.php file comes with the function “Find”.

“Find” works as followed.
$soapmsg = Find($RootFolder,$ObjectType, $Details);

this will create the right soap message to search your object. For the object type you can use things like “VirtualMachine”, “HostSystem” or “ClusterComputeResource”. Check the SDK documentation or visit the Managed object browser on your VC server. You can do this by going to : https://your_vsphere_server/mob

The $Details should be an array of field names that you want to have returned of each object. Again, to find out what fields are returned you can best check your Manages Object browser to see the fields.

here an example to list all VM in your environment

In my download section you will find the file PHP vSphere 5 example code. This examples code includes:
- Nu soap library files
- VC Lib file with 2 functions “Find” and “QueryPerf”
- Sample code to list all VMs + details
- Sample code to query performance stats on objects like VMs, Hosts and Clusters

  • #1 written by Ben Jefferson
    about 1 year ago

    First up, massive thanks to you Richard for the example code.

    I am also working on a Raspberry Pi based monitor for VSphere infrastructure. Just thought I’d share one issue I have spent hours getting to the bottom of today.

    The approach outline here (using the VCenter MOB data to query health of hosts) is subject to a bug in VSphere (affecting v4 and v5) whereby the VCenter MOB data is not updated by the hosts and so becomes wildly out of date. Net result is that when you pull out power from a power supply there is no change in the values.

    This KB article refers to the problem in vSphere 4
    http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2017770

    This post implies it exists in vSphere 5
    http://communities.vmware.com/thread/402899?tstart=0

    As does this one from Veeam…
    http://www.veeam.com/kb1654

    The VMWare and Veeam KB articles give the same solution although with greatly differing timeouts (120 vs 1200s).

    One thing to note – in vSphere5 the config file referred to has moved to /etc/vmware/vpxa/vpxa.cfg

    This fix worked for me. Do you know of a better fix? I’m slightly concerned that this will not scale and will extra load on the vcenter server.

  • #2 written by Richard Garsthagen
    about 1 year ago

    Hi Ben,

    I have not run into this issue yet, as I am mainly pulling performance data. There is a bug with that as well, as you have to specify the time very specifically of what window you want to query. But I got all that working. Have been testing it against my 2 esxi host environement and found no issues on being to heavy for VC in anyway.

    What kind of project are you working on?

*

powered by Virtacore