Fix ping plugin not reporting zero durations (#3778)
(cherry picked from commit f5ea13a9ab
)
This commit is contained in:
parent
c5ddb65ad9
commit
8cb5391f4e
|
@ -128,16 +128,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)
|
||||
|
@ -198,7 +198,7 @@ func (p *Ping) args(url string) []string {
|
|||
// It returns (<transmitted packets>, <received packets>, <average response>)
|
||||
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")
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue