def SheetSummary(auth_list,WORKBOOK,TN_WS, NSX_Config = {}): # Connection to NSX SessionNSX = ConnectNSX(auth_list) system_url = '/api/v1/ui-controller/system-aggregate-status' inventory_url = '/api/v1/ui-controller/inventory-aggregate-status' security_url = '/api/v1/ui-controller/security-aggregate-status' network_url = '/api/v1/ui-controller/networking-aggregate-status' fabric_url = '/api/v1/ui-controller/fabric-aggregate-status' inventory_json = GetAPI(SessionNSX[0],inventory_url, auth_list) security_json = GetAPI(SessionNSX[0],security_url, auth_list) network_json = GetAPI(SessionNSX[0],network_url, auth_list) fabric_json = GetAPI(SessionNSX[0],fabric_url, auth_list) system_json = GetAPI(SessionNSX[0],system_url, auth_list) XLS_Lines = [] TN_HEADER_ROW = (' ', 'Number', 'Alarms', 'Status') XLS_Lines = GetEntity(system_json, XLS_Lines) XLS_Lines = GetEntity(fabric_json, XLS_Lines) XLS_Lines = GetEntity(inventory_json, XLS_Lines) XLS_Lines = GetEntity(network_json, XLS_Lines) XLS_Lines = GetEntity(security_json, XLS_Lines) a1 = TN_WS['A1'] a1.font = Font(name='Arial', size=16, bold=True) a1.value = "Summary" TN_WS[3] FillSheet(WORKBOOK,TN_WS.title,TN_HEADER_ROW,XLS_Lines,"0072BA", "TableStyleLight9", True, start_cell = 'A4') ConditionnalFormat(TN_WS, 'D5:D' + str(len(XLS_Lines) + 4), 'UP', False, 'GREEN') ConditionnalFormat(TN_WS, 'D5:D' + str(len(XLS_Lines) + 4), 'NONE', False, 'ORANGE') ConditionnalFormat(TN_WS, 'D5:D' + str(len(XLS_Lines) + 4), 'UP')
def SheetNSXManagerInfo(auth_list,WORKBOOK,TN_WS, NSX_Config = {}): Dict_NSXManager = {} # Dict NSXManager initialization NSX_Config['NSXManager'] = Dict_NSXManager SessionNSX = ConnectNSX(auth_list) ########### SECTION FOR REPORTING ON NSX-T MANAGER CLUSTER ########### nsxclstr_url = '/api/v1/cluster/status' nsxclstr_json = GetAPI(SessionNSX[0],nsxclstr_url, auth_list) # Check online and offline nodes if 'online_nodes' in nsxclstr_json['mgmt_cluster_status']: online_nodes = str(len(nsxclstr_json['mgmt_cluster_status']['online_nodes'])) else: online_nodes = "0" if 'offline_nodes' in nsxclstr_json['mgmt_cluster_status']: offline_nodes = str(len(nsxclstr_json['mgmt_cluster_status']['offline_nodes'])) else: offline_nodes = "0" NSX_Config['NSXManager']['Cluster_id'] = nsxclstr_json['cluster_id'] NSX_Config['NSXManager']['Cluster_status'] = nsxclstr_json['mgmt_cluster_status']['status'] NSX_Config['NSXManager']['Cluster_ctrl_status'] = nsxclstr_json['control_cluster_status']['status'] NSX_Config['NSXManager']['Cluster_overall_status'] = nsxclstr_json['detailed_cluster_status']['overall_status'] NSX_Config['NSXManager']['Cluster_online_nodes'] = online_nodes NSX_Config['NSXManager']['Cluster_offline_nodes'] = offline_nodes # Summary Table XLS_Lines = [['NSX-T Cluster ID', nsxclstr_json['cluster_id']], ['NSX-T Cluster Status', nsxclstr_json['mgmt_cluster_status']['status']], ['NSX-T Control Cluster Status', nsxclstr_json['control_cluster_status']['status']] , ['Overall NSX-T Cluster Status', nsxclstr_json['detailed_cluster_status']['overall_status']], ['Number of online nodes', online_nodes], ['Number of offline nodes', offline_nodes]] idx_second_sheet = len(XLS_Lines) + 2 # Write in Excel for line in XLS_Lines: TN_WS.append(line) # Format 1st Sheet for i in range(1, len(XLS_Lines) + 1): TN_WS.cell(row=i, column=1).fill = PatternFill('solid', start_color='004F81BD', end_color='004F81BD') #Blue TN_WS.cell(row=i, column=1).font = Font(b=True, color="00FFFFFF") #White ConditionnalFormat(TN_WS, 'B2:B4', 'STABLE') ConditionnalFormat(TN_WS, 'B5:B5', '3') ConditionnalFormat(TN_WS, 'B6:B6', '0') # Create second sheet TN_WS[idx_second_sheet] XLS_Lines = [] TN_HEADER_ROW = ('Group ID', 'Group Type', 'Group Status','Member FQDN', 'Member IP address', 'Member UUID', 'Member Status') for group in nsxclstr_json['detailed_cluster_status']['groups']: for member in group['members']: XLS_Lines.append([group['group_id'],group['group_type'], group['group_status'], member['member_fqdn'], member['member_ip'], member['member_uuid'], member['member_status']]) startCell = "A" + str(idx_second_sheet + 1) FillSheet(WORKBOOK,TN_WS.title,TN_HEADER_ROW,XLS_Lines,"0072BA", "TableStyleLight9", False, startCell) ConditionnalFormat(TN_WS, 'G10:G' + str(len(XLS_Lines) + 1), 'UP') ConditionnalFormat(TN_WS, 'C10:C' + str(len(XLS_Lines) + 1), 'STABLE')
def SheetTunnels(auth_list, WORKBOOK, TN_WS, NSX_Config={}): NSX_Config['Tunnels'] = [] # Connect to NSX SessionNSX = ConnectNSX(auth_list) transport_node_url = '/api/v1/transport-nodes' transport_node_json = GetAPI(SessionNSX[0], transport_node_url, auth_list) XLS_Lines = [] TN_HEADER_ROW = ('Transport Node', 'Tunnel Name', 'Tunnel Status', 'Egress Interface', 'Local Tunnel IP', 'Remote Tunnel IP', 'Remote Node ID', 'Remote Node Name', 'Encap') # Check if Transport Node present if isinstance( transport_node_json, dict) and 'results' in transport_node_json and transport_node_json[ 'result_count'] > 0: for node in transport_node_json['results']: tunnel_url = '/api/v1/transport-nodes/' + node['id'] + '/tunnels' tunnel_json = GetAPI(SessionNSX[0], tunnel_url, auth_list) if tunnel_json["result_count"] > 0: Dict_NodesTunnels = {} Dict_NodesTunnels['node_name'] = node['display_name'] Dict_NodesTunnels['tunnels'] = [] Dict_Tunnels = {} for tunnel in tunnel_json["tunnels"]: Dict_Tunnels['name'] = tunnel['name'] Dict_Tunnels['status'] = tunnel['status'] Dict_Tunnels['Egress_int'] = tunnel['egress_interface'] Dict_Tunnels['local_ip'] = tunnel['local_ip'] Dict_Tunnels['remote_ip'] = tunnel['remote_ip'] Dict_Tunnels['remote_node_id'] = tunnel['remote_node_id'] Dict_Tunnels['remote_node_display_name'] = tunnel[ 'remote_node_display_name'] Dict_Tunnels['encap'] = tunnel['encap'] Dict_NodesTunnels['tunnels'].append(Dict_Tunnels) # Create line XLS_Lines.append([ node['display_name'], tunnel['name'], tunnel['status'], tunnel['egress_interface'], tunnel['local_ip'], tunnel['remote_ip'], tunnel['remote_node_id'], tunnel['remote_node_display_name'], tunnel['encap'] ]) NSX_Config['Tunnels'].append(Dict_NodesTunnels) else: XLS_Lines.append( ["no Transport Nodes", "", "", "", "", "", "", "", ""]) if GetOutputFormat() == 'CSV': CSV = WORKBOOK FillSheetCSV(CSV, TN_HEADER_ROW, XLS_Lines) elif GetOutputFormat() == 'JSON': JSON = WORKBOOK FillSheetJSON(JSON, NSX_Config) elif GetOutputFormat() == 'YAML': YAML = WORKBOOK FillSheetYAML(YAML, NSX_Config) else: FillSheet(WORKBOOK, TN_WS.title, TN_HEADER_ROW, XLS_Lines, "0072BA") ConditionnalFormat(TN_WS, 'C2:C' + str(len(XLS_Lines) + 1), 'UP')
def SheetSegments(auth_list,WORKBOOK,TN_WS,NSX_Config = {}): NSX_Config['Segments'] = [] Dict_LS = {} # NSX Connection SessionNSX = ConnectNSX(auth_list) stub_config = StubConfigurationFactory.new_std_configuration(SessionNSX[1]) ls_svc = LogicalSwitches(stub_config) ls_list = ls_svc.list() nb = len(ls_list.results) tz_svc = TransportZones(stub_config) tz_list = tz_svc.list() XLS_Lines = [] TN_HEADER_ROW = ('Segments', 'VNI', 'VLAN', 'Transport Zone Name', 'Transport Zone Type', 'Replication Mode', 'Admin State') if ls_list.result_count > 0: while True: for segment in ls_list.results: TZ_NAME = "" TZ_Type = "" for tz in tz_list.results: if segment.transport_zone_id == tz.id: TZ_NAME = tz.display_name TZ_Type = tz.transport_type Dict_LS['segment_name'] = segment.display_name Dict_LS['vni'] = segment.vni Dict_LS['vlan'] = segment.vlan Dict_LS['tz_name'] = TZ_NAME Dict_LS['tz_type'] = TZ_Type Dict_LS['replication_mode'] = segment.replication_mode Dict_LS['status'] = segment.admin_state NSX_Config['Segments'].append(Dict_LS) XLS_Lines.append([segment.display_name, segment.vni, segment.vlan, TZ_NAME, TZ_Type, segment.replication_mode,segment.admin_state]) if ls_list.cursor is None: break else: print(" --> more than " + str(nb) + " results for " + style.RED + "Segments" + style.NORMAL + " - please wait") ls_list = LogicalSwitches(stub_config).list(cursor =ls_list.cursor ) nb = len(ls_list.results) + nb else: XLS_Lines.append(["no Segments", "", "", "", "", "",""]) if GetOutputFormat() == 'CSV': CSV = WORKBOOK FillSheetCSV(CSV,TN_HEADER_ROW,XLS_Lines) elif GetOutputFormat() == 'JSON': JSON = WORKBOOK FillSheetJSON(JSON, NSX_Config) elif GetOutputFormat() == 'YAML': YAML = WORKBOOK FillSheetYAML(YAML, NSX_Config) else: FillSheet(WORKBOOK,TN_WS.title,TN_HEADER_ROW,XLS_Lines,"0072BA") ConditionnalFormat(TN_WS, 'G2:G' + str(len(XLS_Lines) + 1), 'UP')
def SheetSegments(auth_list, WORKBOOK, TN_WS, NSX_Config={}): NSX_Config['Segments'] = [] Dict_LS = {} # Connect to NSX SessionNSX = ConnectNSX(auth_list) segments_url = '/api/v1/logical-switches' segments_json = GetAPI(SessionNSX[0], segments_url, auth_list) tz_url = '/policy/api/v1/infra/sites/default/enforcement-points/default/transport-zones' tz_json = GetAPI(SessionNSX[0], tz_url, auth_list) XLS_Lines = [] TN_HEADER_ROW = ('Segments', 'VNI', 'VLAN', 'Transport Zone Name', 'Transport Zone Type', 'Replication Mode', 'Admin State') # Check if Segements present if isinstance( segments_json, dict ) and 'results' in segments_json and segments_json['result_count'] > 0: for segment in segments_json['results']: TZ_NAME = "" TZ_Type = "" Dict_LS['vni'] = '' Dict_LS['vlan'] = '' for tz in tz_json['results']: if segment['transport_zone_id'] == tz['id']: TZ_NAME = tz['display_name'] TZ_Type = tz['tz_type'] break Dict_LS['segment_name'] = segment['display_name'] if 'vni' in segment: Dict_LS['vni'] = segment['vni'] if 'vlan' in segment: Dict_LS['vlan'] = segment['vlan'] Dict_LS['tz_name'] = TZ_NAME Dict_LS['tz_type'] = TZ_Type if 'replication_mode' in segment: Dict_LS['replication_mode'] = segment['replication_mode'] Dict_LS['status'] = segment['admin_state'] NSX_Config['Segments'].append(Dict_LS) XLS_Lines.append([ Dict_LS['segment_name'], Dict_LS['vni'], Dict_LS['vlan'], TZ_NAME, TZ_Type, Dict_LS['replication_mode'], segment['admin_state'] ]) else: XLS_Lines.append(["no Segments", "", "", "", "", "", ""]) if GetOutputFormat() == 'CSV': CSV = WORKBOOK FillSheetCSV(CSV, TN_HEADER_ROW, XLS_Lines) elif GetOutputFormat() == 'JSON': JSON = WORKBOOK FillSheetJSON(JSON, NSX_Config) elif GetOutputFormat() == 'YAML': YAML = WORKBOOK FillSheetYAML(YAML, NSX_Config) else: FillSheet(WORKBOOK, TN_WS.title, TN_HEADER_ROW, XLS_Lines, "0072BA") ConditionnalFormat(TN_WS, 'G2:G' + str(len(XLS_Lines) + 1), 'UP')
def SheetAlarms(auth_list, WORKBOOK, TN_WS, NSX_Config={}): Dict_Alarm = {} # Dict alarm initialization NSX_Config['Alarms'] = [] # Connect NSX SessionNSX = ConnectNSX(auth_list) TN_json = GetAPI(SessionNSX[0], '/api/v1/transport-nodes', auth_list) Edge_json = GetAPI(SessionNSX[0], '/api/v1/cluster/nodes', auth_list) stub_config = StubConfigurationFactory.new_std_configuration(SessionNSX[1]) node_dict = {} # Construct Dicts of Edge Node and Transport Node for Name from ID if TN_json['result_count'] > 0: for TN in TN_json['results']: node_dict.update({TN["id"]: TN["display_name"]}) if Edge_json['result_count'] > 0: for EDGE in Edge_json['results']: node_dict.update({EDGE["id"]: EDGE["display_name"]}) # Alarms extract alarms_svc = Alarms(stub_config) alarms_list = alarms_svc.list() NodeName = "" XLS_Lines = [] TN_HEADER_ROW = ('Feature', 'Event Type', 'Reporting Node', 'Node Ressource Type', 'Entity Name', 'Severity', 'Last Reported Time', 'Status', 'Description', 'Recommended Action') if alarms_list.result_count > 0: for alarm in alarms_list.results: # Get Name of Node from ID for key, value in node_dict.items(): if key == alarm.entity_id: NodeName = value # Transform date and time of alarms dtt = datetime.datetime.fromtimestamp( float(alarm.last_reported_time / 1000)).strftime('%Y-%m-%d %H:%M:%S') # Create line XLS_Lines.append([ alarm.feature_name, alarm.event_type, NodeName, alarm.node_resource_type, alarm.entity_id, alarm.severity, dtt, alarm.status, alarm.description, alarm.recommended_action ]) # Fill alarm Dict Dict_Alarm['feature_name'] = alarm.feature_name Dict_Alarm['event_type'] = alarm.event_type Dict_Alarm['node_name'] = NodeName Dict_Alarm['node_resource_type'] = alarm.node_resource_type Dict_Alarm['entity_id'] = alarm.entity_id Dict_Alarm['severity'] = alarm.severity Dict_Alarm['time'] = dtt Dict_Alarm['status'] = alarm.status Dict_Alarm['description'] = alarm.description Dict_Alarm['recommended_action'] = alarm.recommended_action NSX_Config['Alarms'].append(Dict_Alarm) else: XLS_Lines.append(['No results', "", "", "", "", "", "", "", "", ""]) # Create sheet FillSheet(WORKBOOK, TN_WS.title, TN_HEADER_ROW, XLS_Lines, "0072BA") ConditionnalFormat(TN_WS, 'F', 'CRITICAL', True, 'RED') ConditionnalFormat(TN_WS, 'F', 'HIGH', True, 'ORANGE') ConditionnalFormat(TN_WS, 'H', 'RESOLVED', True, 'GREEN')
def SheetBGPSession(auth_list, WORKBOOK, TN_WS, NSX_Config={}): NSX_Config['T0Sessions'] = [] Dict_Sessions = {} SessionNSX = ConnectNSX(auth_list) ########### GET Tier-0 Gateways ########### t0_url = '/policy/api/v1/infra/tier-0s' t0_json = GetAPI(SessionNSX[0], t0_url, auth_list) XLS_Lines = [] TN_HEADER_ROW = ('T0', 'BGP status', 'ECMP', 'Inter-SR', 'Source IP address', 'Local AS', 'Neighbor IP address', 'Remote AS', 'Total IN Prefixes', 'Total OUT prefixes', 'Session Status') if isinstance( t0_json, dict) and 'results' in t0_json and t0_json['result_count'] > 0: for t0 in t0_json["results"]: #localservice_url = "/policy/api/v1/infra/tier-0s/"+ t0['display_name'] +"/locale-services" localservice = "default" bgpstatus_url = "/policy/api/v1/infra/tier-0s/" + t0[ 'display_name'] + "/locale-services/" + localservice + "/bgp/neighbors/status" bgpconfig_url = "/policy/api/v1/infra/tier-0s/" + t0[ 'display_name'] + "/locale-services/" + localservice + "/bgp" #t0_localservice = GetAPI(SessionNSX[0],localservice_url, auth_list) bgpstatus_json = GetAPI(SessionNSX[0], bgpstatus_url, auth_list) bgpconfig_json = GetAPI(SessionNSX[0], bgpconfig_url, auth_list) Dict_Sessions['T0_name'] = t0['display_name'] # BGP Sessions treatment if isinstance(bgpstatus_json, dict) and 'results' in bgpstatus_json: Dict_Sessions['bgp_sessions'] = [] Dict_bgp_session = {} if 'local_as_num' in bgpconfig_json: Dict_bgp_session['local_as'] = str( bgpconfig_json['local_as_num']) else: Dict_bgp_session['local_as'] = "" if 'inter_sr_ibgp' in bgpconfig_json: Dict_bgp_session['inter_sr_ibgp'] = str( bgpconfig_json['inter_sr_ibgp']).upper() else: Dict_bgp_session['inter_sr_ibgp'] = '' Dict_bgp_session['bgp_status'] = str( bgpconfig_json['enabled']).upper() Dict_bgp_session['ecmp'] = str(bgpconfig_json['ecmp']).upper() for session in bgpstatus_json['results']: Dict_bgp_session['source_ip'] = session['source_address'] Dict_bgp_session['remote_ip'] = session['neighbor_address'] Dict_bgp_session['remote_as'] = str( session['remote_as_number']) Dict_bgp_session['total_in'] = str( session['total_in_prefix_count']) Dict_bgp_session['total_out'] = str( session['total_out_prefix_count']) Dict_bgp_session['connection_state'] = session[ 'connection_state'] Dict_Sessions['bgp_sessions'].append(Dict_bgp_session) XLS_Lines.append([ Dict_Sessions['T0_name'], Dict_bgp_session['bgp_status'], Dict_bgp_session['ecmp'], Dict_bgp_session['inter_sr_ibgp'], Dict_bgp_session['source_ip'], Dict_bgp_session['local_as'], Dict_bgp_session['remote_ip'], Dict_bgp_session['remote_as'], Dict_bgp_session['total_in'], Dict_bgp_session['total_out'], Dict_bgp_session['connection_state'] ]) elif not bgpstatus_json: XLS_Lines.append([ t0['display_name'], "No BGP sessions", "", "", "", "", "", "", "", "", "" ]) else: XLS_Lines.append([ t0['display_name'], " No BGP sessions", "", "", "", "", "", "", "", "", "" ]) NSX_Config['T0Sessions'].append(Dict_Sessions) print(" --> Get BGP sessions for " + style.ORANGE + t0['display_name'] + style.NORMAL) else: XLS_Lines.append( ["No T0 router found", "", "", "", "", "", "", "", "", "", ""]) if GetOutputFormat() == 'CSV': CSV = WORKBOOK FillSheetCSV(CSV, TN_HEADER_ROW, XLS_Lines) elif GetOutputFormat() == 'JSON': JSON = WORKBOOK FillSheetJSON(JSON, NSX_Config) elif GetOutputFormat() == 'YAML': YAML = WORKBOOK FillSheetYAML(YAML, NSX_Config) else: FillSheet(WORKBOOK, TN_WS.title, TN_HEADER_ROW, XLS_Lines, "0072BA") ConditionnalFormat(TN_WS, 'K2:K' + str(len(XLS_Lines) + 1), 'ESTABLISHED') ConditionnalFormat(TN_WS, 'B2:B' + str(len(XLS_Lines) + 1), 'TRUE')
def SheetFabDiscoveredNodes(auth_list, WORKBOOK, TN_WS, NSX_Config={}): SessionNSX = ConnectNSX(auth_list) stub_config = StubConfigurationFactory.new_std_configuration(SessionNSX[1]) disc_node_list = DiscoveredNodes(stub_config).list() Dict_DiscoveredNodes = {} # Dict Discovered nodes initialization NSX_Config['DiscoveredNodes'] = [] # Construct Line XLS_Lines = [] if disc_node_list.result_count > 0: TN_HEADER_ROW = ('Display name', 'OS Type', 'OS Version', 'Node Type', 'Hostname', 'Full Name', 'Management IP', 'Domain name', 'DNS', 'UUID', 'Powerstate', 'In Maintenance Mode', 'Build', 'Vendor', 'Model', 'Serial Number', 'Connection State', 'Licensed Product Name', 'Licensed Product Version', 'Mgmt Server IP', 'Lockdown Mode', 'DAS Host State') for node in disc_node_list.results: Dict_Properties = {} # Loop in properties for propertie in node.origin_properties: if propertie.key == 'hostName': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'fullName': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'managementIp': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'domainName': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'dnsConfigAddress': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'uuid': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'powerState': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'inMaintenanceMode': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'build': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'vendor': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'model': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'serialNumber': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'connectionState': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'licenseProductName': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'licenseProductVersion': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'managementServerIp': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'lockdownMode': Dict_Properties[propertie.key] = propertie.value if propertie.key == 'dasHostState': Dict_Properties[propertie.key] = propertie.value # Fill Discovered Nodes Dict Dict_DiscoveredNodes['node_name'] = node.display_name Dict_DiscoveredNodes['event_type'] = node.os_type Dict_DiscoveredNodes['node_name'] = node.os_version Dict_DiscoveredNodes['node_resource_type'] = node.node_type Dict_DiscoveredNodes['hostName'] = Dict_Properties['hostName'] Dict_DiscoveredNodes['fullName'] = Dict_Properties['fullName'] Dict_DiscoveredNodes['managementIp'] = Dict_Properties[ 'managementIp'] Dict_DiscoveredNodes['domainName'] = Dict_Properties['domainName'] Dict_DiscoveredNodes['dnsConfigAddress'] = Dict_Properties[ 'dnsConfigAddress'] Dict_DiscoveredNodes['uuid'] = Dict_Properties['uuid'] Dict_DiscoveredNodes['powerState'] = Dict_Properties['powerState'] Dict_DiscoveredNodes['inMaintenanceMode'] = Dict_Properties[ 'inMaintenanceMode'] Dict_DiscoveredNodes['build'] = Dict_Properties['build'] Dict_DiscoveredNodes['vendor'] = Dict_Properties['vendor'] Dict_DiscoveredNodes['model'] = Dict_Properties['model'] Dict_DiscoveredNodes['serialNumber'] = Dict_Properties[ 'serialNumber'] Dict_DiscoveredNodes['connectionState'] = Dict_Properties[ 'connectionState'] Dict_DiscoveredNodes['licenseProductName'] = Dict_Properties[ 'licenseProductName'] Dict_DiscoveredNodes['licenseProductVersion'] = Dict_Properties[ 'licenseProductVersion'] Dict_DiscoveredNodes['managementServerIp'] = Dict_Properties[ 'managementServerIp'] Dict_DiscoveredNodes['lockdownMode'] = Dict_Properties[ 'lockdownMode'] Dict_DiscoveredNodes['dasHostState'] = Dict_Properties[ 'dasHostState'] NSX_Config['DiscoveredNodes'].append(Dict_DiscoveredNodes) # write one line for a node XLS_Lines.append([ node.display_name, node.os_type, node.os_version, node.node_type, Dict_Properties['hostName'], Dict_Properties['fullName'], Dict_Properties['managementIp'], Dict_Properties['domainName'], Dict_Properties['dnsConfigAddress'], Dict_Properties['uuid'], Dict_Properties['powerState'], Dict_Properties['inMaintenanceMode'], Dict_Properties['build'], Dict_Properties['vendor'], Dict_Properties['model'], Dict_Properties['serialNumber'], Dict_Properties['connectionState'], Dict_Properties['licenseProductName'], Dict_Properties['licenseProductVersion'], Dict_Properties['managementServerIp'], Dict_Properties['lockdownMode'], Dict_Properties['dasHostState'] ]) else: XLS_Lines = ('No result', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '') FillSheet(WORKBOOK, TN_WS.title, TN_HEADER_ROW, XLS_Lines, "0072BA") ConditionnalFormat(TN_WS, 'K2:K' + str(len(XLS_Lines) + 1), 'poweredOn') ConditionnalFormat(TN_WS, 'L2:L' + str(len(XLS_Lines) + 1), 'false') ConditionnalFormat(TN_WS, 'Q2:Q' + str(len(XLS_Lines) + 1), 'connected')
def SheetAlarms(auth_list, WORKBOOK, TN_WS, NSX_Config={}): Dict_Alarm = {} # Dict alarm initialization NSX_Config['Alarms'] = [] # Connect NSX SessionNSX = ConnectNSX(auth_list) TN_json = GetAPI(SessionNSX[0], '/api/v1/transport-nodes', auth_list) Edge_json = GetAPI(SessionNSX[0], '/api/v1/cluster/nodes', auth_list) alarms_url = '/api/v1/alarms' alarms_json = GetAPI(SessionNSX[0], alarms_url, auth_list) nb = alarms_json['result_count'] node_dict = {} # Construct Dicts of Edge Node and Transport Node for Name from ID if TN_json['result_count'] > 0: for TN in TN_json['results']: node_dict.update({TN["id"]: TN["display_name"]}) if Edge_json['result_count'] > 0: for EDGE in Edge_json['results']: node_dict.update({EDGE["id"]: EDGE["display_name"]}) NodeName = "" XLS_Lines = [] TN_HEADER_ROW = ('Feature', 'Event Type', 'Reporting Node', 'Node Ressource Type', 'Entity Name', 'Severity', 'Last Reported Time', 'Status', 'Description', 'Recommended Action') if isinstance( alarms_json, dict ) and 'results' in alarms_json and alarms_json['result_count'] > 0: for alarm in alarms_json['results']: # Get Name of Node from ID for key, value in node_dict.items(): if key == alarm['entity_id']: NodeName = value # Transform date and time of alarms dtt = datetime.datetime.fromtimestamp( float(alarm['last_reported_time'] / 1000)).strftime('%Y-%m-%d %H:%M:%S') # Create line XLS_Lines.append([ alarm['feature_name'], alarm['event_type'], NodeName, alarm['node_resource_type'], alarm['entity_id'], alarm['severity'], dtt, alarm['status'], alarm['description'], alarm['recommended_action'] ]) # Fill alarm Dict Dict_Alarm['feature_name'] = alarm['feature_name'] Dict_Alarm['event_type'] = alarm['event_type'] Dict_Alarm['node_name'] = NodeName Dict_Alarm['node_resource_type'] = alarm['node_resource_type'] Dict_Alarm['entity_id'] = alarm['entity_id'] Dict_Alarm['severity'] = alarm['severity'] Dict_Alarm['time'] = dtt Dict_Alarm['status'] = alarm['status'] Dict_Alarm['description'] = alarm['description'] Dict_Alarm['recommended_action'] = alarm['recommended_action'] NSX_Config['Alarms'].append(Dict_Alarm) else: XLS_Lines.append(['No results', "", "", "", "", "", "", "", "", ""]) # Create sheet if GetOutputFormat() == 'CSV': CSV = WORKBOOK FillSheetCSV(CSV, TN_HEADER_ROW, XLS_Lines) elif GetOutputFormat() == 'JSON': JSON = WORKBOOK FillSheetJSON(JSON, NSX_Config) elif GetOutputFormat() == 'YAML': YAML = WORKBOOK FillSheetYAML(YAML, NSX_Config) else: FillSheet(WORKBOOK, TN_WS.title, TN_HEADER_ROW, XLS_Lines, "0072BA") ConditionnalFormat(TN_WS, 'F', 'CRITICAL', True, 'RED') ConditionnalFormat(TN_WS, 'F', 'HIGH', True, 'ORANGE') ConditionnalFormat(TN_WS, 'H', 'RESOLVED', True, 'GREEN')
def SheetRouterPorts(auth_list, WORKBOOK, TN_WS, NSX_Config={}): if 'LRPorts' not in NSX_Config: NSX_Config['LRPorts'] = [] Dict_Ports = {} SessionNSX = ConnectNSX(auth_list) ########### GET LogicalPortDownLink ########### LRports_Down_url = '/api/v1/search/query?query=resource_type:LogicalRouterDownLinkPort' LRports_Down_json = GetAPI(SessionNSX[0], LRports_Down_url, auth_list) ########### GET Logical Routers ########### lr_ports_url = '/api/v1/search/query?query=resource_type:LogicalPort' lr_ports_json = GetAPI(SessionNSX[0], lr_ports_url, auth_list) ########### GET Logical Routers ########### lr_list_url = '/api/v1/logical-routers' lr_list_json = GetAPI(SessionNSX[0], lr_list_url, auth_list) ########### CREATE LIST OF TUPLES - EDGE-ID / EDGE NAME ########### lswitch_list = [] XLS_Lines = [] TN_HEADER_ROW = ('LR Port Name', 'ID', 'Attachment Type', 'Logical Router Name', 'Attachment ID', 'Logical Switch ID', 'Logical Switch', 'Create User', 'Admin State', 'Status') ########### GET Logical-Switches ########### lswitch_url = '/api/v1/logical-switches' lswitch_json = GetAPI(SessionNSX[0], lswitch_url, auth_list) for i in lswitch_json["results"]: lswitch_list.append(tuple((i['id'], i['display_name']))) if lr_ports_json['result_count'] > 0: for port in lr_ports_json["results"]: # Check is attachment key is in Dict if 'attachment' in port: Attachement_type = port['attachment']['attachment_type'] Attachement_ID = port['attachment']['id'] else: Attachement_type = 'No Attachment' Attachement_ID = 'No Attachment' # Get the name of LS LS_Name = "" LR_Name = "" for ls in lswitch_list: if port['logical_switch_id'] == ls[0]: LS_Name = ls[1] # Get Router Name for lr in LRports_Down_json['results']: if 'linked_logical_switch_port_id' in lr: if port['id'] == lr[ 'linked_logical_switch_port_id'][ 'target_id']: for router in lr_list_json['results']: if lr['logical_router_id'] == router['id']: LR_Name = router['display_name'] Dict_Ports['name'] = port['display_name'] Dict_Ports['state'] = port['admin_state'] Dict_Ports['create_user'] = port['_create_user'] Dict_Ports['router'] = LR_Name Dict_Ports['id'] = port['id'] Dict_Ports['att_type'] = Attachement_type Dict_Ports['att_id'] = Attachement_ID Dict_Ports['LS_id'] = port['logical_switch_id'] Dict_Ports['LS_name'] = LS_Name Dict_Ports['status'] = port['status']['status'] NSX_Config['LRPorts'].append(Dict_Ports) XLS_Lines.append([ port['display_name'], port['id'], Attachement_type, LR_Name, Attachement_ID, port['logical_switch_id'], LS_Name, port['_create_user'], port['admin_state'], port['status']['status'] ]) else: XLS_Lines.append(['No results', "", "", "", "", "", "", "", ""]) if GetOutputFormat() == 'CSV': CSV = WORKBOOK FillSheetCSV(CSV, TN_HEADER_ROW, XLS_Lines) elif GetOutputFormat() == 'JSON': JSON = WORKBOOK FillSheetJSON(JSON, NSX_Config) elif GetOutputFormat() == 'YAML': YAML = WORKBOOK FillSheetYAML(YAML, NSX_Config) else: FillSheet(WORKBOOK, TN_WS.title, TN_HEADER_ROW, XLS_Lines, "0072BA") if len(XLS_Lines) > 0: ConditionnalFormat(TN_WS, 'J2:J' + str(len(XLS_Lines) + 1), 'UP') ConditionnalFormat(TN_WS, 'I2:I' + str(len(XLS_Lines) + 1), 'UP')
def SheetFabDiscoveredNodes(auth_list, WORKBOOK, TN_WS, NSX_Config={}): Dict_DiscoveredNodes = {} # Dict Discovered nodes initialization NSX_Config['DiscoveredNodes'] = [] # Connect to NSX SessionNSX = ConnectNSX(auth_list) discovered_nodes_url = '/api/v1/fabric/discovered-nodes' discovered_nodes_json = GetAPI(SessionNSX[0], discovered_nodes_url, auth_list) # Construct Line TN_HEADER_ROW = ('Display name', 'OS Type', 'OS Version', 'Node Type', 'Hostname', 'Full Name', 'Management IP', 'Domain name', 'DNS', 'UUID', 'Powerstate', 'In Maintenance Mode', 'Build', 'Vendor', 'Model', 'Serial Number', 'Connection State', 'Licensed Product Name', 'Licensed Product Version', 'Mgmt Server IP', 'Lockdown Mode', 'DAS Host State') XLS_Lines = [] # Check if Discovered Nodes present if isinstance( discovered_nodes_json, dict ) and 'results' in discovered_nodes_json and discovered_nodes_json[ 'result_count'] > 0: for node in discovered_nodes_json['results']: Dict_Properties = {} # Loop in properties for propertie in node['origin_properties']: if propertie['key'] == 'hostName': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'fullName': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'managementIp': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'domainName': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'dnsConfigAddress': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'uuid': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'powerState': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'inMaintenanceMode': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'build': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'vendor': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'model': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'serialNumber': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'connectionState': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'licenseProductName': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'licenseProductVersion': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'managementServerIp': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'lockdownMode': Dict_Properties[propertie['key']] = propertie['value'] if propertie['key'] == 'dasHostState': Dict_Properties[propertie['key']] = propertie['value'] # Fill Discovered Nodes Dict Dict_DiscoveredNodes['node_name'] = node['display_name'] Dict_DiscoveredNodes['event_type'] = node['os_type'] Dict_DiscoveredNodes['node_name'] = node['os_version'] Dict_DiscoveredNodes['node_resource_type'] = node['node_type'] Dict_DiscoveredNodes['hostName'] = Dict_Properties['hostName'] Dict_DiscoveredNodes['fullName'] = Dict_Properties['fullName'] Dict_DiscoveredNodes['managementIp'] = Dict_Properties[ 'managementIp'] try: Dict_DiscoveredNodes['domainName'] = Dict_Properties[ 'domainName'] except: Dict_Properties['domainName'] = 'No Domain Name' Dict_DiscoveredNodes['dnsConfigAddress'] = Dict_Properties[ 'dnsConfigAddress'] Dict_DiscoveredNodes['uuid'] = Dict_Properties['uuid'] Dict_DiscoveredNodes['powerState'] = Dict_Properties['powerState'] Dict_DiscoveredNodes['inMaintenanceMode'] = Dict_Properties[ 'inMaintenanceMode'] Dict_DiscoveredNodes['build'] = Dict_Properties['build'] Dict_DiscoveredNodes['vendor'] = Dict_Properties['vendor'] Dict_DiscoveredNodes['model'] = Dict_Properties['model'] Dict_DiscoveredNodes['serialNumber'] = Dict_Properties[ 'serialNumber'] Dict_DiscoveredNodes['connectionState'] = Dict_Properties[ 'connectionState'] Dict_DiscoveredNodes['licenseProductName'] = Dict_Properties[ 'licenseProductName'] Dict_DiscoveredNodes['licenseProductVersion'] = Dict_Properties[ 'licenseProductVersion'] Dict_DiscoveredNodes['managementServerIp'] = Dict_Properties[ 'managementServerIp'] Dict_DiscoveredNodes['lockdownMode'] = Dict_Properties[ 'lockdownMode'] Dict_DiscoveredNodes['dasHostState'] = Dict_Properties[ 'dasHostState'] NSX_Config['DiscoveredNodes'].append(Dict_DiscoveredNodes) # write one line for a node XLS_Lines.append([ node['display_name'], node['os_type'], node['os_version'], node['node_type'], Dict_Properties['hostName'], Dict_Properties['fullName'], Dict_Properties['managementIp'], Dict_Properties['domainName'], Dict_Properties['dnsConfigAddress'], Dict_Properties['uuid'], Dict_Properties['powerState'], Dict_Properties['inMaintenanceMode'], Dict_Properties['build'], Dict_Properties['vendor'], Dict_Properties['model'], Dict_Properties['serialNumber'], Dict_Properties['connectionState'], Dict_Properties['licenseProductName'], Dict_Properties['licenseProductVersion'], Dict_Properties['managementServerIp'], Dict_Properties['lockdownMode'], Dict_Properties['dasHostState'] ]) else: XLS_Lines.append([ 'No result', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ]) if GetOutputFormat() == 'CSV': CSV = WORKBOOK FillSheetCSV(CSV, TN_HEADER_ROW, XLS_Lines) elif GetOutputFormat() == 'JSON': JSON = WORKBOOK FillSheetJSON(JSON, NSX_Config) elif GetOutputFormat() == 'YAML': YAML = WORKBOOK FillSheetYAML(YAML, NSX_Config) else: FillSheet(WORKBOOK, TN_WS.title, TN_HEADER_ROW, XLS_Lines, "0072BA") ConditionnalFormat(TN_WS, 'K2:K' + str(len(XLS_Lines) + 1), 'poweredOn') ConditionnalFormat(TN_WS, 'L2:L' + str(len(XLS_Lines) + 1), 'false') ConditionnalFormat(TN_WS, 'Q2:Q' + str(len(XLS_Lines) + 1), 'connected')