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

Improved Lookup process for label information to utilize TypeMapping #908

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,7 @@ func pduToSamples(indexOids []int, pdu *gosnmp.SnmpPDU, metric *config.Metric, o

if typeMapping, ok := combinedTypeMapping[metricType]; ok {
// Lookup associated sub type in previous object.
oids := strings.Split(metric.Oid, ".")
i, _ := strconv.Atoi(oids[len(oids)-1])
oids[len(oids)-1] = strconv.Itoa(i - 1)
prevOid := fmt.Sprintf("%s.%s", strings.Join(oids, "."), listToOid(indexOids))
prevOid := fmt.Sprintf("%s.%s", getPrevOid(metric.Oid), listToOid(indexOids))
if prevPdu, ok := oidToPdu[prevOid]; ok {
val := int(getPduValue(&prevPdu))
if t, ok := typeMapping[val]; ok {
Expand Down Expand Up @@ -872,6 +869,13 @@ func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool,
}
}

func getPrevOid(oid string) string {
oids := strings.Split(oid, ".")
i, _ := strconv.Atoi(oids[len(oids)-1])
oids[len(oids)-1] = strconv.Itoa(i - 1)
return strings.Join(oids, ".")
}

func indexesToLabels(indexOids []int, metric *config.Metric, oidToPdu map[string]gosnmp.SnmpPDU, metrics internalMetrics) map[string]string {
labels := map[string]string{}
labelOids := map[string][]int{}
Expand All @@ -898,7 +902,21 @@ func indexesToLabels(indexOids []int, metric *config.Metric, oidToPdu map[string
oid = fmt.Sprintf("%s.%s", oid, listToOid(labelOids[label]))
}
if pdu, ok := oidToPdu[oid]; ok {
labels[lookup.Labelname] = pduValueAsString(&pdu, lookup.Type, metrics)
t := lookup.Type
if typeMapping, ok := combinedTypeMapping[lookup.Type]; ok {
// Lookup associated sub type in previous object.
prevOid := getPrevOid(lookup.Oid)
for _, label := range lookup.Labels {
prevOid = fmt.Sprintf("%s.%s", prevOid, listToOid(labelOids[label]))
}
if prevPdu, ok := oidToPdu[prevOid]; ok {
val := int(getPduValue(&prevPdu))
if ty, ok := typeMapping[val]; ok {
t = ty
}
}
}
labels[lookup.Labelname] = pduValueAsString(&pdu, t, metrics)
labelOids[lookup.Labelname] = []int{int(gosnmp.ToBigInt(pdu.Value).Int64())}
} else {
labels[lookup.Labelname] = ""
Expand Down
19 changes: 19 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,25 @@ func TestIndexesToLabels(t *testing.T) {
},
result: map[string]string{"a": "1", "chainable_id": "42", "targetlabel": "targetvalue"},
},
{
oid: []int{1, 8, 1},
metric: config.Metric{
Indexes: []*config.Index{
{Labelname: "lldpRemTimeMark", Type: "gauge"},
{Labelname: "lldpRemLocalPortNum", Type: "gauge"},
{Labelname: "lldpRemIndex", Type: "gauge"},
},
Lookups: []*config.Lookup{
{Labels: []string{"lldpRemLocalPortNum"}, Labelname: "lldpLocPortId", Oid: "1.1.3", Type: "LldpPortId"},
},
},
oidToPdu: map[string]gosnmp.SnmpPDU{
"1.1.9.1.8.1": gosnmp.SnmpPDU{Value: "hostname"},
"1.1.2.8": gosnmp.SnmpPDU{Value: 3},
"1.1.3.8": gosnmp.SnmpPDU{Value: []byte{4, 5, 6, 7, 8, 9}},
},
result: map[string]string{"lldpRemTimeMark": "1", "lldpRemLocalPortNum": "8", "lldpRemIndex": "1", "lldpLocPortId": "04:05:06:07:08:09"},
},
}
for _, c := range cases {
got := indexesToLabels(c.oid, &c.metric, c.oidToPdu, internalMetrics{})
Expand Down