From be03a7e8e3878ca54051b5158422dd51eee7686e Mon Sep 17 00:00:00 2001 From: greg linton Date: Mon, 14 Oct 2019 15:50:38 -0600 Subject: [PATCH 1/5] Update vendored ping --- Gopkg.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 22520af3aa52d..e1eaf17396857 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -438,12 +438,12 @@ revision = "25d852aebe32c875e9c044af3eef9c7dc6bc777f" [[projects]] - digest = "1:c6f371f2b02c751a83be83139a12a5467e55393feda16d4f8dfa95adfc4efede" + digest = "1:1c43ffc41749039318c291a397bde76f8eb0d92836e685f57fab3bcb8b977a4e" name = "github.com/glinton/ping" packages = ["."] pruneopts = "" - revision = "1983bc2fd5de3ea00aa5457bbc8774300e889db9" - version = "v0.1.1" + revision = "f160bc1281bab2b841d5851990494fef6e63d92f" + version = "v0.1.2" [[projects]] digest = "1:df89444601379b2e1ee82bf8e6b72af9901cbeed4b469fa380a519c89c339310" From fcd658a5c6e3e582ba2799707bc1181e265fdd00 Mon Sep 17 00:00:00 2001 From: greg linton Date: Mon, 21 Oct 2019 11:15:14 -0600 Subject: [PATCH 2/5] Don't report on individual failed pings --- plugins/inputs/ping/ping.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index ac0e9ebdfdfe2..15c54dc0c719b 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -261,8 +261,6 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { Seq: seq, }) if err != nil { - acc.AddFields("ping", map[string]interface{}{"result_code": 2}, map[string]string{"url": destination}) - acc.AddError(err) return } From ba016fac362bf3468779b8e8b79299fe6e7581f0 Mon Sep 17 00:00:00 2001 From: greg linton Date: Tue, 22 Oct 2019 12:36:37 -0600 Subject: [PATCH 3/5] Match native's 'result_code' to exec's --- plugins/inputs/ping/ping.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index 15c54dc0c719b..d9081886a4585 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -3,10 +3,12 @@ package ping import ( "context" "errors" + "log" "math" "net" "os/exec" "runtime" + "strings" "sync" "time" @@ -204,7 +206,11 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { host, err := net.ResolveIPAddr(network, destination) if err != nil { - acc.AddFields("ping", map[string]interface{}{"result_code": 1}, map[string]string{"url": destination}) + acc.AddFields( + "ping", + map[string]interface{}{"result_code": 1}, + map[string]string{"url": destination}, + ) acc.AddError(err) return } @@ -243,8 +249,10 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { wg := &sync.WaitGroup{} c := ping.Client{} - var i int - for i = 0; i < p.Count; i++ { + var doErr error + var packetsSent int + + for i := 0; i < p.Count; i++ { select { case <-ctx.Done(): goto finish @@ -261,9 +269,14 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { Seq: seq, }) if err != nil { + if !strings.Contains(err.Error(), "not permitted") { + packetsSent++ + } + doErr = err return } + packetsSent++ resps <- resp }(i + 1) } @@ -274,11 +287,16 @@ finish: close(resps) r.Wait() - tags, fields := onFin(i, rsps, destination) + + if doErr != nil && strings.Contains(doErr.Error(), "not permitted") { + log.Printf("D! [inputs.ping] %s", doErr.Error()) + } + + tags, fields := onFin(packetsSent, rsps, doErr, destination) acc.AddFields("ping", fields, tags) } -func onFin(packetsSent int, resps []*ping.Response, destination string) (map[string]string, map[string]interface{}) { +func onFin(packetsSent int, resps []*ping.Response, err error, destination string) (map[string]string, map[string]interface{}) { packetsRcvd := len(resps) tags := map[string]string{"url": destination} @@ -289,10 +307,16 @@ func onFin(packetsSent int, resps []*ping.Response, destination string) (map[str } if packetsSent == 0 { + if err != nil { + fields["result_code"] = 2 + } return tags, fields } if packetsRcvd == 0 { + if err != nil { + fields["result_code"] = 1 + } fields["percent_packet_loss"] = float64(100) return tags, fields } From d4fa448458d72b1cd769af7de246ed6aea9c93a3 Mon Sep 17 00:00:00 2001 From: greg linton Date: Tue, 22 Oct 2019 13:00:21 -0600 Subject: [PATCH 4/5] Update ping --- Gopkg.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index e1eaf17396857..ab53d39274544 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -438,12 +438,12 @@ revision = "25d852aebe32c875e9c044af3eef9c7dc6bc777f" [[projects]] - digest = "1:1c43ffc41749039318c291a397bde76f8eb0d92836e685f57fab3bcb8b977a4e" + digest = "1:7a9dc29b3fbc9a6440d98fcff422a2ce1a613975697ea560e3610084234f91ec" name = "github.com/glinton/ping" packages = ["."] pruneopts = "" - revision = "f160bc1281bab2b841d5851990494fef6e63d92f" - version = "v0.1.2" + revision = "d3c0ecf4df108179eccdff2176f4ff569c3aab37" + version = "v0.1.3" [[projects]] digest = "1:df89444601379b2e1ee82bf8e6b72af9901cbeed4b469fa380a519c89c339310" From eeeb0d6e092099169ba6c7c1444248fad992c793 Mon Sep 17 00:00:00 2001 From: greg linton Date: Tue, 22 Oct 2019 14:26:30 -0600 Subject: [PATCH 5/5] Resolve data race --- plugins/inputs/ping/ping.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index d9081886a4585..581d429f7b5c3 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -252,6 +252,25 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { var doErr error var packetsSent int + type sentReq struct { + err error + sent bool + } + sents := make(chan sentReq) + + r.Add(1) + go func() { + for sent := range sents { + if sent.err != nil { + doErr = sent.err + } + if sent.sent { + packetsSent++ + } + } + r.Done() + }() + for i := 0; i < p.Count; i++ { select { case <-ctx.Done(): @@ -268,15 +287,15 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { Src: net.ParseIP(p.listenAddr), Seq: seq, }) + + sent := sentReq{err: err, sent: true} if err != nil { - if !strings.Contains(err.Error(), "not permitted") { - packetsSent++ + if strings.Contains(err.Error(), "not permitted") { + sent.sent = false } - doErr = err return } - packetsSent++ resps <- resp }(i + 1) } @@ -285,6 +304,7 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) { finish: wg.Wait() close(resps) + close(sents) r.Wait()