Skip to content

Commit

Permalink
Add support for LLDP-MIB's LldpPortId
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-brazil committed Sep 19, 2018
1 parent 0d9f871 commit 3c8915c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
prometheus.MustRegister(snmpUnexpectedPduType)
}

// Types preceded by an enum with their actual type.
var combinedTypeMapping = map[string]map[int]string{
"InetAddress": {
1: "InetAddressIPv4",
Expand All @@ -50,6 +51,13 @@ var combinedTypeMapping = map[string]map[int]string{
1: "InetAddressIPv4",
2: "InetAddressIPv6",
},
"LldpPortId": {
1: "DisplayString",
2: "DisplayString",
3: "PhysAddress48",
5: "DisplayString",
7: "DisplayString",
},
}

func oidToList(oid string) []int {
Expand Down
15 changes: 15 additions & 0 deletions collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,21 @@ func TestPduToSample(t *testing.T) {
oidToPdu: make(map[string]gosnmp.SnmpPDU),
expectedMetrics: map[string]string{`label:<name:"test_metric" value:"0x04050607" > gauge:<value:1 > `: `Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [test_metric]}`},
},
{
pdu: &gosnmp.SnmpPDU{
Name: "1.42.2",
Value: []byte{4, 5, 6, 7, 8, 9},
},
indexOids: []int{2},
metric: &config.Metric{
Name: "test_metric",
Oid: "1.42",
Type: "LldpPortId",
Help: "Help string",
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": gosnmp.SnmpPDU{Value: 3}},
expectedMetrics: map[string]string{`label:<name:"test_metric" value:"04:05:06:07:08:09" > gauge:<value:1 > `: `Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [test_metric]}`},
},
}

for i, c := range cases {
Expand Down
5 changes: 5 additions & 0 deletions generator/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
var combinedTypes = map[string]string{
"InetAddress": "InetAddressType",
"InetAddressMissingSize": "InetAddressType",
"LldpPortId": "LldpPortIdSubtype",
}

// Helper to walk MIB nodes.
Expand Down Expand Up @@ -136,6 +137,10 @@ func prepareTree(nodes *Node) map[string]*Node {
if n.TextualConvention == "InetAddressIPv4" || n.TextualConvention == "InetAddressIPv6" || n.TextualConvention == "InetAddress" {
n.Type = n.TextualConvention
}
// Convert LLDP-MIB LldpPortId type textual convention to type.
if n.TextualConvention == "LldpPortId" {
n.Type = n.TextualConvention
}
})

return nameToNode
Expand Down
45 changes: 45 additions & 0 deletions generator/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,51 @@ func TestGenerateConfigModule(t *testing.T) {
Walk: []string{"1"},
},
},
// Table with LldpPortIdSubtype and LldpPortId index.
// Index becomes just LldpPortId.
{
node: &Node{Oid: "1", Label: "root",
Children: []*Node{
{Oid: "1.1", Label: "table",
Children: []*Node{
{Oid: "1.1.1", Label: "tableEntry", Indexes: []string{"tableAddrType", "tableAddr"},
Children: []*Node{
{Oid: "1.1.1.1", Access: "ACCESS_READONLY", Label: "tableAddrType", Type: "INTEGER", TextualConvention: "LldpPortIdSubtype"},
{Oid: "1.1.1.2", Access: "ACCESS_READONLY", Label: "tableAddr", Type: "OCTETSTR", TextualConvention: "LldpPortId"},
}}}}}},
cfg: &ModuleConfig{
Walk: []string{"1"},
},
out: &config.Module{
Walk: []string{"1"},
Metrics: []*config.Metric{
{
Name: "tableAddrType",
Oid: "1.1.1.1",
Type: "gauge",
Help: " - 1.1.1.1",
Indexes: []*config.Index{
{
Labelname: "tableAddr",
Type: "LldpPortId",
},
},
},
{
Name: "tableAddr",
Oid: "1.1.1.2",
Type: "LldpPortId",
Help: " - 1.1.1.2",
Indexes: []*config.Index{
{
Labelname: "tableAddr",
Type: "LldpPortId",
},
},
},
},
},
},
}
for i, c := range cases {
// Indexes and lookups always end up initilized.
Expand Down

0 comments on commit 3c8915c

Please sign in to comment.