Skip to content

Commit

Permalink
Fix iosxe ospf nbr time (#940)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
AndryNick98 and ddutt authored May 23, 2024
1 parent 76218cc commit c1b9b44
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion suzieq/config/textfsm_templates/iosxe_show_ip_ospfnbr.tfsm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Value ifname (\S+)
Value nbrPrio (\d+)
Value state (\w+)
Value numChanges (\d+)
Value lastUpTime (\w+)
Value lastUpTime (.+)
Value lastDownTime (\w+)
Value lsaRetxCnt (\d+)
Value bfdStatus (\S+)
Expand Down
12 changes: 7 additions & 5 deletions suzieq/poller/worker/services/ospfNbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import numpy as np

from suzieq.poller.worker.services.service import Service
from suzieq.shared.utils import get_timestamp_from_cisco_time
from suzieq.shared.utils import get_timestamp_from_junos_time
from suzieq.shared.utils import (get_timestamp_from_cisco_time,
get_timestamp_from_iosxe_time,
get_timestamp_from_junos_time)


class OspfNbrService(Service):
Expand Down Expand Up @@ -136,15 +137,16 @@ def _clean_nxos_data(self, processed_data, raw_data):
return processed_data

def _clean_ios_data(self, processed_data, raw_data):
rel_timestamp = raw_data[0]['cmd_timestamp']/1000
for entry in processed_data:
# make area the dotted model
area = entry.get('area', '')
if area.isdecimal():
entry['area'] = str(ip_address(int(area)))
entry['state'] = entry['state'].lower()
entry['lastUpTime'] = get_timestamp_from_cisco_time(
entry['lastUpTime'], raw_data[0]['timestamp']/1000)
entry['lastChangeTime'] = entry['lastUpTime']
up_ts: int = get_timestamp_from_iosxe_time(entry['lastUpTime'],
rel_timestamp)
entry['lastChangeTime'] = up_ts*1000
entry['lastDownTime'] = 0
entry['lsaRtxCnt'] = int(entry['lsaRetxCnt'])
entry['areaStub'] = entry['areaStub'] == 'Stub'
Expand Down
40 changes: 34 additions & 6 deletions suzieq/shared/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from logging.handlers import RotatingFileHandler
from os import getenv
from time import time
from typing import Any, Dict, List, Tuple
from typing import Any, Dict, List, Optional, Union

import pandas as pd
import psutil
Expand Down Expand Up @@ -386,9 +386,13 @@ def calc_avg(oldval, newval):


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


def get_timestamp_from_junos_time(in_data: Tuple[Dict, str],
relative_to: int = None,
def get_timestamp_from_iosxe_time(in_data: str, timestamp: int) -> int:
"""Get timestamp in ms from the Cisco IOS/XE-specific timestamp string
Examples of Cisco timestamp str are 1w4d, 1d10h, 01:20:35, 00:01:56.
Args:
in_data (str): The IOSXE uptime string
timestamp (int): the base unix timestamp IS SECONDS from which
subtracting the uptime.
Returns:
int: a unix timestamp in milliseconds
"""
if not in_data:
return 0

if ':' in in_data:
# There's time and so we need to convert it into h/m/s
in_data = (in_data.replace(':', 'h', 1)
.replace(':', 'm', 1)
) + 's'

return parse_relative_timestamp(in_data, timestamp, False) or 0


def get_timestamp_from_junos_time(in_data: Union[str, Dict[str, str]],
relative_to: Optional[int] = None,
ms=True) -> int:
"""Get timestamp in ms from the Junos-specific timestamp string
The expected input looks like: "attributes" : {"junos:seconds" : "0"}.
Expand Down

0 comments on commit c1b9b44

Please sign in to comment.