fix(inputs.ipset): Parse lines with timeout (#14262)

This commit is contained in:
Joshua Powers 2023-11-10 04:32:36 -07:00 committed by GitHub
parent d644ffd3d2
commit 7d79111135
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 11 deletions

View File

@ -73,18 +73,31 @@ func (i *Ipset) Gather(acc telegraf.Accumulator) error {
"set": data[1],
"rule": data[2],
}
packetsTotal, err := strconv.ParseUint(data[4], 10, 64)
if err != nil {
acc.AddError(err)
}
bytesTotal, err := strconv.ParseUint(data[6], 10, 64)
if err != nil {
acc.AddError(err)
}
fields := map[string]interface{}{
"packets_total": packetsTotal,
"bytes_total": bytesTotal,
fields := make(map[string]interface{}, 3)
for i, field := range data {
switch field {
case "timeout":
val, err := strconv.ParseUint(data[i+1], 10, 64)
if err != nil {
acc.AddError(err)
}
fields["timeout"] = val
case "packets":
val, err := strconv.ParseUint(data[i+1], 10, 64)
if err != nil {
acc.AddError(err)
}
fields["packets_total"] = val
case "bytes":
val, err := strconv.ParseUint(data[i+1], 10, 64)
if err != nil {
acc.AddError(err)
}
fields["bytes_total"] = val
}
}
acc.AddCounter(measurement, fields, tags)
}
}

View File

@ -74,6 +74,22 @@ func TestIpset(t *testing.T) {
{map[string]interface{}{"packets_total": uint64(3), "bytes_total": uint64(222)}},
},
},
{
name: "Sets with and without timeouts",
value: `create counter-test hash:ip family inet hashsize 1024 maxelem 65536 timeout 1800 counters
add counter-test 192.168.1.1 timeout 1792 packets 8 bytes 672
create counter-test2 hash:ip family inet hashsize 1024 maxelem 65536 counters
add counter-test2 192.168.1.1 packets 18 bytes 673
`,
tags: []map[string]string{
{"set": "counter-test", "rule": "192.168.1.1"},
{"set": "counter-test2", "rule": "192.168.1.1"},
},
fields: [][]map[string]interface{}{
{map[string]interface{}{"packets_total": uint64(8), "bytes_total": uint64(672), "timeout": uint64(1792)}},
{map[string]interface{}{"packets_total": uint64(18), "bytes_total": uint64(673)}},
},
},
}
for i, tt := range tests {