Issues (2963)

includes/discovery/bgp-peers/firebrick.inc.php (1 issue)

1
<?php
2
/**
3
 * firebrick.inc.php
4
 *
5
 * LibreNMS bgp_peers for Firebrick 2700/2900/6000
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2020 Chris Malton (@cjsoftuk)
23
 * @author     Chris Malton (@cjsoftuk)
24
 */
25
26
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...
27
use LibreNMS\Util\IP;
28
29
$bgpPeersCache = snmpwalk_cache_multi_oid($device, 'fbBgpPeerTable', [], 'FIREBRICK-BGP-MIB', 'firebrick');
30
foreach ($bgpPeersCache as $key => $value) {
31
    $oid = explode('.', $key);
32
    $protocol = $oid[0];
33
    $address = str_replace($oid[0] . '.', '', $key);
34
    if (strlen($address) > 15) {
35
        $address = IP::fromHexString($address)->compressed();
36
    }
37
    if (isset($value['fbBgpPeerTableId']) && $value['fbBgpPeerTableId'] !== '') {
38
        $bgpPeers[$value['fbBgpPeerTableId']][$address] = $value;
39
    } else {
40
        $bgpPeers[0][$address] = $value;
41
    }
42
}
43
unset($bgpPeersCache);
44
45
foreach ($bgpPeers as $vrfId => $vrf) {
46
    if (empty($vrfId)) {
47
        $checkVrf = ' AND `vrf_id` IS NULL ';
48
        // Force to null to avoid 0s going to the DB instead of Nulls
49
        $vrfId = null;
50
    } else {
51
        $checkVrf = ' AND vrf_id = ? ';
52
        $vrfs = [
53
            'vrf_oid' => 'firebrick.' . $vrfId,
54
            'vrf_name' => $vrfId,
55
            'device_id' => $device['device_id'],
56
        ];
57
58
        if (! dbFetchCell('SELECT COUNT(*) FROM vrfs WHERE device_id = ? AND `vrf_oid`=?', [$device['device_id'], $vrfs['vrf_oid']])) {
59
            dbInsert($vrfs, 'vrfs');
60
        }
61
    }
62
    foreach ($vrf as $address => $value) {
63
        $astext = get_astext($value['fbBgpPeerRemoteAS']);
64
        if (dbFetchCell('SELECT COUNT(*) from `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ? ' . $checkVrf, [$device['device_id'], $address, $vrfId]) < '1') {
65
            $peers = [
66
                'device_id' => $device['device_id'],
67
                'vrf_id' => $vrfId,
68
                'bgpPeerIdentifier' => $address,
69
                'bgpPeerRemoteAs' => $value['fbBgpPeerRemoteAS'],
70
                'bgpPeerState' => 'idle',
71
                'bgpPeerAdminStatus' => 'stop',
72
                'bgpLocalAddr' => $value['fbBgpPeerLocalAddress'],
73
                'bgpPeerRemoteAddr' => $value['fbBgpPeerAddress'],
74
                'bgpPeerInUpdates' => 0,
75
                'bgpPeerOutUpdates' => 0,
76
                'bgpPeerInTotalMessages' => 0,
77
                'bgpPeerOutTotalMessages' => 0,
78
                'bgpPeerFsmEstablishedTime' => 0,
79
                'bgpPeerInUpdateElapsedTime' => 0,
80
                'astext' => $astext,
81
            ];
82
            dbInsert($peers, 'bgpPeers');
83
            if (Config::get('autodiscovery.bgp')) {
84
                $name = gethostbyaddr($address);
85
                discover_new_device($name, $device, 'BGP');
86
            }
87
            echo '+';
88
        } else {
89
            dbUpdate(['bgpPeerRemoteAs' => $value['fbBgpPeerRemoteAS'], 'astext' => $astext], 'bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
90
            echo '.';
91
        }
92
    }
93
}
94
// clean up peers
95
$peers = dbFetchRows('SELECT `vrf_id`, `bgpPeerIdentifier` FROM `bgpPeers` WHERE `device_id` = ?', [$device['device_id']]);
96
foreach ($peers as $value) {
97
    $vrfId = $value['vrf_id'];
98
    $address = $value['bgpPeerIdentifier'];
99
100
    // Cleanup code to deal with 0 vs Null in the DB
101
    if ($vrfId === 0) {
102
        // Database says it's table 0 - which is wrong.  It should be "null" for global table
103
        $deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
104
        echo str_repeat('-', $deleted);
105
        continue;
106
    } else {
107
        $testVrfId = empty($vrfId) ? 0 : $vrfId;
108
    }
109
110
    if (empty($bgpPeers[$testVrfId][$address])) {
111
        if ($vrfId === null) {
112
            $deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id IS NULL', [$device['device_id'], $address]);
113
        } else {
114
            $deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
115
        }
116
        echo str_repeat('-', $deleted);
117
    }
118
}
119
120
// TODO: Fix me to use the local AS as published
121
$bgpLocalAs = $bgpPeers[0][array_keys($bgpPeers[0])[0]]['fbBgpPeerRemoteAS'];
122