Alert on failed systemd units
You’ll get: an incident naming the unit, the moment systemd marks it as failed — no polling, nothing extra to install.
Before you start
Section titled “Before you start”An ingest key from a generic InfraInbox source, shaped like iik_src_… (see Events API: curl and scripts).
1. Write the reporting script
Section titled “1. Write the reporting script”#!/usr/bin/env bashset -euo pipefailunit="$1"curl -fsS -m 10 \ -H "Authorization: Bearer ${INFRAINBOX_KEY}" \ -H "Content-Type: application/json" \ -d "{\"title\":\"${unit} failed\",\"severity\":\"ERROR\",\"type\":\"systemd.unit_failed\",\"resource\":\"unit:${unit}\",\"dedupKey\":\"systemd:${unit}\"}" \ "${INFRAINBOX_URL}"sudo chmod +x /usr/local/bin/infrainbox-unit-failed.shdedupKey is the unit’s own name, so repeated failures of the same unit fold into one incident instead of opening a new one each time.
2. Create the reporting unit
Section titled “2. Create the reporting unit”# /etc/systemd/system/infrainbox-alert@.service[Unit]Description=Report %i's failure to InfraInbox
[Service]Type=oneshotEnvironment=INFRAINBOX_URL=https://infrainbox.example.com/v1/eventsEnvironment=INFRAINBOX_KEY=iik_src_XXXXXXXXXXXXExecStart=/usr/local/bin/infrainbox-unit-failed.sh %i3. Point a unit at it
Section titled “3. Point a unit at it”For any unit you want watched, add a drop-in (sudo systemctl edit <unit>) or edit its file directly:
[Unit]OnFailure=infrainbox-alert@%n.serviceReload and test:
sudo systemctl daemon-reloadsudo systemd-run --unit=infrainbox-test-fail /bin/falseCheck it works
Section titled “Check it works”- The incident appears in InfraInbox naming
infrainbox-test-fail.service. systemctl status infrainbox-alert@infrainbox-test-fail.serviceshows the reporting run itself exited cleanly.