2016-02-01 23:36:08 +00:00
|
|
|
// +build windows
|
2017-07-24 19:36:33 +00:00
|
|
|
|
2016-02-01 23:36:08 +00:00
|
|
|
package ping
|
2016-07-25 12:17:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2017-06-16 20:39:55 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-07-25 12:17:41 +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)
|
|
|
|
type HostPinger func(timeout float64, args ...string) (string, error)
|
|
|
|
|
|
|
|
type Ping struct {
|
|
|
|
// Number of pings to send (ping -c <COUNT>)
|
|
|
|
Count int
|
|
|
|
|
|
|
|
// Ping timeout, in seconds. 0 means no timeout (ping -W <TIMEOUT>)
|
|
|
|
Timeout float64
|
|
|
|
|
|
|
|
// URLs to ping
|
|
|
|
Urls []string
|
|
|
|
|
|
|
|
// host ping function
|
|
|
|
pingHost HostPinger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Ping) Description() string {
|
|
|
|
return "Ping given url(s) and return statistics"
|
|
|
|
}
|
|
|
|
|
|
|
|
const sampleConfig = `
|
2017-06-16 20:39:55 +00:00
|
|
|
## List of urls to ping
|
|
|
|
urls = ["www.google.com"]
|
2017-03-01 11:22:42 +00:00
|
|
|
|
2016-07-25 12:17:41 +00:00
|
|
|
## number of pings to send per collection (ping -n <COUNT>)
|
2017-06-16 20:39:55 +00:00
|
|
|
# count = 1
|
2017-03-01 11:22:42 +00:00
|
|
|
|
2017-06-06 18:55:01 +00:00
|
|
|
## Ping timeout, in seconds. 0.0 means default timeout (ping -w <TIMEOUT>)
|
2017-06-16 20:39:55 +00:00
|
|
|
# timeout = 0.0
|
2016-07-25 12:17:41 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (s *Ping) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func hostPinger(timeout float64, args ...string) (string, error) {
|
|
|
|
bin, err := exec.LookPath("ping")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
c := exec.Command(bin, args...)
|
|
|
|
out, err := internal.CombinedOutputTimeout(c,
|
|
|
|
time.Second*time.Duration(timeout+1))
|
|
|
|
return string(out), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// processPingOutput takes in a string output from the ping command
|
2017-03-01 11:22:42 +00:00
|
|
|
// based on linux implementation but using regex ( multilanguage support )
|
2016-08-09 07:27:30 +00:00
|
|
|
// It returns (<transmitted packets>, <received reply>, <received packet>, <average response>, <min response>, <max response>)
|
|
|
|
func processPingOutput(out string) (int, int, int, int, int, int, error) {
|
2016-07-25 12:17:41 +00:00
|
|
|
// So find a line contain 3 numbers except reply lines
|
|
|
|
var stats, aproxs []string = nil, nil
|
|
|
|
err := errors.New("Fatal error processing ping output")
|
|
|
|
stat := regexp.MustCompile(`=\W*(\d+)\D*=\W*(\d+)\D*=\W*(\d+)`)
|
|
|
|
aprox := regexp.MustCompile(`=\W*(\d+)\D*ms\D*=\W*(\d+)\D*ms\D*=\W*(\d+)\D*ms`)
|
2016-08-09 07:27:30 +00:00
|
|
|
tttLine := regexp.MustCompile(`TTL=\d+`)
|
2016-07-25 12:17:41 +00:00
|
|
|
lines := strings.Split(out, "\n")
|
2016-08-09 07:27:30 +00:00
|
|
|
var receivedReply int = 0
|
2016-07-25 12:17:41 +00:00
|
|
|
for _, line := range lines {
|
2016-08-09 07:27:30 +00:00
|
|
|
if tttLine.MatchString(line) {
|
|
|
|
receivedReply++
|
|
|
|
} else {
|
2016-07-25 12:17:41 +00:00
|
|
|
if stats == nil {
|
|
|
|
stats = stat.FindStringSubmatch(line)
|
|
|
|
}
|
|
|
|
if stats != nil && aproxs == nil {
|
|
|
|
aproxs = aprox.FindStringSubmatch(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stats data should contain 4 members: entireExpression + ( Send, Receive, Lost )
|
|
|
|
if len(stats) != 4 {
|
2016-08-09 07:27:30 +00:00
|
|
|
return 0, 0, 0, 0, 0, 0, err
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
trans, err := strconv.Atoi(stats[1])
|
|
|
|
if err != nil {
|
2016-08-09 07:27:30 +00:00
|
|
|
return 0, 0, 0, 0, 0, 0, err
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
2016-08-09 07:27:30 +00:00
|
|
|
receivedPacket, err := strconv.Atoi(stats[2])
|
2016-07-25 12:17:41 +00:00
|
|
|
if err != nil {
|
2016-08-09 07:27:30 +00:00
|
|
|
return 0, 0, 0, 0, 0, 0, err
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// aproxs data should contain 4 members: entireExpression + ( min, max, avg )
|
|
|
|
if len(aproxs) != 4 {
|
2016-08-09 07:27:30 +00:00
|
|
|
return trans, receivedReply, receivedPacket, 0, 0, 0, err
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
min, err := strconv.Atoi(aproxs[1])
|
|
|
|
if err != nil {
|
2016-08-09 07:27:30 +00:00
|
|
|
return trans, receivedReply, receivedPacket, 0, 0, 0, err
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
max, err := strconv.Atoi(aproxs[2])
|
|
|
|
if err != nil {
|
2016-08-09 07:27:30 +00:00
|
|
|
return trans, receivedReply, receivedPacket, 0, 0, 0, err
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
avg, err := strconv.Atoi(aproxs[3])
|
|
|
|
if err != nil {
|
2016-08-09 07:27:30 +00:00
|
|
|
return 0, 0, 0, 0, 0, 0, err
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 07:27:30 +00:00
|
|
|
return trans, receivedReply, receivedPacket, avg, min, max, err
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Ping) timeout() float64 {
|
|
|
|
// According to MSDN, default ping timeout for windows is 4 second
|
|
|
|
// Add also one second interval
|
|
|
|
|
|
|
|
if p.Timeout > 0 {
|
|
|
|
return p.Timeout + 1
|
|
|
|
}
|
|
|
|
return 4 + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// args returns the arguments for the 'ping' executable
|
|
|
|
func (p *Ping) args(url string) []string {
|
|
|
|
args := []string{"-n", strconv.Itoa(p.Count)}
|
|
|
|
|
|
|
|
if p.Timeout > 0 {
|
|
|
|
args = append(args, "-w", strconv.FormatFloat(p.Timeout*1000, 'f', 0, 64))
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, url)
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Ping) Gather(acc telegraf.Accumulator) error {
|
2017-06-16 20:39:55 +00:00
|
|
|
if p.Count < 1 {
|
|
|
|
p.Count = 1
|
|
|
|
}
|
2016-07-25 12:17:41 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
errorChannel := make(chan error, len(p.Urls)*2)
|
|
|
|
var pendingError error = nil
|
|
|
|
// Spin off a go routine for each url to ping
|
|
|
|
for _, url := range p.Urls {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(u string) {
|
|
|
|
defer wg.Done()
|
|
|
|
args := p.args(u)
|
|
|
|
totalTimeout := p.timeout() * float64(p.Count)
|
|
|
|
out, err := p.pingHost(totalTimeout, args...)
|
|
|
|
// ping host return exitcode != 0 also when there was no response from host
|
|
|
|
// but command was execute succesfully
|
|
|
|
if err != nil {
|
|
|
|
// Combine go err + stderr output
|
|
|
|
pendingError = errors.New(strings.TrimSpace(out) + ", " + err.Error())
|
|
|
|
}
|
|
|
|
tags := map[string]string{"url": u}
|
2016-08-09 07:27:30 +00:00
|
|
|
trans, recReply, receivePacket, avg, min, max, err := processPingOutput(out)
|
2016-07-25 12:17:41 +00:00
|
|
|
if err != nil {
|
|
|
|
// fatal error
|
|
|
|
if pendingError != nil {
|
|
|
|
errorChannel <- pendingError
|
|
|
|
}
|
|
|
|
errorChannel <- err
|
2016-08-09 07:27:30 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"errors": 100.0,
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields("ping", fields, tags)
|
|
|
|
|
2016-07-25 12:17:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Calculate packet loss percentage
|
2016-08-09 07:27:30 +00:00
|
|
|
lossReply := float64(trans-recReply) / float64(trans) * 100.0
|
|
|
|
lossPackets := float64(trans-receivePacket) / float64(trans) * 100.0
|
2016-07-25 12:17:41 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"packets_transmitted": trans,
|
2016-08-09 07:27:30 +00:00
|
|
|
"reply_received": recReply,
|
|
|
|
"packets_received": receivePacket,
|
|
|
|
"percent_packet_loss": lossPackets,
|
|
|
|
"percent_reply_loss": lossReply,
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
if avg > 0 {
|
2017-03-01 11:22:42 +00:00
|
|
|
fields["average_response_ms"] = float64(avg)
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
if min > 0 {
|
2017-03-01 11:22:42 +00:00
|
|
|
fields["minimum_response_ms"] = float64(min)
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
if max > 0 {
|
2017-03-01 11:22:42 +00:00
|
|
|
fields["maximum_response_ms"] = float64(max)
|
2016-07-25 12:17:41 +00:00
|
|
|
}
|
|
|
|
acc.AddFields("ping", fields, tags)
|
|
|
|
}(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
close(errorChannel)
|
|
|
|
|
|
|
|
// Get all errors and return them as one giant error
|
|
|
|
errorStrings := []string{}
|
|
|
|
for err := range errorChannel {
|
|
|
|
errorStrings = append(errorStrings, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errorStrings) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New(strings.Join(errorStrings, "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("ping", func() telegraf.Input {
|
2017-06-16 20:39:55 +00:00
|
|
|
return &Ping{
|
|
|
|
pingHost: hostPinger,
|
|
|
|
Count: 1,
|
|
|
|
}
|
2016-07-25 12:17:41 +00:00
|
|
|
})
|
|
|
|
}
|