lots of stuff

This commit is contained in:
bee
2026-08-16 14:48:04 +02:00
parent c63db540e8
commit 49fa352476
45 changed files with 32520 additions and 93 deletions
@@ -0,0 +1,105 @@
#!/bin/bash
set -uo pipefail
textfile_dir={{ node_exporter_textfile_dir }}
file=${textfile_dir}/services.prom
tmp=${file}.$$
mkdir -p ${textfile_dir}
declare -A active_count
declare -A unit_count
units=""
record() {
local service=$1
local unit=$2
local kind=$3
local state=$4
units+="service_unit_active{service=\"${service}\",unit=\"${unit}\",kind=\"${kind}\"} ${state}"$'\n'
unit_count[${service}]=$(( ${unit_count[${service}]:-0} + 1 ))
active_count[${service}]=$(( ${active_count[${service}]:-0} + state ))
}
check_user_unit() {
local service=$1
local owner=$2
local unit=$3
local state=0
if systemctl --user --machine ${owner}@.host is-active --quiet ${unit} 2>/dev/null
then
state=1
fi
record ${service} ${unit} rootless ${state}
}
check_system_unit() {
local service=$1
local unit=$2
local state=0
if systemctl is-active --quiet ${unit} 2>/dev/null
then
state=1
fi
record ${service} ${unit} system ${state}
}
check_container() {
local service=$1
local container=$2
local state=0
if [ "$(podman container inspect -f '{% raw %}{{.State.Running}}{% endraw %}' ${container} 2>/dev/null)" = "true" ]
then
state=1
fi
record ${service} ${container} container ${state}
}
{% for service in monitored_services %}
{% for unit in service.units | default([]) %}
check_user_unit {{ service.name }} {{ service.owner }} {{ unit }}
{% endfor %}
{% for unit in service.system_units | default([]) %}
check_system_unit {{ service.name }} {{ unit }}
{% endfor %}
{% for container in service.containers | default([]) %}
check_container {{ service.name }} {{ container }}
{% endfor %}
{% endfor %}
{
echo "# HELP service_unit_active Whether a unit or container backing a service is running."
echo "# TYPE service_unit_active gauge"
printf '%s' "${units}"
echo "# HELP service_enabled Whether a service is expected to be running."
echo "# TYPE service_enabled gauge"
{% for service in monitored_services %}
echo "service_enabled{service=\"{{ service.name }}\",group=\"{{ service.group }}\"} {{ 1 if service.enabled else 0 }}"
{% endfor %}
echo "# HELP service_units_total Number of units or containers backing a service."
echo "# TYPE service_units_total gauge"
for service in "${!unit_count[@]}"
do
echo "service_units_total{service=\"${service}\"} ${unit_count[${service}]}"
done
echo "# HELP service_units_active Number of running units or containers backing a service."
echo "# TYPE service_units_active gauge"
for service in "${!active_count[@]}"
do
echo "service_units_active{service=\"${service}\"} ${active_count[${service}]}"
done
} > "${tmp}"
chmod 0644 "${tmp}"
mv "${tmp}" "${file}"
@@ -0,0 +1,71 @@
#!/bin/bash
set -uo pipefail
textfile_dir={{ node_exporter_textfile_dir }}
file=${textfile_dir}/uptimerobot.prom
tmp=${file}.$$
mkdir -p ${textfile_dir}
response=$(curl -sS -m 25 -X POST \
"https://api.uptimerobot.com/v2/getMonitors?api_key={{ uptimerobot_api_key }}&format=json&custom_uptime_ratios=1-7-30" \
2>/dev/null)
{% raw %}
printf '%s' "${response}" | python3 -c '
import json
import sys
print("# HELP uptimerobot_api_ok Whether the uptimerobot api answered successfully.")
print("# TYPE uptimerobot_api_ok gauge")
print("# HELP uptimerobot_monitor_up Whether an external monitor reports the target as up.")
print("# TYPE uptimerobot_monitor_up gauge")
print("# HELP uptimerobot_monitor_enabled Whether an external monitor is active rather than paused.")
print("# TYPE uptimerobot_monitor_enabled gauge")
print("# HELP uptimerobot_monitor_status Raw uptimerobot status, 0 paused 1 unchecked 2 up 8 seems down 9 down.")
print("# TYPE uptimerobot_monitor_status gauge")
print("# HELP uptimerobot_uptime_ratio Uptime percentage over the given window.")
print("# TYPE uptimerobot_uptime_ratio gauge")
try:
payload = json.load(sys.stdin)
except ValueError:
print("uptimerobot_api_ok 0")
sys.exit(0)
if payload.get("stat") != "ok":
print("uptimerobot_api_ok 0")
sys.exit(0)
print("uptimerobot_api_ok 1")
def escape(value):
return value.replace("\\", "\\\\").replace("\"", "\\\"")
for monitor in payload.get("monitors", []):
name = escape(str(monitor.get("friendly_name", "unknown")))
status = int(monitor.get("status", 1))
labels = "monitor=\"%s\"" % name
print("uptimerobot_monitor_status{%s} %d" % (labels, status))
print("uptimerobot_monitor_enabled{%s} %d" % (labels, 0 if status == 0 else 1))
print("uptimerobot_monitor_up{%s} %d" % (labels, 1 if status == 2 else 0))
ratios = str(monitor.get("custom_uptime_ratio", "")).split("-")
for window, ratio in zip(("1d", "7d", "30d"), ratios):
try:
print("uptimerobot_uptime_ratio{%s,window=\"%s\"} %s" % (labels, window, float(ratio)))
except ValueError:
pass
' > "${tmp}"
{% endraw %}
if [ ! -s "${tmp}" ]
then
rm -f "${tmp}"
exit 1
fi
chmod 0644 "${tmp}"
mv "${tmp}" "${file}"
@@ -0,0 +1,60 @@
#!/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}"