Skip to content

Commit

Permalink
Jitter for Input plugin Ping
Browse files Browse the repository at this point in the history
  • Loading branch information
alekseyp committed Nov 23, 2016
1 parent 5c32521 commit 68af7c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
20 changes: 12 additions & 8 deletions plugins/inputs/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
strings.TrimSpace(out) + ", " + err.Error())
}
tags := map[string]string{"url": u}
trans, rec, avg, err := processPingOutput(out)
trans, rec, avg, jit, err := processPingOutput(out)
if err != nil {
// fatal error
errorChannel <- err
Expand All @@ -100,6 +100,9 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
if avg > 0 {
fields["average_response_ms"] = avg
}
if jit > 0 {
fields["jitter_response_ms"] = jit
}
acc.AddFields("ping", fields, tags)
}(url)
}
Expand Down Expand Up @@ -166,9 +169,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, error) {
func processPingOutput(out string) (int, int, float64, float64, error) {
var trans, recv int
var avg float64
var avg, jit 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")
Expand All @@ -180,22 +183,23 @@ func processPingOutput(out string) (int, int, float64, error) {
// Transmitted packets
trans, err = strconv.Atoi(strings.Split(stats[0], " ")[0])
if err != nil {
return trans, recv, avg, err
return trans, recv, avg, jit, err
}
// Received packets
recv, err = strconv.Atoi(strings.Split(stats[1], " ")[0])
if err != nil {
return trans, recv, avg, err
return trans, recv, avg, jit, err
}
} else if strings.Contains(line, "min/avg/max") {
stats := strings.Split(line, " = ")[1]
stats := strings.Split(line, " ")[3]
avg, err = strconv.ParseFloat(strings.Split(stats, "/")[1], 64)
jit, err = strconv.ParseFloat(strings.Split(stats, "/")[3], 64)
if err != nil {
return trans, recv, avg, err
return trans, recv, avg, jit, err
}
}
}
return trans, recv, avg, err
return trans, recv, avg, jit, err
}

func init() {
Expand Down
10 changes: 7 additions & 3 deletions plugins/inputs/ping/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,25 @@ ping: -i interval too short: Operation not permitted

// Test that ping command output is processed properly
func TestProcessPingOutput(t *testing.T) {
trans, rec, avg, err := processPingOutput(bsdPingOutput)
trans, rec, avg, jit, err := processPingOutput(bsdPingOutput)
assert.NoError(t, err)
assert.Equal(t, 5, trans, "5 packets were transmitted")
assert.Equal(t, 5, rec, "5 packets were transmitted")
assert.InDelta(t, 20.224, avg, 0.001)
assert.InDelta(t, 4.076, jit, 0.001)

trans, rec, avg, err = processPingOutput(linuxPingOutput)
trans, rec, avg, jit, err = processPingOutput(linuxPingOutput)
assert.NoError(t, err)
assert.Equal(t, 5, trans, "5 packets were transmitted")
assert.Equal(t, 5, rec, "5 packets were transmitted")
assert.InDelta(t, 43.628, avg, 0.001)
assert.InDelta(t, 5.325, jit, 0.001)
}

// Test that processPingOutput returns an error when 'ping' fails to run, such
// as when an invalid argument is provided
func TestErrorProcessPingOutput(t *testing.T) {
_, _, _, err := processPingOutput(fatalPingOutput)
_, _, _, _, err := processPingOutput(fatalPingOutput)
assert.Error(t, err, "Error was expected from processPingOutput")
}

Expand Down Expand Up @@ -149,6 +151,7 @@ func TestPingGather(t *testing.T) {
"packets_received": 5,
"percent_packet_loss": 0.0,
"average_response_ms": 43.628,
"jitter_response_ms": 5.325,
}
acc.AssertContainsTaggedFields(t, "ping", fields, tags)

Expand Down Expand Up @@ -186,6 +189,7 @@ func TestLossyPingGather(t *testing.T) {
"packets_received": 3,
"percent_packet_loss": 40.0,
"average_response_ms": 44.033,
"jitter_response_ms": 5.325,
}
acc.AssertContainsTaggedFields(t, "ping", fields, tags)
}
Expand Down

0 comments on commit 68af7c7

Please sign in to comment.