#!/bin/bash set -uo pipefail textfile_dir={{ node_exporter_textfile_dir }} file=${textfile_dir}/hetzner.prom tmp=${file}.$$ now=$(date +%s) ssh_opts="-p {{ hetzner_port }} -i {{ user_home }}/.ssh/id_ed25519 -o UserKnownHostsFile={{ user_home }}/.ssh/known_hosts -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=15" mkdir -p ${textfile_dir} reachable=0 body="" emit_path() { local label=$1 local remote=$2 local listing local files local bytes local pending local newest local age=-1 if ! listing=$(sudo -u {{ user }} rsync --list-only -e "ssh ${ssh_opts}" {{ hetzner_user }}@{{ hetzner_host }}:${remote}/ 2>/dev/null) then body+="hetzner_remote_listing_ok{path=\"${label}\"} 0"$'\n' return fi reachable=1 files=$(echo "${listing}" | awk '$1 ~ /^-/ && substr($5,1,1) != "." { c++ } END { print c + 0 }') bytes=$(echo "${listing}" | awk '$1 ~ /^-/ && substr($5,1,1) != "." { gsub(",", "", $2); s += $2 } END { printf "%.0f", s + 0 }') pending=$(echo "${listing}" | awk '$1 ~ /^-/ && substr($5,1,1) == "." { gsub(",", "", $2); s += $2 } END { printf "%.0f", s + 0 }') newest=$(echo "${listing}" | awk '$1 ~ /^-/ && substr($5,1,1) != "." { print $3" "$4 }' | sort -r | head -1) if [ -n "${newest}" ] then age=$((now - $(date -d "$(echo ${newest} | tr '/' '-')" +%s))) fi body+="hetzner_remote_listing_ok{path=\"${label}\"} 1"$'\n' body+="hetzner_remote_files{path=\"${label}\"} ${files}"$'\n' body+="hetzner_remote_bytes{path=\"${label}\"} ${bytes}"$'\n' body+="hetzner_remote_pending_bytes{path=\"${label}\"} ${pending}"$'\n' body+="hetzner_remote_newest_age_seconds{path=\"${label}\"} ${age}"$'\n' } {% for path in hetzner_paths %} emit_path "{{ path.dest | replace('./', '') }}" "{{ path.dest }}" {% endfor %} { echo "# HELP hetzner_reachable Whether the storage box answered at least one listing." echo "# TYPE hetzner_reachable gauge" echo "hetzner_reachable ${reachable}" echo "# HELP hetzner_last_check_timestamp_seconds Unix time of the last storage box check." echo "# TYPE hetzner_last_check_timestamp_seconds gauge" echo "hetzner_last_check_timestamp_seconds ${now}" echo "# HELP hetzner_remote_listing_ok Whether this remote path could be listed." echo "# TYPE hetzner_remote_listing_ok gauge" echo "# HELP hetzner_remote_files Number of completed files stored at this remote path." echo "# TYPE hetzner_remote_files gauge" echo "# HELP hetzner_remote_bytes Bytes of completed files stored at this remote path." echo "# TYPE hetzner_remote_bytes gauge" echo "# HELP hetzner_remote_pending_bytes Bytes of in flight partial transfers at this remote path." echo "# TYPE hetzner_remote_pending_bytes gauge" echo "# HELP hetzner_remote_newest_age_seconds Age of the newest completed file at this remote path." echo "# TYPE hetzner_remote_newest_age_seconds gauge" printf '%s' "${body}" } > "${tmp}" chmod 0644 "${tmp}" mv "${tmp}" "${file}"