Issues (2963)

includes/discovery/fdb-table.inc.php (1 issue)

1
<?php
2
3
// Build a dictionary of vlans in 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_dict = [];
7
foreach (dbFetchRows('SELECT `vlan_id`, `vlan_vlan` from `vlans` WHERE `device_id` = ?', [$device['device_id']]) as $vlan_entry) {
8
    $vlans_dict[$vlan_entry['vlan_vlan']] = $vlan_entry['vlan_id'];
9
}
10
$vlans_by_id = array_flip($vlans_dict);
11
12
// Build table of existing vlan/mac table
13
$existing_fdbs = [];
14
$sql_result = dbFetchRows('SELECT * FROM `ports_fdb` WHERE `device_id` = ?', [$device['device_id']]);
15
foreach ($sql_result as $entry) {
16
    $existing_fdbs[(int) $entry['vlan_id']][$entry['mac_address']] = $entry;
17
}
18
19
$insert = []; // populate $insert with database entries
20
if (file_exists(Config::get('install_dir') . "/includes/discovery/fdb-table/{$device['os']}.inc.php")) {
21
    require Config::get('install_dir') . "/includes/discovery/fdb-table/{$device['os']}.inc.php";
22
} elseif ($device['os'] == 'ios' || $device['os'] == 'iosxe' || $device['os'] == 'nxos') {
23
    //ios,iosxe,nxos are all Cisco
24
    include Config::get('install_dir') . '/includes/discovery/fdb-table/ios.inc.php';
25
}
26
27
if (empty($insert)) {
28
    // Check generic Q-BRIDGE-MIB and BRIDGE-MIB
29
    include Config::get('install_dir') . '/includes/discovery/fdb-table/bridge.inc.php';
30
}
31
32
if (! empty($insert)) {
33
    $update_time_only = [];
34
    $now = \Carbon\Carbon::now();
35
    // synchronize with the database
36
    foreach ($insert as $vlan_id => $mac_address_table) {
37
        echo " {$vlans_by_id[$vlan_id]}: ";
38
39
        foreach ($mac_address_table as $mac_address_entry => $entry) {
40
            if ($existing_fdbs[$vlan_id][$mac_address_entry]) {
41
                $new_port = $entry['port_id'];
42
                $port_fdb_id = $existing_fdbs[$vlan_id][$mac_address_entry]['ports_fdb_id'];
43
44
                // Sometimes new_port ends up as 0 if we didn't get a complete dot1dBasePort
45
                // dictionary from BRIDGE-MIB - don't write a 0 over a previously known port
46
                if ($existing_fdbs[$vlan_id][$mac_address_entry]['port_id'] != $new_port && $new_port != 0) {
47
                    DB::table('ports_fdb')
48
                        ->where('ports_fdb_id', $port_fdb_id)
49
                        ->update([
50
                            'port_id' => $new_port,
51
                            'updated_at' => $now,
52
                        ]);
53
                    echo 'U';
54
                } else {
55
                    $update_time_only[] = $port_fdb_id;
56
                    echo '.';
57
                }
58
                unset($existing_fdbs[$vlan_id][$mac_address_entry]);
59
            } else {
60
                if (is_null($entry['port_id'])) {
61
                    // fix SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'port_id' cannot be null
62
                    // If $entry['port_id'] truly is null then  Illuminate throws a fatal errory and all subsequent processing stops.
63
                    // Cisco ISO (and others) may have null ids. We still want them inserted as new
64
                    // strings work with DB::table->insert().
65
                    $entry['port_id'] = '';
66
                }
67
68
                DB::table('ports_fdb')->insert([
69
                    'port_id' => $entry['port_id'],
70
                    'mac_address' => $mac_address_entry,
71
                    'vlan_id' => $vlan_id,
72
                    'device_id' => $device['device_id'],
73
                    'created_at' => $now,
74
                    'updated_at' => $now,
75
                ]);
76
                echo '+';
77
            }
78
        }
79
80
        echo PHP_EOL;
81
    }
82
83
    DB::table('ports_fdb')->whereIn('ports_fdb_id', $update_time_only)->update(['updated_at' => $now]);
84
85
    //We do not delete anything here, as daily.sh will take care of the cleaning.
86
87
    // Delete old entries from the database
88
}
89
90
unset(
91
    $vlan_entry,
92
    $vlans_by_id,
93
    $existing_fdbs,
94
    $portid_dict,
95
    $vlans_dict,
96
    $insert,
97
    $sql_result,
98
    $vlans,
99
    $port,
100
    $fdbPort_table,
101
    $entries,
102
    $update_time_only,
103
    $now
104
);
105