Skip to content

Commit

Permalink
Add code to handle juniper chassis/system alarm counts
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Apr 8, 2022
1 parent 2424d37 commit ecccb6d
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
84 changes: 84 additions & 0 deletions python/nav/ipdevpoll/plugins/juniperalarm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#
# Copyright (C) 2022 Sikt
#
# This file is part of Network Administration Visualized (NAV).
#
# NAV is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 3 as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. You should have received a copy of the GNU General Public
# License along with NAV. If not, see <http://www.gnu.org/licenses/>.
#
"""Juniper chassis/system alarms"""

from twisted.internet import defer
from twisted.internet.defer import returnValue

from django.db import transaction

from nav.event2 import EventFactory
from nav.mibs.juniper_alarm_mib import JuniperAlarmMib
from nav.models import event
from nav.ipdevpoll import Plugin, db

YELLOW_EVENT = EventFactory(
'ipdevpoll', 'eventEngine', 'juniperYellowAlarmState', 'juniperYellowAlarmNonZero', 'juniperYellowAlarmZero'
)
RED_EVENT = EventFactory(
'ipdevpoll', 'eventEngine', 'juniperRedAlarmState', 'juniperRedAlarmNonZero', 'juniperRedAlarmZero'
)


class JuniperChassisAlarm(Plugin):
"""Retrives the number of red and yellow alarms in a chassis
This is done by attempting to retrieve the gauges in JUNIPER-ALARM-MIB:
* jnxYellowAlarmCount
* jnxRedAlarmCount
If the count is not zero, a start event for that color is sent. When the
count goes back to zero, an end-event is sent.
"""

def __init__(self, *args, **kwargs):
self.mib = JuniperAlarmMib
super().__init__(*args, **kwargs)

@defer.inlineCallbacks
def handle(self):
self._logger.debug("Collecting any juniper led alarms")

self.yellow_count = yield self.mib.get_yellow_alarm_count()
self.red_count = yield self.mib.get_red_alarm_count()

if self.yellow_count:
yield self._yellow_alarm_count_non_zero(self)
else:
yield self._yellow_alarm_count_zero(self)

if self.red_count:
yield self._red_alarm_count_non_zero(self)
else:
yield self._red_alarm_count_zero(self)

@transaction.atomic()
def _yellow_alarm_count_non_zero(self):
YELLOW_EVENT.start(None, self.netbox.id).save()

@transaction.atomic()
def _yellow_alarm_count_zero(self):
YELLOW_EVENT.end(None, self.netbox.id).save()

@transaction.atomic()
def _red_alarm_count_non_zero(self):
RED_EVENT.start(None, self.netbox.id).save()

@transaction.atomic()
def _red_alarm_count_zero(self):
RED_EVENT.end(None, self.netbox.id).save()
45 changes: 45 additions & 0 deletions python/nav/mibs/juniper_alarm_mib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# Copyright (C) 2013 Uninett AS
#
# This file is part of Network Administration Visualized (NAV).
#
# NAV is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 3 as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details. You should have received a copy of the GNU General Public License
# along with NAV. If not, see <http://www.gnu.org/licenses/>.
#
"""JUNIPER-ALARM-MIB MibRetriever"""
from twisted.internet import defer

from nav.oids import OID
from nav.smidumps import get_mib
from nav.mibs.mibretriever import MibRetriever


class JuniperAlarmMib(MibRetriever):
"""JUNIPER-ALARM-MIB MibRetriever"""

mib = get_mib("JUNIPER-ALARM-MIB")

@defer.inlineCallbacks
def get_yellow_alarm_count(self):
"""Tries to get a yellow alarm count from a Juniper device"""
return self._get_alarm_count("jnxYellowAlarmCount")

def get_red_alarm_count(self):
"""Tries to get a red alarm count from a Juniper device"""
return self._get_alarm_count("jnxRedAlarmCount")

@defer.inlineCallbacks
def _get_alarm_count(self, oid):
count = yield self.get_next(oid)
try:
count = int(count) or 0
except ValueError:
count = 0
defer.returnValue(count)

0 comments on commit ecccb6d

Please sign in to comment.