diff --git a/README.md b/README.md index 3f72fe9..b243e04 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ threshold: 180000s | 'latest handshake'=188016s;300;180000;; $ check_wg handshake wg show wg0 dump WARNING: latest handshake: never -peer: 10.0.0.4/32 +peer: 10.0.0.4/32 (hostname) ``` ``` diff --git a/cmd/handshake.go b/cmd/handshake.go index 3e46b58..9d610c7 100644 --- a/cmd/handshake.go +++ b/cmd/handshake.go @@ -41,7 +41,9 @@ func handshakeResponse(dump *wg.Dump, resp *monitoringplugin.Response) error { peer := dump.OldestHandshake() if peer == nil { return errors.New("no valid peer found") - } else if checkNeverHandshake(peer, resp) { + } else if neverHandshake, err := checkNeverHandshake(peer, resp); err != nil { + return err + } else if neverHandshake { return nil } @@ -73,13 +75,19 @@ func handshakeResponse(dump *wg.Dump, resp *monitoringplugin.Response) error { } func checkNeverHandshake(peer *wg.DumpPeer, resp *monitoringplugin.Response, -) bool { +) (bool, error) { if !peer.LatestHandshake.IsZero() { - return false + return false, nil + } + + peerName, err := peer.ResolvedName() + if err != nil { + return false, err } + resp.UpdateStatus(monitoringplugin.WARNING, "latest handshake: never") - resp.UpdateStatus(monitoringplugin.WARNING, "peer="+peer.Name()) - return true + resp.UpdateStatus(monitoringplugin.WARNING, "peer="+peerName) + return true, nil } func outputPeerEndpoint(peer *wg.DumpPeer, diff --git a/cmd/handshake_test.go b/cmd/handshake_test.go index 165a8fc..32b7e6c 100644 --- a/cmd/handshake_test.go +++ b/cmd/handshake_test.go @@ -81,3 +81,16 @@ func TestHandshakeResponse_errors(t *testing.T) { require.ErrorContains(t, handshakeResponse(&dump, resp), "no valid peer found") } + +func TestCheckNeverHandshake(t *testing.T) { + dump, err := NewWgDump( + []string{"cat", "../wg/testdata/latest_handshake_zero.txt"}) + require.NoError(t, err) + require.NotNil(t, dump) + + resp := monitoringplugin.NewResponse("test OK") + require.NoError(t, handshakeResponse(&dump, resp)) + assert.Equal(t, monitoringplugin.WARNING, resp.GetStatusCode()) + t.Log(resp.GetInfo().RawOutput) + assert.Contains(t, resp.GetInfo().RawOutput, "latest handshake: never") +}