From 6bc241ffad8f9367179524aad6f6bec27c0f3d0f Mon Sep 17 00:00:00 2001 From: David Barroso Date: Mon, 14 Dec 2015 12:58:15 +0100 Subject: [PATCH] Moving utils inside napalm to avoid namespace collisions --- utils/__init__.py | 1 - utils/junos_views.py | 7 -- utils/junos_views.yml | 174 ---------------------------------------- utils/string_parsers.py | 99 ----------------------- 4 files changed, 281 deletions(-) delete mode 100644 utils/__init__.py delete mode 100755 utils/junos_views.py delete mode 100644 utils/junos_views.yml delete mode 100644 utils/string_parsers.py diff --git a/utils/__init__.py b/utils/__init__.py deleted file mode 100644 index 4b2198c..0000000 --- a/utils/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# TODO move utils folder inside napalm \ No newline at end of file diff --git a/utils/junos_views.py b/utils/junos_views.py deleted file mode 100755 index a8f014d..0000000 --- a/utils/junos_views.py +++ /dev/null @@ -1,7 +0,0 @@ -""" -Load tables/views -""" -from jnpr.junos.factory import loadyaml -from os.path import splitext -_YAML_ = splitext(__file__)[0] + '.yml' -globals().update(loadyaml(_YAML_)) diff --git a/utils/junos_views.yml b/utils/junos_views.yml deleted file mode 100644 index 18e215c..0000000 --- a/utils/junos_views.yml +++ /dev/null @@ -1,174 +0,0 @@ ---- - -#### -#### Interface table -#### - -junos_iface_table: - rpc: get-interface-information - args: - extensive: True - args_key: interface_name - item: physical-interface - view: junos_iface_view - -junos_iface_view: - fields: - is_up: { oper-status: True=up } - is_enabled: { admin-status: True=up } - description: { description: unicode } - last_flapped: { interface-flapped/@seconds: int } - #mode: logical-interface/address-family/address-family-name - speed: speed - mac_address: current-physical-address - -#### -#### BGP tables -#### - -junos_route_instance_table: - rpc: get-instance-information - item: instance-core - key: instance-name - view: junos_route_instance_view - -junos_route_instance_view: - fields: - name: instance-name - -junos_bgp_uptime_table: - rpc: get-bgp-summary-information - args: - instance: '' - item: bgp-peer - key: peer-address - view: junos_bgp_uptime_view - -junos_bgp_uptime_view: - fields: - uptime: { elapsed-time/@seconds: int } - tables: bgp-rib/name - -junos_bgp_table: - rpc: get-bgp-neighbor-information - item: bgp-peer - - key: peer-address - view: junos_bgp_view - -junos_bgp_view: - fields: - remote_as: { peer-as: int } - description: description - is_up: { peer-state: True=Established } - is_enabled: { peer-state: False=True } - received_prefixes: { bgp-rib/received-prefix-count: int } - accepted_prefixes: { bgp-rib/accepted-prefix-count: int } - sent_prefixes: { bgp-rib/advertised-prefix-count: int } - tables: bgp-rib/name - local_as: { local-as: int } - peer_as: { peer-as: int } - local_id: local-id - remote_id: { peer-id: unicode } - -#### -#### LLDP table -#### - -junos_lldp_table: - rpc: get-lldp-neighbors-information - item: lldp-neighbor-information - key: lldp-local-port-id | lldp-local-interface - view: junos_lldp_view - -junos_lldp_view: - fields: - hostname: lldp-remote-system-name - port: lldp-remote-port-description | lldp-remote-port-id - - - -#### -#### Interface counters -#### - -junos_iface_counter_table: - rpc: get-interface-information - args: - extensive: True - interface_name: '[vmfgx][mle]*' - args_key: interface_name - item: physical-interface - view: junos_iface_counter_view - - -junos_iface_counter_view: - groups: - ts: traffic-statistics - rxerrs: input-error-list - txerrs: output-error-list - ethernet_traffic_types: ethernet-mac-statistics - - - # fields that are part of groups are called - # "fields_" - fields_ethernet_traffic_types: - rx_broadcast_packets: { input-broadcasts: int } - tx_broadcast_packets: { output-broadcasts: int } - rx_unicast_packets: { input-unicasts: int } - tx_unicast_packets: { output-unicasts: int } - rx_multicast_packets: { input-multicasts: int } - tx_multicast_packets: { output-multicasts: int } - - fields_ts: - rx_octets: { input-bytes: int } - tx_octets: { output-bytes: int } - - fields_rxerrs: - rx_errors: { input-errors: int } - rx_discards: { input-discards: int } - - fields_txerrs: - tx_errors: { output-errors: int } - tx_discards: { output-drops: int } - -junos_enviroment_table: - rpc: get-environment-information - args: - item: environment-item - key: name - view: junos_enviroment_view - -junos_enviroment_view: - fields: - class: class - status: status - temperature: { temperature/@celsius: int } - -junos_routing_engine_table: - rpc: get-route-engine-information - args: - item: route-engine - key: slot - view: junos_routing_engine_view - -junos_routing_engine_view: - fields: - cpu-idle: { cpu-idle: int } - memory-dram-size: { memory-dram-size: int } - memory-buffer-utilization : { memory-buffer-utilization: int } - -junos_temperature_thresholds: - rpc: get-temperature-threshold-information - args: - item: temperature-threshold - key: name - view: junos_temperature_thresholds_view - -junos_temperature_thresholds_view: - fields: - name: name - yellow-alarm: { yellow-alarm: int } - red-alarm: { red-alarm: int } - tx_discards: { output-drops: int } - diff --git a/utils/string_parsers.py b/utils/string_parsers.py deleted file mode 100644 index 161d939..0000000 --- a/utils/string_parsers.py +++ /dev/null @@ -1,99 +0,0 @@ -import re - -def sorted_nicely( l ): - """ Sort the given iterable in the way that humans expect.""" - convert = lambda text: int(text) if text.isdigit() else text - alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] - return sorted(l, key = alphanum_key) - -def colon_separated_string_to_dict(string, separator=':'): - ''' - Converts a string in the format: - - Name: Et3 - Switchport: Enabled - Administrative Mode: trunk - Operational Mode: trunk - MAC Address Learning: enabled - Access Mode VLAN: 3 (VLAN0003) - Trunking Native Mode VLAN: 1 (default) - Administrative Native VLAN tagging: disabled - Administrative private VLAN mapping: ALL - Trunking VLANs Enabled: 2-3,5-7,20-21,23,100-200 - Trunk Groups: - - into a dictionary - - ''' - dictionary = dict() - - for line in string.splitlines(): - line_data = line.split(separator) - - if len(line_data) > 1: - dictionary[line_data[0].strip()] = ''.join(line_data[1:]).strip() - elif len(line_data) == 1: - dictionary[line_data[0].strip()] = None - else: - raise Exception('Something went wrong parsing the colo separated string {}'.format(line)) - - return dictionary - -def hyphen_range(string): - ''' - Expands a string of numbers separated by commas and hyphens into a list of integers. - For example: 2-3,5-7,20-21,23,100-200 - ''' - list_numbers = list() - temporary_list = string.split(',') - - for element in temporary_list: - sub_element = element.split('-') - - if len(sub_element) == 1: - list_numbers.append(int(sub_element[0])) - elif len(sub_element) == 2: - for x in range(int(sub_element[0]), int(sub_element[1])+1): - list_numbers.append(x) - else: - raise Exception('Something went wrong expanding the range'.format(string)) - - return list_numbers - -def convert_uptime_string_seconds(uptime): - ''' - Convert uptime strings to seconds. The string can be formatted various ways, eg. - 1 hour, 56 minutes - ''' - regex_1 = re.compile(r"((?P\d+) week(s)?,\s+)?((?P\d+) day(s)?,\s+)?((?P\d+) hour(s)?,\s+)?((?P\d+) minute(s)?)") - regex_2 = re.compile(r"((?P\d+)):((?P\d+)):((?P\d+))") - - regex_list = [regex_1, regex_2] - - uptime_dict = dict() - for regex in regex_list: - uptime_dict = regex.search(uptime) - if uptime_dict is not None: - uptime_dict = uptime_dict.groupdict() - break - uptime_dict = dict() - - uptime_seconds = 0 - - for unit, value in uptime_dict.iteritems(): - if value != None: - if unit == 'weeks': - uptime_seconds += int(value) * 604800 - elif unit == 'days': - uptime_seconds += int(value) * 86400 - elif unit == 'hours': - uptime_seconds += int(value) * 3600 - elif unit == 'minutes': - uptime_seconds += int(value) * 60 - elif unit == 'seconds': - uptime_seconds += int(value) - else: - raise Exception('Unrecognized unit "{}" in uptime:{}'.format(unit, uptime)) - - return uptime_seconds -