From 4b56760a80b69c171de2830e740b4c36014d9b17 Mon Sep 17 00:00:00 2001 From: pimlie Date: Sat, 19 Apr 2025 16:11:53 +0200 Subject: [PATCH] fix: use public key as graph id's as those are required and unique feat: add support for dirtyconfig --- plugins/wireguard/wireguard_ | 241 +++++++++++++++++++++-------------- 1 file changed, 148 insertions(+), 93 deletions(-) diff --git a/plugins/wireguard/wireguard_ b/plugins/wireguard/wireguard_ index 4e9470a4..abccdf5b 100755 --- a/plugins/wireguard/wireguard_ +++ b/plugins/wireguard/wireguard_ @@ -57,23 +57,49 @@ function wg_exists { 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 { - show_all=$1 - for iface in $(wg show interfaces | tr " " "\n"); do + 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 - - echo "$iface" + _wg_ifaces+=("$iface") done } - +declare -A _CACHE_PEERS function wg_peers { - iface=$1 + local -n _wg_peers=$1 + local iface=$2 +<<<<<<< Updated upstream # 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. @@ -86,15 +112,119 @@ function wg_peers { # First line of dump contains interface info, ignore this line continue fi +======= + 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 +>>>>>>> Stashed changes - echo "$line" - done + readarray -t peers <<< "${_CACHE_PEERS[$iface]}" + _wg_peers=("${peers[@]}") } function safe_peer_id { unsafe_peer_id=$1 - echo "${unsafe_peer_id//[.:]/_}" + 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 { + iface="$1" + + + echo "multigraph wireguard_peertraffic_$iface" + + if should_emit_config; then + cat < threshold' <<< "$iface_peers" | wc -l) - - echo "pc_on_$iface.value $peer_count" - echo "apc_on_$iface.value $active_peer_count" - done - echo "" - - for iface in $(wg_interfaces); do - echo "multigraph wireguard_peertraffic_$iface" - - for line in $(wg_peers "$iface"); do - read -r -a peer <<< "$(echo "$line" | tr ';' ' ')" - - peer_id=$(safe_peer_id "${peer[2]}") - - echo "down_${peer_id}.value ${peer[5]}" - echo "up_${peer_id}.value ${peer[6]}" - done - - echo "" + declare -a ifaces + wg_interfaces ifaces + for iface in "${ifaces[@]}"; do + peer_traffic "$iface" done ;; esac