#!/bin/bash
#
# openmamba system report generation tool
#
# Copyright (c) 2008-2013 by Silvan Calarco <silvan.calarco@mambasoft.it>
#

VERSION=20110807
TEMPFILE=`tempfile`

. ${ROOT}/etc/sysconfig/machine

[ ! "$SYSTEM_MANUFACTURER" -o "$SYSTEM_MANUFACTURER" = "System manufacturer" ]  && {
   SYSTEM_MANUFACTURER=unknown
}

[ ! "$SYSTEM_PRODUCT_NAME" -o "$SYSTEM_PRODUCT_NAME" = "System Product Name" ]  && {
   SYSTEM_PRODUCT_NAME=unknown
}

[ ! "$SYSTEM_SERIAL_NUMBER" -o "$SYSTEM_SERIAL_NUMBER" = "System Serial Number" ]  && {
   SYSTEM_SERIAL_NUMBER=unknown
}

SYSTEM_MANUFACTURER=`echo $SYSTEM_MANUFACTURER | tr ' ' _ | tr '-' _ | tr '/' _`
SYSTEM_PRODUCT_NAME=`echo $SYSTEM_PRODUCT_NAME | tr ' ' _ | tr '-' _ | tr '/' _`
SYSTEM_SERIAL_NUMBER=`echo $SYSTEM_SERIAL_NUMBER | tr ' ' _ | tr '-' _ | tr '/' _`

REPORTNAME="openmamba-report-$SYSTEM_MANUFACTURER-$SYSTEM_PRODUCT_NAME-$SYSTEM_SERIAL_NUMBER.txt"

if [ $1 ]; then
   REPORTNAME="$1/$REPORTNAME"
fi

if [ $2 ]; then
   HOME="$2"
fi

if [ $3 ]; then
   NOTE="$3"
fi

function catfile() {

   FILENAME=$1
   FILEDESC=$2

   for c in $FILENAME; do 
      cat >> $TEMPFILE << _EOF

****************************************************************
* File '$c': $FILEDESC
****************************************************************
_EOF

      cat ${ROOT}/$c >> $TEMPFILE
   done

}

function tailfile() {

   FILENAME=$1
   FILEDESC=$2

   for c in $1; do
      cat >> $TEMPFILE << _EOF

****************************************************************
* File '$c' (last 200 lines): $FILEDESC
****************************************************************
_EOF

      tail -n 200 ${ROOT}/$c >> $TEMPFILE
   done

}

function catinitramfs() {

   FILENAME=$1
   FILEDESC=$2
   
   for c in $1; do
      TMPDIR=`mktemp -d`

      cat >> $TEMPFILE << _EOF

****************************************************************
* Initramfs image '$c': $FILEDESC
****************************************************************
_EOF
      
      pushd $TMPDIR >/dev/null
      gunzip -c ${ROOT}/$c | cpio -i &>/dev/null
      ls -lR . >> $TEMPFILE
      echo "\

************************\
init script in initramfs\
************************" >> $TEMPFILE
      cat init >> $TEMPFILE
      popd >/dev/null
      [ "$TMPDIR" -a "$TMPDIR" != "/" ] && rm -rf $TMPDIR
   done

}

function catcommand() {

   CMDNAME="$1"
   FILEDESC="$2"

   cat >> $TEMPFILE << _EOF

****************************************************************
* Command '$CMDNAME': $FILEDESC
****************************************************************
_EOF

   eval $CMDNAME 2>>$TEMPFILE >> $TEMPFILE

}

echo "openmamba report generation tool $VERSION"
echo "Copyright (c) 2008-2013 by Silvan Calarco"
echo

[ $UID = 0 ] || {
   echo "Error: this program must be run as root; exiting."
   exit 1
}

echo "Gathering information..."



cat > $TEMPFILE << _EOF
****************************************************************************
* openmamba diagnostic information $VERSION for host $HOSTNAME (`date`)
****************************************************************************
_EOF

if [ "$NOTE" ]; then
echo "
REPORTED PROBLEM:
=================" >> $TEMPFILE
cat $NOTE >> $TEMPFILE
echo >> $TEMPFILE
fi

# system information
catfile /etc/openmamba-release "release information"
catfile /etc/sysconfig/machine "machine information from BIOS"
catfile /proc/cpuinfo "processor(s) information"
catfile /proc/meminfo "System memory information"
catcommand "/usr/sbin/lspci -nn" "PCI hardware information (short)"
catcommand /usr/bin/lsusb "USB hardware information"
catcommand /sbin/lsmod "Loadel kernel modules"
catcommand "rpm -qa|grep kernel-mamba" "Installed kernel packages"
catcommand "ls -l /etc/alternatives" "System alternatives"
tailfile /var/log/messages "system messages log"

# process/memory information
catcommand "top -b -n1" "Top running processes"
catcommand "ps aux" "All running processes"

# kernel boot problems
catfile /proc/cmdline "kernel startup command line"
catcommand "ls -l /boot" "boot files"
catfile /boot/grub/grub.cfg "GRUB configuration"
catfile /var/log/dmesg.log "kernel startup messages"
catcommand /bin/dmesg "last kernel messages"
catfile "/etc/modprobe.d/*" "modules configuration"
catcommand "lsinitrd /boot/initramfs-`uname -r`.img" "running kernel initial ramdisk"

# system boot problems
catfile /var/log/initd.sysinit.start "system init logfile"
catfile "/var/log/initd.3.start" "runlevel 3 init logfile"
catfile "/var/log/initd.5.start" "runlevel 5 init logfile"
catfile /var/log/postplug.log "system configuration tool"

# Suspend/Powersave
catfile /var/log/pm-powersave.log "Current powersave logfile"
catfile /var/log/pm-suspend.log "Last suspend logfile"

# Graphical subsystem
catfile /var/log/Xorg.0.log "Xorg startup log"
catfile /etc/X11/xorg.conf "Xorg configuration"
catcommand "DISPLAY=:0 /usr/bin/glxinfo" "GLX information"
tailfile $HOME/.xsession-errors "desktop session log"

# Audio
catfile "/proc/asound/card*/codec*" "Audio driver codecs"
catfile "/etc/asound.conf" "Alsa configuration"
catfile "/var/lib/alsa/asound.state" "Alsa mixer settings"

# Network
catcommand "/sbin/ifconfig -a" "Network interfaces list"
catcommand "/usr/sbin/iwconfig" "Wireless interfaces list"
catcommand "/sbin/route -n" "Routing table"
catfile "/etc/sysconfig/network" "General network configuration"
catfile "/etc/resolv.conf" "DNS configuration"

# Disks
for d in /dev/sd[a-z]; do
   catcommand "smartctl -ia $d" "S.M.A.R.T. status for device $d"
done

# Installation
catcommand "rpm -qa --last | head -n 100" "Last installed packages"
catfile "/var/log/install-*" "Installation log"
catfile /var/log/Xorg.5.log "Xorg installation test log"

# Other details
catcommand "/usr/sbin/lspci -vv" "PCI hardware information (verbose)"

cp $TEMPFILE $REPORTNAME || exit 1
chmod +r $REPORTNAME
rm -f $TEMPFILE

echo "Report saved as $REPORTNAME"
echo "Please send this report to reports@openmamba.org."
