mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Rewrite ipv4 address discovery to Eloquent (#15830)
* rewrite ipv4 discovery to Eloquent * style ci
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
use App\Actions\Device\ValidateDeviceAndCreate;
|
||||
use App\Models\Device;
|
||||
use App\Models\Eventlog;
|
||||
use App\Models\Ipv4Address;
|
||||
use App\Models\Ipv4Network;
|
||||
use App\Models\Ipv6Address;
|
||||
use App\Models\Ipv6Network;
|
||||
use App\Models\Port;
|
||||
@@ -26,6 +28,7 @@ use LibreNMS\Exceptions\HostExistsException;
|
||||
use LibreNMS\Exceptions\InvalidIpException;
|
||||
use LibreNMS\OS;
|
||||
use LibreNMS\Util\IP;
|
||||
use LibreNMS\Util\IPv4;
|
||||
use LibreNMS\Util\IPv6;
|
||||
use LibreNMS\Util\UserFuncHelper;
|
||||
|
||||
@@ -709,6 +712,75 @@ function discover_process_ipv6(&$valid, $ifIndex, $ipv6_address, $ipv6_prefixlen
|
||||
}//endif port_id && others
|
||||
}//end discover_process_ipv6()
|
||||
|
||||
/**
|
||||
* create or update IPv4 Addresses and/or IPv4 Networks
|
||||
*
|
||||
* @param pointer $valid_v4
|
||||
* @param array $device
|
||||
* @param int $ifIndex
|
||||
* @param string $ipv4_address
|
||||
* @param string $mask
|
||||
* @param string $context_name
|
||||
* @return array
|
||||
*
|
||||
* @throws InvalidIpException
|
||||
*/
|
||||
function discover_process_ipv4(&$valid_v4, $device, int $ifIndex, $ipv4_address, $mask, $context_name = '')
|
||||
{
|
||||
$cidr = IPv4::netmask2cidr($mask);
|
||||
try {
|
||||
$ipv4 = new IPv4($ipv4_address . '/' . $cidr);
|
||||
} catch (InvalidIpException $e) {
|
||||
d_echo('Invalid data: ' . $ipv4_address);
|
||||
|
||||
return;
|
||||
}
|
||||
$ipv4_network = $ipv4->getNetworkAddress() . '/' . $ipv4->cidr;
|
||||
|
||||
if ($ipv4_address != '0.0.0.0' && $ifIndex > 0) {
|
||||
$port_id = get_port_by_index_cache($device['device_id'], $ifIndex)['port_id'];
|
||||
|
||||
if (is_numeric($port_id)) {
|
||||
$dbIpv4Net = Ipv4Network::updateOrCreate([
|
||||
'ipv4_network' => $ipv4_network,
|
||||
], [
|
||||
'context_name' => $device['context_name'],
|
||||
]);
|
||||
|
||||
if (! $dbIpv4Net->wasRecentlyCreated && $dbIpv4Net->wasChanged()) {
|
||||
Eventlog::log('IPv4 network ' . $ipv4_network . ' changed', $device['device_id'], 'ipv4', Severity::Warning);
|
||||
echo 'S.';
|
||||
}
|
||||
if ($dbIpv4Net->wasRecentlyCreated) {
|
||||
Eventlog::log('IPv4 network ' . $ipv4_network . ' created', $device['device_id'], 'ipv4', Severity::Warning);
|
||||
echo 'S+';
|
||||
}
|
||||
|
||||
$ipv4_network_id = Ipv4Network::where('ipv4_network', $ipv4_network)->value('ipv4_network_id');
|
||||
$dbIpv4Addr = Ipv4Address::updateOrCreate([
|
||||
'ipv4_address' => $ipv4_address,
|
||||
'ipv4_prefixlen' => $cidr,
|
||||
'ipv4_network_id' => $ipv4_network_id,
|
||||
'port_id' => $port_id,
|
||||
], [
|
||||
'context_name' => $device['context_name'],
|
||||
]);
|
||||
|
||||
if (! $dbIpv4Addr->wasRecentlyCreated && $dbIpv4Addr->wasChanged()) {
|
||||
Eventlog::log('IPv4 address ' . $ipv4 . ' changed', $device['device_id'], 'ipv4', Severity::Warning);
|
||||
echo 'A.';
|
||||
}
|
||||
if ($dbIpv4Addr->wasRecentlyCreated) {
|
||||
Eventlog::log('IPv4 address ' . $ipv4 . ' created', $device['device_id'], 'ipv4', Severity::Warning);
|
||||
echo 'A+';
|
||||
}
|
||||
$full_address = $ipv4 . '|' . $ifIndex;
|
||||
$valid_v4[$full_address] = 1;
|
||||
} else {
|
||||
d_echo('No port id found for ifindex: ' . $ifIndex . PHP_EOL);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Check entity sensors to be excluded
|
||||
*
|
||||
|
@@ -1,83 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* ipv4-addresses.inc.php
|
||||
*
|
||||
* IPv4 address discovery module
|
||||
*
|
||||
*
|
||||
* @link https://www.librenms.org
|
||||
*
|
||||
* @author Peca Nesovanovic <peca.nesovanovic@sattrakt.com>
|
||||
*/
|
||||
|
||||
use LibreNMS\Exceptions\InvalidIpException;
|
||||
use LibreNMS\Util\IPv4;
|
||||
use App\Models\Eventlog;
|
||||
use App\Models\Ipv4Address;
|
||||
use App\Models\Ipv4Network;
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Enum\Severity;
|
||||
|
||||
foreach (DeviceCache::getPrimary()->getVrfContexts() as $context_name) {
|
||||
$device['context_name'] = $context_name;
|
||||
|
||||
$oids = trim(snmp_walk($device, 'ipAdEntIfIndex', '-Osq', 'IP-MIB'));
|
||||
$oids = str_replace('ipAdEntIfIndex.', '', $oids);
|
||||
foreach ($oids ? explode("\n", $oids) : [] as $data) {
|
||||
$data = trim($data);
|
||||
[$oid,$ifIndex] = explode(' ', $data);
|
||||
$mask = trim(snmp_get($device, "ipAdEntNetMask.$oid", '-Oqv', 'IP-MIB'));
|
||||
$cidr = IPv4::netmask2cidr($mask);
|
||||
try {
|
||||
$ipv4 = new IPv4("$oid/$cidr");
|
||||
} catch (InvalidIpException $e) {
|
||||
continue;
|
||||
if (file_exists(Config::get('install_dir') . "/includes/discovery/ipv4-addresses/{$device['os']}.inc.php")) {
|
||||
include Config::get('install_dir') . "/includes/discovery/ipv4-addresses/{$device['os']}.inc.php";
|
||||
} else {
|
||||
unset($valid_v4);
|
||||
$oids = SnmpQuery::hideMib()->walk('IP-MIB::ipAdEntIfIndex')->table(1);
|
||||
foreach ($oids as $ipv4_address => $indexArray) {
|
||||
$ifIndex = intval($indexArray['ipAdEntIfIndex']);
|
||||
$mask = SnmpQuery::get('IP-MIB::ipAdEntNetMask.' . $ipv4_address)->value();
|
||||
discover_process_ipv4($valid_v4, $device, $ifIndex, $ipv4_address, $mask, $context_name);
|
||||
}
|
||||
$network = $ipv4->getNetworkAddress() . '/' . $ipv4->cidr;
|
||||
} // if [custom / standard]
|
||||
|
||||
if (dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifIndex` = ?', [$device['device_id'], $ifIndex]) != '0' && $oid != '0.0.0.0' && $oid != 'ipAdEntIfIndex') {
|
||||
$port_id = dbFetchCell('SELECT `port_id` FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', [$device['device_id'], $ifIndex]);
|
||||
$fromDb = Ipv4Address::where('ports.device_id', $device['device_id'])->orWhere('ports.device_id', null)
|
||||
->select('ipv4_address_id', 'ipv4_address', 'ipv4_prefixlen', 'ipv4_network_id', 'ports.device_id', 'ports.ifIndex')
|
||||
->leftJoin('ports', 'ipv4_addresses.port_id', '=', 'ports.port_id')
|
||||
->get()->toArray();
|
||||
|
||||
if (is_numeric($port_id)) {
|
||||
if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_networks` WHERE `ipv4_network` = ?', [$network]) < '1') {
|
||||
dbInsert(['ipv4_network' => $network, 'context_name' => $device['context_name']], 'ipv4_networks');
|
||||
// echo("Create Subnet $network\n");
|
||||
echo 'S';
|
||||
} else {
|
||||
//Update Context
|
||||
dbUpdate(['context_name' => $device['context_name']], 'ipv4_networks', '`ipv4_network` = ?', [$network]);
|
||||
echo 's';
|
||||
}
|
||||
|
||||
$ipv4_network_id = dbFetchCell('SELECT `ipv4_network_id` FROM `ipv4_networks` WHERE `ipv4_network` = ?', [$network]);
|
||||
|
||||
if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_addresses` WHERE `ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ? ', [$oid, $cidr, $port_id]) == '0') {
|
||||
dbInsert([
|
||||
'ipv4_address' => $oid,
|
||||
'ipv4_prefixlen' => $cidr,
|
||||
'ipv4_network_id' => $ipv4_network_id,
|
||||
'port_id' => $port_id,
|
||||
'context_name' => $device['context_name'],
|
||||
], 'ipv4_addresses');
|
||||
// echo("Added $oid/$cidr to $port_id ( $hostname $ifIndex )\n $i_query\n");
|
||||
echo '+';
|
||||
} else {
|
||||
//Update Context
|
||||
dbUpdate(['context_name' => $device['context_name']], 'ipv4_addresses', '`ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ?', [$oid, $cidr, $port_id]);
|
||||
echo '.';
|
||||
}
|
||||
$full_address = "$oid/$cidr|$ifIndex";
|
||||
$valid_v4[$full_address] = 1;
|
||||
} else {
|
||||
d_echo("No port id found for $ifIndex");
|
||||
}
|
||||
} else {
|
||||
echo '!';
|
||||
}//end if
|
||||
}//end foreach
|
||||
|
||||
$sql = 'SELECT `ipv4_addresses`.*, `ports`.`device_id`, `ports`.`ifIndex` FROM `ipv4_addresses`';
|
||||
$sql .= ' LEFT JOIN `ports` ON `ipv4_addresses`.`port_id` = `ports`.`port_id`';
|
||||
$sql .= ' WHERE `ports`.device_id = ? OR `ports`.`device_id` IS NULL';
|
||||
foreach (dbFetchRows($sql, [$device['device_id']]) as $row) {
|
||||
foreach ($fromDb as $row) {
|
||||
$full_address = $row['ipv4_address'] . '/' . $row['ipv4_prefixlen'] . '|' . $row['ifIndex'];
|
||||
|
||||
if (! $valid_v4[$full_address]) {
|
||||
echo '-';
|
||||
$query = dbDelete('ipv4_addresses', '`ipv4_address_id` = ?', [$row['ipv4_address_id']]);
|
||||
if (! dbFetchCell('SELECT COUNT(*) FROM `ipv4_addresses` WHERE `ipv4_network_id` = ?', [$row['ipv4_network_id']])) {
|
||||
$query = dbDelete('ipv4_networks', '`ipv4_network_id` = ?', [$row['ipv4_network_id']]);
|
||||
if (empty($valid_v4[$full_address])) {
|
||||
Ipv4Address::where('ipv4_address_id', $row['ipv4_address_id'])->delete();
|
||||
Eventlog::log('IPv4 address: ' . $row['ipv4_address'] . '/' . $row['ipv4_prefixlen'] . ' deleted', $device['device_id'], 'ipv4', Severity::Warning);
|
||||
echo 'A-';
|
||||
if (! Ipv4Address::where('ipv4_network_id', $row['ipv4_network_id'])->count()) {
|
||||
Ipv4Network::where('ipv4_network_id', $row['ipv4_network_id'])->delete();
|
||||
echo 'S-';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n";
|
||||
echo PHP_EOL;
|
||||
unset($device['context_name']);
|
||||
unset($valid_v4);
|
||||
}
|
||||
unset($valid_v4);
|
||||
unset($vrfs_c);
|
||||
|
Reference in New Issue
Block a user