#!/usr/bin/bash
# Collect post-crash information from local node.

. /usr/share/shaman/functions

verify_caller

MIN_FREE_MB_DIR=128


# Sync all relevant log files' contents onto disk

tmo=60

{
	if [ -n "$CLUSTER_NAME" ]; then
		sync -d /var/log/vstorage/$CLUSTER_NAME/vstorage-mount*.blog
	fi
	sync -f /var/spool/abrt
} < /dev/null >& /dev/null &

wpid=$!
wait_timed "$wpid"


# Dump vstorage-mount core
if [ -n "$CLUSTER_NAME" ]; then

	corepfx="$WORK_DIR/vstorage-mount"
	tmo=1200

	{
		if coredumpctl info -1 /usr/bin/vstorage-mount > "${corepfx}.coreinfo"; then
			coref="$(awk '/^Storage:/ {print $2}' "${corepfx}.coreinfo")"
			if [ -n "$coref" ] && [ -f "$coref" ]; then
				mtime="$(stat -c '%Y' "$coref")"
				curtime="$(date +%s)"
				if [ $((mtime + 300)) -ge "$curtime" ]; then
					if ! ln "$coref" "${corepfx}.corez"; then
						cp -a "$coref" "${corepfx}.corez"
					fi
					sync -f "${corepfx}.corez"
				fi
			fi
		fi
	} < /dev/null >& /dev/null &

	wpid=$!
	wait_timed "$wpid"

	# Snapshot vstorage logs
	tmo=30

	{
		# Try hardlink first; fallback to full copy if cross-FS
		if ! cp -l -f "/var/log/vstorage/$CLUSTER_NAME"/vstorage-mount*.blog "$WORK_DIR"; then
			cp -a -f "/var/log/vstorage/$CLUSTER_NAME"/vstorage-mount*.blog "$WORK_DIR"
		fi
		sync -f "$WORK_DIR"
	} < /dev/null >& /dev/null &

	wpid=$!
	wait_timed "$wpid"

fi

exit 0
