Issues (2963)

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

1
<?php
2
3
/**
4
 * timos.inc.php
5
 *
6
 * LibreNMS bgp_peers for Timos
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 *
21
 * @link       https://www.librenms.org
22
 *
23
 * @copyright  2020 LibreNMS Contributors
24
 * @author     LibreNMS Contributors
25
 */
26
27
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...
28
use LibreNMS\Util\IP;
29
30
if (Config::get('enable_bgp')) {
31
    if ($device['os'] == 'timos') {
32
        $bgpPeersCache = snmpwalk_cache_multi_oid($device, 'tBgpPeerNgTable', [], 'TIMETRA-BGP-MIB', 'nokia');
33
        foreach ($bgpPeersCache as $key => $value) {
34
            $oid = explode('.', $key);
35
            $vrfInstance = $oid[0];
36
            $address = str_replace($oid[0] . '.' . $oid[1] . '.', '', $key);
37
            if (strlen($address) > 15) {
38
                $address = IP::fromHexString($address)->compressed();
39
            }
40
            $bgpPeers[$vrfInstance][$address] = $value;
41
        }
42
        unset($bgpPeersCache);
43
44
        foreach ($bgpPeers as $vrfOid => $vrf) {
45
            $vrfId = dbFetchCell('SELECT vrf_id from `vrfs` WHERE vrf_oid = ?', [$vrfOid]);
46
            foreach ($vrf as $address => $value) {
47
                $astext = get_astext($value['tBgpPeerNgPeerAS4Byte']);
48
49
                if (dbFetchCell('SELECT COUNT(*) from `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]) < '1') {
50
                    $peers = [
51
                        'device_id' => $device['device_id'],
52
                        'vrf_id' => $vrfId,
53
                        'bgpPeerIdentifier' => $address,
54
                        'bgpPeerRemoteAs' => $value['tBgpPeerNgPeerAS4Byte'],
55
                        'bgpPeerState' => 'idle',
56
                        'bgpPeerAdminStatus' => 'stop',
57
                        'bgpLocalAddr' => '0.0.0.0',
58
                        'bgpPeerRemoteAddr' => '0.0.0.0',
59
                        'bgpPeerInUpdates' => 0,
60
                        'bgpPeerOutUpdates' => 0,
61
                        'bgpPeerInTotalMessages' => 0,
62
                        'bgpPeerOutTotalMessages' => 0,
63
                        'bgpPeerFsmEstablishedTime' => 0,
64
                        'bgpPeerInUpdateElapsedTime' => 0,
65
                        'astext' => $astext,
66
                    ];
67
                    dbInsert($peers, 'bgpPeers');
68
                    if (Config::get('autodiscovery.bgp')) {
69
                        $name = gethostbyaddr($address);
70
                        discover_new_device($name, $device, 'BGP');
71
                    }
72
                    echo '+';
73
                } else {
74
                    dbUpdate(['bgpPeerRemoteAs' => $value['tBgpPeerNgPeerAS4Byte'], 'astext' => $astext], 'bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
75
                    echo '.';
76
                }
77
            }
78
        }
79
        // clean up peers
80
        $peers = dbFetchRows('SELECT `B`.`vrf_id` AS `vrf_id`, `bgpPeerIdentifier`, `vrf_oid` FROM `bgpPeers` AS B LEFT JOIN `vrfs` AS V ON `B`.`vrf_id` = `V`.`vrf_id` WHERE `B`.`device_id` = ?', [$device['device_id']]);
81
        foreach ($peers as $value) {
82
            $vrfId = $value['vrf_id'];
83
            $vrfOid = $value['vrf_oid'];
84
            $address = $value['bgpPeerIdentifier'];
85
86
            if (empty($bgpPeers[$vrfOid][$address])) {
87
                $deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
88
89
                echo str_repeat('-', $deleted);
90
                echo PHP_EOL;
91
            }
92
        }
93
        unset($bgpPeers);
94
        // No return statement here, so standard BGP mib will still be polled after this file is executed.
95
    }
96
}
97