Skip to content

Commit

Permalink
Fix generator lookups to match OIDs closer to the index OID. (#828)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Signed-off-by: stephan-windischmann-sky <[email protected]>
Signed-off-by: Stephan Windischmann <[email protected]>
Co-authored-by: Ben Kochie <[email protected]>
Co-authored-by: Stephan Windischmann <[email protected]>
  • Loading branch information
3 people authored Oct 28, 2023
1 parent dbfe95c commit f427269
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
22 changes: 21 additions & 1 deletion generator/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions snmp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f427269

Please sign in to comment.