Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue:3563885:UFM Enterprise network reports #123

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
# @date: Aug 9, 2023
#

from enum import Enum


class EventTypeEnum(Enum):
LINK_IS_UP = '328'
LINK_IS_DOWN = '329'
Node_IS_UP = '332'
Node_IS_DOWN = '331'
SWITCH_IS_UP = '908'
SWITCH_IS_DOWN = '907'
DIRECTOR_SWITCH_IS_UP = '910'
DIRECTOR_SWITCH_IS_DOWN = '909'


class EventsConstants:
ID = "id"
NAME = "name"
Expand All @@ -25,6 +39,35 @@ class EventsConstants:
OBJECT_PATH = "object_path"
WRITE_TO_SYSLOG = "write_to_syslog"
DESCRIPTION = "description"
EVENT_LOG_PATTERN = r"(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.*\d*) \[(?P<id>\d+)\] \[(?P<event_type>\d+)\] (?P<severity>\w+) \[(?P<category>.+?)\] (?P<object_name>\w+) \[(?P<object_path>\w+)\]: (?P<description>.+)"
EVENT_LOG_PATTERN = r"(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.*\d*) \[(?P<id>\d+)\] \[(?P<event_type>\d+)\] (?P<severity>\w+)( Site \[.+?\])? \[(?P<category>.+?)\] (?P<type>\w+) \[(?P<object_path>.+?)\]( \[dev_id:?(?P<object_name>.+?)\])?.*?: (?P<description>.+)"
EVENT_LOGS_DIRECTORY = '/log'
EVENT_LOG = "event.log"

EVENTS_INFO = {
EventTypeEnum.LINK_IS_UP.value: {
"name":"Link is Up",
},
EventTypeEnum.LINK_IS_DOWN.value: {
"name": "Link is Down",
},
EventTypeEnum.Node_IS_UP.value: {
"name": "Node is Up",
},
EventTypeEnum.Node_IS_DOWN.value: {
"name": "Node is Down",
},
EventTypeEnum.SWITCH_IS_UP.value: {
"name": "Switch is Up",
},
EventTypeEnum.SWITCH_IS_DOWN.value: {
"name": "Switch is Down",
},
EventTypeEnum.DIRECTOR_SWITCH_IS_UP.value: {
"name": "Director Switch is Up",
},
EventTypeEnum.DIRECTOR_SWITCH_IS_DOWN.value: {
"name": "Director Switch is Down",
},

}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import gzip
from utils.logger import Logger, LOG_LEVELS
from model.event import Event
from constants.events_constants import EventsConstants
from constants.events_constants import EventsConstants, EventTypeEnum
import re


Expand All @@ -30,10 +30,19 @@ class EventsHistoryMgr(Singleton):

def __init__(self):

self._events = EventsCollection(Event)
self.events_collections = self._init_events_collections()
self.parse_event_log_files()

events = property(lambda self: self._events)
def _init_events_collections(self):
"""
This function is designed to initialize an event collections map. Each supported event will have a corresponding
collection. This will increase filtering performance.
:return:
"""
events_collections = {}
for supported_event in EventTypeEnum:
events_collections[supported_event.value] = EventsCollection(Event)
return events_collections

def openEventFile(self, fname, mode):
"""
Expand Down Expand Up @@ -65,12 +74,14 @@ def parse_event_log_files(self):
files.append(EventsConstants.EVENT_LOG)
for file in files:
fname = os.path.join(EventsConstants.EVENT_LOGS_DIRECTORY, file)
Logger.log_message(f"Parsing file {fname}", LOG_LEVELS.INFO)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If file not exists this message is incorrect. Message should be moved under the if, but need to mention list of files you are going to parse.

if os.path.exists(fname):
f = self.openEventFile(fname, 'rt')
if f:
for line in f:
self.create_event(line)
f.close()
Logger.log_message(f"Parsing file {fname} completed successfully", LOG_LEVELS.INFO)
except Exception as e:
Logger.log_message("Error occurred while parsing events logs files: " + str(e),
LOG_LEVELS.ERROR)
Expand All @@ -89,15 +100,22 @@ def create_event(self, log_line):
id = match.group(EventsConstants.ID)
event_type = match.group(EventsConstants.EVENT_TYPE)
severity = match.group(EventsConstants.SEVERITY)
category = match.group(EventsConstants.CATEGORY)
object_name = match.group(EventsConstants.OBJECT_NAME)
category = match.group(EventsConstants.CATEGORY).replace('_', ' ')
type = match.group(EventsConstants.TYPE)
object_path = match.group(EventsConstants.OBJECT_PATH)
description = match.group(EventsConstants.DESCRIPTION)
#TODO extract event name, and add only the topology change events
event = Event(timestamp=time, id=id, event_type=event_type, severity=severity,
category=category, object_name=object_name, object_path=object_path,
description=description, name="N/A")
self.events.add(event)
# object_name not always exists
if match.group(EventsConstants.OBJECT_NAME):
object_name = match.group(EventsConstants.OBJECT_NAME)
else:
object_name = "N/A"
if event_type in [s_event.value for s_event in EventTypeEnum]:
event_name = EventsConstants.EVENTS_INFO[event_type]["name"]
event = Event(timestamp=time, id=id, event_type=event_type, severity=severity,
category=category, object_name=object_name, object_path=object_path,
description=description, name=event_name, type=type)
Logger.log_message(f"Event {event_name} with id={id} was created successfully", LOG_LEVELS.INFO)
self.events_collections[event_type].add(event)
except Exception as ex:
Logger.log_message(f"Error occurred while parsing event log line {log_line}: {str(ex)}",
LOG_LEVELS.ERROR)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Event:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the copyrights

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


def __init__(self, id, name, event_type, severity, timestamp, category, description, object_name,
object_path, write_to_syslog=False, type="N/A", counter="N/A"):
def __init__(self, id, name, event_type, severity, timestamp, category, description, type,
object_path, write_to_syslog=False, object_name="N/A", counter="N/A"):
self.id = id
self.name = name
self.type = type
Expand Down