Issues (2963)

discovery/sensors/cisco-entity-sensor.inc.php (1 issue)

1
<?php
2
3
use Illuminate\Support\Str;
4
5
if ($device['os_group'] == 'cisco') {
6
    echo ' CISCO-ENTITY-SENSOR: ';
7
8
    $oids = [];
9
    echo 'Caching OIDs:';
10
11
    if (empty($entity_array)) {
12
        $tmp_oids = ['entPhysicalDescr', 'entPhysicalName', 'entPhysicalClass', 'entPhysicalContainedIn', 'entPhysicalParentRelPos'];
13
        $entity_array = [];
14
        foreach ($tmp_oids as $tmp_oid) {
15
            echo " $tmp_oid";
16
            $entity_array = snmpwalk_cache_multi_oid($device, $tmp_oid, $entity_array, 'ENTITY-MIB:CISCO-ENTITY-SENSOR-MIB');
17
        }
18
        echo ' entAliasMappingIdentifier';
19
        $entity_array = snmpwalk_cache_twopart_oid($device, 'entAliasMappingIdentifier', $entity_array, 'ENTITY-MIB:IF-MIB');
20
    }
21
22
    echo '  entSensorType';
23
    $oids = snmpwalk_cache_multi_oid($device, 'entSensorType', $oids, 'CISCO-ENTITY-SENSOR-MIB');
24
    echo ' entSensorScale';
25
    $oids = snmpwalk_cache_multi_oid($device, 'entSensorScale', $oids, 'CISCO-ENTITY-SENSOR-MIB');
26
    echo ' entSensorValue';
27
    $oids = snmpwalk_cache_multi_oid($device, 'entSensorValue', $oids, 'CISCO-ENTITY-SENSOR-MIB');
28
    echo ' entSensorMeasuredEntity';
29
    $oids = snmpwalk_cache_multi_oid($device, 'entSensorMeasuredEntity', $oids, 'CISCO-ENTITY-SENSOR-MIB');
30
    echo ' entSensorPrecision';
31
    $oids = snmpwalk_cache_multi_oid($device, 'entSensorPrecision', $oids, 'CISCO-ENTITY-SENSOR-MIB');
32
33
    $t_oids = [];
34
    echo ' entSensorThresholdSeverity';
35
    $t_oids = snmpwalk_cache_twopart_oid($device, 'entSensorThresholdSeverity', $t_oids, 'CISCO-ENTITY-SENSOR-MIB');
36
    echo ' entSensorThresholdRelation';
37
    $t_oids = snmpwalk_cache_twopart_oid($device, 'entSensorThresholdRelation', $t_oids, 'CISCO-ENTITY-SENSOR-MIB');
38
    echo ' entSensorThresholdValue';
39
    $t_oids = snmpwalk_cache_twopart_oid($device, 'entSensorThresholdValue', $t_oids, 'CISCO-ENTITY-SENSOR-MIB');
40
41
    d_echo($oids);
42
43
    $entitysensor['voltsDC'] = 'voltage';
44
    $entitysensor['voltsAC'] = 'voltage';
45
    $entitysensor['amperes'] = 'current';
46
    $entitysensor['watt'] = 'power';
47
    $entitysensor['hertz'] = 'freq';
48
    $entitysensor['percentRH'] = 'humidity';
49
    $entitysensor['rpm'] = 'fanspeed';
50
    $entitysensor['celsius'] = 'temperature';
51
    $entitysensor['watts'] = 'power';
52
    $entitysensor['dBm'] = 'dbm';
53
54
    if (is_array($oids)) {
55
        foreach ($oids as $index => $entry) {
56
            // echo("[" . $entry['entSensorType'] . "|" . $entry['entSensorValue']. "|" . $index . "]");
57
            if ($entitysensor[$entry['entSensorType']] && is_numeric($entry['entSensorValue']) && is_numeric($index)) {
58
                $entPhysicalIndex = $index;
59
                if ($entity_array[$index]['entPhysicalName'] || $device['os'] == 'iosxr') {
60
                    $descr = rewrite_entity_descr($entity_array[$index]['entPhysicalName']);
61
                } else {
62
                    $descr = rewrite_entity_descr($entity_array[$index]['entPhysicalDescr']);
63
                }
64
65
                // Set description based on measured entity if it exists
66
                if (is_numeric($entry['entSensorMeasuredEntity']) && $entry['entSensorMeasuredEntity']) {
67
                    $measured_descr = $entity_array[$entry['entSensorMeasuredEntity']]['entPhysicalName'];
68
                    if (! $measured_descr) {
69
                        $measured_descr = $entity_array[$entry['entSensorMeasuredEntity']]['entPhysicalDescr'];
70
                    }
71
72
                    $descr = $measured_descr . ' - ' . $descr;
73
                }
74
75
                // Bit dirty also, clean later
76
                $descr = str_replace('Temp: ', '', $descr);
77
                $descr = str_ireplace('temperature ', '', $descr);
78
79
                $oid = '.1.3.6.1.4.1.9.9.91.1.1.1.1.4.' . $index;
80
                $current = $entry['entSensorValue'];
81
                $type = $entitysensor[$entry['entSensorType']];
82
83
                // echo("$index : ".$entry['entSensorScale']."|");
84
                // FIXME this stuff is foul
85
                if ($entry['entSensorScale'] == 'nano') {
86
                    $divisor = '1000000000';
87
                    $multiplier = '1';
88
                }
89
90
                if ($entry['entSensorScale'] == 'micro') {
91
                    $divisor = '1000000';
92
                    $multiplier = '1';
93
                }
94
95
                if ($entry['entSensorScale'] == 'milli') {
96
                    $divisor = '1000';
97
                    $multiplier = '1';
98
                }
99
100
                if ($entry['entSensorScale'] == 'units') {
101
                    $divisor = '1';
102
                    $multiplier = '1';
103
                }
104
105
                if ($entry['entSensorScale'] == 'kilo') {
106
                    $divisor = '1';
107
                    $multiplier = '1000';
108
                }
109
110
                if ($entry['entSensorScale'] == 'mega') {
111
                    $divisor = '1';
112
                    $multiplier = '1000000';
113
                }
114
115
                if ($entry['entSensorScale'] == 'giga') {
116
                    $divisor = '1';
117
                    $multiplier = '1000000000';
118
                }
119
120
                if (is_numeric($entry['entSensorPrecision'])
121
                        && $entry['entSensorPrecision'] > '0'
122
                        // Workaround for a Cisco SNMP bug
123
                        && $entry['entSensorPrecision'] != '1615384784'
124
                ) {
125
                    // Use precision value to determine decimal point place on returned value, then apply divisor
126
                    $divisor = (10 ** $entry['entSensorPrecision']) * $divisor;
127
                }
128
129
                $current = ($current * $multiplier / $divisor);
130
131
                // Set thresholds to null
132
                $limit = null;
133
                $limit_low = null;
134
                $warn_limit = null;
135
                $warn_limit_low = null;
136
137
                // Check thresholds for this entry (bit dirty, but it works!)
138
                if (is_array($t_oids[$index])) {
139
                    foreach ($t_oids[$index] as $t_index => $key) {
140
                        // Skip invalid treshold values
141
                        if ($key['entSensorThresholdValue'] == '-32768') {
142
                            continue;
143
                        }
144
                        // Critical Limit
145
                        if (($key['entSensorThresholdSeverity'] == 'major' || $key['entSensorThresholdSeverity'] == 'critical') && ($key['entSensorThresholdValue'] != 0) && ($key['entSensorThresholdRelation'] == 'greaterOrEqual' || $key['entSensorThresholdRelation'] == 'greaterThan')) {
146
                            $limit = ($key['entSensorThresholdValue'] * $multiplier / $divisor);
147
                        }
148
149
                        if (($key['entSensorThresholdSeverity'] == 'major' || $key['entSensorThresholdSeverity'] == 'critical') && ($key['entSensorThresholdValue'] != 0) && ($key['entSensorThresholdRelation'] == 'lessOrEqual' || $key['entSensorThresholdRelation'] == 'lessThan')) {
150
                            $limit_low = ($key['entSensorThresholdValue'] * $multiplier / $divisor);
151
                        }
152
153
                        // Warning Limit
154
                        if ($key['entSensorThresholdSeverity'] == 'minor' && ($key['entSensorThresholdRelation'] == 'greaterOrEqual' || $key['entSensorThresholdRelation'] == 'greaterThan')) {
155
                            $warn_limit = ($key['entSensorThresholdValue'] * $multiplier / $divisor);
156
                        }
157
158
                        if ($key['entSensorThresholdSeverity'] == 'minor' && ($key['entSensorThresholdRelation'] == 'lessOrEqual' || $key['entSensorThresholdRelation'] == 'lessThan')) {
159
                            $warn_limit_low = ($key['entSensorThresholdValue'] * $multiplier / $divisor);
160
                        }
161
                    }//end foreach
162
                }//end if
163
164
                // If temperature sensor, set low thresholds to -1 and -5. Many sensors don't return low thresholds, therefore LibreNMS takes the runtime low
165
                // Also changing 0 values (not just null) as Libre loses these somewhere along the line and shows an empty value in the Web UI
166
                if ($type == 'temperature') {
167
                    if ($warn_limit_low == 0) {
168
                        $warn_limit_low = -1;
169
                    }
170
                    if ($limit_low == 0) {
171
                        $limit_low = -5;
172
                    }
173
                }
174
175
                // End Threshold code
176
                $ok = true;
177
178
                if ($current == '-127' || $descr == '') {
179
                    $ok = false;
180
                }
181
182
                if ($ok) {
183
                    $phys_index = $entity_array[$index]['entPhysicalContainedIn'];
184
                    while ($phys_index != 0) {
185
                        if ($index === $phys_index) {
186
                            break;
187
                        }
188
                        if ($entity_array[$phys_index]['entPhysicalClass'] === 'port') {
189
                            if (Str::contains($entity_array[$phys_index][0]['entAliasMappingIdentifier'], 'ifIndex.')) {
190
                                [, $tmp_ifindex] = explode('.', $entity_array[$phys_index][0]['entAliasMappingIdentifier']);
191
                                $tmp_port = get_port_by_index_cache($device['device_id'], $tmp_ifindex);
192
                                if (is_array($tmp_port)) {
193
                                    $entPhysicalIndex = $tmp_ifindex;
194
                                    $entry['entSensorMeasuredEntity'] = 'ports';
195
                                }
196
                            }
197
                            break;
198
                        } else {
199
                            $phys_index = $entity_array[$phys_index]['entPhysicalContainedIn'];
200
                        }
201
                    }
202
                    discover_sensor($valid['sensor'], $type, $device, $oid, $index, 'cisco-entity-sensor', ucwords($descr), $divisor, $multiplier, $limit_low, $warn_limit_low, $warn_limit, $limit, $current, 'snmp', $entPhysicalIndex, $entry['entSensorMeasuredEntity'], null);
0 ignored issues
show
It seems like $descr can also be of type array; however, parameter $string of ucwords() 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

202
                    discover_sensor($valid['sensor'], $type, $device, $oid, $index, 'cisco-entity-sensor', ucwords(/** @scrutinizer ignore-type */ $descr), $divisor, $multiplier, $limit_low, $warn_limit_low, $warn_limit, $limit, $current, 'snmp', $entPhysicalIndex, $entry['entSensorMeasuredEntity'], null);
Loading history...
203
                    //Cisco IOS-XR : add a fake sensor to graph as dbm
204
                    if ($type == 'power' and $device['os'] == 'iosxr' and (preg_match('/power (R|T)x/i', $descr) or preg_match('/(R|T)x Power/i', $descr) or preg_match('/(R|T)x Lane/i', $descr))) {
205
                        // convert Watts to dbm
206
                        $user_func = 'mw_to_dbm';
207
                        $type = 'dbm';
208
                        $limit_low = 10 * log10($limit_low * 1000);
209
                        $warn_limit_low = 10 * log10($warn_limit_low * 1000);
210
                        $warn_limit = 10 * log10($warn_limit * 1000);
211
                        $limit = 10 * log10($limit * 1000);
212
                        $current = round(10 * log10($current * 1000), 3);
213
                        $multiplier = 1000;
214
                        //echo("\n".$valid['sensor'].", $type, $device, $oid, $index, 'cisco-entity-sensor', $descr, $divisor, $multiplier, $limit_low, $warn_limit_low, $warn_limit, $limit, $current, $user_func");
215
                        discover_sensor($valid['sensor'], $type, $device, $oid, $index, 'cisco-entity-sensor', $descr, $divisor, $multiplier, $limit_low, $warn_limit_low, $warn_limit, $limit, $current, 'snmp', $entPhysicalIndex, $entry['entSensorMeasuredEntity'], $user_func);
216
                    }
217
                }
218
219
                $cisco_entity_temperature = 1;
220
                unset($limit, $limit_low, $warn_limit, $warn_limit_low);
221
            }//end if
222
        }//end foreach
223
    }//end if
224
    unset(
225
        $entity_array
226
    );
227
}//end if
228