From d67e46bb4e8cc9e0ef0f9872e8936f5005a68f1c Mon Sep 17 00:00:00 2001 From: efficks Date: Fri, 9 Feb 2018 15:11:19 -0500 Subject: [PATCH] Fix ping plugin not reporting zero durations (#3778) --- plugins/inputs/ping/ping.go | 10 +++++----- plugins/inputs/ping/ping_windows.go | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index 4ed36bd05..2fb48a20b 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -129,16 +129,16 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error { fields["packets_transmitted"] = trans fields["packets_received"] = rec fields["percent_packet_loss"] = loss - if min > 0 { + if min >= 0 { fields["minimum_response_ms"] = min } - if avg > 0 { + if avg >= 0 { fields["average_response_ms"] = avg } - if max > 0 { + if max >= 0 { fields["maximum_response_ms"] = max } - if stddev > 0 { + if stddev >= 0 { fields["standard_deviation_ms"] = stddev } acc.AddFields("ping", fields, tags) @@ -207,7 +207,7 @@ func (p *Ping) args(url string) []string { // It returns (, , ) func processPingOutput(out string) (int, int, float64, float64, float64, float64, error) { var trans, recv int - var min, avg, max, stddev float64 + var min, avg, max, stddev float64 = -1.0, -1.0, -1.0, -1.0 // Set this error to nil if we find a 'transmitted' line err := errors.New("Fatal error processing ping output") lines := strings.Split(out, "\n") diff --git a/plugins/inputs/ping/ping_windows.go b/plugins/inputs/ping/ping_windows.go index 862c82c5f..06a7f590e 100644 --- a/plugins/inputs/ping/ping_windows.go +++ b/plugins/inputs/ping/ping_windows.go @@ -93,32 +93,32 @@ func processPingOutput(out string) (int, int, int, int, int, int, error) { // stats data should contain 4 members: entireExpression + ( Send, Receive, Lost ) if len(stats) != 4 { - return 0, 0, 0, 0, 0, 0, err + return 0, 0, 0, -1, -1, -1, err } trans, err := strconv.Atoi(stats[1]) if err != nil { - return 0, 0, 0, 0, 0, 0, err + return 0, 0, 0, -1, -1, -1, err } receivedPacket, err := strconv.Atoi(stats[2]) if err != nil { - return 0, 0, 0, 0, 0, 0, err + return 0, 0, 0, -1, -1, -1, err } // aproxs data should contain 4 members: entireExpression + ( min, max, avg ) if len(aproxs) != 4 { - return trans, receivedReply, receivedPacket, 0, 0, 0, err + return trans, receivedReply, receivedPacket, -1, -1, -1, err } min, err := strconv.Atoi(aproxs[1]) if err != nil { - return trans, receivedReply, receivedPacket, 0, 0, 0, err + return trans, receivedReply, receivedPacket, -1, -1, -1, err } max, err := strconv.Atoi(aproxs[2]) if err != nil { - return trans, receivedReply, receivedPacket, 0, 0, 0, err + return trans, receivedReply, receivedPacket, -1, -1, -1, err } avg, err := strconv.Atoi(aproxs[3]) if err != nil { - return 0, 0, 0, 0, 0, 0, err + return 0, 0, 0, -1, -1, -1, err } return trans, receivedReply, receivedPacket, avg, min, max, err @@ -201,13 +201,13 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error { fields["packets_received"] = receivePacket fields["percent_packet_loss"] = lossPackets fields["percent_reply_loss"] = lossReply - if avg > 0 { + if avg >= 0 { fields["average_response_ms"] = float64(avg) } - if min > 0 { + if min >= 0 { fields["minimum_response_ms"] = float64(min) } - if max > 0 { + if max >= 0 { fields["maximum_response_ms"] = float64(max) } acc.AddFields("ping", fields, tags)