我是Python的新手,正在尝试编写一个脚本来为在多个位置运行的所有VMWare vCenter创建清单报告。该脚本基本上使用我的凭据在vCenter中创建会话ID,然后运行REST API调用以从vCenter获取所需信息。我可以为一个vCenter运行它,但是我想进行修改,使其应该一个一个地读取所有vCenter列表,并为每个vCenter执行所有REST API请求并生成输出,如下所示。你能帮忙吗?
输入文件格式:
vcenter1.abc.com
vcenter2.abc.com
vcenter3.xyz.com
vcenter4.xyz.com
我当前的一个vCenter脚本:
#!/usr/bin/python
import requests
import json
vcenter="vcenter1.abc.com"
sess = requests.post(
"https://vcenter1.abc.com/rest/com/vmware/cis/session",
auth=('vcusr_testapi@vsphere.local', 'xxxxxxx'),
verify=False
)
session_id = sess.json()['value']
print 'vcenter,#datacenters,#clusters,#Hosts,#HostsConnected,#HostsDisconnected,#VMs,#PoweredOnVMs,#PoweredOffVMs'
resp = requests.get(
"https://vcenter1.abc.com/rest/vcenter/datacenter",
verify=False,
headers={
"vmware-api-session-id": session_id
}
)
vc_response=json.loads(resp.text)
list_dc=vc_response["value"]
count_dc=len(list_dc)
resp = requests.get(
"https://vcenter1.abc.com/rest/vcenter/cluster",
verify=False,
headers={
"vmware-api-session-id": session_id
}
)
vc_response=json.loads(resp.text)
list_cluster=vc_response["value"]
count_cluster=len(list_cluster)
resp = requests.get(
"https://vcenter1.abc.com/rest/vcenter/host",
verify=False,
headers={
"vmware-api-session-id": session_id
}
)
host_response=json.loads(resp.text)
list_host=host_response["value"]
count_host=len(list_host)
resp = requests.get(
"https://vcenter1.abc.com/rest/vcenter/host?filter.connection_states=CONNECTED",
verify=False,
headers={
"vmware-api-session-id": session_id
}
)
host_response=json.loads(resp.text)
list_host_connect=host_response["value"]
count_host_connect=len(list_host_connect)
resp = requests.get(
"https://vcenter1.abc.com/rest/vcenter/host?filter.connection_states=DISCONNECTED",
verify=False,
headers={
"vmware-api-session-id": session_id
}
)
host_response=json.loads(resp.text)
list_host_disconnect=host_response["value"]
count_host_disconnect=len(list_host_disconnect)
resp = requests.get(
"https://vcenter1.abc.com/rest/vcenter/vm",
verify=False,
headers={
"vmware-api-session-id": session_id
}
)
vc_response=json.loads(resp.text)
list_vm=vc_response["value"]
count_vm=len(list_vm)
resp = requests.get(
"https://vcenter1.abc.com/rest/vcenter/vm?filter.power_states=POWERED_OFF",
verify=False,
headers={
"vmware-api-session-id": session_id
}
)
vc_response=json.loads(resp.text)
list_vm_on=vc_response["value"]
count_vm_on=len(list_vm_on)
resp = requests.get(
"https://vcenter1.abc.com/rest/vcenter/vm?filter.power_states=POWERED_ON",
verify=False,
headers={
"vmware-api-session-id": session_id
}
)
vc_response=json.loads(resp.text)
list_vm_off=vc_response["value"]
count_vm_off=len(list_vm_off)
print vcenter,count_dc,count_cluster,count_host,count_host_connect,count_host_disconnect,count_vm,count_vm_on,count_vm_off
##print(vcenter, count_dc, sep=",")
当前脚本的输出:
vcenter,#datacenters,#clusters,#Hosts,#HostsConnected,#HostsDisconnected,#VMs,#PoweredOnVMs,#PoweredOffVMs
vcenter1.abc.com 5 6 16 14 2 389 140 249
我想以逗号分隔格式打印变量输出的输出,类似于输出文件中的标题行,请使用语法帮助我。
另外,需要修改脚本以使用for循环或任何其他机制为所有vCenter运行脚本。
我正在使用Python 2.7.5。
这是脚本的重写版本,可处理vCenter列表并对其执行所有请求(不过请耐心等待,因为我没有环境可以这样做):
!/usr/bin/python
from __future__ import print_function
import requests
import json
vcenters = (
'vcenter1.abc.com',
'vcenter2.abc.com',
'vcenter3.xyz.com',
'vcenter4.xyz.com'
)
apis = dict(
dcs = '/rest/vcenter/datacenter',
clusters = '/rest/vcenter/cluster',
hosts = '/rest/vcenter/host',
connected = '/rest/vcenter/host?filter.connection_states=CONNECTED',
disconnected = '/rest/vcenter/host?filter.connection_states=DISCONNECTED',
vms = '/rest/vcenter/vm',
powered_off = '/rest/vcenter/vm?filter.power_states=POWERED_OFF',
powered_on = '/rest/vcenter/vm?filter.power_states=POWERED_ON',
)
auth = ('vcusr_testapi@vsphere.local', 'xxxxxxx')
counter = {}
for vcenter in vcenters:
counter[vcenter] = {}
session = requests.post(
'https://%s/rest/com/vmware/cis/session' % vcenter,
auth=auth,
verify=False
)
kwargs = dict(
headers = dict(
vmware-api-session-id = session.json()['value']
),
verify=False
)
for api, url in apis.items():
r = requests.get(
'https://%s%s' % (vcenter, url),
*kwargs
)
counter[vcenter][api] = json.loads(r.text)['value']
print('vcenter,#datacenters,#clusters,#Hosts,#HostsConnected,#HostsDisconnected,#VMs,#PoweredOnVMs,#PoweredOffVMs')
for vcenter in counter:
count = counter[vcenter]
print(
vcenter,
count['dcs'],
count['clusters'],
count['hosts'],
count['connected'],
count['disconnected'],
count['vms'],
count['powered_on'],
count['powered_off',
sep=','
)
我希望它至少可以作为一个起点。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。