diff --git a/plugins/inputs/ipset/ipset.go b/plugins/inputs/ipset/ipset.go index 4dfee2da7..88ce04072 100644 --- a/plugins/inputs/ipset/ipset.go +++ b/plugins/inputs/ipset/ipset.go @@ -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) } } diff --git a/plugins/inputs/ipset/ipset_test.go b/plugins/inputs/ipset/ipset_test.go index f205728c0..117fcc3ea 100644 --- a/plugins/inputs/ipset/ipset_test.go +++ b/plugins/inputs/ipset/ipset_test.go @@ -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 {