2015-09-02 23:16:52 +00:00
|
|
|
package ping
|
|
|
|
|
|
|
|
import (
|
2019-07-11 22:07:58 +00:00
|
|
|
"context"
|
2015-09-02 23:16:52 +00:00
|
|
|
"errors"
|
2019-10-22 23:46:57 +00:00
|
|
|
"log"
|
2019-07-11 22:07:58 +00:00
|
|
|
"math"
|
2017-10-26 20:35:37 +00:00
|
|
|
"net"
|
2015-09-02 23:16:52 +00:00
|
|
|
"os/exec"
|
2016-02-03 01:43:03 +00:00
|
|
|
"runtime"
|
2019-10-22 23:46:57 +00:00
|
|
|
"strings"
|
2015-09-02 23:16:52 +00:00
|
|
|
"sync"
|
2016-04-29 01:23:45 +00:00
|
|
|
"time"
|
2015-09-02 23:16:52 +00:00
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
"github.com/glinton/ping"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-04-29 01:23:45 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-09-02 23:16:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HostPinger is a function that runs the "ping" function using a list of
|
|
|
|
// passed arguments. This can be easily switched with a mocked ping function
|
|
|
|
// for unit test purposes (see ping_test.go)
|
2018-10-02 00:38:13 +00:00
|
|
|
type HostPinger func(binary string, timeout float64, args ...string) (string, error)
|
2015-09-02 23:16:52 +00:00
|
|
|
|
|
|
|
type Ping struct {
|
2018-10-02 00:38:13 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2015-09-02 23:16:52 +00:00
|
|
|
// Interval at which to ping (ping -i <INTERVAL>)
|
|
|
|
PingInterval float64 `toml:"ping_interval"`
|
|
|
|
|
|
|
|
// Number of pings to send (ping -c <COUNT>)
|
|
|
|
Count int
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
// Per-ping timeout, in seconds. 0 means no timeout (ping -W <TIMEOUT>)
|
2015-09-02 23:16:52 +00:00
|
|
|
Timeout float64
|
|
|
|
|
2018-02-16 04:11:07 +00:00
|
|
|
// Ping deadline, in seconds. 0 means no deadline. (ping -w <DEADLINE>)
|
|
|
|
Deadline int
|
|
|
|
|
2018-01-29 22:01:00 +00:00
|
|
|
// Interface or source address to send ping from (ping -I/-S <INTERFACE/SRC_ADDR>)
|
2015-09-02 23:16:52 +00:00
|
|
|
Interface string
|
|
|
|
|
|
|
|
// URLs to ping
|
|
|
|
Urls []string
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
// Method defines how to ping (native or exec)
|
|
|
|
Method string
|
|
|
|
|
2018-10-02 00:38:13 +00:00
|
|
|
// Ping executable binary
|
|
|
|
Binary string
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
// Arguments for ping command. When arguments is not empty, system binary will be used and
|
|
|
|
// other options (ping_interval, timeout, etc) will be ignored
|
2018-10-02 00:38:13 +00:00
|
|
|
Arguments []string
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
// Whether to resolve addresses using ipv6 or not.
|
|
|
|
IPv6 bool
|
|
|
|
|
2015-09-02 23:16:52 +00:00
|
|
|
// host ping function
|
|
|
|
pingHost HostPinger
|
2019-07-11 22:07:58 +00:00
|
|
|
|
|
|
|
// listenAddr is the address associated with the interface defined.
|
|
|
|
listenAddr string
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
func (*Ping) Description() string {
|
2015-09-02 23:16:52 +00:00
|
|
|
return "Ping given url(s) and return statistics"
|
|
|
|
}
|
|
|
|
|
2016-04-27 21:08:38 +00:00
|
|
|
const sampleConfig = `
|
2019-09-20 23:49:14 +00:00
|
|
|
## Hosts to send ping packets to.
|
2018-08-31 20:59:30 +00:00
|
|
|
urls = ["example.org"]
|
|
|
|
|
2019-09-20 23:49:14 +00:00
|
|
|
## Method used for sending pings, can be either "exec" or "native". When set
|
|
|
|
## to "exec" the systems ping command will be executed. When set to "native"
|
|
|
|
## the plugin will send pings directly.
|
|
|
|
##
|
|
|
|
## While the default is "exec" for backwards compatibility, new deployments
|
|
|
|
## are encouraged to use the "native" method for improved compatibility and
|
|
|
|
## performance.
|
|
|
|
# method = "exec"
|
|
|
|
|
|
|
|
## Number of ping packets to send per interval. Corresponds to the "-c"
|
|
|
|
## option of the ping command.
|
2016-10-12 11:12:07 +00:00
|
|
|
# count = 1
|
2018-08-31 20:59:30 +00:00
|
|
|
|
2019-09-20 23:49:14 +00:00
|
|
|
## Time to wait between sending ping packets in seconds. Operates like the
|
|
|
|
## "-i" option of the ping command.
|
2016-10-12 11:12:07 +00:00
|
|
|
# ping_interval = 1.0
|
2018-08-31 20:59:30 +00:00
|
|
|
|
2019-09-20 23:49:14 +00:00
|
|
|
## If set, the time to wait for a ping response in seconds. Operates like
|
|
|
|
## the "-W" option of the ping command.
|
2016-10-12 11:12:07 +00:00
|
|
|
# timeout = 1.0
|
2018-08-31 20:59:30 +00:00
|
|
|
|
2019-09-20 23:49:14 +00:00
|
|
|
## If set, the total ping deadline, in seconds. Operates like the -w option
|
|
|
|
## of the ping command.
|
2018-02-16 04:11:07 +00:00
|
|
|
# deadline = 10
|
2018-08-31 20:59:30 +00:00
|
|
|
|
2019-09-20 23:49:14 +00:00
|
|
|
## Interface or source address to send ping from. Operates like the -I or -S
|
|
|
|
## option of the ping command.
|
2016-10-12 11:12:07 +00:00
|
|
|
# interface = ""
|
2018-10-02 00:38:13 +00:00
|
|
|
|
2019-09-20 23:49:14 +00:00
|
|
|
## Specify the ping executable binary.
|
|
|
|
# binary = "ping"
|
2018-10-02 00:38:13 +00:00
|
|
|
|
2019-09-20 23:49:14 +00:00
|
|
|
## Arguments for ping command. When arguments is not empty, the command from
|
|
|
|
## the binary option will be used and other options (ping_interval, timeout,
|
|
|
|
## etc) will be ignored.
|
2018-10-02 00:38:13 +00:00
|
|
|
# arguments = ["-c", "3"]
|
2019-07-11 22:07:58 +00:00
|
|
|
|
2019-09-20 23:49:14 +00:00
|
|
|
## Use only IPv6 addresses when resolving a hostname.
|
2019-07-11 22:07:58 +00:00
|
|
|
# ipv6 = false
|
2015-09-02 23:16:52 +00:00
|
|
|
`
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
func (*Ping) SampleConfig() string {
|
2015-09-02 23:16:52 +00:00
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (p *Ping) Gather(acc telegraf.Accumulator) error {
|
2019-11-18 18:27:31 +00:00
|
|
|
if p.Interface != "" && p.listenAddr == "" {
|
2019-07-11 22:07:58 +00:00
|
|
|
p.listenAddr = getAddr(p.Interface)
|
|
|
|
}
|
|
|
|
|
2019-12-03 19:27:33 +00:00
|
|
|
for _, host := range p.Urls {
|
|
|
|
_, err := net.LookupHost(host)
|
2019-07-11 22:07:58 +00:00
|
|
|
if err != nil {
|
2019-12-03 19:27:33 +00:00
|
|
|
acc.AddFields("ping", map[string]interface{}{"result_code": 1}, map[string]string{"url": host})
|
2019-07-11 22:07:58 +00:00
|
|
|
acc.AddError(err)
|
2019-12-03 19:27:33 +00:00
|
|
|
continue
|
2019-07-11 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 19:27:33 +00:00
|
|
|
p.wg.Add(1)
|
|
|
|
go func(host string) {
|
|
|
|
defer p.wg.Done()
|
|
|
|
|
|
|
|
switch p.Method {
|
|
|
|
case "native":
|
|
|
|
p.pingToURLNative(host, acc)
|
|
|
|
default:
|
|
|
|
p.pingToURL(host, acc)
|
|
|
|
}
|
|
|
|
}(host)
|
2018-10-02 00:38:13 +00:00
|
|
|
}
|
2017-10-26 20:35:37 +00:00
|
|
|
|
2018-10-02 00:38:13 +00:00
|
|
|
p.wg.Wait()
|
2017-10-26 20:35:37 +00:00
|
|
|
|
2018-10-02 00:38:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-16 18:59:41 +00:00
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
func getAddr(iface string) string {
|
|
|
|
if addr := net.ParseIP(iface); addr != nil {
|
|
|
|
return addr.String()
|
2018-10-02 00:38:13 +00:00
|
|
|
}
|
2017-08-16 18:59:41 +00:00
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
2018-10-02 00:38:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
var ip net.IP
|
|
|
|
for i := range ifaces {
|
|
|
|
if ifaces[i].Name == iface {
|
|
|
|
addrs, err := ifaces[i].Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
2017-06-13 21:09:17 +00:00
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
if len(addrs) > 0 {
|
|
|
|
switch v := addrs[0].(type) {
|
|
|
|
case *net.IPNet:
|
|
|
|
ip = v.IP
|
|
|
|
case *net.IPAddr:
|
|
|
|
ip = v.IP
|
|
|
|
}
|
|
|
|
if len(ip) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ip.String()
|
2016-12-16 13:58:27 +00:00
|
|
|
}
|
2018-10-02 00:38:13 +00:00
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
return ""
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 00:38:13 +00:00
|
|
|
func hostPinger(binary string, timeout float64, args ...string) (string, error) {
|
|
|
|
bin, err := exec.LookPath(binary)
|
2016-02-01 23:36:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
c := exec.Command(bin, args...)
|
2016-04-29 01:23:45 +00:00
|
|
|
out, err := internal.CombinedOutputTimeout(c,
|
2017-10-09 22:09:07 +00:00
|
|
|
time.Second*time.Duration(timeout+5))
|
2015-09-02 23:16:52 +00:00
|
|
|
return string(out), err
|
|
|
|
}
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
network := "ip4"
|
|
|
|
if p.IPv6 {
|
|
|
|
network = "ip6"
|
2018-10-02 00:38:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
host, err := net.ResolveIPAddr(network, destination)
|
|
|
|
if err != nil {
|
2019-10-22 23:46:57 +00:00
|
|
|
acc.AddFields(
|
|
|
|
"ping",
|
|
|
|
map[string]interface{}{"result_code": 1},
|
|
|
|
map[string]string{"url": destination},
|
|
|
|
)
|
2019-07-11 22:07:58 +00:00
|
|
|
acc.AddError(err)
|
|
|
|
return
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
|
|
|
|
interval := p.PingInterval
|
|
|
|
if interval < 0.2 {
|
|
|
|
interval = 0.2
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
|
|
|
|
timeout := p.Timeout
|
|
|
|
if timeout == 0 {
|
|
|
|
timeout = 5
|
|
|
|
}
|
|
|
|
|
|
|
|
tick := time.NewTicker(time.Duration(interval * float64(time.Second)))
|
|
|
|
defer tick.Stop()
|
|
|
|
|
2018-02-16 04:11:07 +00:00
|
|
|
if p.Deadline > 0 {
|
2019-07-11 22:07:58 +00:00
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(p.Deadline)*time.Second)
|
|
|
|
defer cancel()
|
2018-02-16 04:11:07 +00:00
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
|
|
|
|
resps := make(chan *ping.Response)
|
|
|
|
rsps := []*ping.Response{}
|
|
|
|
|
|
|
|
r := &sync.WaitGroup{}
|
|
|
|
r.Add(1)
|
|
|
|
go func() {
|
|
|
|
for res := range resps {
|
|
|
|
rsps = append(rsps, res)
|
|
|
|
}
|
|
|
|
r.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
c := ping.Client{}
|
|
|
|
|
2019-10-22 23:46:57 +00:00
|
|
|
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++ {
|
2019-07-11 22:07:58 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
goto finish
|
|
|
|
case <-tick.C:
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeout*float64(time.Second)))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(seq int) {
|
|
|
|
defer wg.Done()
|
|
|
|
resp, err := c.Do(ctx, &ping.Request{
|
|
|
|
Dst: net.ParseIP(host.String()),
|
|
|
|
Src: net.ParseIP(p.listenAddr),
|
|
|
|
Seq: seq,
|
|
|
|
})
|
2019-10-22 23:46:57 +00:00
|
|
|
|
|
|
|
sent := sentReq{err: err, sent: true}
|
2019-07-11 22:07:58 +00:00
|
|
|
if err != nil {
|
2019-10-22 23:46:57 +00:00
|
|
|
if strings.Contains(err.Error(), "not permitted") {
|
|
|
|
sent.sent = false
|
|
|
|
}
|
2019-11-05 18:34:18 +00:00
|
|
|
sents <- sent
|
2019-07-11 22:07:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resps <- resp
|
2019-11-05 18:34:18 +00:00
|
|
|
sents <- sent
|
2019-07-11 22:07:58 +00:00
|
|
|
}(i + 1)
|
2018-01-29 22:01:00 +00:00
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
|
|
|
|
finish:
|
|
|
|
wg.Wait()
|
|
|
|
close(resps)
|
2019-10-22 23:46:57 +00:00
|
|
|
close(sents)
|
2019-07-11 22:07:58 +00:00
|
|
|
|
|
|
|
r.Wait()
|
2019-10-22 23:46:57 +00:00
|
|
|
|
|
|
|
if doErr != nil && strings.Contains(doErr.Error(), "not permitted") {
|
|
|
|
log.Printf("D! [inputs.ping] %s", doErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, fields := onFin(packetsSent, rsps, doErr, destination)
|
2019-07-11 22:07:58 +00:00
|
|
|
acc.AddFields("ping", fields, tags)
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 23:46:57 +00:00
|
|
|
func onFin(packetsSent int, resps []*ping.Response, err error, destination string) (map[string]string, map[string]interface{}) {
|
2019-07-11 22:07:58 +00:00
|
|
|
packetsRcvd := len(resps)
|
|
|
|
|
|
|
|
tags := map[string]string{"url": destination}
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"result_code": 0,
|
|
|
|
"packets_transmitted": packetsSent,
|
|
|
|
"packets_received": packetsRcvd,
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
2019-03-08 22:30:38 +00:00
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
if packetsSent == 0 {
|
2019-10-22 23:46:57 +00:00
|
|
|
if err != nil {
|
|
|
|
fields["result_code"] = 2
|
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
return tags, fields
|
2019-03-08 22:30:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
if packetsRcvd == 0 {
|
2019-10-22 23:46:57 +00:00
|
|
|
if err != nil {
|
|
|
|
fields["result_code"] = 1
|
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
fields["percent_packet_loss"] = float64(100)
|
|
|
|
return tags, fields
|
|
|
|
}
|
2019-03-08 22:30:38 +00:00
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
fields["percent_packet_loss"] = float64(packetsSent-packetsRcvd) / float64(packetsSent) * 100
|
|
|
|
ttl := resps[0].TTL
|
2019-03-08 22:30:38 +00:00
|
|
|
|
2019-07-11 22:07:58 +00:00
|
|
|
var min, max, avg, total time.Duration
|
|
|
|
min = resps[0].RTT
|
|
|
|
max = resps[0].RTT
|
|
|
|
|
|
|
|
for _, res := range resps {
|
|
|
|
if res.RTT < min {
|
|
|
|
min = res.RTT
|
|
|
|
}
|
|
|
|
if res.RTT > max {
|
|
|
|
max = res.RTT
|
|
|
|
}
|
|
|
|
total += res.RTT
|
2019-03-08 22:30:38 +00:00
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
|
|
|
|
avg = total / time.Duration(packetsRcvd)
|
|
|
|
var sumsquares time.Duration
|
|
|
|
for _, res := range resps {
|
|
|
|
sumsquares += (res.RTT - avg) * (res.RTT - avg)
|
2019-03-08 22:30:38 +00:00
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
stdDev := time.Duration(math.Sqrt(float64(sumsquares / time.Duration(packetsRcvd))))
|
|
|
|
|
|
|
|
// Set TTL only on supported platform. See golang.org/x/net/ipv4/payload_cmsg.go
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "aix", "darwin", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris":
|
|
|
|
fields["ttl"] = ttl
|
2019-03-08 22:30:38 +00:00
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
|
|
|
|
fields["minimum_response_ms"] = float64(min.Nanoseconds()) / float64(time.Millisecond)
|
|
|
|
fields["average_response_ms"] = float64(avg.Nanoseconds()) / float64(time.Millisecond)
|
|
|
|
fields["maximum_response_ms"] = float64(max.Nanoseconds()) / float64(time.Millisecond)
|
|
|
|
fields["standard_deviation_ms"] = float64(stdDev.Nanoseconds()) / float64(time.Millisecond)
|
|
|
|
|
|
|
|
return tags, fields
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init ensures the plugin is configured correctly.
|
|
|
|
func (p *Ping) Init() error {
|
|
|
|
if p.Count < 1 {
|
|
|
|
return errors.New("bad number of packets to transmit")
|
2019-03-08 22:30:38 +00:00
|
|
|
}
|
2019-07-11 22:07:58 +00:00
|
|
|
|
|
|
|
return nil
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("ping", func() telegraf.Input {
|
2016-10-12 11:12:07 +00:00
|
|
|
return &Ping{
|
|
|
|
pingHost: hostPinger,
|
|
|
|
PingInterval: 1.0,
|
|
|
|
Count: 1,
|
|
|
|
Timeout: 1.0,
|
2018-02-16 04:11:07 +00:00
|
|
|
Deadline: 10,
|
2019-07-11 22:07:58 +00:00
|
|
|
Method: "exec",
|
2018-10-02 00:38:13 +00:00
|
|
|
Binary: "ping",
|
|
|
|
Arguments: []string{},
|
2016-10-12 11:12:07 +00:00
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
})
|
|
|
|
}
|