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

look up EnumAsInfo indexes in enum_values #559

Merged
merged 5 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 9 additions & 6 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -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" {
Expand All @@ -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 {
Expand Down Expand Up @@ -669,6 +669,9 @@ 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
fredericve marked this conversation as resolved.
Show resolved Hide resolved
default:
panic(fmt.Sprintf("Unknown index type %s", typ))
return "", nil, nil
Expand All @@ -681,7 +684,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.
Expand Down
9 changes: 5 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions generator/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 51 additions & 0 deletions generator/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,57 @@ func TestGenerateConfigModule(t *testing.T) {
},
},
},
// Basic table with integer index and enum_values, overridden as EnumAsInfo.
{
node: &Node{Oid: "1", Label: "root",
Children: []*Node{
{Oid: "1.1", Label: "table",
Children: []*Node{
{Oid: "1.1.1", Label: "tableEntry", Indexes: []string{"tableIndex"},
Children: []*Node{
{Oid: "1.1.1.1", Access: "ACCESS_READONLY", Label: "tableIndex", Type: "INTEGER", EnumValues: map[int]string{0: "a"}},
{Oid: "1.1.1.2", Access: "ACCESS_READONLY", Label: "tableFoo", Type: "INTEGER"},
}}}}}},
cfg: &ModuleConfig{
Walk: []string{"1"},
Overrides: map[string]MetricOverrides{
"tableIndex": MetricOverrides{Type: "EnumAsInfo"},
},
},
out: &config.Module{
Walk: []string{"1"},
Metrics: []*config.Metric{
{
Name: "tableIndex",
Oid: "1.1.1.1",
Type: "EnumAsInfo",
Help: " - 1.1.1.1",
Indexes: []*config.Index{
{
Labelname: "tableIndex",
Type: "EnumAsInfo",
EnumValues: map[int]string{0: "a"},
},
},
EnumValues: map[int]string{0: "a"},
},
{
Name: "tableFoo",
Oid: "1.1.1.2",
Type: "gauge",
Help: " - 1.1.1.2",
Indexes: []*config.Index{
{
Labelname: "tableIndex",
Type: "EnumAsInfo",
EnumValues: map[int]string{0: "a"},
},
},
},
},
},
},

// One table lookup, lookup not walked, labels kept.
{
node: &Node{Oid: "1", Label: "root",
Expand Down
Loading