added a check for oid and name to prevent empty metrics (#9366)

This commit is contained in:
Mya 2021-06-22 14:59:02 -06:00 committed by GitHub
parent 84a37642d5
commit b846c5069d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View File

@ -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
}

View File

@ -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"},
}},
},
}
@ -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"},