Skip to content

Commit

Permalink
Implement event generation for port state
Browse files Browse the repository at this point in the history
This also factors out state updating and event generation to separate
methods.
  • Loading branch information
lunkwill42 committed Oct 4, 2023
1 parent c29c90a commit 1a7e088
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/zino/tasks/linkstatetask.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any

from zino.snmp import SNMP, SparseWalkResponse
from zino.statemodels import InterfaceState, Port
from zino.statemodels import EventState, InterfaceState, Port, PortStateEvent
from zino.tasks.task import Task

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -63,6 +63,9 @@ def _update_single_interface(self, row: dict[str, Any]):
if not self._is_interface_watched(data):
return

self._update_state(data, port, row)

def _update_state(self, data: BaseInterfaceRow, port: Port, row: dict[str, Any]):
for attr in ("ifAdminStatus", "ifOperStatus"):
if not row.get(attr):
raise MissingInterfaceTableData(self.device.name, data.index, attr)
Expand All @@ -73,22 +76,33 @@ def _update_single_interface(self, row: dict[str, Any]):
port.state = InterfaceState.UNKNOWN
if state == "adminUp":
state = data.oper_status

state = InterfaceState(state)
if port.state and port.state != state:
# TODO make or update event
# TODO Re-verify state change after 2 minutes
_logger.info(
"%s port %s ix %s port changed state from %s to %s",
self.device.name,
data.descr,
data.index,
port.state,
state,
)

self._make_or_update_state_event(port, state)
port.state = state

def _make_or_update_state_event(self, port: Port, new_state: InterfaceState):
event, created = self.state.events.get_or_create_event(self.device.name, port.ifindex, PortStateEvent)
if created:
event.state = EventState.OPEN
event.add_history("Change state to Open")

event.portstate = new_state
event.ifindex = port.ifindex
event.polladdr = self.device.address
event.priority = self.device.priority
event.descr = port.ifdescr

# this is where we need to use sysUpTime and ifLastChange to calculate a timestamp for the change
log = (
f'{event.router}: port "{port.ifdescr}" ix {port.ifindex} ({port.ifalias}) '
f"changed state from {port.state} to {new_state} on TIMESTAMP"
)
_logger.info(log)
event.add_log(log)

# at this point we should re-schedule a new job in 2 minutes to verify the state change

def _get_or_create_port(self, ifindex: int):
ports = self.state.devices.get(self.device.name).ports
if ifindex not in ports:
Expand Down

0 comments on commit 1a7e088

Please sign in to comment.