Issues (2963)

includes/discovery/stp.inc.php (2 issues)

1
<?php
2
/*
3
 * LibreNMS
4
 *
5
 * Copyright (c) 2015 Vitali Kari <[email protected]>
6
 *
7
 * This program is free software: you can redistribute it and/or modify it
8
 * under the terms of the GNU General Public License as published by the
9
 * Free Software Foundation, either version 3 of the License, or (at your
10
 * option) any later version.  Please see LICENSE.txt at the top level of
11
 * the source code distribution for details.
12
 *
13
 * Based on IEEE-802.1D-2004, (STP, RSTP)
14
 * needs RSTP-MIB
15
 */
16
17
// Pre-cache existing state of STP for this device from database
18
$stp_db = dbFetchRow('SELECT * FROM `stp` WHERE `device_id` = ?', [$device['device_id']]);
19
//d_echo($stp_db);
20
21
$stpprotocol = snmp_get($device, 'dot1dStpProtocolSpecification.0', '-Oqv', 'RSTP-MIB');
22
23
// FIXME I don't know what "unknown" means, perhaps MSTP? (saw it on some cisco devices)
24
// But we can try to retrieve data
25
if ($stpprotocol == 'ieee8021d' || $stpprotocol == 'unknown') {
26
    // set time multiplier to convert from centiseconds to seconds
27
    // all time values are stored in databese as seconds
28
    $tm = '0.01';
29
    // some vendors like PBN dont follow the 802.1D implementation and use seconds in SNMP
30
    if ($device['os'] == 'pbn') {
31
        preg_match('/^.* Build (?<build>\d+)/', $device['version'], $version);
32
        if ($version['build'] <= 16607) { // Buggy version :-(
33
            $tm = '1';
34
        }
35
    }
36
37
    // read the 802.1D subtree
38
    $stp_raw = snmpwalk_cache_oid($device, 'dot1dStp', [], 'RSTP-MIB');
39
    $stp = [
40
        'protocolSpecification'   => $stp_raw[0]['dot1dStpProtocolSpecification'],
41
        'priority'                => $stp_raw[0]['dot1dStpPriority'],
42
        'topChanges'              => $stp_raw[0]['dot1dStpTopChanges'],
43
        'rootCost'                => $stp_raw[0]['dot1dStpRootCost'],
44
        'rootPort'                => $stp_raw[0]['dot1dStpRootPort'],
45
        'maxAge'                  => $stp_raw[0]['dot1dStpMaxAge'] * $tm,
46
        'helloTime'               => $stp_raw[0]['dot1dStpHelloTime'] * $tm,
47
        'holdTime'                => $stp_raw[0]['dot1dStpHoldTime'] * $tm,
48
        'forwardDelay'            => $stp_raw[0]['dot1dStpForwardDelay'] * $tm,
49
        'bridgeMaxAge'            => $stp_raw[0]['dot1dStpBridgeMaxAge'] * $tm,
50
        'bridgeHelloTime'         => $stp_raw[0]['dot1dStpBridgeHelloTime'] * $tm,
51
        'bridgeForwardDelay'      => $stp_raw[0]['dot1dStpBridgeForwardDelay'] * $tm,
52
    ];
53
54
    // set device binding
55
    $stp['device_id'] = $device['device_id'];
56
57
    // read the 802.1D bridge address and set as MAC in database
58
    $mac_raw = snmp_get($device, 'dot1dBaseBridgeAddress.0', '-Oqv', 'RSTP-MIB');
59
60
    // read Time as timetics (in hundredths of a seconds) since last topology change and convert to seconds
61
    $time_since_change = snmp_get($device, 'dot1dStpTimeSinceTopologyChange.0', '-Ovt', 'RSTP-MIB');
62
    if ($time_since_change > '100') {
63
        $time_since_change = substr($time_since_change, 0, -2); // convert to seconds since change
0 ignored issues
show
It seems like $time_since_change can also be of type false; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $time_since_change = substr(/** @scrutinizer ignore-type */ $time_since_change, 0, -2); // convert to seconds since change
Loading history...
64
    } else {
65
        $time_since_change = '0';
66
    }
67
    $stp['timeSinceTopologyChange'] = $time_since_change;
68
69
    // designated root is stored in format 2 octet bridge priority + MAC address, so we need to normalize it
70
    $dr = str_replace([' ', ':', '-'], '', strtolower($stp_raw[0]['dot1dStpDesignatedRoot']));
71
    $dr = substr($dr, -12); //remove first two octets
72
    $stp['designatedRoot'] = $dr;
73
74
    // normalize the MAC
75
    $mac_array = explode(':', $mac_raw);
0 ignored issues
show
It seems like $mac_raw can also be of type false; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
    $mac_array = explode(':', /** @scrutinizer ignore-type */ $mac_raw);
Loading history...
76
    foreach ($mac_array as &$octet) {
77
        if (strlen($octet) < 2) {
78
            $octet = '0' . $octet; // add suppressed 0
79
        }
80
    }
81
    $stp['bridgeAddress'] = implode($mac_array);
82
83
    // I'm the boss?
84
    if ($stp['bridgeAddress'] == $stp['designatedRoot']) {
85
        $stp['rootBridge'] = '1';
86
    } else {
87
        $stp['rootBridge'] = '0';
88
    }
89
90
    d_echo($stp);
91
92
    if ($stp_raw[0]['version'] == '3') {
93
        echo 'RSTP ';
94
    } else {
95
        echo 'STP ';
96
    }
97
98
    if (! $stp_db['bridgeAddress'] && $stp['bridgeAddress']) {
99
        dbInsert($stp, 'stp');
100
        log_event('STP added, bridge address: ' . $stp['bridgeAddress'], $device, 'stp', 3);
101
        echo '+';
102
    }
103
104
    if ($stp_db['bridgeAddress'] && ! $stp['bridgeAddress']) {
105
        dbDelete('stp', 'device_id = ?', [$device['device_id']]);
106
        log_event('STP removed', $device, 'stp', 4);
107
        echo '-';
108
    }
109
110
    // STP port related stuff
111
    foreach ($stp_raw as $port => $value) {
112
        if ($port) { // $stp_raw[0] ist not port related so we skip this one
113
            $stp_port = [
114
                'priority'              => $stp_raw[$port]['dot1dStpPortPriority'],
115
                'state'                 => $stp_raw[$port]['dot1dStpPortState'],
116
                'enable'                => $stp_raw[$port]['dot1dStpPortEnable'],
117
                'pathCost'              => $stp_raw[$port]['dot1dStpPortPathCost'],
118
                'designatedCost'        => $stp_raw[$port]['dot1dStpPortDesignatedCost'],
119
                'designatedPort'        => $stp_raw[$port]['dot1dStpPortDesignatedPort'],
120
                'forwardTransitions'    => $stp_raw[$port]['dot1dStpPortForwardTransitions'],
121
            ];
122
123
            // set device binding
124
            $stp_port['device_id'] = $device['device_id'];
125
126
            // set port binding
127
            $stp_port['port_id'] = dbFetchCell('SELECT port_id FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', [$device['device_id'], $stp_raw[$port]['dot1dStpPort']]);
128
129
            $dr = str_replace(['.', ' ', ':', '-'], '', strtolower($stp_raw[$port]['dot1dStpPortDesignatedRoot']));
130
            $dr = substr($dr, -12); //keep the last 12 chars (the mac address)
131
            $stp_port['designatedRoot'] = $dr;
132
133
            $db = str_replace(['.', ' ', ':', '-'], '', strtolower($stp_raw[$port]['dot1dStpPortDesignatedBridge']));
134
            $db = substr($db, -12); //keep the last 12 chars (the mac address)
135
            $stp_port['designatedBridge'] = $db;
136
137
            if ($device['os'] == 'pbn') {
138
                // It seems that PBN guys don't care about ieee 802.1d :-(
139
                // So try to find the right port with some crazy conversations
140
                $dp_value = dechex($stp_port['priority']);
141
                $dp_value = $dp_value . '00';
142
                $dp_value = hexdec($dp_value);
143
                if ($stp_raw[$port]['dot1dStpPortDesignatedPort']) {
144
                    $dp = $stp_raw[$port]['dot1dStpPortDesignatedPort'] - $dp_value;
145
                    $stp_port['designatedPort'] = $dp;
146
                }
147
            } else {
148
                if (preg_match('/-/', $stp_raw[$port]['dot1dStpPortDesignatedPort'])) {
149
                    // Syntax with "priority" dash "portID" like so : 32768-54, both in decimal
150
                    $dp = explode('-', $stp_raw[$port]['dot1dStpPortDesignatedPort'])[1];
151
                    $stp_port['designatedPort'] = $dp;
152
                } else {
153
                    // Port saved in format priority+port (ieee 802.1d-1998: clause 8.5.5.1)
154
                    $dp = substr($stp_raw[$port]['dot1dStpPortDesignatedPort'], -2); //discard the first octet (priority part)
155
                    $stp_port['designatedPort'] = hexdec($dp);
156
                }
157
            }
158
159
            d_echo($stp_port);
160
161
            // Write to db
162
            if (! dbFetchCell('SELECT 1 FROM `ports_stp` WHERE `device_id` = ? AND `port_id` = ?', [$device['device_id'], $stp_port['port_id']])) {
163
                dbInsert($stp_port, 'ports_stp');
164
                echo '+';
165
            }
166
        }
167
    }
168
169
    // Delete STP ports from db if absent in SNMP query
170
    $stp_port_db = dbFetchRows('SELECT * FROM `ports_stp` WHERE `device_id` = ?', [$device['device_id']]);
171
    //d_echo($stp_port_db);
172
    foreach ($stp_port_db as $port => $value) {
173
        $if_index = dbFetchCell('SELECT `ifIndex` FROM `ports` WHERE `device_id` = ? AND `port_id` = ?', [$device['device_id'], $value['port_id']]);
174
        if ($if_index != $stp_raw[$if_index]['dot1dStpPort']) {
175
            dbDelete('ports_stp', '`device_id` = ? AND `port_id` = ?', [$device['device_id'], $value['port_id']]);
176
            echo '-';
177
        }
178
    }
179
}
180
181
unset($stp_raw, $stp, $stp_db, $stp_port, $stp_port_db);
182
echo "\n";
183