Skip to content
Permalink
Browse files

Send systemd failure email via external script

Dealing with this entirely from the service file as a one-liner was
awkward and brittle. Also expand the notification to include journal
entries from the past 12 hours.
  • Loading branch information
lovett committed Nov 29, 2019
1 parent d092e20 commit 0a6ae58e87c59653b644ed3ad2b749b86e7c3d1d
@@ -2,8 +2,8 @@
Description=Medley Failure Notifier

[Service]
Type=exec
ExecStart=/usr/bin/systemctl status %i | /usr/bin/mail -s "[medley] %i service failed" {{ admin_email }}
Type=oneshot
ExecStart={{ project_root }}/venv/bin/pthon {{ project_root }}/scripts/failmail.py %i {{ admin_email }}

# Local Variables:
# mode: conf
@@ -16,12 +16,15 @@
append=yes
groups="{{ project_user }},audio"

- name: Put ansible user in application group
- name: Set application user groups
become: true
user:
name="{{ ansible_user }}"
group="{{ project_user }}"
group="{{ item }}"
append=yes
with_items:
- "{{ project_user }}"
- "systemd-journal"

- name: Create application directories
become: true
@@ -0,0 +1,32 @@
"""Send an email when one of the application's systemd services failes."""

import email.mime.text
import subprocess
import sys

try:
SERVICE = sys.argv[1]
RECIPIENT = sys.argv[2]
except IndexError:
print(
"Did not receive service and recipient as arguments",
file=sys.stderr
)
sys.exit(2)

JOURNAL_PROCESS = subprocess.run(
("/usr/bin/env", "journalctl", "-u", "medley", "--since", "-12 hours"),
capture_output=True,
text=True,
check=True
)

MESSAGE = email.mime.text.MIMEText(JOURNAL_PROCESS.stdout)
MESSAGE["TO"] = RECIPIENT
MESSAGE["Subject"] = f"[medley] {SERVICE} failed"

subprocess.run(
("/usr/sbin/sendmail", "-t", "-oi"),
input=MESSAGE.as_bytes(),
check=True
)

0 comments on commit 0a6ae58

Please sign in to comment.
You can’t perform that action at this time.