Skip to content

Commit

Permalink
"latest handshake: never" outputs hostname
Browse files Browse the repository at this point in the history
WARNING: latest handshake: never
peer: 10.0.0.4/32 (hostname)
  • Loading branch information
dsh2dsh committed May 20, 2024
1 parent f008ad5 commit fa2d6a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

```
Expand Down
18 changes: 13 additions & 5 deletions cmd/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions cmd/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit fa2d6a0

Please sign in to comment.