From bcbcb692e1490f4ed6347343ec89cb02a42fd181 Mon Sep 17 00:00:00 2001 From: Frederic Van Espen Date: Fri, 28 Aug 2020 17:32:12 +0200 Subject: [PATCH] look up EnumAsInfo indexes in enum_values Signed-off-by: Frederic Van Espen --- collector.go | 16 ++++++++++------ config/config.go | 9 +++++---- generator/tree.go | 1 + 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/collector.go b/collector.go index 838f56471..9b621ab93 100644 --- a/collector.go +++ b/collector.go @@ -567,7 +567,7 @@ func pduValueAsString(pdu *gosnmp.SnmpPDU, typ string) string { // Prepend the length, as it is explicit in an index. parts = append([]int{len(pdu.Value.([]byte))}, parts...) } - str, _, _ := indexOidsAsString(parts, typ, 0, false) + str, _, _ := indexOidsAsString(parts, typ, 0, false, nil) return str case nil: return "" @@ -581,7 +581,7 @@ func pduValueAsString(pdu *gosnmp.SnmpPDU, typ string) string { // Convert oids to a string index value. // // Returns the string, the oids that were used and the oids left over. -func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool) (string, []int, []int) { +func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool, enumValues map[int]string) (string, []int, []int) { if typeMapping, ok := combinedTypeMapping[typ]; ok { subOid, valueOids := splitOid(indexOids, 2) if typ == "InetAddressMissingSize" { @@ -591,15 +591,15 @@ func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool) var str string var used, remaining []int if t, ok := typeMapping[subOid[0]]; ok { - str, used, remaining = indexOidsAsString(valueOids, t, 0, false) + str, used, remaining = indexOidsAsString(valueOids, t, 0, false, enumValues) return str, append(subOid, used...), remaining } if typ == "InetAddressMissingSize" { // We don't know the size, so pass everything remaining. - return indexOidsAsString(indexOids, "OctetString", 0, true) + return indexOidsAsString(indexOids, "OctetString", 0, true, enumValues) } // The 2nd oid is the length. - return indexOidsAsString(indexOids, "OctetString", subOid[1]+2, false) + return indexOidsAsString(indexOids, "OctetString", subOid[1]+2, false, enumValues) } switch typ { @@ -669,6 +669,10 @@ func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool) parts[i] = o } return fmt.Sprintf("%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X", parts...), subOid, indexOids + case "EnumAsInfo": + subOid, indexOids := splitOid(indexOids, 1) + return enumValues[subOid[0]], subOid, indexOids + default: panic(fmt.Sprintf("Unknown index type %s", typ)) return "", nil, nil @@ -681,7 +685,7 @@ func indexesToLabels(indexOids []int, metric *config.Metric, oidToPdu map[string // Covert indexes to useful strings. for _, index := range metric.Indexes { - str, subOid, remainingOids := indexOidsAsString(indexOids, index.Type, index.FixedSize, index.Implied) + str, subOid, remainingOids := indexOidsAsString(indexOids, index.Type, index.FixedSize, index.Implied, index.EnumValues) // The labelvalue is the text form of the index oids. labels[index.Labelname] = str // Save its oid in case we need it for lookups. diff --git a/config/config.go b/config/config.go index 10e053df4..a97f32f87 100644 --- a/config/config.go +++ b/config/config.go @@ -181,10 +181,11 @@ type Metric struct { } type Index struct { - Labelname string `yaml:"labelname"` - Type string `yaml:"type"` - FixedSize int `yaml:"fixed_size,omitempty"` - Implied bool `yaml:"implied,omitempty"` + Labelname string `yaml:"labelname"` + Type string `yaml:"type"` + FixedSize int `yaml:"fixed_size,omitempty"` + Implied bool `yaml:"implied,omitempty"` + EnumValues map[int]string `yaml:"enum_values,omitempty"` } type Lookup struct { diff --git a/generator/tree.go b/generator/tree.go index 936840300..cb370fb90 100644 --- a/generator/tree.go +++ b/generator/tree.go @@ -358,6 +358,7 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]* if n.ImpliedIndex && count+1 == len(n.Indexes) { index.Implied = true } + index.EnumValues = indexNode.EnumValues // Convert (InetAddressType,InetAddress) to (InetAddress) if subtype, ok := combinedTypes[index.Type]; ok {