#!/bin/bash # Edited with Claude Code # wirecon-monitor.sh — long-running observer. Emits one structured log line # every MONITOR_INTERVAL seconds with handshake age, throughput deltas, # endpoint, and in-tunnel RTT. Designed to make silent-death diagnosable # after the fact via journalctl. # # Quiet mode: when MONITOR_QUIET=yes, switches to MONITOR_QUIET_INTERVAL # and suppresses info-level lines (warn/error still emitted). Toggle from # the web UI once the link is stable to cut log volume + wake-ups. set -uo pipefail CONF="${WIRECON_CONF:-/etc/wirecon/wirecon.conf}" # shellcheck source=/dev/null [[ -r "$CONF" ]] && source "$CONF" : "${WG_INTERFACE:=wg0}" : "${PEER_IP:=10.42.0.1}" : "${MONITOR_INTERVAL:=10}" : "${MONITOR_QUIET:=no}" : "${MONITOR_QUIET_INTERVAL:=60}" if [[ "$MONITOR_QUIET" == "yes" ]]; then ACTIVE_INTERVAL="$MONITOR_QUIET_INTERVAL" QUIET=1 else ACTIVE_INTERVAL="$MONITOR_INTERVAL" QUIET=0 fi prev_rx="" prev_tx="" prev_hs="" prev_ep="" prev_mtu="" log() { local level="$1"; shift # In quiet mode, drop info-level chatter. Warn/error always emit. if [[ "$QUIET" -eq 1 && "$level" == "info" ]]; then return; fi printf 'level=%s ts=%s %s\n' "$level" "$(date -u +%FT%TZ)" "$*" } rtt_probe() { # single ping, short timeout, returns ms or "-" local out rtt out=$(ping -I "$WG_INTERFACE" -c 1 -W 2 -n "$PEER_IP" 2>/dev/null) || { echo "-"; return; } rtt=$(echo "$out" | awk -F'=' '/time=/{split($NF,a," "); print a[1]; exit}') [[ -z "$rtt" ]] && rtt="-" echo "$rtt" } # monitor_start is logged at warn so it's visible even after a quiet-mode # restart — useful for confirming the toggle landed. log warn "event=monitor_start iface=$WG_INTERFACE peer_ip=$PEER_IP interval=${ACTIVE_INTERVAL}s quiet=$MONITOR_QUIET" while :; do if ! ip link show "$WG_INTERFACE" >/dev/null 2>&1; then log warn "event=iface_missing iface=$WG_INTERFACE" sleep "$ACTIVE_INTERVAL" continue fi # wg show dump has one header line then one per peer: # iface: private pub listen_port fwmark # peer: pub psk endpoint allowed_ips last_handshake rx_bytes tx_bytes keepalive dump=$(wg show "$WG_INTERFACE" dump 2>/dev/null | awk 'NR==2') if [[ -z "$dump" ]]; then log warn "event=no_peer_dump iface=$WG_INTERFACE" sleep "$ACTIVE_INTERVAL" continue fi endpoint=$(awk '{print $3}' <<<"$dump") hs_ts=$(awk '{print $5}' <<<"$dump") rx=$(awk '{print $6}' <<<"$dump") tx=$(awk '{print $7}' <<<"$dump") mtu=$(ip -o link show "$WG_INTERFACE" | awk '{for(i=1;i<=NF;i++) if($i=="mtu"){print $(i+1); exit}}') now=$(date +%s) hs_age=-1 [[ "$hs_ts" != "0" && -n "$hs_ts" ]] && hs_age=$((now - hs_ts)) # byte deltas (bps averaged over interval) rx_bps="-"; tx_bps="-" if [[ -n "$prev_rx" && -n "$prev_tx" ]]; then rx_bps=$(( (rx - prev_rx) * 8 / ACTIVE_INTERVAL )) tx_bps=$(( (tx - prev_tx) * 8 / ACTIVE_INTERVAL )) fi rtt=$(rtt_probe) # Highlight state changes at warn level so they're easy to grep. level=info notes="" if [[ -n "$prev_ep" && "$prev_ep" != "$endpoint" ]]; then level=warn; notes+=" endpoint_changed_from=$prev_ep" fi if [[ -n "$prev_mtu" && "$prev_mtu" != "$mtu" ]]; then level=warn; notes+=" mtu_changed_from=$prev_mtu" fi if [[ -n "$prev_hs" && "$prev_hs" != "0" && "$hs_ts" == "$prev_hs" && "$hs_age" -gt 180 ]]; then level=warn; notes+=" handshake_stuck" fi log "$level" "event=tick iface=$WG_INTERFACE endpoint=$endpoint handshake_age=${hs_age}s mtu=$mtu rtt_ms=$rtt rx_bps=$rx_bps tx_bps=$tx_bps rx_total=$rx tx_total=$tx${notes}" prev_rx="$rx"; prev_tx="$tx"; prev_hs="$hs_ts"; prev_ep="$endpoint"; prev_mtu="$mtu" sleep "$ACTIVE_INTERVAL" done