Infer character encoding for ifAlias and sysLocation (#13248)

* Infer character encoding for ifAlias and sysLocation
Tries to convert character encoding for non-UTF-8 encoded strings.
This will only work for snmp strings that are type-hinted, not ones forced to ASCII with -Oa or similar
Only works for your default charset or Windows-1251 or LATIN1.
You can set your character encoding in .env with CHARSET

Please save us all and just use UTF-8

* style fixes

* less Yoda

* ensure return type

* fall back to passed string

* don't convert strings with any unprintable characters (such as line return)

* Update LibreNMS/Util/StringHelpers.php

Co-authored-by: Jellyfrog <Jellyfrog@users.noreply.github.com>

* Fix CP850

* fix space

Co-authored-by: Jellyfrog <Jellyfrog@users.noreply.github.com>
This commit is contained in:
Tony Murray
2021-09-21 08:47:44 -05:00
committed by GitHub
parent 11dfbc02b7
commit 8a883140cb
6 changed files with 92 additions and 1 deletions

View File

@@ -3,6 +3,7 @@
// Build SNMP Cache Array
use App\Models\PortGroup;
use LibreNMS\Config;
use LibreNMS\Util\StringHelpers;
$port_stats = [];
$port_stats = snmpwalk_cache_oid($device, 'ifDescr', $port_stats, 'IF-MIB');
@@ -60,6 +61,7 @@ $default_port_group = Config::get('default_port_group');
// New interface detection
foreach ($port_stats as $ifIndex => $snmp_data) {
$snmp_data['ifIndex'] = $ifIndex; // Store ifIndex in port entry
$snmp_data['ifAlias'] = StringHelpers::inferEncoding($snmp_data['ifAlias']);
// Get port_id according to port_association_mode used for this device
$port_id = get_port_id($ports_mapped, $snmp_data, $port_association_mode);