Issues (2963)

includes/rewrites.php (1 issue)

1
<?php
2
3
use LibreNMS\Config;
4
use LibreNMS\Util\Rewrite;
5
6
function rewrite_entity_descr($descr)
7
{
8
    $descr = str_replace('Distributed Forwarding Card', 'DFC', $descr);
9
    $descr = preg_replace('/7600 Series SPA Interface Processor-/', '7600 SIP-', $descr);
10
    $descr = preg_replace('/Rev\.\ [0-9\.]+\ /', '', $descr);
11
    $descr = preg_replace('/12000 Series Performance Route Processor/', '12000 PRP', $descr);
12
    $descr = preg_replace('/^12000/', '', $descr);
13
    $descr = preg_replace('/Gigabit Ethernet/', 'GigE', $descr);
14
    $descr = preg_replace('/^ASR1000\ /', '', $descr);
15
    $descr = str_replace('Routing Processor', 'RP', $descr);
16
    $descr = str_replace('Route Processor', 'RP', $descr);
17
    $descr = str_replace('Switching Processor', 'SP', $descr);
18
    $descr = str_replace('Sub-Module', 'Module ', $descr);
19
    $descr = str_replace('DFC Card', 'DFC', $descr);
20
    $descr = str_replace('Centralized Forwarding Card', 'CFC', $descr);
21
    $descr = str_replace('Power Supply Module', 'PSU ', $descr);
22
    $descr = str_replace('/Voltage Sensor/', 'Voltage', $descr);
23
    $descr = str_replace('Sensor', '', $descr);
24
    $descr = str_replace('PMOD', 'PSU', $descr);
25
    $descr = preg_replace('/^temperatures /', '', $descr);
26
    $descr = preg_replace('/^voltages /', '', $descr);
27
    $descr = str_replace('PowerSupply', 'PSU ', $descr);
28
29
    return $descr;
30
}
31
32
/**
33
 * Clean port values for html display
34
 * Add label to the port array (usually one of ifAlias, ifName, ifDescr)
35
 *
36
 * @param  array  $interface
37
 * @param  null|array  $device
38
 * @return mixed
39
 */
40
function cleanPort($interface, $device = null)
41
{
42
    if (! $interface) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $interface of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
        return $interface;
44
    }
45
    $interface['ifAlias'] = \LibreNMS\Util\Clean::html($interface['ifAlias'], []);
46
    $interface['ifName'] = \LibreNMS\Util\Clean::html($interface['ifName'], []);
47
    $interface['ifDescr'] = \LibreNMS\Util\Clean::html($interface['ifDescr'], []);
48
49
    if (! $device) {
50
        $device = device_by_id_cache($interface['device_id']);
51
    }
52
53
    $os = strtolower($device['os']);
54
55
    if (Config::get("os.$os.ifname")) {
56
        $interface['label'] = $interface['ifName'];
57
58
        if ($interface['ifName'] == '') {
59
            $interface['label'] = $interface['ifDescr'];
60
        }
61
    } elseif (Config::get("os.$os.ifalias")) {
62
        $interface['label'] = $interface['ifAlias'];
63
    } else {
64
        $interface['label'] = $interface['ifDescr'];
65
        if (Config::get("os.$os.ifindex")) {
66
            $interface['label'] = $interface['label'] . ' ' . $interface['ifIndex'];
67
        }
68
    }
69
70
    if ($device['os'] == 'speedtouch') {
71
        [$interface['label']] = explode('thomson', $interface['label']);
72
    }
73
74
    if (is_array(Config::get('rewrite_if'))) {
75
        foreach (Config::get('rewrite_if') as $src => $val) {
76
            if (stristr($interface['label'], $src)) {
77
                $interface['label'] = $val;
78
            }
79
        }
80
    }
81
82
    if (is_array(Config::get('rewrite_if_regexp'))) {
83
        foreach (Config::get('rewrite_if_regexp') as $reg => $val) {
84
            if (preg_match($reg . 'i', $interface['label'])) {
85
                $interface['label'] = preg_replace($reg . 'i', $val, $interface['label']);
86
            }
87
        }
88
    }
89
90
    return $interface;
91
}
92
93
// Specific rewrite functions
94
95
function makeshortif($if)
96
{
97
    $rewrite_shortif = [
98
        'tengigabitethernet'  => 'Te',
99
        'ten-gigabitethernet' => 'Te',
100
        'tengige'             => 'Te',
101
        'gigabitethernet'     => 'Gi',
102
        'fastethernet'        => 'Fa',
103
        'ethernet'            => 'Et',
104
        'serial'              => 'Se',
105
        'pos'                 => 'Pos',
106
        'port-channel'        => 'Po',
107
        'atm'                 => 'Atm',
108
        'null'                => 'Null',
109
        'loopback'            => 'Lo',
110
        'dialer'              => 'Di',
111
        'vlan'                => 'Vlan',
112
        'tunnel'              => 'Tunnel',
113
        'serviceinstance'     => 'SI',
114
        'dwdm'                => 'DWDM',
115
        'bundle-ether'        => 'BE',
116
    ];
117
118
    $if = \LibreNMS\Util\Rewrite::normalizeIfName($if);
119
    $if = strtolower($if);
120
    $if = str_replace(array_keys($rewrite_shortif), array_values($rewrite_shortif), $if);
121
122
    return $if;
123
}
124
125
function rewrite_junose_hardware($hardware)
126
{
127
    $rewrite_junose_hardware = [
128
        'juniErx1400' => 'ERX-1400',
129
        'juniErx700'  => 'ERX-700',
130
        'juniErx1440' => 'ERX-1440',
131
        'juniErx705'  => 'ERX-705',
132
        'juniErx310'  => 'ERX-310',
133
        'juniE320'    => 'E320',
134
        'juniE120'    => 'E120',
135
        'juniSsx1400' => 'SSX-1400',
136
        'juniSsx700'  => 'SSX-700',
137
        'juniSsx1440' => 'SSX-1440',
138
    ];
139
140
    $hardware = str_replace(array_keys($rewrite_junose_hardware), array_values($rewrite_junose_hardware), $hardware);
141
142
    return $hardware;
143
}
144
145
function rewrite_generic_hardware($hardware)
146
{
147
    $rewrite_GenericHW = [
148
        ' Computer Corporation' => '',
149
        ' Corporation'          => '',
150
        ' Inc.'                 => '',
151
    ];
152
153
    return str_replace(array_keys($rewrite_GenericHW), array_values($rewrite_GenericHW), $hardware);
154
}
155
156
function short_hrDeviceDescr($dev)
157
{
158
    $rewrite_hrDevice = [
159
        'GenuineIntel:' => '',
160
        'AuthenticAMD:' => '',
161
        'Intel(R)'      => '',
162
        'CPU'           => '',
163
        '(R)'           => '',
164
        '  '            => ' ',
165
    ];
166
167
    $dev = str_replace(array_keys($rewrite_hrDevice), array_values($rewrite_hrDevice), $dev);
168
    $dev = preg_replace('/\ +/', ' ', $dev);
169
    $dev = trim($dev);
170
171
    return $dev;
172
}
173
174
function short_port_descr($desc)
175
{
176
    [$desc] = explode('(', $desc);
177
    [$desc] = explode('[', $desc);
178
    [$desc] = explode('{', $desc);
179
    [$desc] = explode('|', $desc);
180
    [$desc] = explode('<', $desc);
181
    $desc = trim($desc);
182
183
    return $desc;
184
}
185
186
function rewrite_adslLineType($adslLineType)
187
{
188
    $adslLineTypes = [
189
        'noChannel'          => 'No Channel',
190
        'fastOnly'           => 'Fastpath',
191
        'interleavedOnly'    => 'Interleaved',
192
        'fastOrInterleaved'  => 'Fast/Interleaved',
193
        'fastAndInterleaved' => 'Fast+Interleaved',
194
    ];
195
196
    foreach ($adslLineTypes as $type => $text) {
197
        if ($adslLineType == $type) {
198
            $adslLineType = $text;
199
        }
200
    }
201
202
    return $adslLineType;
203
}
204
205
function ipmiSensorName($hardwareId, $sensorIpmi)
206
{
207
    $ipmiSensorsNames = [
208
        'HP ProLiant BL460c G6' => [
209
            'Temp 1' => 'Ambient zone',
210
            'Temp 2' => 'CPU 1',
211
            'Temp 3' => 'CPU 2',
212
            'Temp 4' => 'Memory zone',
213
            'Temp 5' => 'Memory zone',
214
            'Temp 6' => 'Memory zone',
215
            'Temp 7' => 'System zone',
216
            'Temp 8' => 'System zone',
217
            'Temp 9' => 'System zone',
218
            'Temp 10' => 'Storage zone',
219
            'Power Meter' => 'Power usage',
220
        ],
221
        'HP ProLiant BL460c G1' => [
222
            'Temp 1' => 'System zone',
223
            'Temp 2' => 'CPU 1 zone',
224
            'Temp 3' => 'CPU 1',
225
            'Temp 4' => 'CPU 1',
226
            'Temp 5' => 'CPU 2 zone',
227
            'Temp 6' => 'CPU 2',
228
            'Temp 7' => 'CPU 2',
229
            'Temp 8' => 'Memory zone',
230
            'Temp 9' => 'Ambient zone',
231
            'Power Meter' => 'Power usage',
232
        ],
233
    ];
234
235
    if (isset($ipmiSensorsNames[$hardwareId], $ipmiSensorsNames[$hardwareId][$sensorIpmi])) {
236
        return $ipmiSensorsNames[$hardwareId][$sensorIpmi];
237
    }
238
239
    return $sensorIpmi;
240
}
241
242
/**
243
 * @param $descr
244
 * @return int
245
 */
246
function get_nagios_state($descr)
247
{
248
    switch ($descr) {
249
        case 'On':
250
        case 'Okay':
251
        case 'Ok':
252
            return 0;
253
            break;
254
        case 'Standby':
255
        case 'Idle':
256
        case 'Maintenance':
257
            return 1;
258
            break;
259
        case 'Under':
260
        case 'Over':
261
            return 2;
262
            break;
263
        default:
264
            return 3;
265
            break;
266
    }
267
}
268
269
/**
270
 * @param $state
271
 * @return int
272
 */
273
function apc_relay_state($state)
274
{
275
    switch ($state) {
276
        case 'immediateCloseEMS':
277
        case 'immediateOnEMS':
278
            return 1;
279
            break;
280
        case 'immediateOpenEMS':
281
        case 'immediateOffEMS':
282
            return 2;
283
            break;
284
    }
285
}
286
287
/**
288
 * @param $value
289
 * @return mixed
290
 */
291
function return_number($value)
292
{
293
    preg_match('/[\d\.\-]+/', $value, $temp_response);
294
    if (! empty($temp_response[0])) {
295
        $value = $temp_response[0];
296
    }
297
298
    return $value;
299
}
300
301
function parse_entity_state($state, $value)
302
{
303
    $data = [
304
        'entStateOper' => [
305
            1 => ['text' => 'unavailable', 'color' => 'default'],
306
            2 => ['text' => 'disabled', 'color' => 'danger'],
307
            3 => ['text' => 'enabled', 'color' => 'success'],
308
            4 => ['text' => 'testing', 'color' => 'warning'],
309
        ],
310
        'entStateUsage' => [
311
            1 => ['text' => 'unavailable', 'color' => 'default'],
312
            2 => ['text' => 'idle', 'color' => 'info'],
313
            3 => ['text' => 'active', 'color' => 'success'],
314
            4 => ['text' => 'busy', 'color' => 'success'],
315
        ],
316
        'entStateStandby' => [
317
            1 => ['text' => 'unavailable', 'color' => 'default'],
318
            2 => ['text' => 'hotStandby', 'color' => 'info'],
319
            3 => ['text' => 'coldStandby', 'color' => 'info'],
320
            4 => ['text' => 'providingService', 'color' => 'success'],
321
        ],
322
        'entStateAdmin' => [
323
            1 => ['text' => 'unknown', 'color' => 'default'],
324
            2 => ['text' => 'locked', 'color' => 'info'],
325
            3 => ['text' => 'shuttingDown', 'color' => 'warning'],
326
            4 => ['text' => 'unlocked', 'color' => 'success'],
327
        ],
328
    ];
329
330
    if (isset($data[$state][$value])) {
331
        return $data[$state][$value];
332
    }
333
334
    return ['text'=>'na', 'color'=>'default'];
335
}
336
337
function parse_entity_state_alarm($bits)
338
{
339
    // not sure if this is correct
340
    $data = [
341
        0 => ['text' => 'unavailable', 'color' => 'default'],
342
        1 => ['text' => 'underRepair', 'color' => 'warning'],
343
        2 => ['text' => 'critical', 'color' => 'danger'],
344
        3 => ['text' => 'major', 'color' => 'danger'],
345
        4 => ['text' => 'minor', 'color' => 'info'],
346
        5 => ['text' => 'warning', 'color' => 'warning'],
347
        6 => ['text' => 'indeterminate', 'color' => 'default'],
348
    ];
349
350
    $alarms = str_split(base_convert($bits, 16, 2));
351
    $active_alarms = array_filter($alarms);
352
353
    return array_intersect_key($data, $active_alarms);
354
}
355