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}"