Ensure metrics generated are correct in ping plugin using "native" (#6563)
This commit is contained in:
parent
b46bb222c4
commit
c1521b5f68
|
@ -438,12 +438,12 @@
|
||||||
revision = "25d852aebe32c875e9c044af3eef9c7dc6bc777f"
|
revision = "25d852aebe32c875e9c044af3eef9c7dc6bc777f"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
digest = "1:c6f371f2b02c751a83be83139a12a5467e55393feda16d4f8dfa95adfc4efede"
|
digest = "1:7a9dc29b3fbc9a6440d98fcff422a2ce1a613975697ea560e3610084234f91ec"
|
||||||
name = "github.com/glinton/ping"
|
name = "github.com/glinton/ping"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
pruneopts = ""
|
pruneopts = ""
|
||||||
revision = "1983bc2fd5de3ea00aa5457bbc8774300e889db9"
|
revision = "d3c0ecf4df108179eccdff2176f4ff569c3aab37"
|
||||||
version = "v0.1.1"
|
version = "v0.1.3"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
digest = "1:df89444601379b2e1ee82bf8e6b72af9901cbeed4b469fa380a519c89c339310"
|
digest = "1:df89444601379b2e1ee82bf8e6b72af9901cbeed4b469fa380a519c89c339310"
|
||||||
|
|
|
@ -3,10 +3,12 @@ package ping
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -204,7 +206,11 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {
|
||||||
|
|
||||||
host, err := net.ResolveIPAddr(network, destination)
|
host, err := net.ResolveIPAddr(network, destination)
|
||||||
if err != nil {
|
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)
|
acc.AddError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -243,8 +249,29 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
c := ping.Client{}
|
c := ping.Client{}
|
||||||
|
|
||||||
var i int
|
var doErr error
|
||||||
for i = 0; i < p.Count; i++ {
|
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 {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
goto finish
|
goto finish
|
||||||
|
@ -260,9 +287,12 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {
|
||||||
Src: net.ParseIP(p.listenAddr),
|
Src: net.ParseIP(p.listenAddr),
|
||||||
Seq: seq,
|
Seq: seq,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
sent := sentReq{err: err, sent: true}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
acc.AddFields("ping", map[string]interface{}{"result_code": 2}, map[string]string{"url": destination})
|
if strings.Contains(err.Error(), "not permitted") {
|
||||||
acc.AddError(err)
|
sent.sent = false
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,13 +304,19 @@ func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {
|
||||||
finish:
|
finish:
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(resps)
|
close(resps)
|
||||||
|
close(sents)
|
||||||
|
|
||||||
r.Wait()
|
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)
|
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)
|
packetsRcvd := len(resps)
|
||||||
|
|
||||||
tags := map[string]string{"url": destination}
|
tags := map[string]string{"url": destination}
|
||||||
|
@ -291,10 +327,16 @@ func onFin(packetsSent int, resps []*ping.Response, destination string) (map[str
|
||||||
}
|
}
|
||||||
|
|
||||||
if packetsSent == 0 {
|
if packetsSent == 0 {
|
||||||
|
if err != nil {
|
||||||
|
fields["result_code"] = 2
|
||||||
|
}
|
||||||
return tags, fields
|
return tags, fields
|
||||||
}
|
}
|
||||||
|
|
||||||
if packetsRcvd == 0 {
|
if packetsRcvd == 0 {
|
||||||
|
if err != nil {
|
||||||
|
fields["result_code"] = 1
|
||||||
|
}
|
||||||
fields["percent_packet_loss"] = float64(100)
|
fields["percent_packet_loss"] = float64(100)
|
||||||
return tags, fields
|
return tags, fields
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue