Created
August 20, 2022 22:36
-
-
Save gowatana/322b2b991146647b8c5192d31d496c96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import json | |
import requests | |
args = sys.argv | |
conf_file = args[1] | |
if len(args) == 3: | |
output_format = args[2] | |
else: | |
output_format = 'list' | |
cvm_port_network_name = 'CVM-Port' | |
no_ip_strings = 'NA' | |
with open(conf_file, 'r') as file: | |
conf = file.read() | |
conf = json.loads(conf) | |
prism_addr = conf['prism_address'] | |
prism_user = conf['user_name'] | |
prism_pass = conf['password'] | |
base_url = 'https://' + prism_addr + ':9440/PrismGateway/services/rest' | |
# ------------------------------------------------------------ | |
# functions | |
def config_session(prism_user, prism_pass): | |
requests.packages.urllib3.disable_warnings() | |
s = requests.Session() | |
s.auth = (prism_user, prism_pass) | |
s.headers.update({'Content-Type': 'application/json; charset=utf-8'}) | |
return s | |
def get_poweron_vms_v1(session): | |
api_url = base_url + '/v1/vms' | |
vms = session.get(api_url, verify=False).json() | |
vm_array = [] | |
for vm in vms['entities']: | |
if vm['powerState'] == 'on': | |
vm_array.append(vm) | |
return vm_array | |
def get_vnics_v1(session, vm_uuid): | |
api_url = base_url + '/v1/vms/' + vm_uuid + '/virtual_nics' | |
vnics = session.get(api_url, verify=False).json() | |
vnic_array = [] | |
for vnic in vnics: | |
vnic_array.append(vnic) | |
return vnic_array | |
def get_poweron_vms_v2(session): | |
api_url = base_url + '/v2.0/vms/?include_vm_nic_config=true' | |
vms = session.get(api_url, verify=False).json() | |
vm_array = [] | |
for vm in vms['entities']: | |
if vm['power_state'] == 'on': | |
vm_array.append(vm) | |
return vm_array | |
def get_vnics_v2(vm_array_v2, vm_uuid): | |
for vm in vm_array_v2: | |
if vm['uuid'] == vm_uuid: | |
return vm['vm_nics'] | |
def get_networks_v2(session): | |
api_url = base_url + '/v2.0/networks' | |
nws = session.get(api_url, verify=False).json() | |
return nws['entities'] | |
def output_vnic_info_json(vnic_infos): | |
print(json.dumps(vnic_info_list, indent=2)) | |
def output_vnic_info_list(vnic_infos, divider_strings): | |
for v in vnic_info_list: | |
print(divider_strings) | |
print('VM Name (VM UUID) : ' + v['vm_name'] + ' (' + v['vm_uuid'] + ')') | |
print('AHV Host Name : ' + v['host_name']) | |
print('Network Name : ' + v['vnic_nw_name']) | |
print('Network VLAN ID : ' + str(v['vlan_id'])) | |
print('vNIC Port Name : ' + v['vnic_name']) | |
print('vNIC MAC Address : ' + v['vnic_mac']) | |
#print('vNIC UUID : ' + v['vnic_uuid']) | |
print('IPv4 Address count: ' + str(v['vnic_ip_count'])) | |
ip_count = 0 | |
if v.get('vnic_ips'): | |
for ip in v['vnic_ips']: | |
ip_count += 1 | |
print('IPv4 Address ' + str(ip_count) + ' : ' + ip) | |
def output_vnic_info_csv(vnic_info_list): | |
vnic_header = [ | |
'AHV_Host', | |
'vNIC_Name', | |
'MAC', | |
'IPs', | |
'IP_1', | |
'Network_Name', | |
'VLAN_ID', | |
'VM_UUID', | |
'VM_Name', | |
] | |
print(','.join(vnic_header)) | |
for v in vnic_info_list: | |
vnic = [ | |
v['host_name'], | |
v['vnic_name'], | |
v['vnic_mac'], | |
str(v['vnic_ip_count']), | |
v['vnic_ips'][0], | |
v['vnic_nw_name'], | |
str(v['vlan_id']), | |
v['vm_uuid'], | |
v['vm_name'], | |
] | |
print(','.join(vnic)) | |
# ------------------------------------------------------------ | |
# main | |
if __name__ == '__main__': | |
s = config_session(prism_user, prism_pass) | |
vms_v1 = get_poweron_vms_v1(s) | |
vms_v2 = get_poweron_vms_v2(s) | |
networks = get_networks_v2(s) | |
vnic_info_list = [] | |
for vm in vms_v1: | |
vm_uuid = vm['uuid'] | |
vm_name = vm['vmName'] | |
host_name = vm['hostName'] | |
vnics = get_vnics_v1(s, vm['uuid']) | |
for vnic in vnics: | |
vnic_name = vnic['portName'] | |
vnic_uuid = vnic['uuid'] | |
vnic_mac = vnic['macAddress'] | |
# fix output for CVM VLAN ID | |
vlan_id = vnic['vlanId'] | |
if str(vlan_id) == 'None': | |
vlan_id = 0 | |
vnic_ip_count = len(vnic.get('ipv4Addresses')) | |
vnic_ips = [] | |
if(vnic.get('ipv4Addresses')): | |
ip_count = 0 | |
for ip in vnic['ipv4Addresses']: | |
vnic_ips.append(ip) | |
else: | |
vnic_ips.append(no_ip_strings) | |
# get Acropolis network name | |
if vnic['portName'].startswith('vnet'): | |
vnic_nw_name = cvm_port_network_name | |
else: | |
vnics_v2 = get_vnics_v2(vms_v2, vm_uuid) | |
for vnic_v2 in vnics_v2: | |
if vnic_v2['nic_uuid'] == vnic_uuid: | |
for nw in networks: | |
if vnic_v2['network_uuid'] == nw['uuid']: | |
vnic_nw_name = nw['name'] | |
vnic_info = {} | |
vnic_info['vm_name'] = vm_name | |
vnic_info['vm_uuid'] = vm_uuid | |
vnic_info['host_name'] = host_name | |
vnic_info['vnic_nw_name'] = vnic_nw_name | |
vnic_info['vlan_id'] = vlan_id | |
vnic_info['vnic_name'] = vnic_name | |
vnic_info['vnic_mac'] = vnic_mac | |
vnic_info['vnic_uuid'] = vnic_uuid | |
vnic_info['vnic_ip_count'] = vnic_ip_count | |
vnic_info['vnic_ips'] = vnic_ips | |
vnic_info_list.append(vnic_info.copy()) | |
if output_format == 'json': | |
output_vnic_info_json(vnic_info_list) | |
if output_format == 'list': | |
output_vnic_info_list(vnic_info_list, '-----') | |
if output_format == 'csv': | |
output_vnic_info_csv(vnic_info_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行方法
ログイン情報
実行