Issues (2963)

includes/discovery/bgp-peers/dell-os10.inc.php (3 issues)

1
<?php
2
3
/**
4
 * dell-os10.inc.php
5
 *
6
 * LibreNMS bgp_peers for Dell OS10
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  2021 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'] == 'dell-os10') {
32
        $bgpPeersCache = snmpwalk_cache_multi_oid($device, 'os10bgp4V2PeerTable', [], 'DELLEMC-OS10-BGP4V2-MIB', 'dell');
33
        foreach ($bgpPeersCache as $key => $value) {
34
            $oid = explode('.', $key);
35
            $vrfInstance = array_shift($oid);       // os10bgp4V2PeerInstance
36
            $remoteAddressType = array_shift($oid); // os10bgp4V2PeerRemoteAddrType
37
            $address = IP::fromSnmpString(implode(' ', $oid))->compressed(); // os10bgp4V2PeerRemoteAddr
38
            $bgpPeers[$vrfInstance][$address] = $value;
39
        }
40
        unset($bgpPeersCache);
41
42
        foreach ($bgpPeers as $vrfInstance => $peer) {
43
            $vrfId = dbFetchCell('SELECT vrf_id from `vrfs` WHERE vrf_oid = ?', [$vrfInstance]);
44
            if (is_null($vrfId)) {
45
                $vrfId = 1; // According to the MIB
46
            }
47
            foreach ($peer as $address => $value) {
48
                // resolve AS number by DNS_TXT record
49
                $astext = get_astext($value['os10bgp4V2PeerRemoteAs']);
50
51
                // FIXME - the `devices` table gets updated in the main bgp-peers.inc.php
52
                // Setting it here avoids the code that resets it to null if not found in BGP4-MIB.
53
                $bgpLocalAs = $value['os10bgp4V2PeerLocalAs'];
54
55
                if (dbFetchCell('SELECT count(*) FROM `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]) == 0) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing dbFetchCell('SELECT coun...d'], $address, $vrfId)) of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
56
                    $row = [
57
                        'device_id' => $device['device_id'],
58
                        'vrf_id' => $vrfId,
59
                        'bgpPeerIdentifier' => $address,
60
                        'bgpPeerRemoteAs' => $value['os10bgp4V2PeerRemoteAs'],
61
                        'bgpPeerState' => 'idle',
62
                        'bgpPeerAdminStatus' => 'stop',
63
                        'bgpLocalAddr' => '0.0.0.0',
64
                        'bgpPeerRemoteAddr' => '0.0.0.0',
65
                        'bgpPeerInUpdates' => 0,
66
                        'bgpPeerOutUpdates' => 0,
67
                        'bgpPeerInTotalMessages' => 0,
68
                        'bgpPeerOutTotalMessages' => 0,
69
                        'bgpPeerFsmEstablishedTime' => 0,
70
                        'bgpPeerInUpdateElapsedTime' => 0,
71
                        'astext' => $astext,
72
                    ];
73
                    dbInsert($row, 'bgpPeers');
74
75
                    if (Config::get('autodiscovery.bgp')) {
76
                        $name = gethostbyaddr($address);
77
                        discover_new_device($name, $device, 'BGP');
78
                    }
79
                    echo '+';
80
                } else {
81
                    dbUpdate(['bgpPeerRemoteAs' => $value['os10bgp4V2PeerRemoteAs'], 'astext' => $astext], 'bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
82
                    echo '.';
83
                }
84
            }
85
        }
86
87
        $af_data = snmpwalk_cache_oid($device, 'os10bgp4V2PrefixInPrefixes', [], 'DELLEMC-OS10-BGP4V2-MIB', 'dell');
88
        foreach ($af_data as $key => $value) {
89
            $oid = explode('.', $key);
90
            $vrfInstance = array_shift($oid);       // os10bgp4V2PeerInstance
91
            $remoteAddressType = array_shift($oid); // os10bgp4V2PeerRemoteAddrType
92
            $safi = array_pop($oid);                // os10bgp4V2PrefixGaugesSafi
93
            $afi = array_pop($oid);                 // os10bgp4V2PrefixGaugesAfi
94
            $address = IP::fromSnmpString(implode(' ', $oid))->compressed(); // os10bgp4V2PeerRemoteAddr
95
            // add to `bgpPeers_cbgp` table
96
            add_cbgp_peer($device, ['ip' => $address], $afi, $safi);
97
        }
98
99
        // clean up peers
100
        if (dbFetchCell('SELECT count(*) FROM `vrfs` WHERE `device_id` = ?', [$device['device_id']]) == 0) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing dbFetchCell('SELECT coun...($device['device_id'])) of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
101
            $peers = dbFetchRows('SELECT `vrf_id`, `bgpPeerIdentifier` FROM `bgpPeers` WHERE `device_id` = ?', [$device['device_id']]);
102
        } else {
103
            $peers = dbFetchRows('SELECT `B`.`vrf_id` AS `vrf_id`, `bgpPeerIdentifier` FROM `bgpPeers` AS B LEFT JOIN `vrfs` AS V ON `B`.`vrf_id` = `V`.`vrf_id` WHERE `B`.`device_id` = ?', [$device['device_id']]);
104
        }
105
        foreach ($peers as $peer) {
106
            $vrfId = $peer['vrf_id'];
107
            $address = $peer['bgpPeerIdentifier'];
108
109
            if (empty($bgpPeers[$vrfInstance][$address])) {
110
                $deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
111
112
                echo str_repeat('-', $deleted);
113
                echo PHP_EOL;
114
            }
115
        }
116
        unset($bgpPeers);
117
        // No return statement here, so standard BGP mib will still be polled after this file is executed.
118
    }
119
}
120