From f4998bcecf656f482a6003e016ff595f0c8b859c Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Tue, 25 Sep 2018 14:42:09 +0100 Subject: [PATCH] Allow keeping of old labels from lookups. Signed-off-by: Brian Brazil --- generator/README.md | 3 ++- generator/config.go | 1 + generator/tree.go | 6 ++++-- generator/tree_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/generator/README.md b/generator/README.md index c37ae581..2394af6d 100644 --- a/generator/README.md +++ b/generator/README.md @@ -82,7 +82,7 @@ modules: # Required if context is configured on the device. lookups: # Optional list of lookups to perform. - # This must only be used when the new index is unique. + # If keep_old is the default of false, this must only be used when the new index is unique. # If the index of a table is bsnDot11EssIndex, usually that'd be the label # on the resulting metrics from that table. Instead, use the index to @@ -90,6 +90,7 @@ modules: # with that value. - old_indexes: [bsnDot11EssIndex] new_index: bsnDot11EssSsid + keep_old: false # If true, don't delete old index label for this lookup. overrides: # Allows for per-module overrides of bits of MIBs metricName: diff --git a/generator/config.go b/generator/config.go index 109a7415..d0dabb14 100644 --- a/generator/config.go +++ b/generator/config.go @@ -55,4 +55,5 @@ type ModuleConfig struct { type Lookup struct { OldIndexes []string `yaml:"old_indexes"` NewIndex string `yaml:"new_index"` + KeepOld bool `yaml:"keep_old,omitempty"` } diff --git a/generator/tree.go b/generator/tree.go index 8a968be9..31a23b84 100644 --- a/generator/tree.go +++ b/generator/tree.go @@ -407,8 +407,10 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]* } else { needToWalk[indexNode.Oid] = struct{}{} } - // Avoid leaving the old labelname around. - toDelete = append(toDelete, lookup.OldIndexes...) + if !lookup.KeepOld { + // Avoid leaving the old labelname around. + toDelete = append(toDelete, lookup.OldIndexes...) + } } } for _, l := range toDelete { diff --git a/generator/tree_test.go b/generator/tree_test.go index a91a9e6e..8f6e5ab3 100644 --- a/generator/tree_test.go +++ b/generator/tree_test.go @@ -940,6 +940,54 @@ func TestGenerateConfigModule(t *testing.T) { }, }, }, + // One table lookup, lookup not walked, labels kept. + { + node: &Node{Oid: "1", Label: "root", + Children: []*Node{ + {Oid: "1.1", Label: "octet", + Children: []*Node{ + {Oid: "1.1.1", Label: "octetEntry", Indexes: []string{"octetIndex"}, + Children: []*Node{ + {Oid: "1.1.1.1", Access: "ACCESS_READONLY", Label: "octetIndex", Type: "INTEGER"}, + {Oid: "1.1.1.2", Access: "ACCESS_READONLY", Label: "octetDesc", Type: "OCTETSTR"}, + {Oid: "1.1.1.3", Access: "ACCESS_READONLY", Label: "octetFoo", Type: "INTEGER"}}}}}}}, + cfg: &ModuleConfig{ + Walk: []string{"octetFoo"}, + Lookups: []*Lookup{ + { + OldIndexes: []string{"octetIndex"}, + NewIndex: "octetDesc", + KeepOld: true, + }, + }, + }, + out: &config.Module{ + // Walk is expanded to include the lookup OID. + Walk: []string{"1.1.1.2", "1.1.1.3"}, + Metrics: []*config.Metric{ + { + Name: "octetFoo", + Oid: "1.1.1.3", + Help: " - 1.1.1.3", + Type: "gauge", + Indexes: []*config.Index{ + { + Labelname: "octetIndex", + Type: "gauge", + }, + }, + Lookups: []*config.Lookup{ + { + Labels: []string{"octetIndex"}, + Labelname: "octetDesc", + Type: "OctetString", + Oid: "1.1.1.2", + }, + }, + }, + }, + }, + }, // One table lookup, lookup not walked. { node: &Node{Oid: "1", Label: "root",