#!/bin/bash

set -e

stamp=$(date '+%F_%T.%N_%z' | tr -d ':-')
script_pid="$$"

Now=$(date +%Y%m%d_%H%M%S |tr -d '\n' ;echo -n "$$";echo $HOSTNAME)

Dir="debug_info.$stamp-$script_pid-$HOSTNAME"
TDir="/tmp/$Dir"
mkdir -p "$TDir"

{ echo "$Now"; uname -a; } > "$TDir"/Now

collect_some_junk() {
	echo "=== linux release ==="
	for f in /etc/*rel*; do echo "$f=[$(< $f)]"; done
}

show_kernelmod_params() {
	local m p cwd=$(pwd)
	cd /sys/module

	for m in *; do
		for p in $m/parameters/*; do
			[ -e "$p" ]  || continue
			set +e
			[ -r "$p" ] && echo "$p=[$(< $p)]" || echo "$p EXISTS"
			set -e
		done
	done | sed -e '1,$s/\/parameters\//./' -

	cd $cwd
}

exec > "$TDir"/OUTPUT.script.log 2>&1

set +e
set -x

collect_some_junk >> "$TDir"/Now

rpm -qa > "$TDir"/OUTPUT.rpm-qa.txt
 
lsmod > "$TDir"/OUTPUT.lsmod.txt
lsblk > "$TDir"/OUTPUT.lsblk.txt
df > "$TDir"/OUTPUT.df.txt
mount > "$TDir"/OUTPUT.mount.txt
 
show_kernelmod_params > "$TDir"/OUTPUT.show_kernelmod_params.txt 

cat /sys/kernel/debug/tracing/trace > "$TDir"/OUTPUT.kernel_debug_trace.txt

dmesg > "$TDir"/OUTPUT.dmesg.txt 2>&1 

zpool events > "$TDir"/OUTPUT.zpool_events.txt 2>&1 ; echo $?
zpool events -v > "$TDir"/OUTPUT.zpool_events_verbose.txt 2>&1 ; echo $?

lctl dl > "$TDir"/OUTPUT.lctl_dl.txt 2>&1 ; echo $?
lctl dk > "$TDir"/OUTPUT.lctl_dk.txt 2>&1 ; echo $?

cp /var/log/messages "$TDir"/OUTPUT.messages

cd /tmp
zip -rm9 "$Dir".zip "$Dir" >/dev/tty 2>&1

# EOF #
