Skip to content

Commit bd8b62e

Browse files
committed
Port parsing documentation
And new function method of defining a parser convert default parser being careful to preserve functionality
1 parent df1d78d commit bd8b62e

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed

doc/Extensions/Interface-Description-Parsing.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Interface Description Parsing
22

33
Librenms can interpret, display and group certain additional information on ports.
4-
This is done based on the format that the port description is written
4+
This is done based on the format that the port description is written,
55
although it's possible to customise the parser to be specific for your setup.
66

77
## Keywords
@@ -63,11 +63,51 @@ It's also possible to write your own parser, the existing one is: includes/port-
6363

6464
Once you've created your own then you can enable it with:
6565

66+
!!! setting "webui/port-descr"
67+
```bash
68+
lnms config:set port_descr_parser includes/custom/my-port-descr-parser.inc.php
69+
```
70+
71+
Here is an example of a very simple file that parses type and descr.
6672
```php
67-
$config['port_descr_parser'] = "includes/custom/my-port-descr-parser.inc.php";
73+
<?php
74+
75+
return function (string $ifAlias): array {
76+
if (! str_contains(':', $ifAlias)) {
77+
return [];
78+
}
79+
80+
$parts = explode(':', $ifAlias, 2);
81+
82+
return [
83+
'type' => $parts[0],
84+
'descr' => $parts[1],
85+
];
86+
};
6887
```
6988

70-
### Setup
89+
#### Fields
90+
91+
Fields you can fill in your returned array
92+
93+
- type: port type (only defined types will show as graphs)
94+
- descr: description of the port
95+
- circuit: usually a circuit id
96+
- speed: can be a plan name or a numeric speed 10G (or asymmetric 10G/500M), numeric speeds
97+
can used by custom maps and some graphs
98+
- notes: notes
99+
100+
#### Variables
101+
102+
Variables you can request for your function include:
103+
104+
- string $ifAlias
105+
- int $port_id
106+
- string $ifName
107+
- int $ifIndex
108+
- \App\Models\Device $device
109+
110+
### Device Setup
71111

72112
For Unix / Linux based systems, you need to run an additional script
73113
to support the parsing of interface information.

includes/polling/ports.inc.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -745,21 +745,27 @@
745745
];
746746

747747
$port_ifAlias = []; // for port descr parser mappings
748-
include LibrenmsConfig::get('install_dir') . '/' . LibrenmsConfig::get('port_descr_parser');
748+
$port_parser ??= include LibrenmsConfig::get('install_dir') . '/' . LibrenmsConfig::get('port_descr_parser');
749+
750+
// handle functional style parsers
751+
if(is_callable($port_parser)) {
752+
$port_ifAlias = app()->call($port_parser, [
753+
'ifAlias' => $this_port['ifAlias'] ,
754+
'ifIndex' => $port['ifIndex'],
755+
'ifName' => $this_port['ifName'],
756+
'port_id' => $port['port_id']
757+
]);
758+
} else {
759+
unset($port_parser);
760+
}
749761

750762
foreach ($port_attribs as $attrib) {
751763
$attrib_key = 'port_descr_' . $attrib;
752-
if (($port_ifAlias[$attrib] ?? null) != $port[$attrib_key]) {
753-
if (! isset($port_ifAlias[$attrib])) {
754-
$port_ifAlias[$attrib] = null;
755-
$log_port = 'NULL';
756-
} else {
757-
$log_port = $port_ifAlias[$attrib];
758-
}
764+
$attrib_value = $port_ifAlias[$attrib] ?? null;
765+
if ($attrib_value != $port[$attrib_key]) {
766+
$port['update'][$attrib_key] = $attrib_value;
759767

760-
$port['update'][$attrib_key] = $port_ifAlias[$attrib];
761-
Eventlog::log($attrib . ': ' . $port[$attrib_key] . ' -> ' . $log_port, $device['device_id'], 'interface', Severity::Notice, $port['port_id']);
762-
unset($log_port);
768+
Eventlog::log($attrib . ': ' . $port[$attrib_key] . ' -> ' . ($attrib_value ?? 'NULL'), $device['device_id'], 'interface', Severity::Notice, $port['port_id']);
763769
}
764770
}
765771
}//end if

includes/port-descr-parser.inc.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
<?php
22

3-
// Parser should populate $port_ifAlias array with type, descr, circuit, speed and notes
4-
unset($port_ifAlias);
3+
// Parser should return an array with type, descr, circuit, speed and notes
54

6-
$split = preg_split('/[:\[\]{}()]/', (string) $this_port['ifAlias']);
7-
$type = isset($split[0]) ? trim($split[0]) : null;
8-
$descr = isset($split[1]) ? trim($split[1]) : null;
5+
return function (string $ifAlias): array {
6+
$split = preg_split('/[:\[\]{}()]/', $ifAlias);
7+
$type = trim($split[0] ?? '');
8+
$descr = trim($split[1] ?? '');
99

10-
$circuit = trim(preg_split('/[{}]/', (string) $this_port['ifAlias'])[1] ?? '');
11-
$notes = trim(preg_split('/[()]/', (string) $this_port['ifAlias'])[1] ?? '');
12-
$speed = trim(preg_split('/[\[\]]/', (string) $this_port['ifAlias'])[1] ?? '');
10+
if ($type && $descr) {
11+
return [
12+
'type' => strtolower($type),
13+
'descr' => $descr,
14+
'circuit' => trim(preg_split('/[{}]/', $ifAlias)[1] ?? ''),
15+
'speed' => trim(preg_split('/[\[\]]/', $ifAlias)[1] ?? ''),
16+
'notes' => trim(preg_split('/[()]/', $ifAlias)[1] ?? ''),
17+
];
18+
}
1319

14-
if ($type && $descr) {
15-
$type = strtolower($type);
16-
$port_ifAlias['type'] = $type;
17-
$port_ifAlias['descr'] = $descr;
18-
$port_ifAlias['circuit'] = $circuit;
19-
$port_ifAlias['speed'] = substr($speed, 0, 32);
20-
$port_ifAlias['notes'] = $notes;
21-
22-
d_echo($port_ifAlias);
23-
}
24-
25-
unset($type, $descr, $circuit, $notes, $speed, $split);
20+
return [];
21+
};

0 commit comments

Comments
 (0)