summaryrefslogtreecommitdiff
path: root/statuspage/checks/wireguard.sh
blob: 2928e3e99fad7b25e31cb5c20e44bbdb00357187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/sh
# checks/wireguard.sh
# wg0 interface: at least one recent handshake means the tunnel is alive.
# Also lists each configured peer with time since its last handshake.
set -eu

DIR=$(dirname "$0")
. "$DIR/../lib/common.sh"

STATUS=$(wg_status wg0)
json_line "host services" "wireguard (wg0)" "" "$STATUS"

NOW=$(date +%s)
TAB=$(printf '\t')

wg show wg0 dump 2>/dev/null | tail -n +2 | while IFS="$TAB" read -r pubkey psk endpoint allowedips handshake rx tx keepalive; do
    [ -n "$pubkey" ] || continue
    peer_id=$(printf '%s' "$pubkey" | cut -c1-8)
    label="peer $peer_id ($allowedips)"

    if [ "$handshake" = "0" ]; then
        json_line "wireguard peers" "$label" "never" "down"
        continue
    fi

    age=$((NOW - handshake))
    if [ "$age" -lt 180 ]; then
        pstatus="ok"
    elif [ "$age" -lt 600 ]; then
        pstatus="warn"
    else
        pstatus="down"
    fi

    if [ "$age" -lt 60 ]; then
        rel="${age}s ago"
    elif [ "$age" -lt 3600 ]; then
        rel="$((age / 60))m ago"
    elif [ "$age" -lt 86400 ]; then
        rel="$((age / 3600))h ago"
    else
        rel="$((age / 86400))d ago"
    fi

    json_line "wireguard peers" "$label" "$rel" "$pstatus"
done