Skip to content

Commit c1b9b44

Browse files
AndryNick98ddutt
andauthored
Fix iosxe ospf nbr time (#940)
* iosxe ospfnbr: change regex used to pull uptime Signed-off-by: Dinesh Dutt <[email protected]> * poller/ospfNbr: fix IOSXE uptime parsing Signed-off-by: Dinesh Dutt <[email protected]> * utils.py: add support for ios/xe time parsing Signed-off-by: Dinesh Dutt <[email protected]> * shared/utils: fixed linting Signed-off-by: AndryNick98 <[email protected]> --------- Signed-off-by: Dinesh Dutt <[email protected]> Signed-off-by: AndryNick98 <[email protected]> Co-authored-by: Dinesh Dutt <[email protected]>
1 parent 76218cc commit c1b9b44

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

suzieq/config/textfsm_templates/iosxe_show_ip_ospfnbr.tfsm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Value ifname (\S+)
77
Value nbrPrio (\d+)
88
Value state (\w+)
99
Value numChanges (\d+)
10-
Value lastUpTime (\w+)
10+
Value lastUpTime (.+)
1111
Value lastDownTime (\w+)
1212
Value lsaRetxCnt (\d+)
1313
Value bfdStatus (\S+)

suzieq/poller/worker/services/ospfNbr.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import numpy as np
44

55
from suzieq.poller.worker.services.service import Service
6-
from suzieq.shared.utils import get_timestamp_from_cisco_time
7-
from suzieq.shared.utils import get_timestamp_from_junos_time
6+
from suzieq.shared.utils import (get_timestamp_from_cisco_time,
7+
get_timestamp_from_iosxe_time,
8+
get_timestamp_from_junos_time)
89

910

1011
class OspfNbrService(Service):
@@ -136,15 +137,16 @@ def _clean_nxos_data(self, processed_data, raw_data):
136137
return processed_data
137138

138139
def _clean_ios_data(self, processed_data, raw_data):
140+
rel_timestamp = raw_data[0]['cmd_timestamp']/1000
139141
for entry in processed_data:
140142
# make area the dotted model
141143
area = entry.get('area', '')
142144
if area.isdecimal():
143145
entry['area'] = str(ip_address(int(area)))
144146
entry['state'] = entry['state'].lower()
145-
entry['lastUpTime'] = get_timestamp_from_cisco_time(
146-
entry['lastUpTime'], raw_data[0]['timestamp']/1000)
147-
entry['lastChangeTime'] = entry['lastUpTime']
147+
up_ts: int = get_timestamp_from_iosxe_time(entry['lastUpTime'],
148+
rel_timestamp)
149+
entry['lastChangeTime'] = up_ts*1000
148150
entry['lastDownTime'] = 0
149151
entry['lsaRtxCnt'] = int(entry['lsaRetxCnt'])
150152
entry['areaStub'] = entry['areaStub'] == 'Stub'

suzieq/shared/utils.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from logging.handlers import RotatingFileHandler
1616
from os import getenv
1717
from time import time
18-
from typing import Any, Dict, List, Tuple
18+
from typing import Any, Dict, List, Optional, Union
1919

2020
import pandas as pd
2121
import psutil
@@ -386,9 +386,13 @@ def calc_avg(oldval, newval):
386386

387387

388388
def parse_relative_timestamp(
389-
uptime: str, relative_to: int = None, ms=False) -> int:
390-
"""Get a relative time (i.e. with format 10 weeks, 4 days, 3 hours 11 mins)
391-
and convert it into a timestamp.
389+
uptime: str, relative_to: Optional[int] = None,
390+
ms=False) -> Optional[int]:
391+
"""Convert a string of relative time into a timestamp
392+
393+
This takes dateparser parsable strings such as
394+
'10 weeks, 4 days, 3 hours 11 mins' or '1w4d' or '00h05m040s'
395+
and converts it into a timestamp.
392396
393397
Args:
394398
uptime (str): _description_
@@ -477,8 +481,32 @@ def get_timestamp_from_cisco_time(in_data: str, timestamp: int) -> int:
477481
return int((datetime.fromtimestamp(timestamp)-delta).timestamp()*1000)
478482

479483

480-
def get_timestamp_from_junos_time(in_data: Tuple[Dict, str],
481-
relative_to: int = None,
484+
def get_timestamp_from_iosxe_time(in_data: str, timestamp: int) -> int:
485+
"""Get timestamp in ms from the Cisco IOS/XE-specific timestamp string
486+
Examples of Cisco timestamp str are 1w4d, 1d10h, 01:20:35, 00:01:56.
487+
488+
Args:
489+
in_data (str): The IOSXE uptime string
490+
timestamp (int): the base unix timestamp IS SECONDS from which
491+
subtracting the uptime.
492+
493+
Returns:
494+
int: a unix timestamp in milliseconds
495+
"""
496+
if not in_data:
497+
return 0
498+
499+
if ':' in in_data:
500+
# There's time and so we need to convert it into h/m/s
501+
in_data = (in_data.replace(':', 'h', 1)
502+
.replace(':', 'm', 1)
503+
) + 's'
504+
505+
return parse_relative_timestamp(in_data, timestamp, False) or 0
506+
507+
508+
def get_timestamp_from_junos_time(in_data: Union[str, Dict[str, str]],
509+
relative_to: Optional[int] = None,
482510
ms=True) -> int:
483511
"""Get timestamp in ms from the Junos-specific timestamp string
484512
The expected input looks like: "attributes" : {"junos:seconds" : "0"}.

0 commit comments

Comments
 (0)