#!/bin/bash

# olpc-log-0.6
# Copyright (C) 2008 OLPC
# Author: Giannis Galanis <echo gxlxnis@lxptop.org | sed s/x/a/g>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

source /usr/share/olpc-utils/olpc-utils-functions

timestamp=`date +20%y-%m-%d.%H-%M-%S`
serial=`get_ofw_file /serial-number`
target="logtemp"
sugarpath="/home/olpc/.sugar/default/logs"

function general {
	[ -d $target ] && rm -r $target
	mkdir $target

	uptime &> $target/uptime.out;echo -n =
	free -t &> $target/free.out; echo -n =
	ps auxfwww > $target/ps.out;echo -n =
	olpc-netstatus -i &> $target/info;echo -n =
	df -h &> $target/df.out;echo -n =
	du -k / 2>/dev/null | sort -nr > $target/du.out; echo -n =
	rpm -qa &> $target/rpm.out; echo -n =
}

function sugar {
	echo -n '|'
	mkdir $target/sugar
	cp $sugarpath/*.log $target/sugar;echo -n =
	find $sugarpath -mindepth 1 -maxdepth 1 -type d | xargs cp -r -t $target/sugar;echo -n =
}

function kernel {
	echo -n '|'
	mkdir $target/kernel
	cp /var/log/messages $target/kernel;echo -n =
	cp /var/log/Xorg.0.log $target/kernel;echo -n =
	dmesg > $target/kernel/dmesg.out;echo -n =
	slabtop -o > $target/kernel/slabtop.out;echo -n =
}

function proc {
	echo -n '|'
	mkdir $target/proc
	cp /proc/meminfo $target/proc;echo -n =
	cp /proc/slabinfo $target/proc;echo -n =
	cp /proc/vmstat $target/proc;echo -n =
	cp /proc/mounts $target/proc;echo -n =
	for pid in `(cd /proc; find . -mindepth 1 -maxdepth 1 -regex '.*/[0-9]+$')`; do
		mkdir $target/proc/$pid
		cp -r -t $target/proc/$pid /proc/$pid/{cmdline,environ,limits,mounts,net,oom_adj,oom_score,sched,smaps,stat,status} &> /dev/null
		echo -n .
	done
	echo -n =
}

function net {
	echo -n '|'
	mkdir $target/net

	cat /home/olpc/.sugar/default/config &> $target/net/config;echo -n =
	cat /etc/resolv.conf &> $target/net/resolv.conf;echo -n =
	cat /home/olpc/.sugar/default/nm/networks.cfg &> $target/net/networks.cfg;echo -n =
	cat /etc/NetworkManager/mesh-start &> $target/net/mesh-start;echo -n =

	ifconfig &> $target/net/ifconfig.out;echo -n =
	iwconfig &> $target/net/iwconfig.out;echo -n =
	route -n &> $target/net/route.out;echo -n =
	netstat -apT &> $target/net/netstat.out;echo -n =

	olpc-netstatus -n &> $target/net/olpc-netstatus.out;echo -n =
	olpc-mesh &> $target/net/olpc-mesh.out;echo -n =
	olpc-xos sugar &> $target/net/xos;echo -n =
}

function help {
cat <<EOF
Usage: olpc-log [OPTION]
Store important logs in a tarball named by time and serial number.
Specify which logs to store using the options. Default is all logs.

  general output                   :  olpc-netstatus -i output
                                      free -t output
                                      ps auxfwww output
                                      uptime output
                                      rpm -qa output
                                      df -h output
                                      du -k | sort -nr output

Options:

  -n     network logs              :  ifconfig output
                                      iwconfig output
                                      route output
                                      netstat -apT output
                                      olpc-netstatus output
                                      olpc-mesh output
                                      presenceservice XO list

                                      /home/olpc/.sugar/default/config
                                      /etc/resolv.conf
                                      /home/olpc/.sugar/default/nm/networks.cfg
                                      /etc/NetworkManager/mesh-start

  -s     sugar and activities logs :  $sugarpath/datastore.log
                                      $sugarpath/telepathy-salut.log
                                      $sugarpath/telepathy-gabble.log
                                      $sugarpath/presenceservice.log
                                      $sugarpath/shellservice.log
                                      $sugarpath/shell.log
                                      all activity logs
                                      all 'old' sugar logs (in case of X crashes)

  -k     kernel related logs       :  dmesg output
                                      /var/log/messages
                                      /var/log/Xorg.0.log

  -p     process related logs      :  meminfo
                                      slabinfo
                                      vmstat
                                      ps auxfwww output
                                      /proc/*/cmdline
                                              environ
                                              limits
                                              mounts
                                              net/
                                              oom_adj
                                              oom_score
                                              sched
                                              smaps
                                              stat
                                              status

EOF
}

if [ "$1" = "--help" ]; then
	help
	exit 0
fi

param=`echo " $*"|sed s/" "//g|sed s/-//g`

echo -ne "Collecting: ["

general

if [ -z "$param" ]; then kernel;sugar;net;proc
elif ! echo " $param"|grep -E "(n|k|s|p)" > /dev/null
then
	echo "Warning: all logs were selected"
	echo "For help use 'olpc-log' --help"
	sugar;kernel;net;proc
else

	echo "$param"|grep n > /dev/null && net
	echo "$param"|grep k > /dev/null && kernel
	echo "$param"|grep s > /dev/null && sugar
	echo "$param"|grep p > /dev/null && proc

	rest=`echo $param|sed s/[nksp]//g`
	if [ -n "$rest" ];then
		echo -e "Warning: Parameters \"$rest\" were ignored"
		echo "For help use 'olpc-log --help'"
	fi
fi

echo -n '|'

cd $target
tar -cjf ../logs.$serial.$timestamp.tar.bz2 *;echo -n ==
cd ../

rm -r logtemp

echo -e "]\033[1C"
