fix: Updated ipv4/ipv6 discovery to exclude IPs with invalid port_ids (#6495)

* fix: Updated ipv4/ipv6 discovery to exclude IPs with invalid port_ids

* Update the valid IP sql queries to include addresses without a port

* Fix arp-table too.
This commit is contained in:
Neil Lathwood
2017-04-25 21:28:25 +01:00
committed by GitHub
parent 2361541e33
commit 66b7077a01
4 changed files with 76 additions and 50 deletions

View File

@@ -21,37 +21,48 @@ foreach ($vrfs_lite_cisco as $vrf) {
if (dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifIndex` = ?', array($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` = ?', array($device['device_id'], $ifIndex));
if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_networks` WHERE `ipv4_network` = ?', array($network)) < '1') {
dbInsert(array('ipv4_network' => $network,'context_name' => $device['context_name']), 'ipv4_networks');
// echo("Create Subnet $network\n");
echo 'S';
if (is_numeric($port_id)) {
if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_networks` WHERE `ipv4_network` = ?', array($network)) < '1') {
dbInsert(array('ipv4_network' => $network, 'context_name' => $device['context_name']), 'ipv4_networks');
// echo("Create Subnet $network\n");
echo 'S';
} else {
//Update Context
dbUpdate(array('context_name' => $device['context_name']), 'ipv4_networks', '`ipv4_network` = ?', array($network));
echo 's';
}
$ipv4_network_id = dbFetchCell('SELECT `ipv4_network_id` FROM `ipv4_networks` WHERE `ipv4_network` = ?', array($network));
if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_addresses` WHERE `ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ? ', array($oid, $cidr, $port_id)) == '0') {
dbInsert(array(
'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(array('context_name' => $device['context_name']), 'ipv4_addresses', '`ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ?', array($oid, $cidr, $port_id));
echo '.';
}
$full_address = "$oid/$cidr|$ifIndex";
$valid_v4[$full_address] = 1;
} else {
//Update Context
dbUpdate(array('context_name' => $device['context_name']), 'ipv4_networks', '`ipv4_network` = ?', array($network));
echo 's';
d_echo("No port id found for $ifIndex");
}
$ipv4_network_id = dbFetchCell('SELECT `ipv4_network_id` FROM `ipv4_networks` WHERE `ipv4_network` = ?', array($network));
if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_addresses` WHERE `ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ? ', array($oid, $cidr, $port_id)) == '0') {
dbInsert(array('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(array('context_name' => $device['context_name']), 'ipv4_addresses', '`ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ?', array($oid, $cidr, $port_id));
echo '.';
}
$full_address = "$oid/$cidr|$ifIndex";
$valid_v4[$full_address] = 1;
} else {
echo '!';
}//end if
}//end foreach
$sql = "SELECT * FROM ipv4_addresses AS A, ports AS I WHERE I.device_id = '".$device['device_id']."' AND A.port_id = I.port_id";
foreach (dbFetchRows($sql) as $row) {
$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, array($device['device_id'])) as $row) {
$full_address = $row['ipv4_address'].'/'.$row['ipv4_prefixlen'].'|'.$row['ifIndex'];
if (!$valid_v4[$full_address]) {