105 lines
2.7 KiB
Django/Jinja
105 lines
2.7 KiB
Django/Jinja
#!/bin/bash
|
|
|
|
textfile_dir={{ node_exporter_textfile_dir }}
|
|
|
|
metric_job=""
|
|
metric_started=0
|
|
metric_bytes=0
|
|
metric_signalled=""
|
|
|
|
metric_on_signal() {
|
|
metric_signalled=$1
|
|
exit $((128 + $1))
|
|
}
|
|
|
|
metric_install_traps() {
|
|
trap 'metric_on_signal 1' HUP
|
|
trap 'metric_on_signal 2' INT
|
|
trap 'metric_on_signal 15' TERM
|
|
}
|
|
|
|
metric_status() {
|
|
if [ -n "${metric_signalled}" ]
|
|
then
|
|
echo $((128 + metric_signalled))
|
|
else
|
|
echo "$1"
|
|
fi
|
|
}
|
|
|
|
log() {
|
|
echo "[$(date --rfc-3339=seconds)] ${metric_job}: $*"
|
|
}
|
|
|
|
metric_previous_success() {
|
|
local file=${textfile_dir}/backup_${metric_job}.prom
|
|
|
|
if [ -r "${file}" ]
|
|
then
|
|
grep "^backup_last_success_timestamp_seconds{backup_job=\"${metric_job}\"} " "${file}" | awk '{ print $2 }'
|
|
fi
|
|
}
|
|
|
|
metric_init() {
|
|
metric_job=$1
|
|
metric_started=$(date +%s)
|
|
mkdir -p ${textfile_dir}
|
|
metric_install_traps
|
|
log "start"
|
|
}
|
|
|
|
metric_archive() {
|
|
metric_bytes=$(stat -c %s "$1" 2>/dev/null || echo 0)
|
|
}
|
|
|
|
metric_finish() {
|
|
local status=$1
|
|
local now
|
|
local success
|
|
local previous
|
|
local file=${textfile_dir}/backup_${metric_job}.prom
|
|
local tmp=${file}.$$
|
|
|
|
now=$(date +%s)
|
|
previous=$(metric_previous_success)
|
|
|
|
if [ "${status}" -eq 0 ]
|
|
then
|
|
success=${now}
|
|
log "done in $((now - metric_started))s"
|
|
else
|
|
success=${previous:-0}
|
|
log "FAILED with status ${status} after $((now - metric_started))s"
|
|
fi
|
|
|
|
cat > "${tmp}" <<EOF
|
|
# HELP backup_last_run_timestamp_seconds Unix time of the last run of this backup job.
|
|
# TYPE backup_last_run_timestamp_seconds gauge
|
|
backup_last_run_timestamp_seconds{backup_job="${metric_job}"} ${now}
|
|
# HELP backup_last_success_timestamp_seconds Unix time of the last successful run of this backup job.
|
|
# TYPE backup_last_success_timestamp_seconds gauge
|
|
backup_last_success_timestamp_seconds{backup_job="${metric_job}"} ${success}
|
|
# HELP backup_last_duration_seconds Duration of the last run of this backup job.
|
|
# TYPE backup_last_duration_seconds gauge
|
|
backup_last_duration_seconds{backup_job="${metric_job}"} $((now - metric_started))
|
|
# HELP backup_last_status Exit status of the last run of this backup job, 1 is success.
|
|
# TYPE backup_last_status gauge
|
|
backup_last_status{backup_job="${metric_job}"} $([ "${status}" -eq 0 ] && echo 1 || echo 0)
|
|
# HELP backup_last_archive_bytes Size of the archive produced by the last run of this backup job.
|
|
# TYPE backup_last_archive_bytes gauge
|
|
backup_last_archive_bytes{backup_job="${metric_job}"} ${metric_bytes}
|
|
EOF
|
|
|
|
chmod 0644 "${tmp}"
|
|
mv "${tmp}" "${file}"
|
|
}
|
|
|
|
metric_guard() {
|
|
local raw=$?
|
|
local status
|
|
|
|
status=$(metric_status ${raw})
|
|
metric_finish ${status}
|
|
exit ${status}
|
|
}
|