Interview Prep/Shell Scripting/Scenarios: health checks, monitoring and alerts

You want your script's results on Grafana dashboards without running a new service. How?

AdvancedScenarioScenarios: health checks, monitoring and alerts
Answer

Situation read → If node_exporter runs on the host, its textfile collector exposes metrics from .prom files.

What I'd do →

dir=/var/lib/node_exporter/textfile_collector
tmp=$(mktemp "$dir/.backup.prom.XXXXXX")
cat > "$tmp" <<EOF
# HELP opskit_backup_last_success_timestamp_seconds Last successful backup.
# TYPE opskit_backup_last_success_timestamp_seconds gauge
opskit_backup_last_success_timestamp_seconds $(date +%s)
# HELP opskit_backup_size_bytes Size of the last backup.
# TYPE opskit_backup_size_bytes gauge
opskit_backup_size_bytes $size
EOF
chmod 0644 "$tmp"
mv -f -- "$tmp" "$dir/opskit_backup.prom"
  1. Start node_exporter with --collector.textfile.directory=/var/lib/node_exporter/textfile_collector.
  2. Write atomically (temp file + mv) so the exporter never reads half a file.
  3. Alert in Prometheus: time() - opskit_backup_last_success_timestamp_seconds > 93600.

Why → You reuse existing scraping, dashboards and alerting with a few lines of shell.

Interview tip → Mention the atomic write; partially written .prom files cause scrape errors.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts