2010-01-03 22:13:50 +00:00
|
|
|
<?php
|
2021-12-01 14:24:35 +01:00
|
|
|
/**
|
|
|
|
* q-bridge-mib.inc.php
|
|
|
|
*
|
|
|
|
* LibreNMS vlan discovery module for Q-BRIDGE-MIB
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @author peca.nesovanovic@sattrakt.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
use App\Models\Vlan;
|
2023-08-05 12:12:36 -05:00
|
|
|
use LibreNMS\Enum\Severity;
|
2020-09-21 15:40:17 +02:00
|
|
|
|
2016-10-11 15:29:06 -06:00
|
|
|
echo 'IEEE8021-Q-BRIDGE-MIB VLANs: ';
|
2010-01-03 22:13:50 +00:00
|
|
|
|
2016-01-24 20:50:42 +01:00
|
|
|
$vlanversion = snmp_get($device, 'dot1qVlanVersionNumber.0', '-Oqv', 'IEEE8021-Q-BRIDGE-MIB');
|
2010-01-03 22:13:50 +00:00
|
|
|
|
2016-08-28 12:32:58 -05:00
|
|
|
if ($vlanversion == 'version1' || $vlanversion == '2') {
|
2016-10-11 15:29:06 -06:00
|
|
|
echo "ver $vlanversion ";
|
2010-01-03 22:13:50 +00:00
|
|
|
|
2015-07-10 13:36:21 +02:00
|
|
|
$vtpdomain_id = '1';
|
2020-09-21 15:40:17 +02:00
|
|
|
|
2021-12-01 14:24:35 +01:00
|
|
|
// fetch vlan data
|
|
|
|
$vlans = SnmpQuery::walk('Q-BRIDGE-MIB::dot1qVlanCurrentUntaggedPorts')->table(2);
|
|
|
|
$vlans = SnmpQuery::walk('Q-BRIDGE-MIB::dot1qVlanCurrentEgressPorts')->table(2, $vlans);
|
|
|
|
if (empty($vlans)) {
|
|
|
|
// fall back to static
|
|
|
|
$vlans = SnmpQuery::walk('Q-BRIDGE-MIB::dot1qVlanStaticUntaggedPorts')->table(1, $vlans);
|
|
|
|
$vlans = SnmpQuery::walk('Q-BRIDGE-MIB::dot1qVlanStaticEgressPorts')->table(1, $vlans);
|
2019-10-21 15:41:47 +02:00
|
|
|
} else {
|
2021-12-01 14:24:35 +01:00
|
|
|
// collapse timefilter from dot1qVlanCurrentTable results to only the newest
|
|
|
|
$vlans = array_reduce($vlans, function ($result, $time_data) {
|
|
|
|
foreach ($time_data as $vlan_id => $vlan_data) {
|
|
|
|
$result[$vlan_id] = isset($result[$vlan_id]) ? array_merge($result[$vlan_id], $vlan_data) : $vlan_data;
|
|
|
|
}
|
2020-09-21 15:40:17 +02:00
|
|
|
|
2019-10-21 15:41:47 +02:00
|
|
|
return $result;
|
|
|
|
}, []);
|
|
|
|
}
|
2018-04-27 08:22:49 -05:00
|
|
|
|
2021-12-01 14:24:35 +01:00
|
|
|
$vlans = SnmpQuery::walk('Q-BRIDGE-MIB::dot1qVlanStaticName')->table(1, $vlans);
|
|
|
|
|
2015-07-10 13:36:21 +02:00
|
|
|
foreach ($vlans as $vlan_id => $vlan) {
|
2021-12-01 14:24:35 +01:00
|
|
|
d_echo('Processing vlan ID: ' . $vlan_id);
|
|
|
|
$vlan_name = empty($vlan['Q-BRIDGE-MIB::dot1qVlanStaticName']) ? "VLAN $vlan_id" : $vlan['Q-BRIDGE-MIB::dot1qVlanStaticName'];
|
|
|
|
|
|
|
|
//try to get existing data from DB
|
|
|
|
$vlanDB = Vlan::firstOrNew([
|
|
|
|
'device_id' => $device['device_id'],
|
|
|
|
'vlan_vlan' => $vlan_id,
|
|
|
|
], [
|
|
|
|
'vlan_domain' => $vtpdomain_id,
|
|
|
|
'vlan_name' => $vlan_name,
|
|
|
|
]);
|
|
|
|
|
|
|
|
//vlan does not exist
|
|
|
|
if (! $vlanDB->exists) {
|
2023-08-05 12:12:36 -05:00
|
|
|
\App\Models\Eventlog::log("Vlan added: $vlan_id with name $vlan_name ", $device['device_id'], 'vlan', Severity::Warning);
|
2021-12-01 14:24:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($vlanDB->vlan_name != $vlan_name) {
|
|
|
|
$vlanDB->vlan_name = $vlan_name;
|
2023-08-05 12:12:36 -05:00
|
|
|
\App\Models\Eventlog::log("Vlan changed: $vlan_id new name $vlan_name", $device['device_id'], 'vlan', Severity::Warning);
|
2012-05-02 15:04:31 +00:00
|
|
|
}
|
2010-01-03 22:13:50 +00:00
|
|
|
|
2021-12-01 14:24:35 +01:00
|
|
|
$vlanDB->save();
|
2016-10-11 15:29:06 -06:00
|
|
|
|
2021-12-01 14:24:35 +01:00
|
|
|
$device['vlans'][$vtpdomain_id][$vlan_id] = $vlan_id; //populate device['vlans'] with ID's
|
2016-10-11 15:29:06 -06:00
|
|
|
|
2021-12-01 14:24:35 +01:00
|
|
|
//portmap for untagged ports
|
2022-10-25 19:27:28 +02:00
|
|
|
$untagged_ids = q_bridge_bits2indices($vlan['Q-BRIDGE-MIB::dot1qVlanCurrentUntaggedPorts'] ?? $vlan['Q-BRIDGE-MIB::dot1qVlanStaticUntaggedPorts'] ?? '');
|
2021-12-01 14:24:35 +01:00
|
|
|
//portmap for members ports (might be tagged)
|
2022-10-25 19:27:28 +02:00
|
|
|
$egress_ids = q_bridge_bits2indices($vlan['Q-BRIDGE-MIB::dot1qVlanCurrentEgressPorts'] ?? $vlan['Q-BRIDGE-MIB::dot1qVlanStaticEgressPorts'] ?? '');
|
2016-10-19 16:44:49 -05:00
|
|
|
foreach ($egress_ids as $port_id) {
|
2018-04-27 08:22:49 -05:00
|
|
|
if (isset($base_to_index[$port_id])) {
|
|
|
|
$ifIndex = $base_to_index[$port_id];
|
|
|
|
$per_vlan_data[$vlan_id][$ifIndex]['untagged'] = (in_array($port_id, $untagged_ids) ? 1 : 0);
|
|
|
|
}
|
2016-10-11 15:29:06 -06:00
|
|
|
}
|
2015-07-10 13:36:21 +02:00
|
|
|
}
|
2011-03-15 11:39:50 +00:00
|
|
|
}
|
2016-10-11 15:29:06 -06:00
|
|
|
echo PHP_EOL;
|