Skip to content

Commit

Permalink
Skip ARP collection if ARP already collected
Browse files Browse the repository at this point in the history
This ensures that the SNMP-based `arp` plugin exits early if it appears
that some other plugin has already collected Arp records before it.

This makes the `arp` plugin a sort of fallback in a scenario where
vendor specific ARP collection plugins run first, rather than having
multiple ARP plugin step on each others toes.
  • Loading branch information
lunkwill42 committed Dec 12, 2024
1 parent 0b3074a commit 9168764
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/nav/ipdevpoll/plugins/arp.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def can_handle(cls, netbox):
@defer.inlineCallbacks
def handle(self):
self._check_and_update_prefix_cache()
if self._is_arp_already_collected():
self._logger.debug("ARP records already collected for this device")
return
self._logger.debug("Collecting IP/MAC mappings")

# Fetch standard MIBs
Expand Down Expand Up @@ -101,6 +104,10 @@ def _check_and_update_prefix_cache(self):
if prefix_cache_age > self.prefix_cache_max_age:
yield self._update_prefix_cache()

def _is_arp_already_collected(self):
"""Returns True if ARP entries have already been collected in this run"""
return shadows.Arp in self.containers and bool(self.containers[shadows.Arp])

@defer.inlineCallbacks
def _get_ip_mib(self):
if not self.is_arista():
Expand Down
13 changes: 13 additions & 0 deletions tests/unittests/ipdevpoll/plugins_arp_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from nav.ipdevpoll import shadows
from nav.ipdevpoll.storage import ContainerRepository
from nav.ipdevpoll.plugins.arp import ipv6_address_in_mappings, Arp

Expand All @@ -11,3 +12,15 @@ def test_make_new_mappings_should_not_raise_on_empty_ip():
a = Arp(None, None, ContainerRepository())
mappings = [(None, '00:0b:ad:c0:ff:ee')]
a._make_new_mappings(mappings)


def test_when_arp_records_exist_is_arp_already_collected_should_return_true():
containers = ContainerRepository()
containers.factory(('192.168.0.1', '00:co:ff:ee:ba:be'), shadows.Arp)
plugin = Arp(None, None, containers)
assert plugin._is_arp_already_collected()


def test_when_arp_records_do_not_exist_is_arp_already_collected_should_return_false():
plugin = Arp(None, None, ContainerRepository())
assert not plugin._is_arp_already_collected()

0 comments on commit 9168764

Please sign in to comment.