1 | <?php |
||
2 | |||
3 | use LibreNMS\Exceptions\InvalidIpException; |
||
4 | use LibreNMS\Util\IPv4; |
||
5 | |||
6 | foreach (DeviceCache::getPrimary()->getVrfContexts() as $context_name) { |
||
7 | $device['context_name'] = $context_name; |
||
8 | |||
9 | $oids = trim(snmp_walk($device, 'ipAdEntIfIndex', '-Osq', 'IP-MIB')); |
||
10 | $oids = str_replace('ipAdEntIfIndex.', '', $oids); |
||
11 | foreach (explode("\n", $oids) as $data) { |
||
12 | $data = trim($data); |
||
13 | [$oid,$ifIndex] = explode(' ', $data); |
||
14 | $mask = trim(snmp_get($device, "ipAdEntNetMask.$oid", '-Oqv', 'IP-MIB')); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
15 | $cidr = IPv4::netmask2cidr($mask); |
||
16 | try { |
||
17 | $ipv4 = new IPv4("$oid/$cidr"); |
||
18 | } catch (InvalidIpException $e) { |
||
19 | continue; |
||
20 | } |
||
21 | $network = $ipv4->getNetworkAddress() . '/' . $ipv4->cidr; |
||
22 | |||
23 | if (dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifIndex` = ?', [$device['device_id'], $ifIndex]) != '0' && $oid != '0.0.0.0' && $oid != 'ipAdEntIfIndex') { |
||
24 | $port_id = dbFetchCell('SELECT `port_id` FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', [$device['device_id'], $ifIndex]); |
||
25 | |||
26 | if (is_numeric($port_id)) { |
||
27 | if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_networks` WHERE `ipv4_network` = ?', [$network]) < '1') { |
||
28 | dbInsert(['ipv4_network' => $network, 'context_name' => $device['context_name']], 'ipv4_networks'); |
||
29 | // echo("Create Subnet $network\n"); |
||
30 | echo 'S'; |
||
31 | } else { |
||
32 | //Update Context |
||
33 | dbUpdate(['context_name' => $device['context_name']], 'ipv4_networks', '`ipv4_network` = ?', [$network]); |
||
34 | echo 's'; |
||
35 | } |
||
36 | |||
37 | $ipv4_network_id = dbFetchCell('SELECT `ipv4_network_id` FROM `ipv4_networks` WHERE `ipv4_network` = ?', [$network]); |
||
38 | |||
39 | if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_addresses` WHERE `ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ? ', [$oid, $cidr, $port_id]) == '0') { |
||
40 | dbInsert([ |
||
41 | 'ipv4_address' => $oid, |
||
42 | 'ipv4_prefixlen' => $cidr, |
||
43 | 'ipv4_network_id' => $ipv4_network_id, |
||
44 | 'port_id' => $port_id, |
||
45 | 'context_name' => $device['context_name'], |
||
46 | ], 'ipv4_addresses'); |
||
47 | // echo("Added $oid/$cidr to $port_id ( $hostname $ifIndex )\n $i_query\n"); |
||
48 | echo '+'; |
||
49 | } else { |
||
50 | //Update Context |
||
51 | dbUpdate(['context_name' => $device['context_name']], 'ipv4_addresses', '`ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ?', [$oid, $cidr, $port_id]); |
||
52 | echo '.'; |
||
53 | } |
||
54 | $full_address = "$oid/$cidr|$ifIndex"; |
||
55 | $valid_v4[$full_address] = 1; |
||
56 | } else { |
||
57 | d_echo("No port id found for $ifIndex"); |
||
58 | } |
||
59 | } else { |
||
60 | echo '!'; |
||
61 | }//end if |
||
62 | }//end foreach |
||
63 | |||
64 | $sql = 'SELECT `ipv4_addresses`.*, `ports`.`device_id`, `ports`.`ifIndex` FROM `ipv4_addresses`'; |
||
65 | $sql .= ' LEFT JOIN `ports` ON `ipv4_addresses`.`port_id` = `ports`.`port_id`'; |
||
66 | $sql .= ' WHERE `ports`.device_id = ? OR `ports`.`device_id` IS NULL'; |
||
67 | foreach (dbFetchRows($sql, [$device['device_id']]) as $row) { |
||
68 | $full_address = $row['ipv4_address'] . '/' . $row['ipv4_prefixlen'] . '|' . $row['ifIndex']; |
||
69 | |||
70 | if (! $valid_v4[$full_address]) { |
||
71 | echo '-'; |
||
72 | $query = dbDelete('ipv4_addresses', '`ipv4_address_id` = ?', [$row['ipv4_address_id']]); |
||
73 | if (! dbFetchCell('SELECT COUNT(*) FROM `ipv4_addresses` WHERE `ipv4_network_id` = ?', [$row['ipv4_network_id']])) { |
||
74 | $query = dbDelete('ipv4_networks', '`ipv4_network_id` = ?', [$row['ipv4_network_id']]); |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | |||
79 | echo "\n"; |
||
80 | unset($device['context_name']); |
||
81 | unset($valid_v4); |
||
82 | } |
||
83 | unset($vrfs_c); |
||
84 |