#!/bin/bash

# h4 script to dump a systemd unit's journal.
# Requires root privileges.
# We also cause the respective service to dump its threads, deadlocks and heap
# info on stdout (which is captured in the journal).
# We dump the journal to outputdir, so it can be included in support bundles.
#
# Usage:
#     dumpjournal servicename /output/folder
# Where servicename is one of replicator, manager, lwdproxy.

if [ "$2" == "" ]; then
   /usr/bin/echo "Usage: $0 servicename outputdir" 1>&2
   exit 1
fi

case "$1" in
   cloud)
      ;;
   manager)
      ;;
   replicator)
      ;;
   tunnel)
      ;;
   lwdproxy)
      ;;
   *)
      echo "Invalid service name: $1"
      exit 9
esac

PID=$(/usr/bin/systemctl -p MainPID show "$1" | /usr/bin/awk -F = '{print $2}')
/usr/bin/kill -3 "$PID"
/usr/bin/sleep 0.5
/usr/bin/journalctl --utc -u "$1" > "$2/$1.dmp"
/usr/bin/chown h4 "$2/$1.dmp"
