From f427269b24b137e938b8014bbd1402f1a9ee5fb9 Mon Sep 17 00:00:00 2001 From: stephan-windischmann-sky <120649376+stephan-windischmann-sky@users.noreply.github.com> Date: Sat, 28 Oct 2023 20:25:36 +0100 Subject: [PATCH] Fix generator lookups to match OIDs closer to the index OID. (#828) * Update lookup mechanism to improve in case of multiple oid with same label When running the configuration generator with lookups were multilpe different nodes have the same label the generator was always grabbing the OID of the last MIB that was parsed. I modified the lookup mechanism to first attempt to match the lookup node so that the OID matches that of the metric in all but the last branch. In case no match is found, it reverts to the old behavior. --------- Signed-off-by: Windischmann, Stephan Signed-off-by: stephan-windischmann-sky <120649376+stephan-windischmann-sky@users.noreply.github.com> Signed-off-by: Stephan Windischmann Co-authored-by: Ben Kochie Co-authored-by: Stephan Windischmann --- generator/tree.go | 22 +++++++++++++++++++++- snmp.yml | 14 +++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/generator/tree.go b/generator/tree.go index d3e30da4..8b833c34 100644 --- a/generator/tree.go +++ b/generator/tree.go @@ -251,6 +251,26 @@ func getMetricNode(oid string, node *Node, nameToNode map[string]*Node) (*Node, return n, oidInstance } +// In the case of multiple nodes with the same label try to return the node +// where the OID matches in every branch apart from the last one. +func getIndexNode(lookup string, nameToNode map[string]*Node, metricOid string) *Node { + for _, node := range nameToNode { + if node.Label != lookup { + continue + } + + oid := strings.Split(metricOid, ".") + oidPrefix := strings.Join(oid[:len(oid)-1], ".") + + if strings.HasPrefix(node.Oid, oidPrefix) { + return node + } + } + + // If no node matches, revert to previous behavior. + return nameToNode[lookup] +} + func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]*Node, logger log.Logger) (*config.Module, error) { out := &config.Module{} needToWalk := map[string]struct{}{} @@ -412,7 +432,7 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]* if _, ok := nameToNode[lookup.Lookup]; !ok { return nil, fmt.Errorf("unknown index '%s'", lookup.Lookup) } - indexNode := nameToNode[lookup.Lookup] + indexNode := getIndexNode(lookup.Lookup, nameToNode, metric.Oid) typ, ok := metricType(indexNode.Type) if !ok { return nil, fmt.Errorf("unknown index type %s for %s", indexNode.Type, lookup.Lookup) diff --git a/snmp.yml b/snmp.yml index af6e9418..97542838 100644 --- a/snmp.yml +++ b/snmp.yml @@ -20137,6 +20137,7 @@ modules: 6: interlockClosed raritan: walk: + - 1.3.6.1.4.1.13742.4.1.2.2.1.2 - 1.3.6.1.4.1.13742.4.1.2.2.1.3 - 1.3.6.1.4.1.13742.4.1.2.2.1.31 - 1.3.6.1.4.1.13742.4.1.2.2.1.4 @@ -20146,7 +20147,6 @@ modules: - 1.3.6.1.4.1.13742.4.1.20.2.1.7 - 1.3.6.1.4.1.13742.4.1.20.2.1.8 - 1.3.6.1.4.1.13742.4.1.20.2.1.9 - - 1.3.6.1.4.1.13742.6.3.5.3.1.2 - 1.3.6.1.4.1.13742.6.5.5.3.1 get: - 1.3.6.1.4.1.13742.4.1.3.1.5.0 @@ -20163,7 +20163,7 @@ modules: - labels: - outletIndex labelname: outletLabel - oid: 1.3.6.1.4.1.13742.6.3.5.3.1.2 + oid: 1.3.6.1.4.1.13742.4.1.2.2.1.2 type: DisplayString enum_values: -1: error @@ -20181,7 +20181,7 @@ modules: - labels: - outletIndex labelname: outletLabel - oid: 1.3.6.1.4.1.13742.6.3.5.3.1.2 + oid: 1.3.6.1.4.1.13742.4.1.2.2.1.2 type: DisplayString - name: outletCurrent oid: 1.3.6.1.4.1.13742.4.1.2.2.1.4 @@ -20194,7 +20194,7 @@ modules: - labels: - outletIndex labelname: outletLabel - oid: 1.3.6.1.4.1.13742.6.3.5.3.1.2 + oid: 1.3.6.1.4.1.13742.4.1.2.2.1.2 type: DisplayString - name: outletMaxCurrent oid: 1.3.6.1.4.1.13742.4.1.2.2.1.5 @@ -20207,7 +20207,7 @@ modules: - labels: - outletIndex labelname: outletLabel - oid: 1.3.6.1.4.1.13742.6.3.5.3.1.2 + oid: 1.3.6.1.4.1.13742.4.1.2.2.1.2 type: DisplayString - name: outletVoltage oid: 1.3.6.1.4.1.13742.4.1.2.2.1.6 @@ -20221,7 +20221,7 @@ modules: - labels: - outletIndex labelname: outletLabel - oid: 1.3.6.1.4.1.13742.6.3.5.3.1.2 + oid: 1.3.6.1.4.1.13742.4.1.2.2.1.2 type: DisplayString - name: outletActivePower oid: 1.3.6.1.4.1.13742.4.1.2.2.1.7 @@ -20234,7 +20234,7 @@ modules: - labels: - outletIndex labelname: outletLabel - oid: 1.3.6.1.4.1.13742.6.3.5.3.1.2 + oid: 1.3.6.1.4.1.13742.4.1.2.2.1.2 type: DisplayString - name: inletCurrent oid: 1.3.6.1.4.1.13742.4.1.20.2.1.7