VMware {code} Community
ksvreddy
Contributor
Contributor

getting VM Info by using VI3 API in C++

Hi

I want to get the details of VM name and IP address by using the VI3 API in C++ specifically.

If any body having the code snipet for getting the above mentioned details, please post it for me.

thanks,

vkr

0 Kudos
13 Replies
jlegan
Contributor
Contributor

I could give you my C# code Smiley Happy

0 Kudos
dsubram
Enthusiast
Enthusiast

You can look into my pervious posts in regards to this. I have posted sample programs on how to list the VMs. We can create MORs for the same for each VM and get the details of each VM such as VM Name and IP Addr.

Please let me know if more details are required.

0 Kudos
Ashwin89
Contributor
Contributor

Can we get the Operating system type using these APIS?

0 Kudos
dsubram
Enthusiast
Enthusiast

YES, we can get the operating system type. The return value is same as the operating system which got selected when we created a VM.

0 Kudos
ksvreddy
Contributor
Contributor

Hi,

I saw ur previous posts , that will give the list of VM Id's . I am unable to find the XML parser in the attachments. Can you please send me the XML parser code and also if u have already code snipet to get the each VM MOR ?

thanks

vkr

0 Kudos
stumpr
Virtuoso
Virtuoso

I just posted this in another thread about the TraversalSpec. I was able to get TraversalSpec property filters working with C++ / gSOAP. I'll post my code and Makefile below. I've been developing this on OS X, so no guarantees about compiler differences. I don't pull out the VM's IP, but it might get you started.

Also, code isn't very clean or pretty...just something I used to test out TraversalSpec filters.

========

#include "soapVimBindingProxy.h"

#include "VimBinding.nsmap"

#include "soapH.h"

#include

using namespace std;

void sigpipe_handle(int x) { cout << "sigpipe " << x << endl; }

int main()

{

VimBinding vim;

ns1__ManagedObjectReference ManagedObjectRef;

ns1__RetrievePropertiesRequestType RetrievePropertiesReq;

ns1_RetrievePropertiesResponse RetrievePropertiesRes;

ns1__RetrieveServiceContentRequestType RetrieveServiceContentReq;

ns1_RetrieveServiceContentResponse RetrieveServiceContentRes;

ns1__ServiceContent *ServiceContent;

ns1__AboutInfo *AboutInfo;

ns1__LoginRequestType LoginReq;

ns1_LoginResponse LoginRes;

ns1__UserSession *UserSession;

ns1__TraversalSpec DataCenterVMTraversalSpec, FolderTraversalSpec;

ns1__SelectionSpec DataCenterVMTraversalSelectionSpec, FolderTraversalSelectionSpec;

ns1__PropertySpec PropertySpec;

ns1__ObjectSpec ObjectSpec;

ns1__PropertyFilterSpec PropertyFilterSpec;

ns1__DynamicProperty DynamicProperty;

bool xsd_true = 1;

bool xsd_false = 0;

int i = 0;

int j = 0;

xsd__string * name;

char* service_url = "https://xxxxxx/sdk";

char* username = "xxxxx";

char* password = "xxxxx";

vim.endpoint = service_url;

soap_ssl_init();

if (soap_ssl_client_context(

vim.soap,

SOAP_SSL_NO_AUTHENTICATION,

NULL,

NULL,

NULL,

NULL,

NULL ))

{

cout << "SSL:";

soap_print_fault(vim.soap, stderr);

exit(1);

}

ManagedObjectRef.__item = "ServiceInstance";

ManagedObjectRef.type = new string("ServiceInstance");

RetrieveServiceContentReq._USCOREthis = &ManagedObjectRef;

if ( vim.__ns1__RetrieveServiceContent(&RetrieveServiceContentReq, &RetrieveServiceContentRes) == SOAP_OK )

{

cout << "RetrieveServiceContent - OK" << endl;

}

else

{

soap_print_fault(vim.soap,stderr);

exit(1);

}

ServiceContent = RetrieveServiceContentRes.returnval;

if (ServiceContent && ServiceContent->about)

{

AboutInfo = ServiceContent->about;

cout << "fullName: " << AboutInfo->fullName << endl;

}

if (ServiceContent && ServiceContent->sessionManager)

{

LoginReq._USCOREthis = ServiceContent->sessionManager;

LoginReq.userName = username;

LoginReq.password = password;

LoginReq.locale = NULL;

if( vim.__ns1__Login(&LoginReq, &LoginRes) == SOAP_OK )

{

UserSession = LoginRes.returnval;

cout << "Login Successful: " << UserSession->userName << endl;

cout << "Login Time: " << ctime(&UserSession->loginTime) << endl;

}

else

{

soap_print_fault(vim.soap, stderr);

}

FolderTraversalSelectionSpec.name = new string("FolderTraversalSpec");

DataCenterVMTraversalSelectionSpec.name = new string("DataCenterVMTraversalSpec");

DataCenterVMTraversalSpec.name = new string("DataCenterVMTraversalSpec");

DataCenterVMTraversalSpec.type = "Datacenter";

DataCenterVMTraversalSpec.path = "vmFolder";

DataCenterVMTraversalSpec.skip = &xsd_true;

FolderTraversalSpec.name = new string("FolderTraversalSpec");

FolderTraversalSpec.type = "Folder";

FolderTraversalSpec.path = "childEntity";

FolderTraversalSpec.skip = &xsd_true;

DataCenterVMTraversalSpec.selectSet.push_back(&FolderTraversalSelectionSpec);

FolderTraversalSpec.selectSet.push_back(&DataCenterVMTraversalSelectionSpec);

FolderTraversalSpec.selectSet.push_back(&FolderTraversalSelectionSpec);

PropertySpec.type = "VirtualMachine";

PropertySpec.all = &xsd_false;

PropertySpec.pathSet.push_back("name");

ObjectSpec.obj = ServiceContent->rootFolder;

ObjectSpec.skip = &xsd_true;

ObjectSpec.selectSet.push_back(&FolderTraversalSpec);

ObjectSpec.selectSet.push_back(&DataCenterVMTraversalSpec);

PropertyFilterSpec.propSet.push_back(&PropertySpec);

PropertyFilterSpec.objectSet.push_back(&ObjectSpec);

RetrievePropertiesReq._USCOREthis = ServiceContent->propertyCollector;

RetrievePropertiesReq.specSet.push_back(&PropertyFilterSpec);

if ( vim.__ns1__RetrieveProperties(&RetrievePropertiesReq, &RetrievePropertiesRes) == SOAP_OK )

{

cout << "Number of Objects " << RetrievePropertiesRes.returnval.size() << endl;

for (i=0; i < RetrievePropertiesRes.returnval.size(); i++)

{

cout << " Object Type: " << *RetrievePropertiesRes.returnval[i]->obj->type;

cout << " (" << RetrievePropertiesRes.returnval[i]->propSet.size() << " properties), ";

for (j=0; j < RetrievePropertiesRes.returnval[i]->propSet.size(); j++)

{

DynamicProperty = *RetrievePropertiesRes.returnval[i]->propSet[j];

if ( DynamicProperty.name == "name" )

{

name = (xsd__string *) DynamicProperty.val;

//cout << "Name = " << ((xsd__string *) DynamicProperty.val)->__item;

cout << "Name: " << name->__item;

}

}

cout << endl;

}

}

else

{

soap_print_fault(vim.soap, stderr);

}

}

delete FolderTraversalSelectionSpec.name;

delete DataCenterVMTraversalSelectionSpec.name;

delete FolderTraversalSpec.name;

delete DataCenterVMTraversalSpec.name;

delete ManagedObjectRef.type;

soap_done(vim.soap);

soap_end(vim.soap);

return 0;

}

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
stumpr
Virtuoso
Virtuoso

Just fooling around, added the guest.ipAddress property to the filterspec and pulled it out. Slight modification to the sample I posted previously. This prints out the Virtual Machine's name (not the hostname) and it's primary IP as reported by the guest tools.

#include "soapVimBindingProxy.h"

#include "VimBinding.nsmap"

#include "soapH.h"

#include

using namespace std;

void sigpipe_handle(int x) { cout << "sigpipe " << x << endl; }

int main()

{

VimBinding vim;

ns1__ManagedObjectReference ManagedObjectRef;

ns1__RetrievePropertiesRequestType RetrievePropertiesReq;

ns1_RetrievePropertiesResponse RetrievePropertiesRes;

ns1__RetrieveServiceContentRequestType RetrieveServiceContentReq;

ns1_RetrieveServiceContentResponse RetrieveServiceContentRes;

ns1__ServiceContent *ServiceContent;

ns1__AboutInfo *AboutInfo;

ns1__LoginRequestType LoginReq;

ns1_LoginResponse LoginRes;

ns1__UserSession *UserSession;

ns1__TraversalSpec DataCenterVMTraversalSpec, FolderTraversalSpec;

ns1__SelectionSpec DataCenterVMTraversalSelectionSpec, FolderTraversalSelectionSpec;

ns1__PropertySpec PropertySpec;

ns1__ObjectSpec ObjectSpec;

ns1__PropertyFilterSpec PropertyFilterSpec;

ns1__DynamicProperty DynamicProperty;

bool xsd_true = 1;

bool xsd_false = 0;

int i = 0;

int j = 0;

xsd__string * name;

//string name, ip;

char* service_url = "https://xxxxxx/sdk";

char* username = "xxxxx";

char* password = "xxxxx";

vim.endpoint = service_url;

soap_ssl_init();

if (soap_ssl_client_context(

vim.soap,

SOAP_SSL_NO_AUTHENTICATION,

NULL,

NULL,

NULL,

NULL,

NULL ))

{

cout << "SSL:";

soap_print_fault(vim.soap, stderr);

exit(1);

}

ManagedObjectRef.__item = "ServiceInstance";

ManagedObjectRef.type = new string("ServiceInstance");

RetrieveServiceContentReq._USCOREthis = &ManagedObjectRef;

if ( vim.__ns1__RetrieveServiceContent(&RetrieveServiceContentReq, &RetrieveServiceContentRes) == SOAP_OK )

{

cout << "RetrieveServiceContent - OK" << endl;

}

else

{

soap_print_fault(vim.soap,stderr);

exit(1);

}

ServiceContent = RetrieveServiceContentRes.returnval;

if (ServiceContent && ServiceContent->about)

{

AboutInfo = ServiceContent->about;

cout << "fullName: " << AboutInfo->fullName << endl;

}

if (ServiceContent && ServiceContent->sessionManager)

{

LoginReq._USCOREthis = ServiceContent->sessionManager;

LoginReq.userName = username;

LoginReq.password = password;

LoginReq.locale = NULL;

if( vim.__ns1__Login(&LoginReq, &LoginRes) == SOAP_OK )

{

UserSession = LoginRes.returnval;

cout << "Login Successful: " << UserSession->userName << endl;

cout << "Login Time: " << ctime(&UserSession->loginTime) << endl;

}

else

{

soap_print_fault(vim.soap, stderr);

}

FolderTraversalSelectionSpec.name = new string("FolderTraversalSpec");

DataCenterVMTraversalSelectionSpec.name = new string("DataCenterVMTraversalSpec");

DataCenterVMTraversalSpec.name = new string("DataCenterVMTraversalSpec");

DataCenterVMTraversalSpec.type = "Datacenter";

DataCenterVMTraversalSpec.path = "vmFolder";

DataCenterVMTraversalSpec.skip = &xsd_true;

FolderTraversalSpec.name = new string("FolderTraversalSpec");

FolderTraversalSpec.type = "Folder";

FolderTraversalSpec.path = "childEntity";

FolderTraversalSpec.skip = &xsd_true;

DataCenterVMTraversalSpec.selectSet.push_back(&FolderTraversalSelectionSpec);

FolderTraversalSpec.selectSet.push_back(&DataCenterVMTraversalSelectionSpec);

FolderTraversalSpec.selectSet.push_back(&FolderTraversalSelectionSpec);

PropertySpec.type = "VirtualMachine";

PropertySpec.all = &xsd_false;

PropertySpec.pathSet.push_back("name");

PropertySpec.pathSet.push_back("guest.ipAddress");

ObjectSpec.obj = ServiceContent->rootFolder;

ObjectSpec.skip = &xsd_true;

ObjectSpec.selectSet.push_back(&FolderTraversalSpec);

ObjectSpec.selectSet.push_back(&DataCenterVMTraversalSpec);

PropertyFilterSpec.propSet.push_back(&PropertySpec);

PropertyFilterSpec.objectSet.push_back(&ObjectSpec);

RetrievePropertiesReq._USCOREthis = ServiceContent->propertyCollector;

RetrievePropertiesReq.specSet.push_back(&PropertyFilterSpec);

if ( vim.__ns1__RetrieveProperties(&RetrievePropertiesReq, &RetrievePropertiesRes) == SOAP_OK )

{

cout << "Number of Objects " << RetrievePropertiesRes.returnval.size() << endl;

for (i=0; i < RetrievePropertiesRes.returnval.size(); i++)

{

cout << " Object Type: " << *RetrievePropertiesRes.returnval[i]->obj->type;

cout << " (" << RetrievePropertiesRes.returnval[i]->propSet.size() << " properties)" << endl;

for (j=0; j < RetrievePropertiesRes.returnval[i]->propSet.size(); j++)

{

DynamicProperty = *RetrievePropertiesRes.returnval[i]->propSet[j];

//cout << " (Name, Value): " << DynamicProperty.name << ", " << DynamicProperty.val << endl;

if ( DynamicProperty.name == "name" )

{

name = (xsd__string *) DynamicProperty.val;

//cout << "Name = " << ((xsd__string *) DynamicProperty.val)->__item;

cout << " Name: " << name->__item << endl;

}

if ( DynamicProperty.name == "guest.ipAddress" )

{

cout << " IP Address: " << ((xsd__string*) DynamicProperty.val)->__item << endl;

}

}

}

}

else

{

soap_print_fault(vim.soap, stderr);

}

}

delete FolderTraversalSelectionSpec.name;

delete DataCenterVMTraversalSelectionSpec.name;

delete FolderTraversalSpec.name;

delete DataCenterVMTraversalSpec.name;

delete ManagedObjectRef.type;

soap_done(vim.soap);

soap_end(vim.soap);

return 0;

}

Sample output:

RetrieveServiceContent - OK

fullName: VMware VirtualCenter 2.5.0 build-104215

Login Successful: Administrator

Login Time: Thu Aug 14 14:24:58 2008

Number of Objects 19

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.18.110

Name: labocs1

Object Type: VirtualMachine (1 properties)

Name: julybootcamp

Object Type: VirtualMachine (1 properties)

Name: labexmb1a

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.18.111

Name: labocsarch

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.19.52

Name: Pano-XP

Object Type: VirtualMachine (1 properties)

Name: labbes

Object Type: VirtualMachine (1 properties)

Name: labexhub1

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.18.101

Name: labdc2

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.19.51

Name: labxp1

Object Type: VirtualMachine (1 properties)

Name: hqts2008

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.18.61

Name: ocsdb

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.18.100

Name: labdc1

Object Type: VirtualMachine (1 properties)

Name: labexmb1b

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.18.117

Name: rhelbase1-fwallace02

Object Type: VirtualMachine (1 properties)

Name: labexmb2

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.18.102

Name: labdc3

Object Type: VirtualMachine (1 properties)

Name: labdc4

Object Type: VirtualMachine (2 properties)

IP Address: 10.20.18.116

Name: rhelbase1-fwallace01

Object Type: VirtualMachine (1 properties)

Name: labexhub2

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
ksvreddy
Contributor
Contributor

Thanks for your help

0 Kudos
Ashwin89
Contributor
Contributor

Is that possible to get the actual size used by VMDK file instead of getting whole size allocated size for virtual machine?

Thanks

Ashwin

0 Kudos
stumpr
Virtuoso
Virtuoso

Yes, if you have the tools installed in the guest and they OS has run, the disk usage will be available. If not, no.

You can find it under guest.disk. This lists an array of disks that the guest OS has configured, and their usage. GuestDiskInfo

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
catch_hema
Contributor
Contributor

Hi

I'm having issues with RetrieveProperties (gSOAP C++ API). I used the code snippet below (posted by stumpr), and also tried other code by dsubram - my RetrievePropertiesResponse.returnval is always NULL. I am able to access the ServiceContent correctly (able to get all the aboutInfo values etc for ServiceContent). In a simpler example, if I just try to get the properties of rootFolder, without any traversals, I get a list of the properties in the missingSet vector of RetrievePropertiesResponse.returnval (propSet and dynamicProperty vectors are empty, and, obviously, none of the properties listed in the missingSet have any values). Would anyone have any ideas why this would be?

Thanks

H R

0 Kudos
helpmeforvmware
Contributor
Contributor

Hi stumpr,

I have downloaded gsoap tool kit and installed in /root/gsoap-2.7/ directory, and then in /root/test/ directory i have generated stub using following command,

wsdl2h -x -s -o vimService.h vim.wsdl

soapcpp2 -x -C vimService.h

and everything done fine.

Now i have used your code example in file test.cpp and tryied to compile using following command

i m in /root/test directory where soapC.h soapC.cpp soapClient.cpp are residing as well VimBindings.nsmap is also in same directory.

  1. g++ -g -o test -DWITH_OPENSSL -DWITH_COOKIES -l/root/gsoap-2.7/ -Wall test.cpp soapC.cpp soapClient.cpp /root/gsoap-2.7/gsoap/stdsoap2.cpp -lssl -lcrypto

It giving me following set of error.

test.cpp: In function `int main()':

test.cpp:61: cannot convert `std::string' to `char' in assignment

test.cpp:100: cannot convert `std::string' to `char' in assignment

test.cpp:101: cannot convert `std::string' to `char' in assignment

test.cpp:103: cannot convert `std::string' to `char' in assignment

test.cpp:108: cannot convert `std::string' to `char' in assignment

test.cpp:113: request for member `push_back' in `

DataCenterVMTraversalSpec.ns1__TraversalSpec::selectSet', which is of

non-aggregate type `ns1__SelectionSpec**'

test.cpp:114: request for member `push_back' in `

FolderTraversalSpec.ns1__TraversalSpec::selectSet', which is of

non-aggregate type `ns1__SelectionSpec**'

test.cpp:115: request for member `push_back' in `

FolderTraversalSpec.ns1__TraversalSpec::selectSet', which is of

non-aggregate type `ns1__SelectionSpec**'

test.cpp:119: request for member `push_back' in `

PropertySpec.ns1__PropertySpec::pathSet', which is of non-aggregate type `

char**'

test.cpp:120: request for member `push_back' in `

PropertySpec.ns1__PropertySpec::pathSet', which is of non-aggregate type `

char**'

test.cpp:125: request for member `push_back' in `

ObjectSpec.ns1__ObjectSpec::selectSet', which is of non-aggregate type `

ns1__SelectionSpec**'

test.cpp:126: request for member `push_back' in `

ObjectSpec.ns1__ObjectSpec::selectSet', which is of non-aggregate type `

ns1__SelectionSpec**'

test.cpp:128: request for member `push_back' in `

PropertyFilterSpec.ns1__PropertyFilterSpec::propSet', which is of

non-aggregate type `ns1__PropertySpec**'

test.cpp:129: request for member `push_back' in `

PropertyFilterSpec.ns1__PropertyFilterSpec::objectSet', which is of

non-aggregate type `ns1__ObjectSpec**'

test.cpp:133: request for member `push_back' in `

RetrievePropertiesReq.ns1__RetrievePropertiesRequestType::specSet', which is

of non-aggregate type `ns1__PropertyFilterSpec**'

test.cpp:137: request for member `size' in `

RetrievePropertiesRes._ns1__RetrievePropertiesResponse::returnval', which is

of non-aggregate type `ns1__ObjectContent**'

test.cpp:140: request for member `size' in `

RetrievePropertiesRes._ns1__RetrievePropertiesResponse::returnval', which is

of non-aggregate type `ns1__ObjectContent**'

test.cpp:142: request for member `obj' in `

*RetrievePropertiesRes._ns1__RetrievePropertiesResponse::returnval', which

is of non-aggregate type `ns1__ObjectContent*'

test.cpp:143: request for member `propSet' in `

*RetrievePropertiesRes._ns1__RetrievePropertiesResponse::returnval', which

is of non-aggregate type `ns1__ObjectContent*'

test.cpp:144: request for member `propSet' in `

*RetrievePropertiesRes._ns1__RetrievePropertiesResponse::returnval', which

is of non-aggregate type `ns1__ObjectContent*'

test.cpp:146: request for member `propSet' in `

*RetrievePropertiesRes._ns1__RetrievePropertiesResponse::returnval', which

is of non-aggregate type `ns1__ObjectContent*'

Where i m going wrong can u tell me ?

0 Kudos
stumpr
Virtuoso
Virtuoso

Did you include your header files?

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos