13 Sep

Ad-hoc Notifications for Systemd Services

We have recently migrated some of our production servers to a new linux with systemd. I wanted to get an email everytime there was an error in the httpd service. I am sure there should be a proper process and a fancy way, but for me this worked like a charm getting a notification on my phone:

#!/usr/bin/env bash
 
# depends the following tools:
# jq
# mailx
 
sender_address="sender@mydomain.com"
to_address="addressee@mydomain.com"
unit_to_follow=httpd
 
set -e
set -u
 
while read log; do
        message=$(echo "$log" | jq -r ".MESSAGE")
        timestamp=$(echo "$log" | jq -r ".__REALTIME_TIMESTAMP")
        unit=$(echo "$log" | jq -r "._SYSTEMD_UNIT")
        timestamp_in_seconds=$(( $timestamp/1000000 ))
        human_readable_timestamp=$(date -d @${timestamp_in_seconds})
        echo "Sending mail ${human_readable_timestamp}"
        mailx -s "A warning from ${unit}" -r "${sender_address}" "${to_address}" <<HERE
${human_readable_timestamp}
${message}
HERE
 
done < <(journalctl -f -u ${unit_to_follow} --priority 1..3 -o json )

All I did was run this using nohup.