diff --git a/napalm/fortios.py b/napalm/fortios.py index 579aee3..f21807a 100644 --- a/napalm/fortios.py +++ b/napalm/fortios.py @@ -18,7 +18,7 @@ from pyFG.exceptions import FailedCommit, CommandExecutionException from base import NetworkDriver from exceptions import ReplaceConfigException, MergeConfigException -from utils.string_parsers import colon_separated_string_to_dict +from utils.string_parsers import colon_separated_string_to_dict, convert_uptime_string_seconds import re @@ -37,37 +37,6 @@ def execute_get(device, cmd, separator=':', auto=False): return colon_separated_string_to_dict('\n'.join(output), separator) -def convert_uptime_string_seconds(uptime): - regex_1 = re.compile(r"((?P\d+) days,\s+)?((?P\d+) hours,\s+)?((?P\d+) minutes)") - 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.match(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 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 - - class FortiOSDriver(NetworkDriver): def __init__(self, hostname, username, password): @@ -239,4 +208,4 @@ class FortiOSDriver(NetworkDriver): 'router_id': bgp_sum[0].split()[3], 'peers': peers } - } \ No newline at end of file + } diff --git a/utils/string_parsers.py b/utils/string_parsers.py index 1d71cd4..15e293f 100644 --- a/utils/string_parsers.py +++ b/utils/string_parsers.py @@ -39,7 +39,6 @@ def colon_separated_string_to_dict(string, separator=':'): return dictionary - def hyphen_range(string): ''' Expands a string of numbers separated by commas and hyphens into a list of integers. @@ -60,3 +59,40 @@ def hyphen_range(string): 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+) 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] + + # say whaaaat!? + uptime_dict = dict() + for regex in regex_list: + uptime_dict = regex.match(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 == '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 +