Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure metrics generated are correct in ping plugin using "native" #6563

Merged
merged 5 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

36 changes: 29 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 @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -261,11 +269,14 @@ 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)
if !strings.Contains(err.Error(), "not permitted") {
packetsSent++
}
doErr = err
glinton marked this conversation as resolved.
Show resolved Hide resolved
return
}

packetsSent++
resps <- resp
}(i + 1)
}
Expand All @@ -276,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}
Expand All @@ -291,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
}
Expand Down