Issues (2963)

includes/discovery/vlans.inc.php (1 issue)

Labels
Severity
1
<?php
2
3
// Pre-cache the existing state of VLANs for this device from the database
4
use LibreNMS\Config;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Config. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
6
$vlans_db = [];
7
$vlans_db_raw = dbFetchRows('SELECT * FROM `vlans` WHERE `device_id` = ?', [$device['device_id']]);
8
foreach ($vlans_db_raw as $vlan_db) {
9
    $vlans_db[$vlan_db['vlan_domain']][$vlan_db['vlan_vlan']] = $vlan_db;
10
}
11
unset(
12
    $vlans_db_raw
13
);
14
15
// Create an empty array to record what VLANs we discover this session.
16
$device['vlans'] = [];
17
$per_vlan_data = [];  // fill this with data for each vlan
18
$valid_vlan_port = [];
19
20
// get a map of base port to ifIndex and the inverse
21
$base_to_index = [];
22
$tmp_base_indexes = snmpwalk_cache_oid($device, 'dot1dBasePortIfIndex', [], 'BRIDGE-MIB');
23
// flatten the array
24
foreach ($tmp_base_indexes as $index => $array) {
25
    $base_to_index[$index] = $array['dot1dBasePortIfIndex'];
26
}
27
$index_to_base = array_flip($base_to_index);
28
29
if (file_exists(Config::get('install_dir') . "/includes/discovery/vlans/{$device['os']}.inc.php")) {
30
    include Config::get('install_dir') . "/includes/discovery/vlans/{$device['os']}.inc.php";
31
}
32
33
if (empty($device['vlans']) === true) {
34
    require 'includes/discovery/vlans/q-bridge-mib.inc.php';
35
    require 'includes/discovery/vlans/cisco-vtp.inc.php';
36
}
37
38
// Fetch switchport <> VLAN relationships. This is DIRTY.
39
foreach ($device['vlans'] as $domain_id => $vlans) {
40
    foreach ($vlans as $vlan_id => $vlan) {
41
        // grab the populated data for this vlan
42
        $vlan_data = $per_vlan_data[$vlan_id];
43
44
        echo "VLAN $vlan_id \n";
45
46
        if ($vlan_data) {
47
            echo str_pad('dot1d id', 10) . str_pad('ifIndex', 10) . str_pad('Port Name', 25) . str_pad('Priority', 10) . str_pad('State', 15) . str_pad('Cost', 10) . "\n";
48
        }
49
50
        foreach ((array) $vlan_data as $ifIndex => $vlan_port) {
51
            $port = get_port_by_index_cache($device['device_id'], $ifIndex);
52
            echo str_pad($vlan_port_id, 10) . str_pad($ifIndex, 10) . str_pad($port['ifDescr'], 25) . str_pad($vlan_port['dot1dStpPortPriority'], 10) . str_pad($vlan_port['dot1dStpPortState'], 15) . str_pad($vlan_port['dot1dStpPortPathCost'], 10);
53
54
            $db_w = [
55
                'device_id' => $device['device_id'],
56
                'port_id'   => $port['port_id'],
57
                'vlan'      => $vlan_id,
58
            ];
59
60
            $db_a['baseport'] = $index_to_base[$ifIndex];
61
            $db_a['priority'] = isset($vlan_port['dot1dStpPortPriority']) ? $vlan_port['dot1dStpPortPriority'] : 0;
62
            $db_a['state'] = isset($vlan_port['dot1dStpPortState']) ? $vlan_port['dot1dStpPortState'] : 'unknown';
63
            $db_a['cost'] = isset($vlan_port['dot1dStpPortPathCost']) ? $vlan_port['dot1dStpPortPathCost'] : 0;
64
            $db_a['untagged'] = isset($vlan_port['untagged']) ? $vlan_port['untagged'] : 0;
65
66
            $from_db = dbFetchRow('SELECT * FROM `ports_vlans` WHERE device_id = ? AND port_id = ? AND `vlan` = ?', [$device['device_id'], $port['port_id'], $vlan_id]);
67
68
            if ($from_db['port_vlan_id']) {
69
                $db_id = $from_db['port_vlan_id'];
70
                dbUpdate($db_a, 'ports_vlans', '`port_vlan_id` = ?', [$db_id]);
71
                echo 'Updated';
72
            } else {
73
                $db_id = dbInsert(array_merge($db_w, $db_a), 'ports_vlans');
74
                echo 'Inserted';
75
            }
76
            $valid_vlan_port[] = $db_id;
77
78
            echo PHP_EOL;
79
        }//end foreach
80
    }//end foreach
81
}//end foreach
82
83
// remove non-existent vlans
84
foreach ($vlans_db as $domain_id => $vlans) {
85
    foreach ($vlans as $vlan_id => $vlan) {
86
        if (empty($device['vlans'][$domain_id][$vlan_id])) {
87
            dbDelete('vlans', '`device_id` = ? AND vlan_domain = ? AND vlan_vlan = ?', [$device['device_id'], $domain_id, $vlan_id]);
88
        }
89
    }
90
}
91
92
// remove non-existent port-vlan mappings
93
if (! empty($valid_vlan_port)) {
94
    $num = dbDelete('ports_vlans', '`device_id`=? AND `port_vlan_id` NOT IN ' . dbGenPlaceholders(count($valid_vlan_port)), array_merge([$device['device_id']], $valid_vlan_port));
95
    d_echo("Deleted $num vlan mappings\n");
96
}
97
98
unset($device['vlans']);
99
unset($base_to_index, $tmp_base_indexes, $index_to_base, $per_vlan_data, $valid_vlan_port, $num);
100