def get_mac_table(cls, client_object, switch_vni=None): """ Fetches the MAC table for the logical switch. @param switch_vni: VNI to identify the logical switch. @type switch_vni: int @return: Returns the MACTableSchema object. @rtype: mac_table_schema.MACTableSchema """ header_keys = ['Mac Address', 'VTEP Label'] nsxa_socket = cls._get_nsxa_socket(client_object) cmd = ('%s -t %s vni/mac-vtep-label %s' % (cls.CLI, nsxa_socket, switch_vni)) out = client_object.connection.request(cmd).response_data horizontal_parser = horizontal_table_parser.HorizontalTableParser() # Skip the VNI number in the output. mac_to_vtep = horizontal_parser.get_parsed_data( out, header_keys=header_keys, skip_head=1)['table'] py_dicts = [] for mac_vtep in mac_to_vtep: py_dict = {} vm_mac = mac_vtep['mac address'] vtep_label = mac_vtep['vtep label'] vtep_ip = cls.get_vtep_ip_by_label(client_object, label=vtep_label) py_dict['adapter_mac'] = vm_mac py_dict['adapter_ip'] = vtep_ip py_dicts.append(py_dict) py_dict = {'table': py_dicts} return mac_table_schema.MACTableSchema(py_dict=py_dict)
def get_arp_table(cls, client_object, switch_vni=None): """ Fetches the ARP table for the logical switch. @param switch_vni: VNI to identify the logical switch. @type switch_vni: int @return: Returns the ARPTableSchema object. @rtype: arp_table_schema.ARPTableSchema """ attribute_map = { 'mac address': 'adapter_mac', 'ip address': 'adapter_ip' } nsxa_socket = cls._get_nsxa_socket(client_object) cmd = ('%s -t %s vni/arp-table %s' % (cls.CLI, nsxa_socket, switch_vni)) out = client_object.connection.request(cmd).response_data.split('\n') # Skip the VNI number in the output. raw_table_data = '\n'.join(out[1:]) header_keys = ["IP Address", "Mac Address"] parser = horizontal_table_parser.HorizontalTableParser() parsed_data = parser.get_parsed_data(raw_table_data, header_keys=header_keys) mapped_pydict = utilities.map_attributes(attribute_map, parsed_data) return arp_table_schema.ARPTableSchema(py_dict=mapped_pydict)
def get_logical_switch(cls, client_object, get_logical_switch=None): """ Fetches logical switch information. """ _ = get_logical_switch header_keys = ['VNI', 'Controller IP Address', 'Link Status'] attribute_map = { 'vni': 'switch_vni', 'controller ip address': 'controller_ip', 'link status': 'controller_status' } nsxa_socket = cls._get_nsxa_socket(client_object) cmd = ('%s -t %s vni/list ' % (cls.CLI, nsxa_socket)) out = client_object.connection.request(cmd).response_data horizontal_parser = horizontal_table_parser.HorizontalTableParser() switch_dicts = horizontal_parser.get_parsed_data( out, header_keys=header_keys)['table'] for switch_dict in switch_dicts: replication_mode = cls.get_replication_mode( client_object, switch_vni=switch_dict['vni']) switch_dict['replication_mode'] = replication_mode for dict_key in switch_dict.keys(): switch_dict[dict_key] = switch_dict[dict_key].lower() mapped_pydict = utilities.map_attributes(attribute_map, {'table': switch_dicts}) return logical_switch_schema.LogicalSwitchSchema(py_dict=mapped_pydict)
def get_ipfix_capture_data(cls, client_object, file_name=None, read_filter=None, port=None, stop_capture=None, **kwargs): """ Fetches the IPFIX data captured by TShark. @type client_object: BaseClient @param client_object: Used to pass commands to the host. @type file_name: string @param file_name: Name/path of the capture file. @type read_filter: string @param read_filter: Read filter to use while reading the captured file. @type port: integer @param port: port for which captured traffic will be decoded as cflow. @type stop_capture: boolean @param stop_capture: Flag indicating whether an attempt should be made to stop the capture process first. """ attribute_map = { 'cflow.od_id': 'domain_id', 'cflow.packets64': 'pktcount', 'cflow.srcmac': 'src_mac', 'cflow.dstmac': 'dst_mac', 'cflow.dstaddr': 'dst_ip', 'cflow.srcaddr': 'src_ip', 'cflow.ip_version': 'ip_version', 'cflow.protocol': 'protocol', 'cflow.srcaddrv6': 'src_ipv6', 'cflow.dstaddrv6': 'dst_ipv6', 'ip.src': 'exporter_ip', 'cflow.octets64': 'octets', 'cflow.length_min': 'min_length', 'cflow.length_max': 'max_length', 'cflow.flowset_id': 'flowset_id' } if stop_capture: if not cls.stop_capture(client_object): pylogger.error('Failed to stop TShark capture process.') if port: read_filter = "-d udp.port==%s,cflow %s" % (port, read_filter) raw_data = cls.extract_capture_results(client_object, file_name=file_name, read_filter=read_filter) # Handle missing elements in a table by adding NULL string for parsing. while "\t\t" in raw_data: raw_data = raw_data.replace("\t\t", "\tNULL\t") while "\n\t" in raw_data: raw_data = raw_data.replace("\n\t", "\nNULL\t") parser = horizontal_table_parser.HorizontalTableParser() parsed_data = parser.get_parsed_data(raw_data) return utilities.map_attributes(attribute_map, parsed_data)
def get_vtep_label(cls, client_object, switch_vni=None): """ Fetches the VTEP labels for a given VNI. """ header_keys = ['VTEP Label'] nsxa_socket = cls._get_nsxa_socket(client_object) cmd = ('%s -t %s vni/vtep_list %s' % (cls.CLI, nsxa_socket, switch_vni)) out = client_object.connection.request(cmd).response_data horizontal_parser = horizontal_table_parser.HorizontalTableParser() # Skip the VNI number in the output. return horizontal_parser.get_parsed_data(out, header_keys=header_keys, skip_head=1)
def get_adapter_info(cls, client_object): """ Returns parsed data as dictionary for all vmknic that exists on the host. """ horizontal_parser = horizontal_table_parser.HorizontalTableParser() cmd = 'esxcfg-vmknic -l' header_keys = [ 'Interface', 'Port Group/DVPort', 'IP Family', 'IP Address', 'Netmask', 'Broadcast', 'MAC Address', 'MTU', 'TSO MSS', 'Enabled', 'Type' ] raw_data = client_object.connection.request(cmd).response_data horizontal_data = horizontal_parser.get_parsed_data( raw_data, expect_empty_fields=True, header_keys=header_keys)['table'] vertical_parser = vertical_table_parser.VerticalTableParser() cmd = 'esxcli network ip interface list' raw_data = client_object.connection.request(cmd).response_data vertical_data = vertical_parser.get_parsed_data( raw_data, lowercase_data=False)['table'] merged_info = [] for horizontal_info in horizontal_data: for key in vertical_data: if horizontal_info['interface'] == key: horizontal_info.update(vertical_data[key]) merged_info.append(horizontal_info) attribute_map = { 'port group/dvport': 'dvport', 'ip family': 'ip_family', 'ip address': 'ip', 'mac address': 'mac', 'tso mss': 'tso_mss', 'Netstack Instance': 'netstack' } mapped_pydicts = [] for info in merged_info: mapped_pydicts.append(utilities.map_attributes( attribute_map, info)) return {'table': mapped_pydicts}
def get_capture_data(cls, client_object, file_name=None, read_filter=None, stop_capture=None, attribute_map=None, **kwargs): """ Returns the data captured by TShark from a file specified in the input param file_name based on the filters specified by the user in the read_filter input. The attribute_map is a hash that could be optionally specified by the user to map the name of an entry in the captured data to a user defined name. Currently implementation will allow the data to be captured in tabular format only using -T fields option provided by TShark. @type client_object: BaseClient @param client_object: Used to pass commands to the host. @type file_name: string @param file_name: Name/path of the capture file. @type read_filter: string @param read_filter: Read filter to use while reading the captured file. something as '-T fields -E header=y -e ip.src -e ip.dst' @type stop_capture: boolean @param stop_capture: Flag indicating whether an attempt should be made to stop the capture process first. @type attribute_map: dict @param attribute_map: map captured tshark attributes to user defined attributes. """ # XXX(mbindal) Figure out other read filters in tshark packet capture. attribute_map = { 'ip.dst': 'dst_ip', 'ip.src': 'src_ip', 'eth.src': 'eth_src_mac', 'eth.dst': 'eth_dst_mac', 'arp.src.proto_ipv4': 'arp_src_ip', 'arp.dst.proto_ipv4': 'arp_dst_ip', 'arp.src.hw_mac': 'arp_src_mac', 'arp.dst.hw_mac': 'arp_dst_mac', 'vlan.id': 'vlan_id', 'udp.srcport': 'udp_src_port', 'udp.dstport': 'udp_dst_port', 'tcp.srcport': 'tcp_srcport', 'tcp.dstport': 'tcp_dstport' } if stop_capture: if not cls.stop_capture(client_object): raise AssertionError('Failed to stop TShark capture process.') raw_data = cls.extract_capture_results(client_object, file_name=file_name, read_filter=read_filter) if '-T ' not in read_filter: raise AssertionError('Tabular format with -T required in ' 'read_filter, got: %r' % read_filter) # Handle missing elements in a table by adding NULL string for parsing. while "\t\t" in raw_data: raw_data = raw_data.replace("\t\t", "\tNULL\t") while "\n\t" in raw_data: raw_data = raw_data.replace("\n\t", "\nNULL\t") parser = horizontal_table_parser.HorizontalTableParser() parsed_data = parser.get_parsed_data(raw_data) result = utilities.map_attributes(attribute_map, parsed_data) if 'table' in result: result['table'].append( {'pktcount': len(raw_data.strip().splitlines())}) return result
import vmware.interfaces.adapter_interface as adapter_interface import vmware.common.global_config as global_config import vmware.common.utilities as utilities import vmware.parsers.horizontal_table_parser as horizontal_table_parser import vmware.parsers.vertical_table_parser as vertical_table_parser import vmware.schema.switch.vtep_table_schema as vtep_table_schema pylogger = global_config.pylogger horizontal_parser = horizontal_table_parser.HorizontalTableParser() vertical_parser = vertical_table_parser.VerticalTableParser() class ESX55AdapterImpl(adapter_interface.AdapterInterface): @classmethod def get_adapter_info(cls, client_object): """ Returns parsed data as dictionary for all vmknic that exists on the host. """ cmd = 'esxcfg-vmknic -l' header_keys = [ 'Interface', 'Port Group/DVPort', 'IP Family', 'IP Address', 'Netmask', 'Broadcast', 'MAC Address', 'MTU', 'TSO MSS', 'Enabled', 'Type' ] raw_data = client_object.connection.request(cmd).response_data horizontal_data = horizontal_parser.get_parsed_data( raw_data, expect_empty_fields=True, header_keys=header_keys)['table'] cmd = 'esxcli network ip interface list'