Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions LibreNMS/Modules/DiscoveryArp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace LibreNMS\Modules;

use App\Facades\LibrenmsConfig;
use App\Models\Device;
use App\Models\Eventlog;
use App\Models\Ipv4Mac;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use LibreNMS\Enum\Severity;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Interfaces\Data\DataStorageInterface;
use LibreNMS\Interfaces\Module;
use LibreNMS\OS;
use LibreNMS\Polling\ModuleStatus;
use LibreNMS\Util\IPv4;

class DiscoveryArp implements Module
{
/**
* @inheritDoc
*/
public function dependencies(): array
{
return ['arp-table'];
}

/**
* @inheritDoc
*/
public function shouldDiscover(OS $os, ModuleStatus $status): bool
{
return $status->isEnabledAndDeviceUp($os->getDevice());
}

/**
* @inheritDoc
*/
public function shouldPoll(OS $os, ModuleStatus $status): bool
{
return false;
}

/**
* @inheritDoc
*/
public function discover(OS $os): void
{
// Find all IPv4 addresses in the MAC table that haven't been discovered on monitored devices.
$entries = Ipv4Mac::query()
->select(['ipv4_address', 'mac_address', 'port_id'])
->whereHas('port', fn ($query) => $query->isNotDeleted())

Check failure on line 53 in LibreNMS/Modules/DiscoveryArp.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Call to an undefined method Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model>::isNotDeleted().
->whereDoesntHave('ipv4Address')
->orderBy('ipv4_address')
->get();

$discoverable_names_ips = [];
$ignored = [];
$excluded = 0;
$debounced = 0;

foreach ($entries as $entry) {
try {
$ip = IPv4::parse($entry->ipv4_address);

// Even though match_network is done inside discover_new_device, we do it here
// as well in order to skip unnecessary reverse DNS lookups on discovered IPs.
if ($ip->inNetworks(LibrenmsConfig::get('autodiscovery.nets-exclude'))) {
$excluded++;
continue;
}

if (! $ip->inNetworks(LibrenmsConfig::get('nets'))) {
$ignored[] = (string) $ip;
continue;
}

// Attempt discovery of each IP only once per run.
if (! Cache::add('arp_discovery:' . $ip, true, 3600)) {
$debounced++;
continue;
}

$discoverable_names_ips[] = gethostbyaddr((string) $ip);
} catch (InvalidIpException $e) {
Log::debug('Invalid IP address encountered during ARP discovery: ' . $e->getMessage());
}
}

$ignored_count = count($ignored);
Log::info(sprintf('Found %d discoverable IPs, ignored %d, excluded %d, skipped (recent) %d', count($discoverable_names_ips), $ignored_count, $excluded, $debounced));

// send a single eventlog per discovery with at most 5 IPs
if ($ignored_count) {
$ips = implode(',', array_slice($ignored, 0, 5));
if ($ignored_count > 5) {
$ips = '...';
}
Eventlog::log("ARP Discover: ignored $ignored_count IPs ($ips)", $os->getDeviceId(), 'discovery', Severity::Notice);
}

// Run device discovery on each of the devices we've detected so far.
$device = $os->getDeviceArray();
foreach ($discoverable_names_ips as $address) {
discover_new_device($address, $device, 'ARP');
}
}

/**
* @inheritDoc
*/
public function poll(OS $os, DataStorageInterface $datastore): void
{
// no polling
}

/**
* @inheritDoc
*/
public function dataExists(Device $device): bool
{
return false;
}

/**
* @inheritDoc
*/
public function cleanup(Device $device): int
{
return 0;
}

/**
* @inheritDoc
*/
public function dump(Device $device, string $type): ?array
{
return null; // no testing for now
}
}
10 changes: 9 additions & 1 deletion app/Models/Ipv4Mac.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ public function port(): BelongsTo
return $this->belongsTo(Port::class, 'port_id');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Ipv4Address, $this>
*/
public function ipv4Address(): BelongsTo
{
return $this->belongsTo(Ipv4Address::class, 'ipv4_address', 'ipv4_address');
}

// Ports in NMS with a matching MAC address and IP address.
// This can match multiple ports if you have multiple sub-interfaces with the same
// IP address (e.g. different VRFs, or mutiple point to point links on Mikrotik)
// IP address (e.g. different VRFs, or multiple point to point links on Mikrotik)
/**
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough<\App\Models\Port, Ipv4Mac, $this>
*/
Expand Down
83 changes: 0 additions & 83 deletions includes/discovery/discovery-arp.inc.php

This file was deleted.

31 changes: 0 additions & 31 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,37 +129,6 @@ function isDomainResolves($domain)
return ! empty($records);
}

function match_network($nets, $ip, $first = false)
{
$return = false;
if (! is_array($nets)) {
$nets = [$nets];
}
foreach ($nets as $net) {
$rev = (preg_match("/^\!/", (string) $net)) ? true : false;
$net = preg_replace("/^\!/", '', (string) $net);
$ip_arr = explode('/', (string) $net);
$net_long = ip2long($ip_arr[0]);
$x = ip2long($ip_arr[1]);
$mask = long2ip($x) == $ip_arr[1] ? $x : 0xFFFFFFFF << (32 - $ip_arr[1]);
$ip_long = ip2long($ip);
if ($rev) {
if (($ip_long & $mask) == ($net_long & $mask)) {
return false;
}
} else {
if (($ip_long & $mask) == ($net_long & $mask)) {
$return = true;
}
if ($first && $return) {
return true;
}
}
}

return $return;
}

// FIXME port to LibreNMS\Util\IPv6 class
function snmp2ipv6($ipv6_snmp)
{
Expand Down
Loading