#!/bin/bash # -*- sh -*- set -e : << =cut =head1 NAME wireguard_ - Wildcard-plugin to monitor wireguard peer count and traffic =head1 CONFIGURATION The following environment variables are used by this plugin active_threshold_m - threshold to count the connection as inactive (default 3 minutes) The plugin needs to run as root to be able to call the wg show command. This is configured like this: [wireguard_*] user root This is a wildcard plugin which by default monitors all wireguard interfaces. To monitor a single wireguard interface, link wireguard_ to this file. For example, ln -s /usr/share/munin/plugins/wireguard_ \ /etc/munin/plugins/wireguard_wg0 will monitor wg0. =head1 AUTHOR Original author unknown Copyright (C) 2024 pimlie =head1 LICENSE MIT =head1 MAGIC MARKERS #%# family=auto #%# capabilities=autoconf suggest =cut . "$MUNIN_LIBDIR/plugins/plugin.sh" INTERFACE=${0##*wireguard_} function wg_exists { command -v wg >/dev/null return $? } _MAIN_ARG_FIRST=$1 function should_emit_config { if [ "$_MAIN_ARG_FIRST" = "config" ]; then return 0 fi return 1 } function should_emit_values { if ! should_emit_config || [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then return 0 fi return 1 } declare -a _CACHE_INTERFACES function wg_interfaces { local -n _wg_ifaces=$1 local show_all=$2 if [ "${#_CACHE_INTERFACES[@]}" -eq 0 ]; then IFS=' ' read -ra _CACHE_INTERFACES <<< "$(wg show interfaces)" fi local iface for iface in "${_CACHE_INTERFACES[@]}"; do # Filter interfaces if needed if [ -z "$show_all" ] \ && [ -n "$INTERFACE" ] \ && [ "$INTERFACE" != "$iface" ]; then continue fi _wg_ifaces+=("$iface") done } declare -A _CACHE_PEERS function wg_peers { local -n _wg_peers=$1 local iface=$2 if [ -z "${_CACHE_PEERS[$iface]}" ]; then # From wg 8 manpage: # If dump is specified, then several lines are printed; the first contains # in order separated by tab: private-key, public-key, listen-port, fwmark. # Subsequent lines are printed for each peer and contain in order separated # by tab: public-key, preshared-key, endpoint, allowed-ips, latest-handshake, # transfer-rx, transfer-tx, persistent-keepalive # Pipe to tail to skip first line _CACHE_PEERS["$iface"]="$(wg show "$iface" dump | tail -n +2)" fi readarray -t peers <<< "${_CACHE_PEERS[$iface]}" _wg_peers=("${peers[@]}") } function safe_peer_id { unsafe_peer_id=$1 echo "${unsafe_peer_id//[^a-zA-Z0-9-_]/_}" } function peer_count { declare -a ifaces wg_interfaces ifaces echo "multigraph wireguard_peercount" if should_emit_config; then # Config for peer count per interface graph cat << EOF graph_title interface peer count graph_vlabel Number of peers graph_category wireguard graph_info This graph shows the number of peers per wireguard interface EOF fi for iface in "${ifaces[@]}"; do if should_emit_config; then # List config for all interfaces cat < threshold' <<< "$(IFS=$'\n'; echo "${peers[*]}")" | wc -l) echo "pc_on_$iface.value $peer_count" echo "apc_on_$iface.value $active_peer_count" fi done } function peer_traffic { local iface="$1" echo "multigraph wireguard_peertraffic_$iface" if should_emit_config; then cat <