Fix ping plugin not reporting zero durations (#3778)

This commit is contained in:
efficks 2018-02-09 15:11:19 -05:00 committed by Daniel Nelson
parent 23e9306ce0
commit d67e46bb4e
2 changed files with 15 additions and 15 deletions

View File

@ -129,16 +129,16 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
fields["packets_transmitted"] = trans fields["packets_transmitted"] = trans
fields["packets_received"] = rec fields["packets_received"] = rec
fields["percent_packet_loss"] = loss fields["percent_packet_loss"] = loss
if min > 0 { if min >= 0 {
fields["minimum_response_ms"] = min fields["minimum_response_ms"] = min
} }
if avg > 0 { if avg >= 0 {
fields["average_response_ms"] = avg fields["average_response_ms"] = avg
} }
if max > 0 { if max >= 0 {
fields["maximum_response_ms"] = max fields["maximum_response_ms"] = max
} }
if stddev > 0 { if stddev >= 0 {
fields["standard_deviation_ms"] = stddev fields["standard_deviation_ms"] = stddev
} }
acc.AddFields("ping", fields, tags) acc.AddFields("ping", fields, tags)
@ -207,7 +207,7 @@ func (p *Ping) args(url string) []string {
// It returns (<transmitted packets>, <received packets>, <average response>) // It returns (<transmitted packets>, <received packets>, <average response>)
func processPingOutput(out string) (int, int, float64, float64, float64, float64, error) { func processPingOutput(out string) (int, int, float64, float64, float64, float64, error) {
var trans, recv int 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 // Set this error to nil if we find a 'transmitted' line
err := errors.New("Fatal error processing ping output") err := errors.New("Fatal error processing ping output")
lines := strings.Split(out, "\n") lines := strings.Split(out, "\n")

View File

@ -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 ) // stats data should contain 4 members: entireExpression + ( Send, Receive, Lost )
if len(stats) != 4 { 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]) trans, err := strconv.Atoi(stats[1])
if err != nil { 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]) receivedPacket, err := strconv.Atoi(stats[2])
if err != nil { 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 ) // aproxs data should contain 4 members: entireExpression + ( min, max, avg )
if len(aproxs) != 4 { 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]) min, err := strconv.Atoi(aproxs[1])
if err != nil { 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]) max, err := strconv.Atoi(aproxs[2])
if err != nil { 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]) avg, err := strconv.Atoi(aproxs[3])
if err != nil { 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 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["packets_received"] = receivePacket
fields["percent_packet_loss"] = lossPackets fields["percent_packet_loss"] = lossPackets
fields["percent_reply_loss"] = lossReply fields["percent_reply_loss"] = lossReply
if avg > 0 { if avg >= 0 {
fields["average_response_ms"] = float64(avg) fields["average_response_ms"] = float64(avg)
} }
if min > 0 { if min >= 0 {
fields["minimum_response_ms"] = float64(min) fields["minimum_response_ms"] = float64(min)
} }
if max > 0 { if max >= 0 {
fields["maximum_response_ms"] = float64(max) fields["maximum_response_ms"] = float64(max)
} }
acc.AddFields("ping", fields, tags) acc.AddFields("ping", fields, tags)