diff --git a/plugins/wireguard/example-graphs/wireguard_peer_count.png b/plugins/wireguard/example-graphs/wireguard_peer_count.png new file mode 100644 index 00000000..a109bf8d Binary files /dev/null and b/plugins/wireguard/example-graphs/wireguard_peer_count.png differ diff --git a/plugins/wireguard/example-graphs/wireguard_peer_traffic.png b/plugins/wireguard/example-graphs/wireguard_peer_traffic.png new file mode 100644 index 00000000..654b8235 Binary files /dev/null and b/plugins/wireguard/example-graphs/wireguard_peer_traffic.png differ diff --git a/plugins/wireguard/wireguard_ b/plugins/wireguard/wireguard_ new file mode 100644 index 00000000..10d4ec96 --- /dev/null +++ b/plugins/wireguard/wireguard_ @@ -0,0 +1,187 @@ +#!/bin/bash +# -*- sh -*- + +set -e + +: << =cut + +=head1 NAME + +wireguard_ - Wildcard-plugin to monitor wireguard peer count and traffic + +=head1 CONFIGURATION + +This plugin does not normally require configuration. + +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 $? +} + +function wg_interfaces { + show_all=$1 + for iface in $(wg show interfaces | tr " " "\n"); do + # Filter interfaces if needed + if [ -z "$show_all" ] \ + && [ -n "$INTERFACE" ] \ + && [ "$INTERFACE" != "$iface" ]; then + continue + fi + + echo "$iface" + done +} + +function wg_peers { + iface=$1 + + # 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 + for line in $(wg show "$iface" dump | tr '\t' ';'); do + column_count=$(awk -F';' '{print NF}' <<< "$line") + if [ "$column_count" -ne 8 ]; then + # First line of dump contains interface info, ignore this line + continue + fi + + echo "$line" + done +} + +function safe_peer_id { + unsafe_peer_id=$1 + + echo "${unsafe_peer_id//[.:]/_}" +} + +case $1 in + autoconf) + if wg_exists; then + echo "yes" + else + echo "no (wg command not found)" + fi + ;; + suggest) + if wg_exists; then + wg_interfaces 1 + fi + ;; + config) + # Config for peer count per interface graph + cat << EOF +multigraph wireguard_peercount +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 + + for iface in $(wg_interfaces); do + # List config for all interfaces + cat <