Files
radxa-hat/export-prometheus.sh
2026-09-26 02:24:18 +02:00

69 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
set -eu
OUTDIR=/var/lib/node_exporter/textfile_collector
OUT="$OUTDIR/radxa_hat.prom"
TMP="$OUT.tmp.$$"
PWM=/sys/class/pwm/pwmchip1/pwm0
mkdir -p "$OUTDIR"
service_up=0
systemctl is-active --quiet radxa-hat.service && service_up=1
i2c_present=0
[ -e /dev/i2c-8 ] && i2c_present=1
temp_c="NaN"
if [ -r /sys/class/thermal/thermal_zone0/temp ]; then
raw=$(cat /sys/class/thermal/thermal_zone0/temp)
temp_c=$(awk -v v="$raw" 'BEGIN { printf "%.3f", v/1000 }')
fi
period=0
duty=0
enabled=0
polarity="unknown"
[ -r "$PWM/period" ] && period=$(cat "$PWM/period")
[ -r "$PWM/duty_cycle" ] && duty=$(cat "$PWM/duty_cycle")
[ -r "$PWM/enable" ] && enabled=$(cat "$PWM/enable")
[ -r "$PWM/polarity" ] && polarity=$(cat "$PWM/polarity")
duty_ratio="NaN"
fan_command_ratio="NaN"
if [ "$period" -gt 0 ] 2>/dev/null; then
duty_ratio=$(awk -v d="$duty" -v p="$period" 'BEGIN { printf "%.6f", d/p }')
fan_command_ratio=$(awk -v d="$duty" -v p="$period" 'BEGIN { printf "%.6f", 1-(d/p) }')
fi
cat >"$TMP" <<EOF
# HELP radxa_hat_service_up Whether radxa-hat.service is active.
# TYPE radxa_hat_service_up gauge
radxa_hat_service_up $service_up
# HELP radxa_hat_oled_i2c_present Whether the HAT I2C bus device exists.
# TYPE radxa_hat_oled_i2c_present gauge
radxa_hat_oled_i2c_present $i2c_present
# HELP radxa_hat_cpu_temperature_celsius CPU temperature used by the fan controller.
# TYPE radxa_hat_cpu_temperature_celsius gauge
radxa_hat_cpu_temperature_celsius $temp_c
# HELP radxa_hat_pwm_enabled Kernel PWM channel enable state.
# TYPE radxa_hat_pwm_enabled gauge
radxa_hat_pwm_enabled $enabled
# HELP radxa_hat_pwm_period_nanoseconds Raw PWM period from sysfs.
# TYPE radxa_hat_pwm_period_nanoseconds gauge
radxa_hat_pwm_period_nanoseconds $period
# HELP radxa_hat_pwm_duty_cycle_nanoseconds Raw PWM duty cycle from sysfs.
# TYPE radxa_hat_pwm_duty_cycle_nanoseconds gauge
radxa_hat_pwm_duty_cycle_nanoseconds $duty
# HELP radxa_hat_pwm_duty_ratio Raw sysfs duty divided by period.
# TYPE radxa_hat_pwm_duty_ratio gauge
radxa_hat_pwm_duty_ratio $duty_ratio
# HELP radxa_hat_fan_command_ratio Effective fan command ratio for the inverted HAT PWM, 0=minimum/off and 1=full.
# TYPE radxa_hat_fan_command_ratio gauge
radxa_hat_fan_command_ratio $fan_command_ratio
# HELP radxa_hat_pwm_polarity_info PWM polarity information.
# TYPE radxa_hat_pwm_polarity_info gauge
radxa_hat_pwm_polarity_info{polarity="$polarity"} 1
EOF
chmod 0644 "$TMP"
mv -f "$TMP" "$OUT"