From 7ff94424c0805843280eb75c825e48b669794599 Mon Sep 17 00:00:00 2001 From: artickl Date: Tue, 19 Mar 2024 19:21:27 +0000 Subject: [PATCH 1/3] Added count of active peers based on 10 munutes threshold --- plugins/wireguard/wireguard_ | 13 +++++++++++++ 1 file changed, 13 insertions(+) mode change 100644 => 100755 plugins/wireguard/wireguard_ diff --git a/plugins/wireguard/wireguard_ b/plugins/wireguard/wireguard_ old mode 100644 new mode 100755 index 10d4ec96..fd3b3505 --- a/plugins/wireguard/wireguard_ +++ b/plugins/wireguard/wireguard_ @@ -124,6 +124,9 @@ EOF pc_on_$iface.label $iface pc_on_$iface.info Interface $iface pc_on_$iface.min 0 +pc_on_active_$iface.label Active on $iface +pc_on_active_$iface.info Active on $iface +pc_on_active_$iface.min 0 EOF done echo "" @@ -165,7 +168,17 @@ EOF # Collect & print current monitoring values echo "multigraph wireguard_peercount" for iface in $(wg_interfaces); do + threshold=$(date --date="10 min ago" +%s) + isactive=0 + for line in $(wg_peers "$iface"); do + handshake=$(echo "$line" | tr ';' ' ' | awk '{print $5}') + if [[ "handshake" -gt "threshold" ]]; then + isactive=$((isactive + 1)) + fi + done + echo "pc_on_$iface.value $(wg show "$iface" peers | wc -l)" + echo "pc_on_active_$iface.value $isactive" done echo "" From bb26f900b120257224fb6dea06b88c0f88fb1e15 Mon Sep 17 00:00:00 2001 From: Artem Zavyalov Date: Thu, 21 Mar 2024 15:30:26 -0700 Subject: [PATCH 2/3] Update plugins/wireguard/wireguard_ Co-authored-by: Pim --- plugins/wireguard/wireguard_ | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/plugins/wireguard/wireguard_ b/plugins/wireguard/wireguard_ index fd3b3505..a6636a46 100755 --- a/plugins/wireguard/wireguard_ +++ b/plugins/wireguard/wireguard_ @@ -169,16 +169,13 @@ EOF echo "multigraph wireguard_peercount" for iface in $(wg_interfaces); do threshold=$(date --date="10 min ago" +%s) - isactive=0 - for line in $(wg_peers "$iface"); do - handshake=$(echo "$line" | tr ';' ' ' | awk '{print $5}') - if [[ "handshake" -gt "threshold" ]]; then - isactive=$((isactive + 1)) - fi - done + iface_peers=$(wg_peers "$iface") - echo "pc_on_$iface.value $(wg show "$iface" peers | wc -l)" - echo "pc_on_active_$iface.value $isactive" + peer_count=$(wc -l <<< "$iface_peers") + active_peer_count=$(awk -F";" -v threshold=$active_threshold '$5 > threshold' <<< "$iface_peers" | wc -l) + + echo "pc_on_$iface.value $peer_count" + echo "pc_active_on_$iface.value $active_peer_count" done echo "" From 8262e12e74cf0a8e407cdb58b73b9c804c654a75 Mon Sep 17 00:00:00 2001 From: artickl Date: Thu, 21 Mar 2024 23:23:12 +0000 Subject: [PATCH 3/3] Metric pc_active_on_* changed to apc_on_ - active peer connections Threshold for active connections moved out of the loop, plus moved to optional config setting --- plugins/wireguard/wireguard_ | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/plugins/wireguard/wireguard_ b/plugins/wireguard/wireguard_ index a6636a46..ad4fe56b 100755 --- a/plugins/wireguard/wireguard_ +++ b/plugins/wireguard/wireguard_ @@ -11,7 +11,9 @@ wireguard_ - Wildcard-plugin to monitor wireguard peer count and traffic =head1 CONFIGURATION -This plugin does not normally require 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: @@ -124,9 +126,9 @@ EOF pc_on_$iface.label $iface pc_on_$iface.info Interface $iface pc_on_$iface.min 0 -pc_on_active_$iface.label Active on $iface -pc_on_active_$iface.info Active on $iface -pc_on_active_$iface.min 0 +apc_on_$iface.label Active on $iface +apc_on_$iface.info Active on $iface +apc_on_$iface.min 0 EOF done echo "" @@ -167,15 +169,16 @@ EOF *) # Collect & print current monitoring values echo "multigraph wireguard_peercount" + active_threshold=$(date --date="${active_threshold_m:-3} min ago" +%s) + for iface in $(wg_interfaces); do - threshold=$(date --date="10 min ago" +%s) iface_peers=$(wg_peers "$iface") peer_count=$(wc -l <<< "$iface_peers") active_peer_count=$(awk -F";" -v threshold=$active_threshold '$5 > threshold' <<< "$iface_peers" | wc -l) echo "pc_on_$iface.value $peer_count" - echo "pc_active_on_$iface.value $active_peer_count" + echo "apc_on_$iface.value $active_peer_count" done echo ""