Skip to content

Commit

Permalink
Replace read_only and snmp_version Netbox attrs
Browse files Browse the repository at this point in the history
Netbox shadow objects used to use the pseudo attributes `read_only` and
`snmp_version` to store the SNMP v1/v2c read-only community and SNMP
version.  On the Netbox Django model, these are computed properties,
based on the Netbox' preferred SNMP management profile, and have been
deprecated for a while.

Since ipdevpoll now needs to access more complicated SNMP management
configuration for a Netbox, this changes the Netbox shadow model to
store an SNMPParameters object directly, which will be populated from
the Netbox' preferred SNMP profile.

Usages of the removed attributes have been updated.  Usages mostly
concern themselves with verifying that a device actually has an SNMP
profile at all.

However, the ipdevpoll dataloader used the value of the pseudo
attributes to detect whether a Netbox' management credentials had
changed and needed to be reloaded from the database, and this patch does
not fully replace that.

This more or less fixes Uninett#2522
  • Loading branch information
lunkwill42 committed Nov 17, 2023
1 parent a4bc739 commit 21c4e6a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion python/nav/ipdevpoll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def can_handle(cls, netbox):
"""
snmp_up = getattr(netbox, 'snmp_up', True)

basic_req = netbox.is_up() and snmp_up and bool(netbox.read_only)
basic_req = netbox.is_up() and snmp_up and bool(netbox.snmp_parameters)
vendor_check = cls._verify_vendor_restriction(netbox)
return basic_req and vendor_check

Expand Down
3 changes: 1 addition & 2 deletions python/nav/ipdevpoll/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,11 @@ def is_netbox_changed(netbox1, netbox2):
for attr in (
'ip',
'type',
'read_only',
'snmp_version',
'up',
'snmp_up',
'deleted_at',
):
# TODO Need some way to detect an updated SNMP profile
if getattr(netbox1, attr) != getattr(netbox2, attr):
_logger.debug(
"%s.%s changed from %r to %r",
Expand Down
6 changes: 4 additions & 2 deletions python/nav/ipdevpoll/plugins/snmpcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ class SnmpCheck(Plugin):

@classmethod
def can_handle(cls, netbox):
return netbox.is_up() and bool(netbox.read_only)
return netbox.is_up() and bool(netbox.snmp_parameters)

def __init__(self, *args, **kwargs):
super(SnmpCheck, self).__init__(*args, **kwargs)

@defer.inlineCallbacks
def handle(self):
self._logger.debug("snmp version from db: %s", self.netbox.snmp_version)
self._logger.debug(
"snmp version from db: %s", self.netbox.snmp_parameters.version
)
was_down = yield db.run_in_thread(self._currently_down)
is_ok = yield self._do_check()

Expand Down
10 changes: 7 additions & 3 deletions python/nav/ipdevpoll/shadows/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
# License along with NAV. If not, see <http://www.gnu.org/licenses/>.
#
"""netbox related shadow classes"""
from typing import Union

from django.db.models import Q
from django.db import transaction

from nav.ipdevpoll.snmp.common import SNMPParameters
from nav.models import manage
from nav.ipdevpoll.storage import Shadow

Expand All @@ -29,13 +32,14 @@ class Netbox(Shadow):
def __init__(self, *args, **kwargs):
super(Netbox, self).__init__(*args, **kwargs)
if args:
obj = args[0]
obj: Union[Netbox, manage.Netbox] = args[0]
self.snmp_up = getattr(obj, 'snmp_up', not obj.is_snmp_down())
self.last_updated = getattr(
obj, 'last_updated', self._translate_last_jobs(obj)
)
self.read_only = getattr(obj, 'read_only')
self.snmp_version = getattr(obj, 'snmp_version')
self.snmp_parameters = getattr(obj, "snmp_parameters", {})
if not self.snmp_parameters:
self.snmp_parameters = SNMPParameters.factory(obj)

@staticmethod
def _translate_last_jobs(netbox):
Expand Down

0 comments on commit 21c4e6a

Please sign in to comment.