Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,19 @@ linters:
- errcheck
- govet
path: _test.go
formatters:
enable:
- gci
- gofumpt
- goimports
settings:
gci:
sections:
- standard
- default
- prefix(github.com/prometheus/snmp_exporter)
gofumpt:
extra-rules: true
goimports:
local-prefixes:
- github.com/prometheus/snmp_exporter
2 changes: 1 addition & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func parseDateAndTimeWithPattern(metric *config.Metric, pdu *gosnmp.SnmpPDU, met
}

func parseNtpTimestamp(pdu *gosnmp.SnmpPDU) (float64, error) {
var data = pdu.Value.([]byte)
data := pdu.Value.([]byte)

// Prometheus uses the Unix time epoch (seconds since 1970).
// NTP seconds are counted since 1900 and must be corrected
Expand Down
68 changes: 33 additions & 35 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
)

func TestPduToSample(t *testing.T) {

cases := []struct {
pdu *gosnmp.SnmpPDU
indexOids []int
Expand All @@ -50,7 +49,7 @@ func TestPduToSample(t *testing.T) {
Oid: "1.1.1.1.1",
Help: "HelpText",
RegexpExtracts: map[string][]config.RegexpExtract{
"Extension": []config.RegexpExtract{
"Extension": {
{
Regex: config.Regexp{
regexp.MustCompile(".*"),
Expand All @@ -76,7 +75,7 @@ func TestPduToSample(t *testing.T) {
Oid: "1.1.1.1.1",
Help: "HelpText",
RegexpExtracts: map[string][]config.RegexpExtract{
"Extension": []config.RegexpExtract{
"Extension": {
{
Regex: config.Regexp{
regexp.MustCompile(".*"),
Expand All @@ -99,7 +98,7 @@ func TestPduToSample(t *testing.T) {
Oid: "1.1.1.1.1",
Help: "HelpText",
RegexpExtracts: map[string][]config.RegexpExtract{
"Extension": []config.RegexpExtract{
"Extension": {
{
Regex: config.Regexp{
regexp.MustCompile("(will_not_match)"),
Expand All @@ -122,7 +121,7 @@ func TestPduToSample(t *testing.T) {
Oid: "1.1.1.1.1",
Help: "HelpText",
RegexpExtracts: map[string][]config.RegexpExtract{
"Status": []config.RegexpExtract{
"Status": {
{
Regex: config.Regexp{
regexp.MustCompile(".*"),
Expand All @@ -147,23 +146,23 @@ func TestPduToSample(t *testing.T) {
Oid: "1.1.1.1.1",
Help: "HelpText",
RegexpExtracts: map[string][]config.RegexpExtract{
"Blank": []config.RegexpExtract{
"Blank": {
{
Regex: config.Regexp{
regexp.MustCompile("^XXXX$"),
},
Value: "4",
},
},
"Extension": []config.RegexpExtract{
"Extension": {
{
Regex: config.Regexp{
regexp.MustCompile(".*"),
},
Value: "5",
},
},
"MultipleRegexes": []config.RegexpExtract{
"MultipleRegexes": {
{
Regex: config.Regexp{
regexp.MustCompile("^XXXX$"),
Expand All @@ -183,7 +182,7 @@ func TestPduToSample(t *testing.T) {
Value: "777",
},
},
"Template": []config.RegexpExtract{
"Template": {
{
Regex: config.Regexp{
regexp.MustCompile(`(\d.\d+)`),
Expand Down Expand Up @@ -379,7 +378,7 @@ func TestPduToSample(t *testing.T) {
Help: "Help string",
Indexes: []*config.Index{{Labelname: "foo", Type: "DisplayString"}},
RegexpExtracts: map[string][]config.RegexpExtract{
"": []config.RegexpExtract{{Value: "1", Regex: config.Regexp{regexp.MustCompile(".*")}}},
"": {{Value: "1", Regex: config.Regexp{regexp.MustCompile(".*")}}},
},
},
oidToPdu: make(map[string]gosnmp.SnmpPDU),
Expand All @@ -397,7 +396,7 @@ func TestPduToSample(t *testing.T) {
Type: "InetAddress",
Help: "Help string",
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": gosnmp.SnmpPDU{Value: 1}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": {Value: 1}},
expectedMetrics: []string{`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: {test_metric}} label:{name:"test_metric" value:"4.5.6.7"} gauge:{value:1}`},
},
{
Expand All @@ -412,7 +411,7 @@ func TestPduToSample(t *testing.T) {
Type: "InetAddressMissingSize",
Help: "Help string",
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": gosnmp.SnmpPDU{Value: 1}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": {Value: 1}},
expectedMetrics: []string{`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: {test_metric}} label:{name:"test_metric" value:"4.5.6.7"} gauge:{value:1}`},
},
{
Expand All @@ -427,7 +426,7 @@ func TestPduToSample(t *testing.T) {
Type: "InetAddress",
Help: "Help string",
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": gosnmp.SnmpPDU{Value: 2}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": {Value: 2}},
expectedMetrics: []string{`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: {test_metric}} label:{name:"test_metric" value:"0405:0607:0809:0A0B:0C0D:0E0F:1011:1213"} gauge:{value:1}`},
},
{
Expand All @@ -442,7 +441,7 @@ func TestPduToSample(t *testing.T) {
Type: "InetAddress",
Help: "Help string",
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": gosnmp.SnmpPDU{Value: 3}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": {Value: 3}},
expectedMetrics: []string{`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: {test_metric}} label:{name:"test_metric" value:"0x0405060708"} gauge:{value:1}`},
},
{
Expand Down Expand Up @@ -472,7 +471,7 @@ func TestPduToSample(t *testing.T) {
Type: "LldpPortId",
Help: "Help string",
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": gosnmp.SnmpPDU{Value: 3}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.41.2": {Value: 3}},
expectedMetrics: []string{`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: {test_metric}} label:{name:"test_metric" value:"04:05:06:07:08:09"} gauge:{value:1}`},
},
{
Expand Down Expand Up @@ -902,7 +901,7 @@ func TestIndexesToLabels(t *testing.T) {
Indexes: []*config.Index{{Labelname: "a", Type: "gauge"}, {Labelname: "b", Type: "gauge"}},
Lookups: []*config.Lookup{{Labels: []string{"a", "b"}, Labelname: "l", Oid: "1.2"}},
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": gosnmp.SnmpPDU{Value: "eth0"}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": {Value: "eth0"}},
result: map[string]string{"a": "3", "b": "4", "l": "eth0"},
},
{
Expand All @@ -911,7 +910,7 @@ func TestIndexesToLabels(t *testing.T) {
Indexes: []*config.Index{{Labelname: "l", Type: "gauge"}},
Lookups: []*config.Lookup{{Labels: []string{"l"}, Labelname: "l", Oid: "1.2.3"}},
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": gosnmp.SnmpPDU{Value: "eth0"}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": {Value: "eth0"}},
result: map[string]string{"l": "eth0"},
},
{
Expand All @@ -920,7 +919,7 @@ func TestIndexesToLabels(t *testing.T) {
Indexes: []*config.Index{{Labelname: "l", Type: "gauge"}},
Lookups: []*config.Lookup{{Labels: []string{"l"}, Labelname: "l", Oid: "1.2.3", Type: "InetAddressIPv4"}},
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": gosnmp.SnmpPDU{Value: []byte{5, 6, 7, 8}}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": {Value: []byte{5, 6, 7, 8}}},
result: map[string]string{"l": "5.6.7.8"},
},
{
Expand All @@ -929,7 +928,7 @@ func TestIndexesToLabels(t *testing.T) {
Indexes: []*config.Index{{Labelname: "l", Type: "gauge"}},
Lookups: []*config.Lookup{{Labels: []string{"l"}, Labelname: "l", Oid: "1.2.3", Type: "InetAddressIPv6"}},
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": gosnmp.SnmpPDU{Value: []byte{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": {Value: []byte{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}}},
result: map[string]string{"l": "0506:0708:090A:0B0C:0D0E:0F10:1112:1314"},
},
{
Expand All @@ -938,7 +937,7 @@ func TestIndexesToLabels(t *testing.T) {
Indexes: []*config.Index{{Labelname: "l", Type: "gauge"}},
Lookups: []*config.Lookup{{Labels: []string{"l"}, Labelname: "l", Oid: "1.2.3"}},
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": gosnmp.SnmpPDU{Value: []byte{5, 6, 7, 8}}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.2.3.4": {Value: []byte{5, 6, 7, 8}}},
result: map[string]string{"l": "0x05060708"},
},
{
Expand Down Expand Up @@ -1025,7 +1024,7 @@ func TestIndexesToLabels(t *testing.T) {
Indexes: []*config.Index{{Labelname: "l", Type: "OctetString"}},
Lookups: []*config.Lookup{{Labels: []string{"l"}, Labelname: "l", Oid: "1"}},
},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.3.65.32.255": gosnmp.SnmpPDU{Value: "octet"}},
oidToPdu: map[string]gosnmp.SnmpPDU{"1.3.65.32.255": {Value: "octet"}},
result: map[string]string{"l": "octet"},
},
{
Expand Down Expand Up @@ -1098,9 +1097,9 @@ func TestIndexesToLabels(t *testing.T) {
},
},
oidToPdu: map[string]gosnmp.SnmpPDU{
"1.1.1.1.1": gosnmp.SnmpPDU{Value: "source_obj0"},
"1.1.1.2.1": gosnmp.SnmpPDU{Value: 42},
"2.2.2.42": gosnmp.SnmpPDU{Value: "targetvalue"},
"1.1.1.1.1": {Value: "source_obj0"},
"1.1.1.2.1": {Value: 42},
"2.2.2.42": {Value: "targetvalue"},
},
result: map[string]string{"a": "1", "chainable_id": "42", "targetlabel": "targetvalue"},
},
Expand All @@ -1114,9 +1113,9 @@ func TestIndexesToLabels(t *testing.T) {
},
},
oidToPdu: map[string]gosnmp.SnmpPDU{
"1.1.1.1.1": gosnmp.SnmpPDU{Value: "source_obj0"},
"1.1.1.2.1": gosnmp.SnmpPDU{Value: uint(42)},
"2.2.2.42": gosnmp.SnmpPDU{Value: "targetvalue"},
"1.1.1.1.1": {Value: "source_obj0"},
"1.1.1.2.1": {Value: uint(42)},
"2.2.2.42": {Value: "targetvalue"},
},
result: map[string]string{"a": "1", "chainable_id": "42", "targetlabel": "targetvalue"},
},
Expand All @@ -1133,9 +1132,9 @@ func TestIndexesToLabels(t *testing.T) {
},
},
oidToPdu: map[string]gosnmp.SnmpPDU{
"1.1.9.1.8.1": gosnmp.SnmpPDU{Value: "hostname"},
"1.1.2.8": gosnmp.SnmpPDU{Value: 3},
"1.1.3.8": gosnmp.SnmpPDU{Value: []byte{4, 5, 6, 7, 8, 9}},
"1.1.9.1.8.1": {Value: "hostname"},
"1.1.2.8": {Value: 3},
"1.1.3.8": {Value: []byte{4, 5, 6, 7, 8, 9}},
},
result: map[string]string{"lldpRemTimeMark": "1", "lldpRemLocalPortNum": "8", "lldpRemIndex": "1", "lldpLocPortId": "04:05:06:07:08:09"},
},
Expand Down Expand Up @@ -1316,21 +1315,20 @@ func TestConfigureTarget(t *testing.T) {
}

func TestFilterAllowedIndices(t *testing.T) {

pdus := []gosnmp.SnmpPDU{
gosnmp.SnmpPDU{
{
Name: "1.3.6.1.2.1.2.2.1.8.1",
Value: "2",
},
gosnmp.SnmpPDU{
{
Name: "1.3.6.1.2.1.2.2.1.8.2",
Value: "1",
},
gosnmp.SnmpPDU{
{
Name: "1.3.6.1.2.1.2.2.1.8.3",
Value: "1",
},
gosnmp.SnmpPDU{
{
Name: "1.3.6.1.2.1.2.2.1.8.4",
Value: "5",
},
Expand Down
4 changes: 1 addition & 3 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import (
"github.com/prometheus/snmp_exporter/config"
)

var (
cannotFindModuleRE = regexp.MustCompile(`Cannot find module \((.+)\): (.+)`)
)
var cannotFindModuleRE = regexp.MustCompile(`Cannot find module \((.+)\): (.+)`)

// Generate a snmp_exporter config and write it out.
func generateConfig(nodes *Node, nameToNode map[string]*Node, logger *slog.Logger) error {
Expand Down
1 change: 0 additions & 1 deletion generator/net_snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ func buildMIBTree(t *C.struct_tree, n *Node, oid string) {

// Convert the NetSNMP MIB tree to a Go data structure.
func getMIBTree() *Node {

tree := C.get_tree_head()
head := &Node{}
buildMIBTree(tree, head, "")
Expand Down
4 changes: 1 addition & 3 deletions generator/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,7 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]*
return out, nil
}

var (
invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
)
var invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)

func sanitizeLabelName(name string) string {
return invalidLabelCharRE.ReplaceAllString(name, "_")
Expand Down
Loading