Skip to content

Commit

Permalink
Support multi-index lookups.
Browse files Browse the repository at this point in the history
This changes old_index to be old_indexes in generator.yml.

Signed-off-by: Brian Brazil <[email protected]>
  • Loading branch information
brian-brazil committed Sep 25, 2018
1 parent 6b5ce0b commit cbeddc8
Show file tree
Hide file tree
Showing 7 changed files with 1,231 additions and 613 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ type Index struct {
type Lookup struct {
Labels []string `yaml:"labels"`
Labelname string `yaml:"labelname"`
Oid string `yaml:"oid"`
Type string `yaml:"type"`
Oid string `yaml:"oid,omitempty"`
Type string `yaml:"type,omitempty"`
}

// Secret is a string that must not be revealed on marshaling.
Expand Down
2 changes: 1 addition & 1 deletion generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ modules:
# on the resulting metrics from that table. Instead, use the index to
# lookup the bsnDot11EssSsid table entry and create a bsnDot11EssSsid label
# with that value.
- old_index: bsnDot11EssIndex
- old_indexes: [bsnDot11EssIndex]
new_index: bsnDot11EssSsid

overrides: # Allows for per-module overrides of bits of MIBs
Expand Down
4 changes: 2 additions & 2 deletions generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ type ModuleConfig struct {
}

type Lookup struct {
OldIndex string `yaml:"old_index"`
NewIndex string `yaml:"new_index"`
OldIndexes []string `yaml:"old_indexes"`
NewIndex string `yaml:"new_index"`
}
34 changes: 17 additions & 17 deletions generator/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ modules:
if_mib_ifalias:
walk: [sysUpTime, interfaces, ifXTable]
lookups:
- old_index: ifIndex
- old_indexes: [ifIndex]
new_index: ifAlias
# Interfaces if ifDescr is unique.
if_mib_ifdescr:
walk: [sysUpTime, interfaces, ifXTable]
lookups:
- old_index: ifIndex
- old_indexes: [ifIndex]
new_index: ifDescr
# Interfaces if ifName is unique.
if_mib_ifname:
walk: [sysUpTime, interfaces, ifXTable]
lookups:
- old_index: ifIndex
- old_indexes: [ifIndex]
# Use OID to avoid conflict with Netscaler NS-ROOT-MIB.
new_index: 1.3.6.1.2.1.31.1.1.1.1 # ifName

Expand All @@ -35,9 +35,9 @@ modules:
- 1.3.6.1.4.1.14179.2.2.13.1.3 # bsnAPIfLoadChannelUtilization
- 1.3.6.1.4.1.14179.2.2.15.1.21 # bsnAPIfDBNoisePower
lookups:
- old_index: bsnDot11EssIndex
- old_indexes: [bsnDot11EssIndex]
new_index: bsnDot11EssSsid
- old_index: bsnAPDot3MacAddress
- old_indexes: [bsnAPDot3MacAddress]
new_index: bsnAPName

# APC/Schneider UPS Network Management Cards
Expand Down Expand Up @@ -68,9 +68,9 @@ modules:
- 1.3.6.1.4.1.318.1.1.1.12 # upsOutletGroups
- 1.3.6.1.4.1.318.1.1.10.2.3.2 # iemStatusProbesTable
lookups:
- old_index: upsOutletGroupStatusIndex
- old_indexes: [upsOutletGroupStatusIndex]
new_index: upsOutletGroupStatusName
- old_index: iemStatusProbeIndex
- old_indexes: [iemStatusProbeIndex]
new_index: iemStatusProbeName

# ServerTech Sentry 3 MIB
Expand Down Expand Up @@ -159,22 +159,22 @@ modules:
- 1.3.6.1.4.1.6574.102 # spaceIO
- 1.3.6.1.4.1.6574.104 # synologyiSCSILUN
lookups:
- old_index: spaceIOIndex
- old_indexes: [spaceIOIndex]
new_index: spaceIODevice
- old_index: storageIOIndex
- old_indexes: [storageIOIndex]
new_index: storageIODevice
- old_index: serviceInfoIndex
- old_indexes: [serviceInfoIndex]
new_index: serviceName
- old_index: ifIndex
- old_indexes: [ifIndex]
# Use OID to avoid conflict with Netscaler NS-ROOT-MIB.
new_index: 1.3.6.1.2.1.31.1.1.1.1 # ifName
- old_index: diskIndex
- old_indexes: [diskIndex]
new_index: diskID
- old_index: raidIndex
- old_indexes: [raidIndex]
new_index: raidName
- old_index: laIndex
- old_indexes: [laIndex]
new_index: laNames
- old_index: hrStorageIndex
- old_indexes: [hrStorageIndex]
new_index: hrStorageDescr

# DD-WRT
Expand All @@ -195,9 +195,9 @@ modules:
- 1.3.6.1.4.1.2021.10.1.5 # laLoadInt
- 1.3.6.1.4.1.2021.11 # systemStats
lookups:
- old_index: ifIndex
- old_indexes: [ifIndex]
new_index: ifDescr
- old_index: laIndex
- old_indexes: [laIndex]
new_index: laNames

# Ubiquiti / AirMAX
Expand Down
72 changes: 41 additions & 31 deletions generator/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,43 +369,53 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]*
}

// Apply lookups.
for _, lookup := range cfg.Lookups {
for _, metric := range out.Metrics {
toDelete := []string{}
for _, metric := range out.Metrics {
toDelete := []string{}
for _, lookup := range cfg.Lookups {
foundIndexes := 0
// See if all lookup indexes are present.
for _, index := range metric.Indexes {
if index.Labelname == lookup.OldIndex {
if _, ok := nameToNode[lookup.NewIndex]; !ok {
log.Fatalf("Unknown index '%s'", lookup.NewIndex)
}
indexNode := nameToNode[lookup.NewIndex]
// Avoid leaving the old labelname around.
toDelete = append(toDelete, lookup.OldIndex)
typ, ok := metricType(indexNode.Type)
if !ok {
log.Fatalf("Unknown index type %s for %s", indexNode.Type, lookup.NewIndex)
}
metric.Lookups = append(metric.Lookups, &config.Lookup{
Labels: []string{sanitizeLabelName(index.Labelname)},
Labelname: sanitizeLabelName(indexNode.Label),
Type: typ,
Oid: indexNode.Oid,
})
// Make sure we walk the lookup OID(s).
if len(tableInstances[metric.Oid]) > 0 {
for _, index := range tableInstances[metric.Oid] {
needToWalk[indexNode.Oid+index+"."] = struct{}{}
}
} else {
needToWalk[indexNode.Oid] = struct{}{}
for _, lookupIndex := range lookup.OldIndexes {
if index.Labelname == lookupIndex {
foundIndexes++
}
}
}
for _, l := range toDelete {
metric.Lookups = append(metric.Lookups, &config.Lookup{
Labelname: sanitizeLabelName(l),
})
if foundIndexes == len(lookup.OldIndexes) {
if _, ok := nameToNode[lookup.NewIndex]; !ok {
log.Fatalf("Unknown index '%s'", lookup.NewIndex)
}
indexNode := nameToNode[lookup.NewIndex]
typ, ok := metricType(indexNode.Type)
if !ok {
log.Fatalf("Unknown index type %s for %s", indexNode.Type, lookup.NewIndex)
}
l := &config.Lookup{
Labelname: sanitizeLabelName(indexNode.Label),
Type: typ,
Oid: indexNode.Oid,
}
for _, oldIndex := range lookup.OldIndexes {
l.Labels = append(l.Labels, sanitizeLabelName(oldIndex))
}
metric.Lookups = append(metric.Lookups, l)
// Make sure we walk the lookup OID(s).
if len(tableInstances[metric.Oid]) > 0 {
for _, index := range tableInstances[metric.Oid] {
needToWalk[indexNode.Oid+index+"."] = struct{}{}
}
} else {
needToWalk[indexNode.Oid] = struct{}{}
}
// Avoid leaving the old labelname around.
toDelete = append(toDelete, lookup.OldIndexes...)
}
}
for _, l := range toDelete {
metric.Lookups = append(metric.Lookups, &config.Lookup{
Labelname: sanitizeLabelName(l),
})
}
}

// Ensure index label names are sane.
Expand Down
78 changes: 68 additions & 10 deletions generator/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,8 +955,8 @@ func TestGenerateConfigModule(t *testing.T) {
Walk: []string{"octetFoo"},
Lookups: []*Lookup{
{
OldIndex: "octetIndex",
NewIndex: "octetDesc",
OldIndexes: []string{"octetIndex"},
NewIndex: "octetDesc",
},
},
},
Expand Down Expand Up @@ -1005,8 +1005,8 @@ func TestGenerateConfigModule(t *testing.T) {
Walk: []string{"octetFoo"},
Lookups: []*Lookup{
{
OldIndex: "octetIndex",
NewIndex: "1.1.1.2",
OldIndexes: []string{"octetIndex"},
NewIndex: "1.1.1.2",
},
},
},
Expand Down Expand Up @@ -1040,6 +1040,64 @@ func TestGenerateConfigModule(t *testing.T) {
},
},
},
// Multi-index table lookup, lookup not walked.
{
node: &Node{Oid: "1", Label: "root",
Children: []*Node{
{Oid: "1.1", Label: "octet",
Children: []*Node{
{Oid: "1.1.1", Label: "octetEntry", Indexes: []string{"octetIndex", "octetIndex2"},
Children: []*Node{
{Oid: "1.1.1.1", Access: "ACCESS_READONLY", Label: "octetIndex", Type: "INTEGER"},
{Oid: "1.1.1.2", Access: "ACCESS_READONLY", Label: "octetIndex2", Type: "INTEGER"},
{Oid: "1.1.1.3", Access: "ACCESS_READONLY", Label: "octetDesc", Type: "OCTETSTR"},
{Oid: "1.1.1.4", Access: "ACCESS_READONLY", Label: "octetFoo", Type: "INTEGER"}}}}}}},
cfg: &ModuleConfig{
Walk: []string{"octetFoo"},
Lookups: []*Lookup{
{
OldIndexes: []string{"octetIndex", "octetIndex2"},
NewIndex: "octetDesc",
},
},
},
out: &config.Module{
// Walk is expanded to include the lookup OID.
Walk: []string{"1.1.1.3", "1.1.1.4"},
Metrics: []*config.Metric{
{
Name: "octetFoo",
Oid: "1.1.1.4",
Help: " - 1.1.1.4",
Type: "gauge",
Indexes: []*config.Index{
{
Labelname: "octetIndex",
Type: "gauge",
},
{
Labelname: "octetIndex2",
Type: "gauge",
},
},
Lookups: []*config.Lookup{
{
Labels: []string{"octetIndex", "octetIndex2"},
Labelname: "octetDesc",
Type: "OctetString",
Oid: "1.1.1.3",
},
{
Labelname: "octetIndex",
},
{
Labelname: "octetIndex2",
},
},
},
},
},
},
// Validate metric names.
{
node: &Node{Oid: "1", Label: "root",
Expand Down Expand Up @@ -1078,8 +1136,8 @@ func TestGenerateConfigModule(t *testing.T) {
Walk: []string{"octet^Foo"},
Lookups: []*Lookup{
{
OldIndex: "octet&Index",
NewIndex: "1.1.1.2",
OldIndexes: []string{"octet&Index"},
NewIndex: "1.1.1.2",
},
},
},
Expand Down Expand Up @@ -1240,8 +1298,8 @@ func TestGenerateConfigModule(t *testing.T) {
Walk: []string{"1.1.1.2.100", "1.1.1.4.100", "1.1.1.2.200"},
Lookups: []*Lookup{
{
OldIndex: "tableIndex",
NewIndex: "tableDesc",
OldIndexes: []string{"tableIndex"},
NewIndex: "tableDesc",
},
},
},
Expand Down Expand Up @@ -1313,8 +1371,8 @@ func TestGenerateConfigModule(t *testing.T) {
Walk: []string{"1.1.1.2.100", "1.1.1.3"},
Lookups: []*Lookup{
{
OldIndex: "tableIndex",
NewIndex: "tableDesc",
OldIndexes: []string{"tableIndex"},
NewIndex: "tableDesc",
},
},
},
Expand Down
Loading

0 comments on commit cbeddc8

Please sign in to comment.