~kolletzki/scripts

0fd326ff68e84ec537d8e89be505fce091f570e5 — Lukas Kolletzki 1 year, 2 months ago 5bceafe
Add systemd-unit-notify
2 files changed, 53 insertions(+), 0 deletions(-)

M README.md
A systemd-unit-notify.sh
M README.md => README.md +10 -0
@@ 18,3 18,13 @@ The system hostname is used as bot username.
```sh
$ ./discord-notify.sh "Hello, World!"
```

## systemd-unit-notify
Notifies of a systemd unit failure including the last journal entries via [Discord Webhooks](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks).
Basically `discord-notify` extended with some systemd stuff.

**Usage:**
```sh
$ ./systemd-unit-notify.sh my-unit
```


A systemd-unit-notify.sh => systemd-unit-notify.sh +43 -0
@@ 0,0 1,43 @@
#!/usr/bin/env bash
set -euo pipefail

WEBHOOK_URL=""
JOURNAL_LINES=5

if [[ ${#} -lt 1 ]] || [[ -z ${1} ]]; then
		printf "Usage: ${0} <unit>\n" >&2
			exit 1
fi

unit=${1}
host="$(cat /proc/sys/kernel/hostname)"

content=$(cat <<EOF
Systemd unit ${unit} failed.
EOF
)

if [[ ${JOURNAL_LINES} -gt 0 ]]; then
		journal="$(journalctl -u ${unit} -n ${JOURNAL_LINES} --no-hostname -o short-full -q)"
			journal="$(sed 's/^/> /' <<< "${journal}")"

				content="$(cat <<EOF
${content}

**Journal:**
${journal}
EOF
)"
fi

data=$(cat <<EOF
{
	"content": $(printf "${content}" | jq -aRs .),
	"username": "${host}"
}
EOF
)


curl -fsSL -X POST -H "Content-Type: application/json" -d "${data}" "${WEBHOOK_URL}"