added a check for oid and name to prevent empty metrics (#9366)
This commit is contained in:
parent
84a37642d5
commit
b846c5069d
|
|
@ -173,6 +173,12 @@ type Table struct {
|
||||||
|
|
||||||
// Init() builds & initializes the nested fields.
|
// Init() builds & initializes the nested fields.
|
||||||
func (t *Table) Init() error {
|
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 {
|
if t.initialized {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -199,7 +199,8 @@ func TestSnmpInit_noTranslate(t *testing.T) {
|
||||||
{Oid: ".1.1.1.3"},
|
{Oid: ".1.1.1.3"},
|
||||||
},
|
},
|
||||||
Tables: []Table{
|
Tables: []Table{
|
||||||
{Fields: []Field{
|
{Name: "testing",
|
||||||
|
Fields: []Field{
|
||||||
{Oid: ".1.1.1.4", Name: "four", IsTag: true},
|
{Oid: ".1.1.1.4", Name: "four", IsTag: true},
|
||||||
{Oid: ".1.1.1.5", Name: "five"},
|
{Oid: ".1.1.1.5", Name: "five"},
|
||||||
{Oid: ".1.1.1.6"},
|
{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)
|
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) {
|
func TestGetSNMPConnection_v2(t *testing.T) {
|
||||||
s := &Snmp{
|
s := &Snmp{
|
||||||
Agents: []string{"1.2.3.4:567", "1.2.3.4", "udp://127.0.0.1"},
|
Agents: []string{"1.2.3.4:567", "1.2.3.4", "udp://127.0.0.1"},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue