Skip to content

Commit

Permalink
Ensure metrics generated are correct in ping plugin using "native" (#…
Browse files Browse the repository at this point in the history
…6563)

(cherry picked from commit c1521b5)
  • Loading branch information
glinton authored and danielnelson committed Oct 22, 2019
1 parent e3509fd commit b3899a2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 49 additions & 7 deletions plugins/inputs/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package ping
import (
"context"
"errors"
"log"
"math"
"net"
"os/exec"
"runtime"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -193,7 +195,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
}
Expand Down Expand Up @@ -232,8 +238,29 @@ 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

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():
goto finish
Expand All @@ -249,9 +276,12 @@ 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 {
acc.AddFields("ping", map[string]interface{}{"result_code": 2}, map[string]string{"url": destination})
acc.AddError(err)
if strings.Contains(err.Error(), "not permitted") {
sent.sent = false
}
return
}

Expand All @@ -263,13 +293,19 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {
finish:
wg.Wait()
close(resps)
close(sents)

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}
Expand All @@ -280,10 +316,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
}
Expand Down

0 comments on commit b3899a2

Please sign in to comment.