2016-02-01 23:36:08 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2015-09-02 23:16:52 +00:00
|
|
|
package ping
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-08-16 18:59:41 +00:00
|
|
|
"fmt"
|
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"
|
2015-09-02 23:16:52 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-08-16 18:59:41 +00:00
|
|
|
"syscall"
|
2016-04-29 01:23:45 +00:00
|
|
|
"time"
|
2015-09-02 23:16:52 +00:00
|
|
|
|
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
|
|
|
|
|
2016-06-10 11:51:43 +00:00
|
|
|
// 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
|
|
|
|
|
2018-10-02 00:38:13 +00:00
|
|
|
// Ping executable binary
|
|
|
|
Binary string
|
|
|
|
|
|
|
|
// Arguments for ping command.
|
|
|
|
// when `Arguments` is not empty, other options (ping_interval, timeout, etc) will be ignored
|
|
|
|
Arguments []string
|
|
|
|
|
2015-09-02 23:16:52 +00:00
|
|
|
// host ping function
|
|
|
|
pingHost HostPinger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *Ping) Description() string {
|
|
|
|
return "Ping given url(s) and return statistics"
|
|
|
|
}
|
|
|
|
|
2016-04-27 21:08:38 +00:00
|
|
|
const sampleConfig = `
|
2017-04-13 00:46:48 +00:00
|
|
|
## List of urls to ping
|
2018-08-31 20:59:30 +00:00
|
|
|
urls = ["example.org"]
|
|
|
|
|
|
|
|
## Number of pings to send per collection (ping -c <COUNT>)
|
2016-10-12 11:12:07 +00:00
|
|
|
# count = 1
|
2018-08-31 20:59:30 +00:00
|
|
|
|
|
|
|
## Interval, in s, at which to ping. 0 == default (ping -i <PING_INTERVAL>)
|
|
|
|
## Not available in Windows.
|
2016-10-12 11:12:07 +00:00
|
|
|
# ping_interval = 1.0
|
2018-08-31 20:59:30 +00:00
|
|
|
|
|
|
|
## Per-ping timeout, in s. 0 == no timeout (ping -W <TIMEOUT>)
|
2016-10-12 11:12:07 +00:00
|
|
|
# timeout = 1.0
|
2018-08-31 20:59:30 +00:00
|
|
|
|
|
|
|
## Total-ping deadline, in s. 0 == no deadline (ping -w <DEADLINE>)
|
2018-02-16 04:11:07 +00:00
|
|
|
# deadline = 10
|
2018-08-31 20:59:30 +00:00
|
|
|
|
|
|
|
## Interface or source address to send ping from (ping -I <INTERFACE/SRC_ADDR>)
|
2018-01-29 22:01:00 +00:00
|
|
|
## on Darwin and Freebsd only source address possible: (ping -S <SRC_ADDR>)
|
2016-10-12 11:12:07 +00:00
|
|
|
# interface = ""
|
2018-10-02 00:38:13 +00:00
|
|
|
|
|
|
|
## Specify the ping executable binary, default is "ping"
|
|
|
|
# binary = "ping"
|
|
|
|
|
|
|
|
## Arguments for ping command
|
|
|
|
## when arguments is not empty, other options (ping_interval, timeout, etc) will be ignored
|
|
|
|
# arguments = ["-c", "3"]
|
2015-09-02 23:16:52 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (_ *Ping) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (p *Ping) Gather(acc telegraf.Accumulator) error {
|
2015-09-02 23:16:52 +00:00
|
|
|
// Spin off a go routine for each url to ping
|
|
|
|
for _, url := range p.Urls {
|
2018-10-02 00:38:13 +00:00
|
|
|
p.wg.Add(1)
|
|
|
|
go p.pingToURL(url, acc)
|
|
|
|
}
|
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
|
|
|
|
2018-10-02 00:38:13 +00:00
|
|
|
func (p *Ping) pingToURL(u string, acc telegraf.Accumulator) {
|
|
|
|
defer p.wg.Done()
|
|
|
|
tags := map[string]string{"url": u}
|
|
|
|
fields := map[string]interface{}{"result_code": 0}
|
2017-08-16 18:59:41 +00:00
|
|
|
|
2018-10-02 00:38:13 +00:00
|
|
|
_, err := net.LookupHost(u)
|
|
|
|
if err != nil {
|
|
|
|
acc.AddError(err)
|
|
|
|
fields["result_code"] = 1
|
|
|
|
acc.AddFields("ping", fields, tags)
|
|
|
|
return
|
|
|
|
}
|
2017-08-16 18:59:41 +00:00
|
|
|
|
2018-10-02 00:38:13 +00:00
|
|
|
args := p.args(u, runtime.GOOS)
|
|
|
|
totalTimeout := 60.0
|
|
|
|
if len(p.Arguments) == 0 {
|
|
|
|
totalTimeout = float64(p.Count)*p.Timeout + float64(p.Count-1)*p.PingInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := p.pingHost(p.Binary, totalTimeout, args...)
|
|
|
|
if err != nil {
|
|
|
|
// Some implementations of ping return a 1 exit code on
|
|
|
|
// timeout, if this occurs we will not exit and try to parse
|
|
|
|
// the output.
|
|
|
|
status := -1
|
|
|
|
if exitError, ok := err.(*exec.ExitError); ok {
|
|
|
|
if ws, ok := exitError.Sys().(syscall.WaitStatus); ok {
|
|
|
|
status = ws.ExitStatus()
|
2018-10-17 18:46:00 +00:00
|
|
|
fields["result_code"] = status
|
2017-06-13 21:09:17 +00:00
|
|
|
}
|
2018-10-02 00:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if status != 1 {
|
|
|
|
// Combine go err + stderr output
|
|
|
|
out = strings.TrimSpace(out)
|
|
|
|
if len(out) > 0 {
|
|
|
|
acc.AddError(fmt.Errorf("host %s: %s, %s", u, out, err))
|
|
|
|
} else {
|
|
|
|
acc.AddError(fmt.Errorf("host %s: %s", u, err))
|
2016-12-16 13:58:27 +00:00
|
|
|
}
|
2018-10-02 00:38:13 +00:00
|
|
|
fields["result_code"] = 2
|
2015-12-15 00:03:33 +00:00
|
|
|
acc.AddFields("ping", fields, tags)
|
2018-10-02 00:38:13 +00:00
|
|
|
return
|
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 00:38:13 +00:00
|
|
|
trans, rec, min, avg, max, stddev, err := processPingOutput(out)
|
|
|
|
if err != nil {
|
|
|
|
// fatal error
|
|
|
|
acc.AddError(fmt.Errorf("%s: %s", err, u))
|
|
|
|
fields["result_code"] = 2
|
|
|
|
acc.AddFields("ping", fields, tags)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Calculate packet loss percentage
|
|
|
|
loss := float64(trans-rec) / float64(trans) * 100.0
|
|
|
|
fields["packets_transmitted"] = trans
|
|
|
|
fields["packets_received"] = rec
|
|
|
|
fields["percent_packet_loss"] = loss
|
|
|
|
if min >= 0 {
|
|
|
|
fields["minimum_response_ms"] = min
|
|
|
|
}
|
|
|
|
if avg >= 0 {
|
|
|
|
fields["average_response_ms"] = avg
|
|
|
|
}
|
|
|
|
if max >= 0 {
|
|
|
|
fields["maximum_response_ms"] = max
|
|
|
|
}
|
|
|
|
if stddev >= 0 {
|
|
|
|
fields["standard_deviation_ms"] = stddev
|
|
|
|
}
|
|
|
|
acc.AddFields("ping", fields, tags)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// args returns the arguments for the 'ping' executable
|
2018-07-28 01:29:54 +00:00
|
|
|
func (p *Ping) args(url string, system string) []string {
|
2018-10-02 00:38:13 +00:00
|
|
|
if len(p.Arguments) > 0 {
|
2019-02-19 19:18:15 +00:00
|
|
|
return append(p.Arguments, url)
|
2018-10-02 00:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// build the ping command args based on toml config
|
2016-02-02 01:32:01 +00:00
|
|
|
args := []string{"-c", strconv.Itoa(p.Count), "-n", "-s", "16"}
|
2015-09-02 23:16:52 +00:00
|
|
|
if p.PingInterval > 0 {
|
2018-05-01 02:20:13 +00:00
|
|
|
args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', -1, 64))
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
if p.Timeout > 0 {
|
2018-07-28 01:29:54 +00:00
|
|
|
switch system {
|
2018-10-02 00:38:13 +00:00
|
|
|
case "darwin":
|
2018-05-01 02:20:13 +00:00
|
|
|
args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64))
|
2018-10-02 00:38:13 +00:00
|
|
|
case "freebsd", "netbsd", "openbsd":
|
2018-12-11 00:20:04 +00:00
|
|
|
args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64))
|
2016-02-03 01:43:03 +00:00
|
|
|
case "linux":
|
2018-05-01 02:20:13 +00:00
|
|
|
args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', -1, 64))
|
2016-02-03 01:43:03 +00:00
|
|
|
default:
|
|
|
|
// Not sure the best option here, just assume GNU ping?
|
2018-05-01 02:20:13 +00:00
|
|
|
args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', -1, 64))
|
2016-02-03 01:43:03 +00:00
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
2018-02-16 04:11:07 +00:00
|
|
|
if p.Deadline > 0 {
|
2018-07-28 01:29:54 +00:00
|
|
|
switch system {
|
2018-06-06 21:28:12 +00:00
|
|
|
case "darwin", "freebsd", "netbsd", "openbsd":
|
2018-02-16 04:11:07 +00:00
|
|
|
args = append(args, "-t", strconv.Itoa(p.Deadline))
|
|
|
|
case "linux":
|
|
|
|
args = append(args, "-w", strconv.Itoa(p.Deadline))
|
|
|
|
default:
|
2018-10-02 00:38:13 +00:00
|
|
|
// not sure the best option here, just assume gnu ping?
|
2018-02-16 04:11:07 +00:00
|
|
|
args = append(args, "-w", strconv.Itoa(p.Deadline))
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
if p.Interface != "" {
|
2018-07-28 01:29:54 +00:00
|
|
|
switch system {
|
2018-10-02 00:38:13 +00:00
|
|
|
case "darwin":
|
|
|
|
args = append(args, "-I", p.Interface)
|
|
|
|
case "freebsd", "netbsd", "openbsd":
|
|
|
|
args = append(args, "-s", p.Interface)
|
2018-01-29 22:01:00 +00:00
|
|
|
case "linux":
|
|
|
|
args = append(args, "-I", p.Interface)
|
|
|
|
default:
|
2018-10-02 00:38:13 +00:00
|
|
|
// not sure the best option here, just assume gnu ping?
|
|
|
|
args = append(args, "-i", p.Interface)
|
2018-01-29 22:01:00 +00:00
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
args = append(args, url)
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
// processPingOutput takes in a string output from the ping command, like:
|
|
|
|
//
|
2018-10-02 00:38:13 +00:00
|
|
|
// ping www.google.com (173.194.115.84): 56 data bytes
|
2015-09-02 23:16:52 +00:00
|
|
|
// 64 bytes from 173.194.115.84: icmp_seq=0 ttl=54 time=52.172 ms
|
|
|
|
// 64 bytes from 173.194.115.84: icmp_seq=1 ttl=54 time=34.843 ms
|
|
|
|
//
|
|
|
|
// --- www.google.com ping statistics ---
|
|
|
|
// 2 packets transmitted, 2 packets received, 0.0% packet loss
|
|
|
|
// round-trip min/avg/max/stddev = 34.843/43.508/52.172/8.664 ms
|
|
|
|
//
|
|
|
|
// It returns (<transmitted packets>, <received packets>, <average response>)
|
2017-06-13 21:09:17 +00:00
|
|
|
func processPingOutput(out string) (int, int, float64, float64, float64, float64, error) {
|
2015-09-02 23:16:52 +00:00
|
|
|
var trans, recv int
|
2018-02-09 20:11:19 +00:00
|
|
|
var min, avg, max, stddev float64 = -1.0, -1.0, -1.0, -1.0
|
2015-09-02 23:16:52 +00:00
|
|
|
// Set this error to nil if we find a 'transmitted' line
|
|
|
|
err := errors.New("Fatal error processing ping output")
|
|
|
|
lines := strings.Split(out, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.Contains(line, "transmitted") &&
|
|
|
|
strings.Contains(line, "received") {
|
|
|
|
stats := strings.Split(line, ", ")
|
|
|
|
// Transmitted packets
|
|
|
|
trans, err = strconv.Atoi(strings.Split(stats[0], " ")[0])
|
|
|
|
if err != nil {
|
2017-06-13 21:09:17 +00:00
|
|
|
return trans, recv, min, avg, max, stddev, err
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
// Received packets
|
|
|
|
recv, err = strconv.Atoi(strings.Split(stats[1], " ")[0])
|
|
|
|
if err != nil {
|
2017-06-13 21:09:17 +00:00
|
|
|
return trans, recv, min, avg, max, stddev, err
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
} else if strings.Contains(line, "min/avg/max") {
|
2016-12-16 13:58:27 +00:00
|
|
|
stats := strings.Split(line, " ")[3]
|
2018-05-01 02:20:13 +00:00
|
|
|
data := strings.Split(stats, "/")
|
|
|
|
min, err = strconv.ParseFloat(data[0], 64)
|
2017-10-26 20:35:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return trans, recv, min, avg, max, stddev, err
|
|
|
|
}
|
2018-05-01 02:20:13 +00:00
|
|
|
avg, err = strconv.ParseFloat(data[1], 64)
|
2017-10-26 20:35:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return trans, recv, min, avg, max, stddev, err
|
|
|
|
}
|
2018-05-01 02:20:13 +00:00
|
|
|
max, err = strconv.ParseFloat(data[2], 64)
|
2017-10-26 20:35:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return trans, recv, min, avg, max, stddev, err
|
|
|
|
}
|
2018-05-01 02:20:13 +00:00
|
|
|
if len(data) == 4 {
|
|
|
|
stddev, err = strconv.ParseFloat(data[3], 64)
|
|
|
|
if err != nil {
|
|
|
|
return trans, recv, min, avg, max, stddev, err
|
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 21:09:17 +00:00
|
|
|
return trans, recv, min, avg, max, stddev, err
|
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,
|
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
|
|
|
})
|
|
|
|
}
|