|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright OpenFaaS Ltd 2025 |
| 4 | + |
| 5 | +# This script is for use with Droplets created on DigitalOcean, it will |
| 6 | +# ensure that systemd-journald is configured to log to disk and that |
| 7 | +# rsyslog is configured to read from systemd-journald. |
| 8 | + |
| 9 | +# Without this change, no logs will be available in the journal, and only |
| 10 | +# /var/log/syslog will be populated. |
| 11 | + |
| 12 | +set -e |
| 13 | + |
| 14 | +echo "Checking systemd-journald logs..." |
| 15 | +JOURNAL_STATUS=$(journalctl --no-pager -n 10 2>&1) |
| 16 | + |
| 17 | +if echo "$JOURNAL_STATUS" | grep -q "No journal files were found"; then |
| 18 | + echo "No journal files found. Fixing logging configuration..." |
| 19 | +else |
| 20 | + echo "Journald appears to be logging. No changes needed." |
| 21 | + exit 0 |
| 22 | +fi |
| 23 | + |
| 24 | +# Backup original config before making changes |
| 25 | +sudo cp /etc/systemd/journald.conf /etc/systemd/journald.conf.bak |
| 26 | + |
| 27 | +# Ensure Storage is persistent |
| 28 | +sudo sed -i '/^#Storage=/c\Storage=persistent' /etc/systemd/journald.conf |
| 29 | + |
| 30 | +# Ensure logs are not forwarded only to syslog |
| 31 | +sudo sed -i '/^#ForwardToSyslog=/c\ForwardToSyslog=no' /etc/systemd/journald.conf |
| 32 | + |
| 33 | +# Restart systemd-journald |
| 34 | +echo "Restarting systemd-journald..." |
| 35 | +sudo systemctl restart systemd-journald |
| 36 | + |
| 37 | +# Check if rsyslog already loads imjournal |
| 38 | +if ! grep -q 'module(load="imjournal")' /etc/rsyslog.conf; then |
| 39 | + echo "Adding imjournal module to rsyslog..." |
| 40 | + echo 'module(load="imjournal" StateFile="/var/lib/rsyslog/imjournal.state")' | sudo tee -a /etc/rsyslog.conf |
| 41 | +fi |
| 42 | + |
| 43 | +# Restart rsyslog to apply changes |
| 44 | +echo "Restarting rsyslog..." |
| 45 | +sudo systemctl restart rsyslog |
| 46 | + |
| 47 | +echo "Done. Checking if logs appear in journald..." |
| 48 | +journalctl --no-pager -n 10 |
0 commit comments