Add min/max response time on linux/darwin to ping (#2908)
This commit is contained in:
committed by
Daniel Nelson
parent
e3ccd473d2
commit
5d1efdbfda
@@ -83,7 +83,7 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
|
||||
strings.TrimSpace(out) + ", " + err.Error()))
|
||||
}
|
||||
tags := map[string]string{"url": u}
|
||||
trans, rec, avg, stddev, err := processPingOutput(out)
|
||||
trans, rec, min, avg, max, stddev, err := processPingOutput(out)
|
||||
if err != nil {
|
||||
// fatal error
|
||||
acc.AddError(err)
|
||||
@@ -96,9 +96,15 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
|
||||
"packets_received": rec,
|
||||
"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
|
||||
}
|
||||
@@ -158,9 +164,9 @@ func (p *Ping) args(url string) []string {
|
||||
// round-trip min/avg/max/stddev = 34.843/43.508/52.172/8.664 ms
|
||||
//
|
||||
// It returns (<transmitted packets>, <received packets>, <average response>)
|
||||
func processPingOutput(out string) (int, int, float64, float64, error) {
|
||||
func processPingOutput(out string) (int, int, float64, float64, float64, float64, error) {
|
||||
var trans, recv int
|
||||
var avg, stddev float64
|
||||
var min, avg, max, stddev float64
|
||||
// Set this error to nil if we find a 'transmitted' line
|
||||
err := errors.New("Fatal error processing ping output")
|
||||
lines := strings.Split(out, "\n")
|
||||
@@ -172,23 +178,25 @@ func processPingOutput(out string) (int, int, float64, float64, error) {
|
||||
// Transmitted packets
|
||||
trans, err = strconv.Atoi(strings.Split(stats[0], " ")[0])
|
||||
if err != nil {
|
||||
return trans, recv, avg, stddev, err
|
||||
return trans, recv, min, avg, max, stddev, err
|
||||
}
|
||||
// Received packets
|
||||
recv, err = strconv.Atoi(strings.Split(stats[1], " ")[0])
|
||||
if err != nil {
|
||||
return trans, recv, avg, stddev, err
|
||||
return trans, recv, min, avg, max, stddev, err
|
||||
}
|
||||
} else if strings.Contains(line, "min/avg/max") {
|
||||
stats := strings.Split(line, " ")[3]
|
||||
min, err = strconv.ParseFloat(strings.Split(stats, "/")[0], 64)
|
||||
avg, err = strconv.ParseFloat(strings.Split(stats, "/")[1], 64)
|
||||
max, err = strconv.ParseFloat(strings.Split(stats, "/")[2], 64)
|
||||
stddev, err = strconv.ParseFloat(strings.Split(stats, "/")[3], 64)
|
||||
if err != nil {
|
||||
return trans, recv, avg, stddev, err
|
||||
return trans, recv, min, avg, max, stddev, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return trans, recv, avg, stddev, err
|
||||
return trans, recv, min, avg, max, stddev, err
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in New Issue
Block a user