问题
How do i contact the vSphere (or VMWare) form Python either with some form of library or via SUDS to get the number of number of vCPU or a specific host/guest/virtual-machine?
Currently i'm trying:
from suds.client import Client
from suds.sudsobject import Property
client = Client("https://<server>/sdk/vimService?wsdl")
queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo']
print queryCon
And that works, and it gives me some form of output:
(Method){
name = "QueryConnectionInfo"
location = "https://localhost/sdk/vimService"
binding =
(binding){
input = <suds.bindings.document.Document instance at 0x0775C080>
output = <suds.bindings.document.Document instance at 0x0775C080>
}
soap =
(soap){
action = ""urn:vim25/4.1""
style = "document"
input =
(Input){
body =
(Body){
parts[] =
(Part){
root = <part name="parameters" element="vim25:QueryConnectionInfo"/>
name = "parameters"
qname[] =
"parameters",
"urn:vim25",
element = "(u'QueryConnectionInfo', u'urn:vim25')"
type = "None"
},
use = "literal"
namespace[] =
"vim25",
"urn:vim25",
wrapped = True
}
headers[] = <empty>
}
output =
(Output){
body =
(Body){
parts[] =
(Part){
root = <part name="parameters" element="vim25:QueryConnectionInfoResponse"/>
name = "parameters"
qname[] =
"parameters",
"urn:vim25",
element = "(u'QueryConnectionInfoResponse', u'urn:vim25')"
type = "None"
},
use = "literal"
namespace[] =
"vim25",
"urn:vim25",
wrapped = True
}
headers[] = <empty>
}
faults[] =
(Fault){
name = "InvalidLoginFault"
use = "literal"
parts[] =
(Part){
root = <part name="fault" element="vim25:InvalidLoginFault"/>
name = "fault"
qname[] =
"fault",
"urn:vim25",
element = "(u'InvalidLoginFault', u'urn:vim25')"
type = "None"
},
},
(Fault){
name = "HostConnectFaultFault"
use = "literal"
parts[] =
(Part){
root = <part name="fault" element="vim25:HostConnectFaultFault"/>
name = "fault"
qname[] =
"fault",
"urn:vim25",
element = "(u'HostConnectFaultFault', u'urn:vim25')"
type = "None"
},
},
(Fault){
name = "RuntimeFault"
use = "literal"
parts[] =
(Part){
root = <part name="fault" element="vim25:RuntimeFaultFault"/>
name = "fault"
qname[] =
"fault",
"urn:vim25",
element = "(u'RuntimeFaultFault', u'urn:vim25')"
type = "None"
},
},
}
}
I've tried following the following "guides":
SUDS - programmatic access to methods and types
http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html#field_detail
http://communities.vmware.com/thread/273616
I know that all the information probably is here, i just can't see the entire picture :/
After a while of trying this is where i get stuck:
client = Client("https://<server>/sdk/vimService?wsdl")
#queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo']
print client.service.QueryConnectionInfo("https://<server>/sdk", None, r'domain\user', 'Password')
And the output is:
urllib2.URLError: <urlopen error [Errno 1] _ssl.c:490: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol>
回答1:
You might want to try psphere, a Python library (based on suds) which will give you access the entire vSphere Web Services SDK.
Install it:
$ pip install psphere
Find a virtual machine and print the number of CPUs it has:
>>> from psphere.client import Client
>>> from psphere.managedobjects import VirtualMachine
>>> client = Client(server="vcenter.mydomain.com", username="Administrator", password="strong")
>>> vm = VirtualMachine.get(client, name="genesis")
>>> vm
<psphere.managedobjects.VirtualMachine object at 0xd3fbccc>
>>> print("%s has %s CPUs" % (vm.name, vm.config.hardware.numCPU))
genesis has 2 CPUs
You can find more examples in the documentation.
Disclaimer: I am the author of psphere.
回答2:
Solved it with a new library: pysphere (easy_install pysphere)
from pysphere import VIServer
server = VIServer()
server.connect("server", 'user', "pass")
vm = server.get_vm_by_name("virtual_host_name")
info = vm.get_properties()
It keeps the namespace intact of the vSphere variables and it's as transparent as i need it to be, meaning it doesn't change anything which makes it easy to find, it also support batching up information so i don't have to query through all 1500 servers one by one in a slow painful process, it takes a few seconds to gather information regarding any host.
来源:https://stackoverflow.com/questions/7889762/python-vmware-vsphere-web-sdk-suds