#!/bin/bash
# -*- ENCODING: UTF-8 -*-

# picafmonitor
#
# Collect hardware and operating system information that is useful to log system operation crashes.
#
# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: bash, coreutils, gawk | mawk | original-awk, grep, gzip, tar
# Recommends: dstat, hddtemp | lm-sensors, iotop, kmod, procps, sysvinit-utils (<<2.88dsf-59.3) | init-system-helpers, util-linux


### SCRIPT VARIABLES ########################################

CNAME="picafmonitor"
VERSION="1.3.1"
TITLE="'picafmonitor'"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=picainfo
TEXTDOMAINDIR=/usr/share/locale/

## Write errors to log
ERRORLOG="$HOME/.${CNAME}.log"
#if [ -e "$ERRORLOG" ] ; then
#	mv -f $ERRORLOG ${ERRORLOG}.ant
#fi
#if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi

for i in /etc/pica-global.dist /etc/pica-global.orig /etc/pica-global /etc/pica-global.local ~/.pica-global ~/pica-global ; do
	if [ -f "$i" ] ; then
	cat "$i"
	source "$i"
	source <(cat $i | sed -e "s/=\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)/=Y/g" -e "s/=\(No\|no\|n\)/=N/g" -e "s/=\"\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)\"/=Y/g" -e "s/=\"\(No\|no\|n\)\"/=N/g")
	fi
done
if [ "$DEBUG" = "Y" ] ; then
	set -xv
	DEBUG="Y"
else
	ERRORLOG="/tmp/.${CNAME}_$(id -nu).log"
	if [ -e "$ERRORLOG" ] ; then mv -f $ERRORLOG ${ERRORLOG}.ant ; fi
	if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi
fi
if [ "$DEBUG" = "" ] ; then DEBUG="N" ; fi
if [ "$DEBUG" != "N" ] ; then
exec > >(tee -a "$ERRORLOG") 2>&1
echo "$0" "$*" >> "$ERRORLOG"
echo "env:" >> "$ERRORLOG"
env >> "$ERRORLOG"
echo "set:" >> "$ERRORLOG"
set >> "$ERRORLOG"
#else
#exec 2>>"$ERRORLOG"
fi

# Description:
BDESCRIP=$"Collect hardware and operating system information that is useful to log system operation crashes."
LDESCRIP=$"$TITLE is a tool to collect hardware and operating system information that is useful to log system operation crashes.\n\nThis application does NOT check the health status of the internal disk or RAM! \n\nTo launch this application automatically every 1 minute:\n\ncrontab -e\n\n*/1 * * * * /usr/bin/picafmonitor\n\nWhen restarting the computer after an operating system crash, the information of interest is collected in /tmp/picafmonitor.tar.gzip"

# Documentation:
docu_info () {
echo "$CNAME ($VERSION) - $BDESCRIP"
echo 
echo $"Usage:" $CNAME [$"OPTIONS"]
echo 
echo $"Options:"
echo -e "$ODESCRIP"
echo 
echo $"'man $CNAME' for more information."
echo 
}
ODESCRIP=" -x""\t"$"Show help documentation."

while getopts x OPTION ; do
	case $OPTION in
		x )   docu_info ; exit 0 ;;
	esac
done


### SCRIPT ########################################

exec 1>/dev/null 2>&1

picafmonitor_rec () {
date +%Y-%m-%d_%H:%M:%S >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo dstat >> ${COLDIR}/picafmonitor.log_"$i"
timeout 0.1 dstat -c -d -g -i -l -m -n -p -r -s -t -y >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo free >> ${COLDIR}/picafmonitor.log_"$i"
free -m >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo df >> ${COLDIR}/picafmonitor.log_"$i"
df -h | tee -a "$ERRORLOG" | grep -aEi "^/dev/" >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo sensors >> ${COLDIR}/picafmonitor.log_"$i"
sensors >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo hddtemp >> ${COLDIR}/picafmonitor.log_"$i"
hddtemp /dev/sd* >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo w >> ${COLDIR}/picafmonitor.log_"$i"
w >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo iotop >> ${COLDIR}/picafmonitor.log_"$i"
timeout 0.2 iotop -okbtq >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo top >> ${COLDIR}/picafmonitor.log_"$i"
top -n 1 -b >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo psaux >> ${COLDIR}/picafmonitor.log_"$i"
ps aux >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo dmesg >> ${COLDIR}/picafmonitor.log_"$i"
dmesg >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo service >> ${COLDIR}/picafmonitor.log_"$i"
service --status-all >> ${COLDIR}/picafmonitor.log_"$i"
printf "\n------------------------------------------------------------------------------------------\n\n" >> ${COLDIR}/picafmonitor.log_"$i"
echo lsmod >> ${COLDIR}/picafmonitor.log_"$i"
lsmod >> ${COLDIR}/picafmonitor.log_"$i"
cp -a -L --remove-destination /var/log/syslog ${COLDIR}/
cp -a -L --remove-destination /var/log/messages ${COLDIR}/
cp -a -L --remove-destination /var/log/kern.log ${COLDIR}/
cp -a -L --remove-destination /var/log/daemon.log ${COLDIR}/
cp -a -L --remove-destination /var/log/Xorg.0.log ${COLDIR}/
cp -a -L --remove-destination /var/log/Xorg.1.log ${COLDIR}/
cp -a -L --remove-destination /var/log/Xorg.* ${COLDIR}/
cp -a -L --remove-destination /var/log/user.log ${COLDIR}/
cp -a -L --remove-destination /var/log/dmesg ${COLDIR}/
cp -a -L --remove-destination /var/log/debug ${COLDIR}/
cp -a -L --remove-destination /var/log/boot ${COLDIR}/
cp -a -L --remove-destination /var/crash ${COLDIR}/
cp -a -L --remove-destination /home/${USER1}/.xsession ${COLDIR}/
cp -a -L --remove-destination /home/${USER1}/.xsession-errors ${COLDIR}/
cp -a -L --remove-destination /etc/X11/xorg.conf ${COLDIR}/
}

DATEORIG=$(date +%s)
USER1=$(users | awk '{print $1}' | head -n 1)
COLDIR="/home/${USER1}/picafmonitor"

if [ -d ${COLDIR} ] && [ "$( stat -c %U ${COLDIR} )" != "${USER1}" ] ; then chown $(id -nu $USER1):$(id -ng $USER1) "$COLDIR" ; fi
if [ -d ${COLDIR}_COP ] && [ "$( stat -c %U ${COLDIR}_COP )" != "${USER1}" ] ; then chown $(id -nu $USER1):$(id -ng $USER1) "$COLDIR"_COP ; fi

if [ ! -e /tmp/picafmonitor.tar.gzip ] ; then
	if [ -d ${COLDIR} ] && [ "$(ls ${COLDIR})" != "" ] ; then tar -czvf /tmp/picafmonitor.tar.gzip ${COLDIR} ; sync ; chown $(id -nu $USER1):$(id -ng $USER1) /tmp/picafmonitor.tar.gzip
	elif [ -d ${COLDIR}_COP ] && [ "$(ls ${COLDIR}_COP)" != "" ] ; then tar -czvf /tmp/picafmonitor.tar.gzip ${COLDIR}_COP ; sync ; chown $(id -nu $USER1):$(id -ng $USER1) /tmp/picafmonitor.tar.gzip
	else
	true
	fi
fi

if [ -d ${COLDIR} ] && [ "$(ls ${COLDIR})" != "" ] ; then rm -rf ${COLDIR}_COP ; mv -f ${COLDIR} ${COLDIR}_COP ; fi

mkdir -pv ${COLDIR}

#while [ "$(echo $(date +%s) - $DATEORIG | bc -l)" -lt 60 ] ; do i=$(date +%s) ; picafmonitor_rec ; sync ; sleep 0.5 ; done
while [ "$(expr $(date +%s) - $DATEORIG)" -lt 60 ] ; do i=$(date +%s) ; picafmonitor_rec ; sync ; sleep 0.5 ; done

chown $(id -nu $USER1):$(id -ng $USER1) "$COLDIR"

exit 0
