2016-02-01 23:36:08 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2015-09-02 23:16:52 +00:00
|
|
|
package ping
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os/exec"
|
2016-02-03 01:43:03 +00:00
|
|
|
"runtime"
|
2015-09-02 23:16:52 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
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)
|
2016-04-29 01:23:45 +00:00
|
|
|
type HostPinger func(timeout float64, args ...string) (string, error)
|
2015-09-02 23:16:52 +00:00
|
|
|
|
|
|
|
type Ping struct {
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// Interface to send ping from (ping -I <INTERFACE>)
|
|
|
|
Interface string
|
|
|
|
|
|
|
|
// URLs to ping
|
|
|
|
Urls []string
|
|
|
|
|
|
|
|
// 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 = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## NOTE: this plugin forks the ping command. You may need to set capabilities
|
|
|
|
## via setcap cap_net_raw+p /bin/ping
|
2016-04-27 21:08:38 +00:00
|
|
|
#
|
2017-04-13 00:46:48 +00:00
|
|
|
## List of urls to ping
|
2015-10-15 21:53:29 +00:00
|
|
|
urls = ["www.google.com"] # required
|
2016-04-27 21:08:38 +00:00
|
|
|
## number of pings to send per collection (ping -c <COUNT>)
|
2016-10-12 11:12:07 +00:00
|
|
|
# count = 1
|
2016-02-18 21:26:51 +00:00
|
|
|
## interval, in s, at which to ping. 0 == default (ping -i <PING_INTERVAL>)
|
2016-10-12 11:12:07 +00:00
|
|
|
# ping_interval = 1.0
|
2016-06-10 11:51:43 +00:00
|
|
|
## per-ping timeout, in s. 0 == no timeout (ping -W <TIMEOUT>)
|
2016-10-12 11:12:07 +00:00
|
|
|
# timeout = 1.0
|
2016-02-18 21:26:51 +00:00
|
|
|
## interface to send ping from (ping -I <INTERFACE>)
|
2016-10-12 11:12:07 +00:00
|
|
|
# interface = ""
|
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
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// Spin off a go routine for each url to ping
|
|
|
|
for _, url := range p.Urls {
|
|
|
|
wg.Add(1)
|
2016-04-27 21:08:38 +00:00
|
|
|
go func(u string) {
|
2015-09-02 23:16:52 +00:00
|
|
|
defer wg.Done()
|
2016-04-27 21:08:38 +00:00
|
|
|
args := p.args(u)
|
2016-06-01 08:06:55 +00:00
|
|
|
totalTimeout := float64(p.Count)*p.Timeout + float64(p.Count-1)*p.PingInterval
|
|
|
|
out, err := p.pingHost(totalTimeout, args...)
|
2015-09-02 23:16:52 +00:00
|
|
|
if err != nil {
|
|
|
|
// Combine go err + stderr output
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(errors.New(
|
|
|
|
strings.TrimSpace(out) + ", " + err.Error()))
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
2016-04-27 21:08:38 +00:00
|
|
|
tags := map[string]string{"url": u}
|
2017-06-13 21:09:17 +00:00
|
|
|
trans, rec, min, avg, max, stddev, err := processPingOutput(out)
|
2015-09-02 23:16:52 +00:00
|
|
|
if err != nil {
|
|
|
|
// fatal error
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(err)
|
2015-09-02 23:16:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Calculate packet loss percentage
|
|
|
|
loss := float64(trans-rec) / float64(trans) * 100.0
|
2015-12-15 00:03:33 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"packets_transmitted": trans,
|
|
|
|
"packets_received": rec,
|
|
|
|
"percent_packet_loss": loss,
|
2016-01-11 22:19:21 +00:00
|
|
|
}
|
2017-06-13 21:09:17 +00:00
|
|
|
if min > 0 {
|
|
|
|
fields["minimum_response_ms"] = min
|
|
|
|
}
|
2016-01-11 22:19:21 +00:00
|
|
|
if avg > 0 {
|
|
|
|
fields["average_response_ms"] = avg
|
2015-12-15 00:03:33 +00:00
|
|
|
}
|
2017-06-13 21:09:17 +00:00
|
|
|
if max > 0 {
|
|
|
|
fields["maximum_response_ms"] = max
|
|
|
|
}
|
2016-12-16 13:58:27 +00:00
|
|
|
if stddev > 0 {
|
|
|
|
fields["standard_deviation_ms"] = stddev
|
|
|
|
}
|
2015-12-15 00:03:33 +00:00
|
|
|
acc.AddFields("ping", fields, tags)
|
2016-04-27 21:08:38 +00:00
|
|
|
}(url)
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:23:45 +00:00
|
|
|
func hostPinger(timeout float64, args ...string) (string, error) {
|
2016-02-01 23:36:08 +00:00
|
|
|
bin, err := exec.LookPath("ping")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
c := exec.Command(bin, args...)
|
2016-04-29 01:23:45 +00:00
|
|
|
out, err := internal.CombinedOutputTimeout(c,
|
|
|
|
time.Second*time.Duration(timeout+1))
|
2015-09-02 23:16:52 +00:00
|
|
|
return string(out), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// args returns the arguments for the 'ping' executable
|
|
|
|
func (p *Ping) args(url string) []string {
|
|
|
|
// 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 {
|
|
|
|
args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', 1, 64))
|
|
|
|
}
|
|
|
|
if p.Timeout > 0 {
|
2016-02-03 01:43:03 +00:00
|
|
|
switch runtime.GOOS {
|
2016-06-10 11:51:43 +00:00
|
|
|
case "darwin":
|
2016-06-22 17:21:07 +00:00
|
|
|
args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', 1, 64))
|
2016-02-03 01:43:03 +00:00
|
|
|
case "linux":
|
|
|
|
args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', 1, 64))
|
|
|
|
default:
|
|
|
|
// Not sure the best option here, just assume GNU ping?
|
|
|
|
args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', 1, 64))
|
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
}
|
|
|
|
if p.Interface != "" {
|
|
|
|
args = append(args, "-I", p.Interface)
|
|
|
|
}
|
|
|
|
args = append(args, url)
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
// processPingOutput takes in a string output from the ping command, like:
|
|
|
|
//
|
|
|
|
// PING www.google.com (173.194.115.84): 56 data bytes
|
|
|
|
// 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
|
2017-06-13 21:09:17 +00:00
|
|
|
var min, avg, max, stddev float64
|
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") {
|
|
|
|
err = nil
|
|
|
|
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]
|
2017-06-13 21:09:17 +00:00
|
|
|
min, err = strconv.ParseFloat(strings.Split(stats, "/")[0], 64)
|
2015-09-02 23:16:52 +00:00
|
|
|
avg, err = strconv.ParseFloat(strings.Split(stats, "/")[1], 64)
|
2017-06-13 21:09:17 +00:00
|
|
|
max, err = strconv.ParseFloat(strings.Split(stats, "/")[2], 64)
|
2016-12-16 13:58:27 +00:00
|
|
|
stddev, err = strconv.ParseFloat(strings.Split(stats, "/")[3], 64)
|
2015-09-02 23:16:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
}
|
2015-09-02 23:16:52 +00:00
|
|
|
})
|
|
|
|
}
|