Skip to content

Commit

Permalink
check_ping: improve parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Oct 21, 2024
1 parent d731ee7 commit eed9f36
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pkg/snclient/check_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ func (l *CheckPing) parsePingOutput(output, stderr string) (entry map[string]str
output = strings.TrimSpace(output)
// passthrough some known errors
switch {
case strings.Contains(output, "Name or service not known"):
entry["_error"] = output
case strings.Contains(output, "Name or service not known"),
strings.Contains(output, "Unknown host"):
entry["_error"] = "failed to resolve hostname"
default:
entry["_error"] = fmt.Sprintf("cannot parse ping output: %s", output)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/snclient/check_ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ CheckBuiltinPlugins = enabled

res := snc.RunCheck("check_ping", []string{"host=localhost"})
assert.Equalf(t, CheckExitOK, res.State, "state ok")
assert.Regexpf(t, `^OK - Packet loss = \d+%, RTA = [\d.]+ms \|'rta'=[\d.]+ms;1000;5000 'pl'=\d+%;30;80;0`, string(res.BuildPluginOutput()), "output matches")
assert.Regexpf(t, `^OK - Packet loss = [\d.]+%, RTA = [\d.]+ms \|'rta'=[\d.]+ms;1000;5000 'pl'=\d+%;30;80;0`, string(res.BuildPluginOutput()), "output matches")

res = snc.RunCheck("check_ping", []string{"host=10.99.99.99"})
assert.Equalf(t, CheckExitCritical, res.State, "state critical")
assert.Regexpf(t, `^CRITICAL - Packet loss = 100% \|'rta'=U 'pl'=100%;30;80;0`, string(res.BuildPluginOutput()), "output matches")
assert.Regexpf(t, `^CRITICAL - Packet loss = 100(|\.0)% \|'rta'=U 'pl'=100%;30;80;0`, string(res.BuildPluginOutput()), "output matches")

res = snc.RunCheck("check_ping", []string{"host=should_not_resolve.nowhere"})
assert.Equalf(t, CheckExitUnknown, res.State, "state unknown")
assert.Regexpf(t, `^UNKNOWN - ping: should_not_resolve.nowhere: Name or service not known`, string(res.BuildPluginOutput()), "output matches")
assert.Regexpf(t, `^UNKNOWN - failed to resolve hostname`, string(res.BuildPluginOutput()), "output matches")

StopTestAgent(t, snc)
}
Expand Down

0 comments on commit eed9f36

Please sign in to comment.