Files
librenms-librenms/includes/discovery/arp-table.inc.php
Julian Lehrhuber a3ec263f78 Bintec be.IP plus support (#12993)
* Introduce OS flag to support devices that deliver OIDs out of order

* Add MIB for Bintec Be.IP Plus

* Add YAML definitions for Bintec Be.IP Plus

* Add Bintec be.IP plus test files

* Rename bintec mib files

mib-standards.mib was deleted, because it included definitions that also
existed in separate files

* Add test data

* Remove standard mib files from bintec dir

* Add regex to parse Bintec Be.IP Plus OS version

* Update test data for Bintec Be.IP Plus

* Calculate Bintec Be.IP Plus CPU load from inverse of idle time

* Improve Bintec Be.IP Plus discovery and tests

* Use MIB instead of numerical OIDs where possible

* Update tests to comply with updated discovery

* Remove unused MIBs

* Remove json props that let tests fail

* Update Settings.md

* Remove `snmp_out_of_order_oids` definition from bintec-beip-plus

* Add `-Cc` snmp param for prots module

* Add `-Cc` snmp param for arp-table module

* remove snmp_out_of_order_oids

* Remove snmp_out_of_order_oids

* Update snmp.inc.php

Co-authored-by: Tony Murray <murraytony@gmail.com>
2021-10-28 13:05:43 -05:00

129 lines
4.8 KiB
PHP

<?php
/**
* arp-table.php
*
* Collect arp table entries from devices and update the database
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
use LibreNMS\Config;
foreach (DeviceCache::getPrimary()->getVrfContexts() as $context_name) {
$device['context_name'] = $context_name;
if (file_exists(Config::get('install_dir') . "/includes/discovery/arp-table/{$device['os']}.inc.php")) {
include Config::get('install_dir') . "/includes/discovery/arp-table/{$device['os']}.inc.php";
} else {
$netToMediaPhysAddressSnmpFlags = '-OQUsetX';
if ($device['os'] == 'bintec-beip-plus') {
$netToMediaPhysAddressSnmpFlags = ['-OQUsetX', '-Cc'];
}
$arp_data = snmpwalk_group($device, 'ipNetToPhysicalPhysAddress', 'IP-MIB');
$arp_data = snmpwalk_group($device, 'ipNetToMediaPhysAddress', 'IP-MIB', 1, $arp_data, null, null, $netToMediaPhysAddressSnmpFlags);
}
$sql = 'SELECT * from `ipv4_mac` WHERE `device_id`=? AND `context_name`=?';
$existing_data = dbFetchRows($sql, [$device['device_id'], $context_name]);
$ipv4_addresses = array_map(function ($data) {
return $data['ipv4_address'];
}, $existing_data);
$arp_table = [];
$insert_data = [];
foreach ($arp_data as $ifIndex => $data) {
$interface = get_port_by_index_cache($device['device_id'], $ifIndex);
$port_id = $interface['port_id'];
$port_arp = array_merge(
(array) $data['ipNetToMediaPhysAddress'],
is_array($data['ipNetToPhysicalPhysAddress']) ? (array) $data['ipNetToPhysicalPhysAddress']['ipv4'] : []
);
echo "{$interface['ifName']}: \n";
foreach ($port_arp as $ip => $raw_mac) {
if (empty($ip) || empty($raw_mac) || $raw_mac == '0:0:0:0:0:0' || isset($arp_table[$port_id][$ip])) {
continue;
}
$mac = implode(array_map('zeropad', explode(':', $raw_mac)));
$arp_table[$port_id][$ip] = $mac;
$index = array_search($ip, $ipv4_addresses);
if ($index !== false) {
$old_mac = $existing_data[$index]['mac_address'];
if ($mac != $old_mac && $mac != '') {
d_echo("Changed mac address for $ip from $old_mac to $mac\n");
log_event("MAC change: $ip : " . \LibreNMS\Util\Rewrite::readableMac($old_mac) . ' -> ' . \LibreNMS\Util\Rewrite::readableMac($mac), $device, 'interface', 4, $port_id);
dbUpdate(['mac_address' => $mac], 'ipv4_mac', 'port_id=? AND ipv4_address=? AND context_name=?', [$port_id, $ip, $context_name]);
}
d_echo("$raw_mac => $ip\n", '.');
} elseif (isset($interface['port_id'])) {
d_echo("$raw_mac => $ip\n", '+');
$insert_data[] = [
'port_id' => $port_id,
'device_id' => $device['device_id'],
'mac_address' => $mac,
'ipv4_address' => $ip,
'context_name' => (string) $context_name,
];
}
}
echo PHP_EOL;
}
unset(
$interface,
$arp_data,
$ipv4_addresses,
$data
);
// add new entries
if (! empty($insert_data)) {
dbBulkInsert($insert_data, 'ipv4_mac');
}
// remove stale entries
foreach ($existing_data as $entry) {
$entry_mac = $entry['mac_address'];
$entry_if = $entry['port_id'];
$entry_ip = $entry['ipv4_address'];
if ($arp_table[$entry_if][$entry_ip] != $entry_mac) {
dbDelete('ipv4_mac', '`port_id` = ? AND `mac_address`=? AND `ipv4_address`=? AND `context_name`=?', [$entry_if, $entry_mac, $entry_ip, $context_name]);
d_echo(null, '-');
}
}
// remove entries that no longer have an owner
dbDeleteOrphans('ipv4_mac', ['ports.port_id', 'devices.device_id']);
echo PHP_EOL;
unset(
$existing_data,
$arp_table,
$insert_data,
$sql,
$params,
$entry,
$device['context_name']
);
}