From 215cb3406795de27e6166f93d7821f1146dae9a2 Mon Sep 17 00:00:00 2001 From: Laurent Sesques Date: Tue, 17 Oct 2017 06:58:01 +0200 Subject: [PATCH 1/5] Add ipset input plugin --- plugins/inputs/all/all.go | 1 + plugins/inputs/ipset/README.md | 59 +++++++++++++++++ plugins/inputs/ipset/ipset.go | 114 +++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 plugins/inputs/ipset/README.md create mode 100644 plugins/inputs/ipset/ipset.go diff --git a/plugins/inputs/all/all.go b/plugins/inputs/all/all.go index 1f4d3825c2341..19b8c1aafe278 100644 --- a/plugins/inputs/all/all.go +++ b/plugins/inputs/all/all.go @@ -34,6 +34,7 @@ import ( _ "github.com/influxdata/telegraf/plugins/inputs/internal" _ "github.com/influxdata/telegraf/plugins/inputs/interrupts" _ "github.com/influxdata/telegraf/plugins/inputs/ipmi_sensor" + _ "github.com/influxdata/telegraf/plugins/inputs/ipset" _ "github.com/influxdata/telegraf/plugins/inputs/iptables" _ "github.com/influxdata/telegraf/plugins/inputs/jolokia" _ "github.com/influxdata/telegraf/plugins/inputs/jolokia2" diff --git a/plugins/inputs/ipset/README.md b/plugins/inputs/ipset/README.md new file mode 100644 index 0000000000000..b5e9d6d3e49e5 --- /dev/null +++ b/plugins/inputs/ipset/README.md @@ -0,0 +1,59 @@ +# Ipset Plugin + +The ipset plugin gathers packets and bytes counters from Linux ipset. +It uses the output of the command "ipset save". +Ipsets created without the "counters" option are ignored. + +Results are tagged with: +- ipset name +- ipset entry + +There are 3 ways to grant telegraf the right to run ipset: +* Run as root (strongly discouraged) +* Use sudo +* Configure systemd to run telegraf with CAP_NET_ADMIN and CAP_NET_RAW capabilities. + +### Using systemd capabilities + +You may run `systemctl edit telegraf.service` and add the following: + +``` +[Service] +CapabilityBoundingSet=CAP_NET_RAW CAP_NET_ADMIN +AmbientCapabilities=CAP_NET_RAW CAP_NET_ADMIN +``` + +### Using sudo + +You may edit your sudo configuration with the following: + +```sudo +telegraf ALL=(root) NOPASSWD: /sbin/ipset save +``` + +### Configuration + +```toml + [[inputs.ipset]] + ## By default, we only show sets which have already matched at least 1 packet. + ## set show_all_sets = true to gather them all. + show_all_sets = false + ## Adjust your sudo settings appropriately if using this option ("sudo ipset save") + ## You can avoid using sudo or root, by setting appropriate privileges for + ## the telegraf.service systemd service. + use_sudo = false +``` + +### Example Output + +``` +$ sudo ipset save +create myset hash:net family inet hashsize 1024 maxelem 65536 counters comment +add myset 10.69.152.1 packets 8 bytes 672 comment "machine A" +``` + +``` +$ telegraf --config telegraf.conf --input-filter ipset --test --debug +* Plugin: inputs.ipset, Collection 1 +> ipset,rule=10.69.152.1,host=trashme,set=myset bytes_total=8i,packets_total=672i 1507615028000000000 +``` diff --git a/plugins/inputs/ipset/ipset.go b/plugins/inputs/ipset/ipset.go new file mode 100644 index 0000000000000..e4d2a174edb4c --- /dev/null +++ b/plugins/inputs/ipset/ipset.go @@ -0,0 +1,114 @@ +package ipset + +import ( + "bufio" + "fmt" + "os" + "os/exec" + "strconv" + "strings" + + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/plugins/inputs" +) + +// Ipsets is a telegraf plugin to gather packets and bytes counters from ipset +type Ipset struct { + ShowAllSets bool + UseSudo bool +} + +// Description returns a short description of the plugin +func (ipset *Ipset) Description() string { + return "Gather packets and bytes counters from ipsets" +} + +// SampleConfig returns sample configuration options. +func (ipset *Ipset) SampleConfig() string { + return ` + ## By default, we only show sets which have already matched at least 1 packet. + ## set show_all_sets = true to gather them all. + show_all_sets = false + ## Adjust your sudo settings appropriately if using this option ("sudo ipset save") + ## TODO: can we replace this with systemd privileges ? CAP_NET_ADMIN should DTRT + use_sudo = false +` +} + +func (ips *Ipset) Gather(acc telegraf.Accumulator) error { + + // Is ipset installed ? + ipsetPath, err := exec.LookPath("ipset") + if err != nil { + return fmt.Errorf("ipset is not installed.") + } + + var args []string + cmdName := ipsetPath + if ips.UseSudo { + cmdName = "sudo" + args = append(args, ipsetPath) + } + args = append(args, "save") + + cmd := exec.Command(cmdName, args...) + cmdReader, err := cmd.StdoutPipe() + if err != nil { + fmt.Fprintln(os.Stderr, "Error in dumping ipset data.") + os.Exit(1) + } + + scanner := bufio.NewScanner(cmdReader) + go func() { + for scanner.Scan() { + data := strings.Split(scanner.Text(), " ") + if data[0] == "add" && (data[4] != "0" || ips.ShowAllSets == true) { + tags := map[string]string{ + "set": data[1], + "rule": data[2], + } + // Ignore sets created without the "counters" option + nocomment := strings.Split(scanner.Text(), "\"")[0] + if !(strings.Contains(nocomment, "packets") && + strings.Contains(nocomment, "bytes")) { + continue + } + + bytes_total, err := strconv.ParseInt(data[4], 10, 64) + if err != nil { + acc.AddError(err) + } + packets_total, err := strconv.ParseInt(data[6], 10, 64) + if err != nil { + acc.AddError(err) + } + fields := map[string]interface{}{ + "bytes_total": bytes_total, + "packets_total": packets_total, + } + acc.AddCounter("ipset", fields, tags) + } + } + }() + + err = cmd.Start() + if err != nil { + acc.AddError(err) + return nil + } + + err = cmd.Wait() + if err != nil { + acc.AddError(err) + return nil + } + + return nil +} + +func init() { + inputs.Add("ipset", func() telegraf.Input { + ips := new(Ipset) + return ips + }) +} From 5a515022b05e8b25ef2c85420b9234964347f104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Sesqu=C3=A8s?= Date: Tue, 17 Oct 2017 16:34:24 +0200 Subject: [PATCH 2/5] ipset input plugin: add unit tests, fix packets and bytes values being swapped --- plugins/inputs/ipset/ipset.go | 108 ++++++++++++-------------- plugins/inputs/ipset/ipset_test.go | 119 +++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 58 deletions(-) create mode 100644 plugins/inputs/ipset/ipset_test.go diff --git a/plugins/inputs/ipset/ipset.go b/plugins/inputs/ipset/ipset.go index e4d2a174edb4c..f7ab065103f86 100644 --- a/plugins/inputs/ipset/ipset.go +++ b/plugins/inputs/ipset/ipset.go @@ -1,9 +1,8 @@ +// +build linux + package ipset import ( - "bufio" - "fmt" - "os" "os/exec" "strconv" "strings" @@ -16,11 +15,12 @@ import ( type Ipset struct { ShowAllSets bool UseSudo bool + lister setLister } // Description returns a short description of the plugin func (ipset *Ipset) Description() string { - return "Gather packets and bytes counters from ipsets" + return "Gather packets and bytes counters from Linux ipsets" } // SampleConfig returns sample configuration options. @@ -35,14 +35,53 @@ func (ipset *Ipset) SampleConfig() string { ` } +const measurement = "ipset" + func (ips *Ipset) Gather(acc telegraf.Accumulator) error { + list, e := ips.lister() + if e != nil { + acc.AddError(e) + } + + lines := strings.Split(list, "\n") + for _, line := range lines { + // Ignore sets created without the "counters" option + nocomment := strings.Split(line, "\"")[0] + if !(strings.Contains(nocomment, "packets") && + strings.Contains(nocomment, "bytes")) { + continue + } + data := strings.Split(line, " ") + if data[0] == "add" && (data[4] != "0" || ips.ShowAllSets == true) { + tags := map[string]string{ + "set": data[1], + "rule": data[2], + } + packets_total, err := strconv.ParseInt(data[4], 10, 64) + if err != nil { + acc.AddError(err) + } + bytes_total, err := strconv.ParseInt(data[6], 10, 64) + if err != nil { + acc.AddError(err) + } + fields := map[string]interface{}{ + "packets_total": packets_total, + "bytes_total": bytes_total, + } + acc.AddCounter(measurement, fields, tags) + } + } + return nil +} + +func (ips *Ipset) setList() (string, error) { // Is ipset installed ? ipsetPath, err := exec.LookPath("ipset") if err != nil { - return fmt.Errorf("ipset is not installed.") + return "", err } - var args []string cmdName := ipsetPath if ips.UseSudo { @@ -52,63 +91,16 @@ func (ips *Ipset) Gather(acc telegraf.Accumulator) error { args = append(args, "save") cmd := exec.Command(cmdName, args...) - cmdReader, err := cmd.StdoutPipe() - if err != nil { - fmt.Fprintln(os.Stderr, "Error in dumping ipset data.") - os.Exit(1) - } - - scanner := bufio.NewScanner(cmdReader) - go func() { - for scanner.Scan() { - data := strings.Split(scanner.Text(), " ") - if data[0] == "add" && (data[4] != "0" || ips.ShowAllSets == true) { - tags := map[string]string{ - "set": data[1], - "rule": data[2], - } - // Ignore sets created without the "counters" option - nocomment := strings.Split(scanner.Text(), "\"")[0] - if !(strings.Contains(nocomment, "packets") && - strings.Contains(nocomment, "bytes")) { - continue - } - - bytes_total, err := strconv.ParseInt(data[4], 10, 64) - if err != nil { - acc.AddError(err) - } - packets_total, err := strconv.ParseInt(data[6], 10, 64) - if err != nil { - acc.AddError(err) - } - fields := map[string]interface{}{ - "bytes_total": bytes_total, - "packets_total": packets_total, - } - acc.AddCounter("ipset", fields, tags) - } - } - }() - - err = cmd.Start() - if err != nil { - acc.AddError(err) - return nil - } - - err = cmd.Wait() - if err != nil { - acc.AddError(err) - return nil - } - - return nil + out, err := cmd.Output() + return string(out), err } +type setLister func() (string, error) + func init() { inputs.Add("ipset", func() telegraf.Input { ips := new(Ipset) + ips.lister = ips.setList return ips }) } diff --git a/plugins/inputs/ipset/ipset_test.go b/plugins/inputs/ipset/ipset_test.go new file mode 100644 index 0000000000000..0ed2bb6d4bf7f --- /dev/null +++ b/plugins/inputs/ipset/ipset_test.go @@ -0,0 +1,119 @@ +// +build linux + +package ipset + +import ( + "errors" + "reflect" + "testing" + + "github.com/influxdata/telegraf/testutil" +) + +func TestIpset(t *testing.T) { + tests := []struct { + value string + tags []map[string]string + fields [][]map[string]interface{} + err error + }{ + { // 1 - 0 sets => no results + value: "", + }, + { // 2 - only empty sets => no results + value: `create myset hash:net family inet hashsize 1024 maxelem 65536 + create myset2 hash:net,port family inet hashsize 16384 maxelem 524288 counters comment + `, + }, + { // 3 - non-empty sets, but no counters => no results + value: `create myset hash:net family inet hashsize 1024 maxelem 65536 + add myset 1.2.3.4 + `, + }, + { // 4 - non-empty sets, counters, no comment + value: `create myset hash:net family inet hashsize 1024 maxelem 65536 counters + add myset 1.2.3.4 packets 1328 bytes 79680 + add myset 2.3.4.5 packets 0 bytes 0 + add myset 3.4.5.6 packets 3 bytes 222 + `, + tags: []map[string]string{ + map[string]string{"set": "myset", "rule": "1.2.3.4"}, + map[string]string{"set": "myset", "rule": "3.4.5.6"}, + }, + fields: [][]map[string]interface{}{ + {map[string]interface{}{"packets_total": uint64(1328), "bytes_total": uint64(79680)}}, + {map[string]interface{}{"packets_total": uint64(3), "bytes_total": uint64(222)}}, + }, + }, + { // 5 - sets with counters and comment + value: `create myset hash:net family inet hashsize 1024 maxelem 65536 counters comment + add myset 1.2.3.4 packets 1328 bytes 79680 comment "first IP" + add myset 2.3.4.5 packets 0 bytes 0 comment "2nd IP" + add myset 3.4.5.6 packets 3 bytes 222 "3rd IP" + `, + tags: []map[string]string{ + map[string]string{"set": "myset", "rule": "1.2.3.4"}, + map[string]string{"set": "myset", "rule": "3.4.5.6"}, + }, + fields: [][]map[string]interface{}{ + {map[string]interface{}{"packets_total": uint64(1328), "bytes_total": uint64(79680)}}, + {map[string]interface{}{"packets_total": uint64(3), "bytes_total": uint64(222)}}, + }, + }, + } + + for i, tt := range tests { + i++ + ips := &Ipset{ + lister: func() (string, error) { + return tt.value, nil + }, + } + acc := new(testutil.Accumulator) + err := acc.GatherError(ips.Gather) + if !reflect.DeepEqual(tt.err, err) { + t.Errorf("%d: expected error '%#v' got '%#v'", i, tt.err, err) + } + if len(tt.tags) == 0 { + n := acc.NFields() + if n != 0 { + t.Errorf("%d: expected 0 values got %d", i, n) + } + return + } + n := 0 + for j, tags := range tt.tags { + for k, fields := range tt.fields[j] { + if len(acc.Metrics) < n+1 { + t.Errorf("%d: expected at least %d values got %d", i, n+1, len(acc.Metrics)) + break + } + m := acc.Metrics[n] + if !reflect.DeepEqual(m.Measurement, measurement) { + t.Errorf("%d %d %d: expected measurement '%#v' got '%#v'\n", i, j, k, measurement, m.Measurement) + } + if !reflect.DeepEqual(m.Tags, tags) { + t.Errorf("%d %d %d: expected tags\n%#v got\n%#v\n", i, j, k, tags, m.Tags) + } + if !reflect.DeepEqual(m.Fields, fields) { + t.Errorf("%d %d %d: expected fields\n%#v got\n%#v\n", i, j, k, fields, m.Fields) + } + n++ + } + } + } +} + +func TestIpset_Gather_listerError(t *testing.T) { + errFoo := errors.New("error foobar") + ips := &Ipset{ + lister: func() (string, error) { + return "", errFoo + }, + } + acc := new(testutil.Accumulator) + err := acc.GatherError(ips.Gather) + if !reflect.DeepEqual(err, errFoo) { + t.Errorf("Expected error %#v got\n%#v\n", errFoo, err) + } +} From 6f49d088e21f624b825620aac767165266700a5e Mon Sep 17 00:00:00 2001 From: Laurent Sesques Date: Sat, 21 Oct 2017 18:12:33 +0200 Subject: [PATCH 3/5] ipset plugin: add _nocompile --- plugins/inputs/ipset/ipset_nocompile.go | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 plugins/inputs/ipset/ipset_nocompile.go diff --git a/plugins/inputs/ipset/ipset_nocompile.go b/plugins/inputs/ipset/ipset_nocompile.go new file mode 100644 index 0000000000000..1992413f368a1 --- /dev/null +++ b/plugins/inputs/ipset/ipset_nocompile.go @@ -0,0 +1,3 @@ +// +build !linux + +package ipset From 72430de9e580eaf208aeb3d8785ef635162e2e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Sesqu=C3=A8s?= Date: Tue, 23 Jan 2018 10:09:34 +0100 Subject: [PATCH 4/5] ipset input plugin: - Add a timeout for ipset execution - Use a bufio Scanner to parse ipset output - Add names to test cases - Remove build flags --- plugins/inputs/ipset/README.md | 7 +- plugins/inputs/ipset/ipset.go | 74 ++++++++++------- plugins/inputs/ipset/ipset_nocompile.go | 3 - plugins/inputs/ipset/ipset_test.go | 102 ++++++++++++++---------- 4 files changed, 111 insertions(+), 75 deletions(-) delete mode 100644 plugins/inputs/ipset/ipset_nocompile.go diff --git a/plugins/inputs/ipset/README.md b/plugins/inputs/ipset/README.md index b5e9d6d3e49e5..e3a2103a86a85 100644 --- a/plugins/inputs/ipset/README.md +++ b/plugins/inputs/ipset/README.md @@ -36,12 +36,15 @@ telegraf ALL=(root) NOPASSWD: /sbin/ipset save ```toml [[inputs.ipset]] ## By default, we only show sets which have already matched at least 1 packet. - ## set show_all_sets = true to gather them all. - show_all_sets = false + ## set include_unmatched_sets = true to gather them all. + include_unmatched_sets = false ## Adjust your sudo settings appropriately if using this option ("sudo ipset save") ## You can avoid using sudo or root, by setting appropriate privileges for ## the telegraf.service systemd service. use_sudo = false + ## The default timeout of 1s for ss execution can be overridden here: + # timeout = "1s" + ``` ### Example Output diff --git a/plugins/inputs/ipset/ipset.go b/plugins/inputs/ipset/ipset.go index f7ab065103f86..c45ba6fad6cf3 100644 --- a/plugins/inputs/ipset/ipset.go +++ b/plugins/inputs/ipset/ipset.go @@ -1,23 +1,33 @@ -// +build linux - package ipset import ( + "bufio" + "bytes" + "fmt" "os/exec" "strconv" "strings" + "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/plugins/inputs" ) // Ipsets is a telegraf plugin to gather packets and bytes counters from ipset type Ipset struct { - ShowAllSets bool - UseSudo bool - lister setLister + IncludeUnmatchedSets bool + UseSudo bool + Timeout internal.Duration + lister setLister } +type setLister func(Timeout internal.Duration, UseSudo bool) (*bytes.Buffer, error) + +const measurement = "ipset" + +var defaultTimeout = internal.Duration{Duration: time.Second} + // Description returns a short description of the plugin func (ipset *Ipset) Description() string { return "Gather packets and bytes counters from Linux ipsets" @@ -27,24 +37,24 @@ func (ipset *Ipset) Description() string { func (ipset *Ipset) SampleConfig() string { return ` ## By default, we only show sets which have already matched at least 1 packet. - ## set show_all_sets = true to gather them all. - show_all_sets = false + ## set include_unmatched_sets = true to gather them all. + include_unmatched_sets = false ## Adjust your sudo settings appropriately if using this option ("sudo ipset save") - ## TODO: can we replace this with systemd privileges ? CAP_NET_ADMIN should DTRT use_sudo = false + ## The default timeout of 1s for ss execution can be overridden here: + # timeout = "1s" ` } -const measurement = "ipset" - func (ips *Ipset) Gather(acc telegraf.Accumulator) error { - list, e := ips.lister() + out, e := ips.lister(ips.Timeout, ips.UseSudo) if e != nil { acc.AddError(e) } - lines := strings.Split(list, "\n") - for _, line := range lines { + scanner := bufio.NewScanner(out) + for scanner.Scan() { + line := scanner.Text() // Ignore sets created without the "counters" option nocomment := strings.Split(line, "\"")[0] if !(strings.Contains(nocomment, "packets") && @@ -52,17 +62,21 @@ func (ips *Ipset) Gather(acc telegraf.Accumulator) error { continue } - data := strings.Split(line, " ") - if data[0] == "add" && (data[4] != "0" || ips.ShowAllSets == true) { + data := strings.Fields(line) + if len(data) < 7 { + acc.AddError(fmt.Errorf("Error parsing line (expected at least 7 fields): %s", line)) + continue + } + if data[0] == "add" && (data[4] != "0" || ips.IncludeUnmatchedSets) { tags := map[string]string{ "set": data[1], "rule": data[2], } - packets_total, err := strconv.ParseInt(data[4], 10, 64) + packets_total, err := strconv.ParseUint(data[4], 10, 64) if err != nil { acc.AddError(err) } - bytes_total, err := strconv.ParseInt(data[6], 10, 64) + bytes_total, err := strconv.ParseUint(data[6], 10, 64) if err != nil { acc.AddError(err) } @@ -76,31 +90,37 @@ func (ips *Ipset) Gather(acc telegraf.Accumulator) error { return nil } -func (ips *Ipset) setList() (string, error) { +func setList(Timeout internal.Duration, UseSudo bool) (*bytes.Buffer, error) { // Is ipset installed ? ipsetPath, err := exec.LookPath("ipset") if err != nil { - return "", err + return nil, err } var args []string cmdName := ipsetPath - if ips.UseSudo { + if UseSudo { cmdName = "sudo" args = append(args, ipsetPath) } args = append(args, "save") cmd := exec.Command(cmdName, args...) - out, err := cmd.Output() - return string(out), err -} -type setLister func() (string, error) + var out bytes.Buffer + cmd.Stdout = &out + err = internal.RunTimeout(cmd, Timeout.Duration) + if err != nil { + return &out, fmt.Errorf("error running ipset save: %s", err) + } + + return &out, nil +} func init() { inputs.Add("ipset", func() telegraf.Input { - ips := new(Ipset) - ips.lister = ips.setList - return ips + return &Ipset{ + lister: setList, + Timeout: defaultTimeout, + } }) } diff --git a/plugins/inputs/ipset/ipset_nocompile.go b/plugins/inputs/ipset/ipset_nocompile.go deleted file mode 100644 index 1992413f368a1..0000000000000 --- a/plugins/inputs/ipset/ipset_nocompile.go +++ /dev/null @@ -1,3 +0,0 @@ -// +build !linux - -package ipset diff --git a/plugins/inputs/ipset/ipset_test.go b/plugins/inputs/ipset/ipset_test.go index 0ed2bb6d4bf7f..9438c806d9e19 100644 --- a/plugins/inputs/ipset/ipset_test.go +++ b/plugins/inputs/ipset/ipset_test.go @@ -1,36 +1,49 @@ -// +build linux - package ipset import ( + "bytes" "errors" + "fmt" "reflect" "testing" + "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/testutil" ) func TestIpset(t *testing.T) { tests := []struct { + name string value string tags []map[string]string fields [][]map[string]interface{} err error }{ - { // 1 - 0 sets => no results + { + name: "0 sets, no results", value: "", }, - { // 2 - only empty sets => no results + { + name: "Empty sets, no values", value: `create myset hash:net family inet hashsize 1024 maxelem 65536 create myset2 hash:net,port family inet hashsize 16384 maxelem 524288 counters comment `, }, - { // 3 - non-empty sets, but no counters => no results + { + name: "Non-empty sets, but no counters, no results", value: `create myset hash:net family inet hashsize 1024 maxelem 65536 add myset 1.2.3.4 `, }, - { // 4 - non-empty sets, counters, no comment + { + name: "Line with data but not enough fields", + value: `create hash:net family inet hashsize 1024 maxelem 65536 counters + add myset 4.5.6.7 packets 123 bytes + `, + err: fmt.Errorf("Error parsing line (expected at least 7 fields): \t\t\t\tadd myset 4.5.6.7 packets 123 bytes"), + }, + { + name: "Non-empty sets, counters, no comment", value: `create myset hash:net family inet hashsize 1024 maxelem 65536 counters add myset 1.2.3.4 packets 1328 bytes 79680 add myset 2.3.4.5 packets 0 bytes 0 @@ -45,7 +58,8 @@ func TestIpset(t *testing.T) { {map[string]interface{}{"packets_total": uint64(3), "bytes_total": uint64(222)}}, }, }, - { // 5 - sets with counters and comment + { + name: "Sets with counters and comment", value: `create myset hash:net family inet hashsize 1024 maxelem 65536 counters comment add myset 1.2.3.4 packets 1328 bytes 79680 comment "first IP" add myset 2.3.4.5 packets 0 bytes 0 comment "2nd IP" @@ -63,52 +77,54 @@ func TestIpset(t *testing.T) { } for i, tt := range tests { - i++ - ips := &Ipset{ - lister: func() (string, error) { - return tt.value, nil - }, - } - acc := new(testutil.Accumulator) - err := acc.GatherError(ips.Gather) - if !reflect.DeepEqual(tt.err, err) { - t.Errorf("%d: expected error '%#v' got '%#v'", i, tt.err, err) - } - if len(tt.tags) == 0 { - n := acc.NFields() - if n != 0 { - t.Errorf("%d: expected 0 values got %d", i, n) + t.Run(tt.name, func(t *testing.T) { + i++ + ips := &Ipset{ + lister: func(Timeout internal.Duration, UseSudo bool) (*bytes.Buffer, error) { + return bytes.NewBufferString(tt.value), nil + }, } - return - } - n := 0 - for j, tags := range tt.tags { - for k, fields := range tt.fields[j] { - if len(acc.Metrics) < n+1 { - t.Errorf("%d: expected at least %d values got %d", i, n+1, len(acc.Metrics)) - break - } - m := acc.Metrics[n] - if !reflect.DeepEqual(m.Measurement, measurement) { - t.Errorf("%d %d %d: expected measurement '%#v' got '%#v'\n", i, j, k, measurement, m.Measurement) - } - if !reflect.DeepEqual(m.Tags, tags) { - t.Errorf("%d %d %d: expected tags\n%#v got\n%#v\n", i, j, k, tags, m.Tags) + acc := new(testutil.Accumulator) + err := acc.GatherError(ips.Gather) + if !reflect.DeepEqual(tt.err, err) { + t.Errorf("%d: expected error '%#v' got '%#v'", i, tt.err, err) + } + if len(tt.tags) == 0 { + n := acc.NFields() + if n != 0 { + t.Errorf("%d: expected 0 values got %d", i, n) } - if !reflect.DeepEqual(m.Fields, fields) { - t.Errorf("%d %d %d: expected fields\n%#v got\n%#v\n", i, j, k, fields, m.Fields) + return + } + n := 0 + for j, tags := range tt.tags { + for k, fields := range tt.fields[j] { + if len(acc.Metrics) < n+1 { + t.Errorf("%d: expected at least %d values got %d", i, n+1, len(acc.Metrics)) + break + } + m := acc.Metrics[n] + if !reflect.DeepEqual(m.Measurement, measurement) { + t.Errorf("%d %d %d: expected measurement '%#v' got '%#v'\n", i, j, k, measurement, m.Measurement) + } + if !reflect.DeepEqual(m.Tags, tags) { + t.Errorf("%d %d %d: expected tags\n%#v got\n%#v\n", i, j, k, tags, m.Tags) + } + if !reflect.DeepEqual(m.Fields, fields) { + t.Errorf("%d %d %d: expected fields\n%#v got\n%#v\n", i, j, k, fields, m.Fields) + } + n++ } - n++ } - } + }) } } func TestIpset_Gather_listerError(t *testing.T) { errFoo := errors.New("error foobar") ips := &Ipset{ - lister: func() (string, error) { - return "", errFoo + lister: func(Timeout internal.Duration, UseSudo bool) (*bytes.Buffer, error) { + return new(bytes.Buffer), errFoo }, } acc := new(testutil.Accumulator) From 89db42fee444c1a2e190a7b4899bf04653e1e0dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Sesqu=C3=A8s?= Date: Tue, 30 Jan 2018 09:44:10 +0100 Subject: [PATCH 5/5] ipset input plugin: fix lapsus, this is ipset not ss --- plugins/inputs/ipset/README.md | 2 +- plugins/inputs/ipset/ipset.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/ipset/README.md b/plugins/inputs/ipset/README.md index e3a2103a86a85..2209de91159f6 100644 --- a/plugins/inputs/ipset/README.md +++ b/plugins/inputs/ipset/README.md @@ -42,7 +42,7 @@ telegraf ALL=(root) NOPASSWD: /sbin/ipset save ## You can avoid using sudo or root, by setting appropriate privileges for ## the telegraf.service systemd service. use_sudo = false - ## The default timeout of 1s for ss execution can be overridden here: + ## The default timeout of 1s for ipset execution can be overridden here: # timeout = "1s" ``` diff --git a/plugins/inputs/ipset/ipset.go b/plugins/inputs/ipset/ipset.go index c45ba6fad6cf3..c459ebf4cfe26 100644 --- a/plugins/inputs/ipset/ipset.go +++ b/plugins/inputs/ipset/ipset.go @@ -41,7 +41,7 @@ func (ipset *Ipset) SampleConfig() string { include_unmatched_sets = false ## Adjust your sudo settings appropriately if using this option ("sudo ipset save") use_sudo = false - ## The default timeout of 1s for ss execution can be overridden here: + ## The default timeout of 1s for ipset execution can be overridden here: # timeout = "1s" ` }