Skip to content

Commit

Permalink
added a check for oid and name to prevent empty metrics (#9366)
Browse files Browse the repository at this point in the history
(cherry picked from commit b846c50)
  • Loading branch information
MyaLongmire authored and reimda committed Jul 7, 2021
1 parent 44af73d commit 00ecaa9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
6 changes: 6 additions & 0 deletions plugins/inputs/snmp/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ type Table struct {

// Init() builds & initializes the nested fields.
func (t *Table) Init() error {
//makes sure oid or name is set in config file
//otherwise snmp will produce metrics with an empty name
if t.Oid == "" && t.Name == "" {
return fmt.Errorf("SNMP table in config file is not named. One or both of the oid and name settings must be set")
}

if t.initialized {
return nil
}
Expand Down
26 changes: 21 additions & 5 deletions plugins/inputs/snmp/snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ func TestSnmpInit_noTranslate(t *testing.T) {
{Oid: ".1.1.1.3"},
},
Tables: []Table{
{Fields: []Field{
{Oid: ".1.1.1.4", Name: "four", IsTag: true},
{Oid: ".1.1.1.5", Name: "five"},
{Oid: ".1.1.1.6"},
}},
{Name: "testing",
Fields: []Field{
{Oid: ".1.1.1.4", Name: "four", IsTag: true},
{Oid: ".1.1.1.5", Name: "five"},
{Oid: ".1.1.1.6"},
}},
},
}

Expand Down Expand Up @@ -235,6 +236,21 @@ func TestSnmpInit_noTranslate(t *testing.T) {
assert.Equal(t, false, s.Tables[0].Fields[2].IsTag)
}

func TestSnmpInit_noName_noOid(t *testing.T) {
s := &Snmp{
Tables: []Table{
{Fields: []Field{
{Oid: ".1.1.1.4", Name: "four", IsTag: true},
{Oid: ".1.1.1.5", Name: "five"},
{Oid: ".1.1.1.6"},
}},
},
}

err := s.init()
require.Error(t, err)
}

func TestGetSNMPConnection_v2(t *testing.T) {
s := &Snmp{
Agents: []string{"1.2.3.4:567", "1.2.3.4", "udp://127.0.0.1"},
Expand Down

0 comments on commit 00ecaa9

Please sign in to comment.