#!/bin/bash
# machinotero - store description of computer hardware and configuration
# usage: sudo machinotero
# (c) 2007 Tero Karvinen www.iki.fi/karvinen

function r	# r - run command, return stderr and stdout
{
	CMD=$*;
	echo -n "$CMD; ";
	echo -e "\n=== $CMD ===" >> $OUT;
	$CMD >> $OUT 2>&1;
}

function sec	# add section heading
{
	echo -e "\n== $1 ==" >> $OUT;
}

function disk	# print disk info
{
	DISK="$1";
	if [ -e "$DISK" ]
	then
		r grep "$DISK" /etc/fstab;
		r smartctl -a "$DISK";
		for PART in $(ls $DISK?)
		do
			r tune2fs -l "$PART";
		done
		r grep "$DISK" /var/log/syslog |tail -10
	fi
}

function packages	# print package versions
{
	dpkg --list $* | grep -v "^un"
}

#function configFile	# print a small peek of a config file
#{
#	F="$1";
#	TXT=$(head "$F") $(tail "$F")
#}

function isRoot	# print error if not run with sudo/root priviledges
{
	if (( 0 != $(id -u) )); then
		echo
		echo "WARNING: You are not root. Collected information is incomplete. "
		echo
		echo "Try: sudo $(basename $0)"
		echo "WARNING: You are not root. Collected information is incomplete. " >> $OUT
	fi
}

### main

STARTED=$(date +"%Y-%m-%d_%H%M")

HOST=$(hostname)
HOST=$(echo -n $HOST|tr -c "[a-z0-9]" "_")

if [ -z $1 ]; then
	OUT=$HOST-$STARTED.txt;
else
	OUT=$1;
fi

# Erase old file (same minute) if it exists.
echo "$HOST $STARTED" > $OUT;

isRoot

echo "Machinotero http://www.iki.fi/karvinen/machinotero" >> $OUT; 
echo "Jump to next section by searching for \"== \"." >> $OUT; 

sec Overview
r uname -a
r cat /etc/lsb-release
r 'cat /var/lib/acpi-support/*-*'
r free -m
r df -h
r lshw

sec Network
r ifconfig
r iwconfig
r ip link
r ip addr
r ip route
r ip rule
r ip neigh
r ip tunnel
r ip maddr
r ip mroute
# not: ip monitor; ip xfrm

sec Software
r dpkg --list linux-image* xserver-xorg hal
r grep -v '^#' /etc/apt/sources.list
r cat /etc/apt/sources.list.d/*
r dpkg --list xresprobe dmidecode lshw vbetool smartmontools
r tail /var/log/apache/error.log
r tail /var/log/apache2/error.log

sec Specific hardware and drivers
r lsmod
r dmidecode
r lspci
r lspci -n
r lspci -v
r lsusb

sec Disks
r fdisk -l
r mount
r cat /etc/fstab
for DISK in /dev/hd{a..d} /dev/sd{a..d}
do
	disk $DISK
done

sec Display and X
r ddcprobe
# switches to non-X virtual term and back: r xresprobe nv
r vbetool vbefp panelsize

sec Long Logs, Config Files and Outputs
r dmesg | head -25
r dmesg | tail -25
r head -25 /var/log/syslog
r tail -25 /var/log/syslog
r cat /etc/X11/xorg.conf
r cat /var/log/Xorg.0.log
r lsusb -v
r packages *driver* *usb* *grub* *firefox* *thunderbird* 915resolution*

echo -e "\nDone. "

# netstat -pea -n --inet|grep LISTEN
# iptables -L

isRoot
