Files
2026-02-21 16:54:18 +01:00

106 lines
3.9 KiB
Django/Jinja

#!/bin/sh
# Prometheus metrics exporter for WireGuard Lab Overlay
# Generated by Ansible
. /etc/wg-lab-exporter.conf
# Helper function to output metrics
metric() {
echo "$1 $2"
}
metric_label() {
echo "$1{$2} $3"
}
# Current timestamp
TIMESTAMP=$(date +%s)
echo "# HELP wireguard_tunnel_up WireGuard tunnel status (1=up, 0=down)"
echo "# TYPE wireguard_tunnel_up gauge"
# Check WireGuard tunnels
for WG_IF in ${WG_INTERFACES}; do
if ip link show ${WG_IF} >/dev/null 2>&1; then
# Get peer information
wg show ${WG_IF} dump | tail -n +2 | while IFS=$'\t' read pubkey psk endpoint allowed_ip latest_handshake rx_bytes tx_bytes persistent_keepalive; do
# Calculate handshake age
if [ "$latest_handshake" != "0" ]; then
handshake_age=$((TIMESTAMP - latest_handshake))
if [ $handshake_age -lt 180 ]; then
status=1
else
status=0
fi
else
status=0
handshake_age=-1
fi
peer_short=$(echo $pubkey | cut -c1-16)
metric_label "wireguard_tunnel_up" "lab=\"${LAB_NAME}\",interface=\"${WG_IF}\",peer=\"${peer_short}\"" "$status"
metric_label "wireguard_latest_handshake_seconds" "lab=\"${LAB_NAME}\",interface=\"${WG_IF}\",peer=\"${peer_short}\"" "$handshake_age"
metric_label "wireguard_rx_bytes_total" "lab=\"${LAB_NAME}\",interface=\"${WG_IF}\",peer=\"${peer_short}\"" "$rx_bytes"
metric_label "wireguard_tx_bytes_total" "lab=\"${LAB_NAME}\",interface=\"${WG_IF}\",peer=\"${peer_short}\"" "$tx_bytes"
done
else
metric_label "wireguard_tunnel_up" "lab=\"${LAB_NAME}\",interface=\"${WG_IF}\",peer=\"all\"" "0"
fi
done
echo ""
echo "# HELP bridge_fdb_entries Number of FDB entries in bridge"
echo "# TYPE bridge_fdb_entries gauge"
# Bridge FDB entries
for BR_IF in ${BRIDGE_INTERFACES}; do
if ip link show ${BR_IF} >/dev/null 2>&1; then
fdb_count=$(bridge fdb show br ${BR_IF} 2>/dev/null | wc -l)
metric_label "bridge_fdb_entries" "lab=\"${LAB_NAME}\",bridge=\"${BR_IF}\"" "$fdb_count"
fi
done
echo ""
echo "# HELP vxlan_rx_packets_total VXLAN received packets"
echo "# TYPE vxlan_rx_packets_total counter"
echo "# HELP vxlan_tx_packets_total VXLAN transmitted packets"
echo "# TYPE vxlan_tx_packets_total counter"
# VXLAN statistics
for VXLAN_IF in ${VXLAN_INTERFACES}; do
if [ -f "/sys/class/net/${VXLAN_IF}/statistics/rx_packets" ]; then
rx_packets=$(cat /sys/class/net/${VXLAN_IF}/statistics/rx_packets)
tx_packets=$(cat /sys/class/net/${VXLAN_IF}/statistics/tx_packets)
rx_bytes=$(cat /sys/class/net/${VXLAN_IF}/statistics/rx_bytes)
tx_bytes=$(cat /sys/class/net/${VXLAN_IF}/statistics/tx_bytes)
metric_label "vxlan_rx_packets_total" "lab=\"${LAB_NAME}\",vni=\"${VNI}\"" "$rx_packets"
metric_label "vxlan_tx_packets_total" "lab=\"${LAB_NAME}\",vni=\"${VNI}\"" "$tx_packets"
metric_label "vxlan_rx_bytes_total" "lab=\"${LAB_NAME}\",vni=\"${VNI}\"" "$rx_bytes"
metric_label "vxlan_tx_bytes_total" "lab=\"${LAB_NAME}\",vni=\"${VNI}\"" "$tx_bytes"
fi
done
echo ""
echo "# HELP dhcp_leases_active Number of active DHCP leases"
echo "# TYPE dhcp_leases_active gauge"
# DHCP lease count
if [ -f "/tmp/dhcp.leases" ]; then
lease_count=$(wc -l < /tmp/dhcp.leases)
metric_label "dhcp_leases_active" "lab=\"${LAB_NAME}\"" "$lease_count"
fi
echo ""
echo "# HELP openwrt_system_uptime_seconds System uptime in seconds"
echo "# TYPE openwrt_system_uptime_seconds counter"
uptime_seconds=$(cat /proc/uptime | cut -d' ' -f1 | cut -d. -f1)
metric "openwrt_system_uptime_seconds" "$uptime_seconds"
echo ""
echo "# HELP openwrt_memory_free_bytes Free memory in bytes"
echo "# TYPE openwrt_memory_free_bytes gauge"
mem_free=$(free | grep Mem | awk '{print $4}')
metric "openwrt_memory_free_bytes" "$((mem_free * 1024))"