Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generator lookups to match OIDs closer to the index OID. #828

Merged
merged 7 commits into from
Oct 28, 2023
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