From abc0c9c182d8241a90dc4351e3965cae8d707fe0 Mon Sep 17 00:00:00 2001 From: Chris Ottinger Date: Tue, 13 Mar 2018 19:00:34 +1100 Subject: [PATCH] added config flag to skip collection of network protocol metrics --- plugins/inputs/system/NET_README.md | 5 +++++ plugins/inputs/system/net.go | 35 ++++++++++++++++++----------- plugins/inputs/system/net_test.go | 6 +++++ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/plugins/inputs/system/NET_README.md b/plugins/inputs/system/NET_README.md index 771cabb480352..f265e2448657b 100644 --- a/plugins/inputs/system/NET_README.md +++ b/plugins/inputs/system/NET_README.md @@ -14,6 +14,11 @@ This plugin gathers metrics about network interface and protocol usage (Linux on ## # interfaces = ["eth*", "enp0s[0-1]", "lo"] ## + ## On linux systems telegraf also collects protocol stats. + ## Setting ignore_protocol_stats to true will skip reporting of protocol metrics. + ## + # ignore_protocol_stats = false + ## ``` ### Measurements & Fields: diff --git a/plugins/inputs/system/net.go b/plugins/inputs/system/net.go index cfb712dfb47b3..a7ba5c63d35f9 100644 --- a/plugins/inputs/system/net.go +++ b/plugins/inputs/system/net.go @@ -14,8 +14,9 @@ type NetIOStats struct { filter filter.Filter ps PS - skipChecks bool - Interfaces []string + skipChecks bool + IgnoreProtocolStats bool + Interfaces []string } func (_ *NetIOStats) Description() string { @@ -28,6 +29,12 @@ var netSampleConfig = ` ## regardless of status. ## # interfaces = ["eth0"] + ## + ## On linux systems telegraf also collects protocol stats. + ## Setting ignore_protocol_stats to true will skip reporting of protocol metrics. + ## + # ignore_protocol_stats = false + ## ` func (_ *NetIOStats) SampleConfig() string { @@ -91,19 +98,21 @@ func (s *NetIOStats) Gather(acc telegraf.Accumulator) error { // Get system wide stats for different network protocols // (ignore these stats if the call fails) - netprotos, _ := s.ps.NetProto() - fields := make(map[string]interface{}) - for _, proto := range netprotos { - for stat, value := range proto.Stats { - name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol), - strings.ToLower(stat)) - fields[name] = value + if !s.IgnoreProtocolStats { + netprotos, _ := s.ps.NetProto() + fields := make(map[string]interface{}) + for _, proto := range netprotos { + for stat, value := range proto.Stats { + name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol), + strings.ToLower(stat)) + fields[name] = value + } } + tags := map[string]string{ + "interface": "all", + } + acc.AddFields("net", fields, tags) } - tags := map[string]string{ - "interface": "all", - } - acc.AddFields("net", fields, tags) return nil } diff --git a/plugins/inputs/system/net_test.go b/plugins/inputs/system/net_test.go index 5eb30fe4f30f9..83b9bd4601d8b 100644 --- a/plugins/inputs/system/net_test.go +++ b/plugins/inputs/system/net_test.go @@ -106,4 +106,10 @@ func TestNetStats(t *testing.T) { "udp_socket": 1, } acc.AssertContainsTaggedFields(t, "netstat", fields3, make(map[string]string)) + + acc.Metrics = nil + err = (&NetIOStats{ps: &mps, IgnoreProtocolStats: true}).Gather(&acc) + require.NoError(t, err) + + acc.AssertDoesNotContainsTaggedFields(t, "netstat", fields3, make(map[string]string)) }