#!/bin/bash set -uo pipefail textfile_dir={{ node_exporter_textfile_dir }} file=${textfile_dir}/wireguard.prom tmp=${file}.$$ now=$(date +%s) mkdir -p ${textfile_dir} peer_name() { case "$1" in {% for key, name in wireguard_peer_names.items() %} "{{ key }}") echo "{{ name }}" ;; {% endfor %} *) echo "unknown" ;; esac } { echo "# HELP wireguard_peer_last_handshake_seconds Unix time of the last handshake with a peer." echo "# TYPE wireguard_peer_last_handshake_seconds gauge" echo "# HELP wireguard_peer_handshake_age_seconds Seconds since the last handshake with a peer." echo "# TYPE wireguard_peer_handshake_age_seconds gauge" echo "# HELP wireguard_peer_up Whether a peer handshaked within the last five minutes." echo "# TYPE wireguard_peer_up gauge" echo "# HELP wireguard_peer_receive_bytes_total Bytes received from a peer." echo "# TYPE wireguard_peer_receive_bytes_total counter" echo "# HELP wireguard_peer_transmit_bytes_total Bytes sent to a peer." echo "# TYPE wireguard_peer_transmit_bytes_total counter" wg show all dump 2>/dev/null | awk 'NF >= 8' | while read -r interface key psk endpoint allowed handshake rx tx keepalive do name=$(peer_name "${key}") labels="interface=\"${interface}\",peer=\"${name}\"" age=$((now - handshake)) up=0 if [ "${handshake}" -gt 0 ] && [ "${age}" -lt 300 ] then up=1 fi if [ "${handshake}" -eq 0 ] then age=-1 fi echo "wireguard_peer_last_handshake_seconds{${labels}} ${handshake}" echo "wireguard_peer_handshake_age_seconds{${labels}} ${age}" echo "wireguard_peer_up{${labels}} ${up}" echo "wireguard_peer_receive_bytes_total{${labels}} ${rx}" echo "wireguard_peer_transmit_bytes_total{${labels}} ${tx}" done } > "${tmp}" chmod 0644 "${tmp}" mv "${tmp}" "${file}"