Initial commit

This commit is contained in:
2026-02-21 16:54:18 +01:00
commit 0cf33cd95b
80 changed files with 8042 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
---
- name: Create metrics exporter directory
file:
path: /usr/lib/wg-lab-exporter
state: directory
mode: '0755'
- name: Deploy metrics exporter script
template:
src: metrics-exporter.sh.j2
dest: /usr/lib/wg-lab-exporter/exporter.sh
mode: '0755'
- name: Create exporter configuration
template:
src: exporter-config.j2
dest: /etc/wg-lab-exporter.conf
mode: '0644'
- name: Create uhttpd configuration for metrics endpoint
blockinfile:
path: /etc/uhttpd.conf
create: yes
marker: "# {mark} ANSIBLE MANAGED - Metrics Exporter"
block: |
config uhttpd 'metrics'
list listen_http '0.0.0.0:9586'
option home '/www-metrics'
option cgi_prefix '/metrics'
option script_timeout '10'
option network_timeout '30'
option tcp_keepalive '1'
- name: Create metrics web directory
file:
path: /www-metrics/metrics
state: directory
mode: '0755'
- name: Create CGI wrapper for metrics
copy:
dest: /www-metrics/metrics/index.cgi
mode: '0755'
content: |
#!/bin/sh
echo "Content-Type: text/plain"
echo ""
/usr/lib/wg-lab-exporter/exporter.sh
- name: Create cron job for metrics collection (fallback)
cron:
name: "WireGuard Lab Metrics Collection"
minute: "*/5"
job: "/usr/lib/wg-lab-exporter/exporter.sh > /tmp/wg-lab-metrics.txt"
user: root
- name: Restart uhttpd
shell: /etc/init.d/uhttpd restart
changed_when: true
- name: Test metrics endpoint
uri:
url: "http://localhost:9586/metrics/"
timeout: 5
register: metrics_test
ignore_errors: yes
changed_when: false
- name: Display metrics test result
debug:
msg: "Metrics endpoint {{ 'OK' if metrics_test.status == 200 else 'FAILED' }}"
- name: Show sample metrics output
shell: /usr/lib/wg-lab-exporter/exporter.sh | head -20
register: metrics_sample
changed_when: false
- name: Display sample metrics
debug:
var: metrics_sample.stdout_lines

View File

@@ -0,0 +1,10 @@
# WireGuard Lab Exporter Configuration
# Lab: {{ gateway_config.lab_name }}
LAB_NAME="{{ gateway_config.lab_name }}"
VNI="{{ gateway_config.vni }}"
# Interfaces to monitor
WG_INTERFACES="wg_{{ gateway_config.lab_name }}"
BRIDGE_INTERFACES="br_{{ gateway_config.lab_name }}"
VXLAN_INTERFACES="vxlan{{ gateway_config.vni }}"

View File

@@ -0,0 +1,105 @@
#!/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))"