Yaml value filling improvements (#15116)

* Yaml value filling improvements
search all data for fully qualified oids (to prevent incorrect matches)
Reduce searches when not needed
subindex code optimization

* fix aos7

* fix e3meterdc

* Cleanups

* fix hpe-mapdu

* fix timos add note to dev:check

* remove inconsistent Str::contains usage
This commit is contained in:
Tony Murray
2023-06-23 13:57:44 -05:00
committed by GitHub
parent eaf0253412
commit 63fd62ca12
7 changed files with 92 additions and 72 deletions

View File

@@ -97,14 +97,10 @@ class YamlDiscovery
}
foreach ($data as $name => $value) {
if (in_array($name, ['oid', 'skip_values', 'snmp_flags', 'rrd_type'])) {
$current_data[$name] = $value;
} elseif (Str::contains($value, '{{')) {
// replace embedded values
$current_data[$name] = static::replaceValues($name, $index, $count, $data, $pre_cache);
if (! in_array($name, ['oid', 'skip_values', 'snmp_flags', 'rrd_type'])) {
$current_data[$name] = self::fillValues($name, $index, $data, $count, $pre_cache, $value);
} else {
// replace references to data
$current_data[$name] = static::getValueFromData($name, $index, $data, $pre_cache, $value);
$current_data[$name] = $value;
}
}
@@ -124,6 +120,28 @@ class YamlDiscovery
return $items;
}
/**
* @param string $name The oid of the value we are searching for
* @param int|string $index The index of the current entity we are searching from
* @param array $discovery_data The yaml discovery data
* @param int $count The count of where we are in the discovery data
* @param array $pre_cache Data that has been previously fetched (should contain all snmp data)
* @param int|string $value The current value of the data that we might need to transform (or return as is)
* @return mixed
*/
public static function fillValues($name, $index, $discovery_data, $count, $pre_cache, $value): mixed
{
if (str_contains($value, '{{')) {
// replace embedded values
return static::replaceValues($name, $index, $count, $discovery_data, $pre_cache);
} elseif (! str_contains($value, ' ')) {
// replace references to data
return static::getValueFromData($name, $index, $discovery_data, $pre_cache, $value);
}
return $value;
}
/**
* @param \LibreNMS\OS $os OS/device we areworking on
* @param array $data Array derived from YAML
@@ -217,17 +235,21 @@ class YamlDiscovery
return $pre_cache[$index][$name];
}
//create the sub-index values in order to try to match them with precache
$sub_indexes = explode('.', $index);
// parse sub_index options name with trailing colon and index
$sub_index = 0;
$sub_index_end = null;
if (preg_match('/^(.+):(\d+)(?:-(\d+))?$/', $name, $matches)) {
$name = $matches[1] ?? null;
$sub_index = $matches[2] ?? null;
$sub_index_end = $matches[3] ?? null;
// subindex found
$name = $matches[1];
//create the sub-index values in order to try to match them with precache
$sub_indexes = explode('.', $index);
// if subindex is a range, get them all, otherwise just get the first
$index = isset($matches[3])
? implode('.', array_slice($sub_indexes, (int) $matches[2], (int) $matches[3]))
: $sub_indexes[(int) $matches[2]];
}
// look for the data in pre_cache
if (isset($pre_cache[$name]) && ! is_numeric($name)) {
if (is_array($pre_cache[$name])) {
if (isset($pre_cache[$name][$index][$name])) {
@@ -236,23 +258,21 @@ class YamlDiscovery
return $pre_cache[$name][$index];
} elseif (count($pre_cache[$name]) === 1 && ! is_array(current($pre_cache[$name]))) {
return current($pre_cache[$name]);
} elseif (isset($sub_indexes[$sub_index])) {
if ($sub_index_end) {
$multi_sub_index = implode('.', array_slice($sub_indexes, (int) $sub_index, (int) $sub_index_end));
if (isset($pre_cache[$name][$multi_sub_index][$name])) {
return $pre_cache[$name][$multi_sub_index][$name];
}
}
if (isset($pre_cache[$name][$sub_indexes[$sub_index]][$name])) {
return $pre_cache[$name][$sub_indexes[$sub_index]][$name];
}
}
} else {
return $pre_cache[$name];
}
}
// search for name inside walked tables if oid is fully qualified
if (str_contains($name, '::')) {
foreach ($pre_cache as $table_name => $table) {
if (is_array($table) && isset($table[$index][$name])) {
return $table[$index][$name];
}
}
}
return $default;
}
@@ -294,7 +314,7 @@ class YamlDiscovery
if (! array_key_exists($oid, $pre_cache)) {
if (isset($data['snmp_flags'])) {
$snmp_flag = Arr::wrap($data['snmp_flags']);
} elseif (Str::contains($oid, '::')) {
} elseif (str_contains($oid, '::')) {
$snmp_flag = ['-OteQUSa'];
} else {
$snmp_flag = ['-OteQUsa'];
@@ -346,7 +366,7 @@ class YamlDiscovery
} else {
// oid previously fetched from the device
$tmp_value = static::getValueFromData($skip_value['oid'], $index, $yaml_item_data, $pre_cache);
if (Str::contains($skip_value['oid'], '.')) {
if (str_contains($skip_value['oid'], '.')) {
[$skip_value['oid'], $targeted_index] = explode('.', $skip_value['oid'], 2);
$tmp_value = static::getValueFromData($skip_value['oid'], $targeted_index, $yaml_item_data, $pre_cache);
}

View File

@@ -34,7 +34,7 @@ modules:
oid: ddmPortInfoTable
value: ddmPortTxBiasCurrent
num_oid: '.1.3.6.1.4.1.6486.801.1.2.1.5.1.1.2.6.1.12.{{ $index }}'
descr: '{{ $ifName }} TX Bias'
descr: '{{ $ifName:0 }} TX Bias'
divisor: 1000
low_limit: ddmPortTxBiasCurrentLowAlarm
low_warn_limit: ddmPortTxBiasCurrentLowWarning
@@ -49,7 +49,7 @@ modules:
oid: ddmPortInfoTable
value: ddmPortRxOpticalPower
num_oid: '.1.3.6.1.4.1.6486.801.1.2.1.5.1.1.2.6.1.22.{{ $index }}'
descr: '{{ $ifName }} RX Power'
descr: '{{ $ifName:0 }} RX Power'
divisor: 1000
index: 'rx-{{ $index }}'
low_limit: ddmPortRxOpticalPowerLowAlarm
@@ -72,7 +72,7 @@ modules:
oid: ddmPortInfoTable
value: ddmPortTxOutputPower
num_oid: '.1.3.6.1.4.1.6486.801.1.2.1.5.1.1.2.6.1.17.{{ $index }}'
descr: '{{ $ifName }} TX Power'
descr: '{{ $ifName:0 }} TX Power'
divisor: 1000
index: 'tx-{{ $index }}'
low_limit: ddmPortTxOutputPowerLowAlarm
@@ -96,7 +96,7 @@ modules:
oid: alaChasEntPhysFanTable
value: alaChasEntPhysFanSpeed
num_oid: '.1.3.6.1.4.1.6486.801.1.1.1.3.1.1.11.1.4.{{ $index }}'
descr: 'CHASSIS-{{ $entPhysicalName }} Fan {{ $subindex1 }}'
descr: 'CHASSIS-{{ $entPhysicalName:0 }} Fan {{ $subindex1 }}'
low_limit: 1000
high_limit: 18000
skip_values:
@@ -160,7 +160,7 @@ modules:
oid: alaChasEntPhysFanTable
value: alaChasEntPhysFanStatus
num_oid: '.1.3.6.1.4.1.6486.801.1.1.1.3.1.1.11.1.2.{{ $index }}'
descr: 'CHASSIS-{{ $entPhysicalName }} Fan {{ $subindex1 }}'
descr: 'CHASSIS-{{ $entPhysicalName:0 }} Fan {{ $subindex1 }}'
group: Fans
index: 'alaChasEntPhysFanStatus.{{ $index }}'
state_name: alaChasEntPhysFanStatus
@@ -171,7 +171,7 @@ modules:
-
oid: chasEntPhysOperStatus
num_oid: '.1.3.6.1.4.1.6486.801.1.1.1.1.1.1.1.2.{{ $index }}'
descr: '{{ $entPhysicalName }}'
descr: '{{ $entPhysicalName:0 }}'
group: Components
index: 'chasEntPhysOperStatus.{{ $index }}'
states:
@@ -230,7 +230,7 @@ modules:
oid: chasChassisEntry
value: chasCPMAHardwareBoardTemp
num_oid: '.1.3.6.1.4.1.6486.801.1.1.1.3.1.1.3.1.8.{{ $index }}'
descr: '{{ $entPhysicalName }} Temperature'
descr: '{{ $entPhysicalName:0 }} Temperature'
group: Chassis
index: 'chasCPMAHardwareBoardTemp.{{ $index }}'
high_limit: chasDangerTempThreshold
@@ -244,7 +244,7 @@ modules:
high_limit: ddmPortTempHiAlarm
warn_limit: ddmPortTempHiWarning
divisor: 1000
descr: '{{ $ifName }} DDM Temperature'
descr: '{{ $ifName:0 }} DDM Temperature'
group: DDM
index: 'rx.{{ $index }}'
entPhysicalIndex_measured: ports
@@ -256,7 +256,7 @@ modules:
oid: ddmPortInfoTable
value: ddmPortSupplyVoltage
num_oid: '.1.3.6.1.4.1.6486.801.1.2.1.5.1.1.2.6.1.7.{{ $index }}'
descr: '{{ $ifName }} TX Voltage'
descr: '{{ $ifName:0 }} TX Voltage'
divisor: 1000
low_limit: ddmPortSupplyVoltageLowAlarm
low_warn_limit: ddmPortSupplyVoltageLowWarning

View File

@@ -42,8 +42,8 @@ modules:
oid: e3IpsSensorEntry
value: e3IpsSensorTemperatureCelsius
num_oid: '.1.3.6.1.4.1.21695.1.10.5.1.4.{{ $index }}'
descr: 'Sensor {{ $subindex1 }}, {{ $e3IpsLabel }}'
group: 'Model {{ $e3IpsModel }} rev{{ $e3IpsHWVersion }}, {{ $e3IpsLabel }}'
descr: 'Sensor {{ $subindex1 }}, {{ $e3IpsLabel:0 }}'
group: 'Model {{ $e3IpsModel:0 }} rev{{ $e3IpsHWVersion:0 }}, {{ $e3IpsLabel:0 }}'
index: '{{ $index }}'
divisor: 10
skip_values:
@@ -61,8 +61,8 @@ modules:
oid: e3IpsMeterEntry
value: e3IpsFrequency
num_oid: '.1.3.6.1.4.1.21695.1.10.4.1.11.{{ $index }}'
descr: 'Phase {{ $subindex1 }}, {{ $e3IpsLabel }}'
group: 'Model {{ $e3IpsModel }} rev{{ $e3IpsHWVersion }}, {{ $e3IpsLabel }}'
descr: 'Phase {{ $subindex1 }}, {{ $e3IpsLabel:0 }}'
group: 'Model {{ $e3IpsModel:0 }} rev{{ $e3IpsHWVersion:0 }}, {{ $e3IpsLabel:0 }}'
index: '{{ $index }}'
divisor: 1000
skip_values:
@@ -76,8 +76,8 @@ modules:
oid: e3IpsMeterEntry
value: e3IpsPAvg
num_oid: '.1.3.6.1.4.1.21695.1.10.4.1.18.{{ $index }}'
descr: 'Phase {{ $subindex1 }}, {{ $e3IpsLabel }}'
group: 'Model {{ $e3IpsModel }} rev{{ $e3IpsHWVersion }}, {{ $e3IpsLabel }}'
descr: 'Phase {{ $subindex1 }}, {{ $e3IpsLabel:0 }}'
group: 'Model {{ $e3IpsModel:0 }} rev{{ $e3IpsHWVersion:0 }}, {{ $e3IpsLabel:0 }}'
index: '{{ $index }}'
skip_values:
-

View File

@@ -181,7 +181,7 @@ modules:
descr: 'Input Phase {{ $pdu2InputPhaseIndex }} Power'
skip_values:
-
oid: 'pdu2InputType'
oid: 'pdu2InputType:0'
op: '!='
value: '1'
-
@@ -211,7 +211,7 @@ modules:
descr: 'Input Phase {{ $pdu3InputPhaseIndex }} Power'
skip_values:
-
oid: 'pdu3InputType'
oid: 'pdu3InputType:0'
op: '!='
value: '1'
-
@@ -242,7 +242,7 @@ modules:
divisor: 1000
skip_values:
-
oid: 'pdu2InputType'
oid: 'pdu2InputType:0'
op: '!='
value: '1'
-

View File

@@ -108,7 +108,7 @@ modules:
-
oid: tmnxCellPortRegistrationStatus
num_oid: '.1.3.6.1.4.1.6527.3.1.2.109.3.1.1.1.2.{{ $index }}'
descr: 'Registration state {{ $ifName }}'
descr: 'Registration state {{ ifName }}'
index: 'tmnxCellPortRegistrationStatus.{{ $index }}'
state_name: tmnxCellPortRegistrationStatus
states:
@@ -127,7 +127,7 @@ modules:
-
oid: tmnxCellPortWirelessTechnology
num_oid: '.1.3.6.1.4.1.6527.3.1.2.109.3.1.1.1.3.{{ $index }}'
descr: 'Wireless technology {{ $ifName }}'
descr: 'Wireless technology {{ ifName }}'
index: 'tmnxCellPortWirelessTechnology.{{ $index }}'
state_name: tmnxCellPortWirelessTechnology
states:
@@ -139,7 +139,7 @@ modules:
-
oid: tmnxCellSimCardEquipped
num_oid: '.1.3.6.1.4.1.6527.3.1.2.109.3.1.3.1.1.{{ $index }}'
descr: 'Simcard {{ $ifName }}'
descr: 'Simcard {{ ifName:0 }}'
index: 'tmnxCellSimCardEquipped.{{ $index }}'
state_name: tmnxCellSimCardEquipped
states:
@@ -150,7 +150,7 @@ modules:
oid: tmnxCellPdnConnectionState
num_oid: '.1.3.6.1.4.1.6527.3.1.2.109.3.1.4.1.1.{{ $index }}'
descr: 'Pdn state {{ $ifName }} {{ $tmnxCellPdnApn }}'
descr: 'Pdn state {{ ifName:0 }} {{ $tmnxCellPdnApn }}'
index: 'tmnxCellPdnConnectionState.{{ $index }}'
state_name: tmnxCellPdnConnectionState
states:
@@ -219,7 +219,7 @@ modules:
oid: tmnxDigitalDiagMonitorEntry
value: tmnxDDMTemperature
num_oid: '.1.3.6.1.4.1.6527.3.1.2.2.4.31.1.1.{{ $index }}'
descr: '{{ $ifName:1 }}'
descr: '{{ ifName:1 }}'
index: '{{ $index }}'
high_limit: tmnxDDMTempHiAlarm
warn_limit: tmnxDDMTempHiWarning
@@ -241,7 +241,7 @@ modules:
oid: tmnxDigitalDiagMonitorEntry
value: tmnxDDMTxBiasCurrent
num_oid: '.1.3.6.1.4.1.6527.3.1.2.2.4.31.1.11.{{ $index }}'
descr: '{{ $ifName:1 }} Tx Bias'
descr: '{{ ifName:1 }} Tx Bias'
entPhysicalIndex: '{{ $index }}'
entPhysicalIndex_measured: ports
index: 'tx-bias-{{ $index }}'
@@ -263,7 +263,7 @@ modules:
oid: tmnxDDMLaneTable
value: tmnxDDMLaneTxBiasCurrent
num_oid: '.1.3.6.1.4.1.6527.3.1.2.2.4.66.1.7.{{ $index }}'
descr: '{{ $ifName:1 }} Lane {{ $subindex2 }} Tx Bias'
descr: '{{ ifName:1 }} Lane {{ $subindex2 }} Tx Bias'
entPhysicalIndex: '{{ $index }}'
entPhysicalIndex_measured: ports
index: 'tx-bias-{{ $index }}'
@@ -272,7 +272,7 @@ modules:
warn_limit: tmnxDDMLaneTxBiasCurrentHiWarning
low_limit: tmnxDDMLaneTxBiasCurrentLowAlarm
low_warn_limit: tmnxDDMLaneTxBiasCurrentLowWarning
group: '{{ $ifName:1 }}'
group: '{{ ifName:1 }}'
skip_values:
-
oid: tmnxDDMLaneTxBiasCurrent
@@ -288,7 +288,7 @@ modules:
oid: tmnxDigitalDiagMonitorEntry
value: tmnxDDMRxOpticalPower
num_oid: '.1.3.6.1.4.1.6527.3.1.2.2.4.31.1.21.{{ $index }}'
descr: '{{ $ifName:1 }} Rx'
descr: '{{ ifName:1 }} Rx'
entPhysicalIndex: '{{ $index }}'
entPhysicalIndex_measured: ports
index: 'rx-{{ $index }}'
@@ -298,7 +298,7 @@ modules:
low_limit: tmnxDDMRxOpticalPowerLowAlarm
low_warn_limit: tmnxDDMRxOpticalPowerLowWarning
user_func: 'uw_to_dbm'
group: '{{ $ifName:1 }}'
group: '{{ ifName:1 }}'
skip_values:
-
oid: tmnxPortAdminStatus
@@ -312,7 +312,7 @@ modules:
oid: tmnxDigitalDiagMonitorEntry
value: tmnxDDMTxOutputPower
num_oid: '.1.3.6.1.4.1.6527.3.1.2.2.4.31.1.16.{{ $index }}'
descr: '{{ $ifName:1 }} Tx'
descr: '{{ ifName:1 }} Tx'
entPhysicalIndex: '{{ $index }}'
entPhysicalIndex_measured: ports
index: 'tx-{{ $index }}'
@@ -322,7 +322,7 @@ modules:
low_limit: tmnxDDMTxOutputPowerLowAlarm
low_warn_limit: tmnxDDMTxOutputPowerLowWarning
user_func: 'uw_to_dbm'
group: '{{ $ifName:1 }}'
group: '{{ ifName:1 }}'
skip_values:
-
oid: tmnxPortAdminStatus
@@ -336,13 +336,13 @@ modules:
oid: tmnxDDMLaneTable
value: tmnxDDMLaneRxOpticalPower
num_oid: '.1.3.6.1.4.1.6527.3.1.2.2.4.66.1.17.{{ $index }}'
descr: '{{ $ifName:1 }} Lane {{ $subindex2 }} Rx'
descr: '{{ ifName:1 }} Lane {{ $subindex2 }} Rx'
entPhysicalIndex: '{{ $index }}'
entPhysicalIndex_measured: ports
index: 'lane-rx-{{ $index }}'
user_func: 'uw_to_dbm'
divisor: 10
group: '{{ $ifName:1 }}'
group: '{{ ifName:1 }}'
high_limit: tmnxDDMLaneRxOpticalPwrHiAlarm
warn_limit: tmnxDDMLaneRxOpticalPwrHiWarn
low_limit: tmnxDDMLaneRxOpticalPwrLowAlarm
@@ -356,13 +356,13 @@ modules:
oid: tmnxDDMLaneTable
value: tmnxDDMLaneTxOutputPower
num_oid: '.1.3.6.1.4.1.6527.3.1.2.2.4.66.1.12.{{ $index }}'
descr: '{{ $ifName:1 }} Lane {{ $subindex2 }} Tx'
descr: '{{ ifName:1 }} Lane {{ $subindex2 }} Tx'
entPhysicalIndex: '{{ $index }}'
entPhysicalIndex_measured: ports
index: 'lane-tx-{{ $index }}'
user_func: 'uw_to_dbm'
divisor: 10
group: '{{ $ifName:1 }}'
group: '{{ ifName:1 }}'
high_limit: tmnxDDMLaneTxOutputPowerHiAlarm
warn_limit: tmnxDDMLaneTxOutputPowerHiWarn
low_limit: tmnxDDMLaneTxOutputPowerLowAlarm
@@ -378,7 +378,7 @@ modules:
oid: tmnxDigitalDiagMonitorEntry
value: tmnxDDMSupplyVoltage
num_oid: '.1.3.6.1.4.1.6527.3.1.2.2.4.31.1.6.{{ $index }}'
descr: '{{ $ifName:1 }}'
descr: '{{ ifName:1 }}'
index: '{{ $index }}'
high_limit: tmnxDDMSupplyVoltageHiAlarm
warn_limit: tmnxDDMSupplyVoltageHiWarning
@@ -432,14 +432,14 @@ modules:
-
oid: tmnxCellPortFrequencyBand
num_oid: '.1.3.6.1.4.1.6527.3.1.2.109.3.1.1.1.4.{{ $index }}'
descr: 'Interface {{ $ifName }}'
descr: 'Interface {{ ifName }}'
index: 'tmnxCellPortFrequencyBand.{{ $index }}'
group: 'Frequency band'
-
oid: tmnxCellularPortBearerTable
value: tmnxCellPortBearerQci
num_oid: '.1.3.6.1.4.1.6527.3.1.2.109.3.1.5.1.3.{{ $index }}'
descr: 'Interface {{ $ifName }}'
descr: 'Interface {{ ifName:0 }}'
index: 'tmnxCellularPortBearerTable.{{ $index }}'
group: 'QoS Class Identifier'
frequency:

View File

@@ -969,7 +969,7 @@ function discovery_process(&$valid, $os, $sensor_class, $pre_cache)
$value = ($value / $divisor) * $multiplier;
}
echo "Cur $value, Low: $low_limit, Low Warn: $low_warn_limit, Warn: $warn_limit, High: $high_limit" . PHP_EOL;
echo "$descr: Cur $value, Low: $low_limit, Low Warn: $low_warn_limit, Warn: $warn_limit, High: $high_limit" . PHP_EOL;
$entPhysicalIndex = YamlDiscovery::replaceValues('entPhysicalIndex', $index, null, $data, $pre_cache) ?: null;
$entPhysicalIndex_measured = isset($data['entPhysicalIndex_measured']) ? $data['entPhysicalIndex_measured'] : null;
@@ -991,7 +991,7 @@ function discovery_process(&$valid, $os, $sensor_class, $pre_cache)
}
}
discover_sensor($valid['sensor'], $sensor_class, $device, $oid, $uindex, $sensor_name, $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value, 'snmp', $entPhysicalIndex, $entPhysicalIndex_measured, $user_function, $group, $data['rrd_type']);
discover_sensor($valid['sensor'], $sensor_class, $device, $oid, $uindex, $sensor_name, $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value, 'snmp', $entPhysicalIndex, $entPhysicalIndex_measured, $user_function, $group, $data['rrd_type'] ?? 'GAUGE');
if ($sensor_class === 'state') {
create_sensor_to_state_index($device, $sensor_name, $uindex);

View File

@@ -44,7 +44,7 @@ return [
'fail-fast' => 'Stop checks when any failure is encountered',
'full' => 'Run full checks ignoring changed file filtering',
'module' => 'Specific Module to run tests on. Implies unit, --db, --snmpsim',
'os' => 'Specific OS to run tests on. Implies unit, --db, --snmpsim',
'os' => 'Specific OS to run tests on. May be a regex or comma seperated list. Implies unit, --db, --snmpsim',
'os-modules-only' => 'Skip os detection test when specifying a specific OS. Speeds up test time when checking non-detection changes.',
'quiet' => 'Hide output unless there is an error',
'snmpsim' => 'Use snmpsim for unit tests',