#!/bin/bash

# Sangoma Tech Support Script

# April 09 2011 Moises Silva <moy@sangoma.com> -- initial version

# This script is aimed to collect all kind of information that can be useful for
# a tech support engineer to get an idea of what is running in the customer
# computer and give an initial assesment of the problems 

# NOTE: Try to avoid printing errors, either log them or discard them by sending them to /dev/null, some commands
# are meant to fail in some Linux distributions

# TODO
# - Make major directories (/usr/local/freeswitch for example) configurable thru cmd line option --fsdir=/opt/freeswitch
# - Add support for Ubuntu, Debian and SuSE (ie: retrieve list of packages installed and check specific distro version)

debugdir=/tmp/sangoma-debug
linuxdir=$debugdir/linux
wandir=$debugdir/wanpipe
fsdir=$debugdir/freeswitch
astdir=$debugdir/asterisk
nsgdir=$debugdir/nsg
logfile=$debugdir/log.txt

# create a tech support debug directory
mkdir -p $debugdir
mkdir -p $linuxdir
mkdir -p $wandir
mkdir -p $fsdir
mkdir -p $astdir
mkdir -p $nsgdir

today=`date "+%Y-%B-%d-%H-%M-%S"`
output="wancollect-$today.tar.gz"

######### Linux Settings ##########
echo 'unknown' > $linuxdir/distribution.txt
# if it is centos
if [ -f /etc/redhat-release ] ; then
	cp /etc/redhat-release $linuxdir/distribution.txt >> $logfile 2>&1
fi

# general Linux name and kernel start options
uname -a &> $linuxdir/uname.txt
cat /proc/cmdline &> $linuxdir/kernel-cmdline.txt

# many problems can be due to a full hard-drive
mount &> $linuxdir/mount.txt
df --si &> $linuxdir/disk-usage.txt

# processes
ps axu &> $linuxdir/processes.txt

# devices
ls -slh /dev/ &> $linuxdir/dev.txt

# kernel ring buffer
dmesg &> $linuxdir/dmesg.txt

# kernel logging
cp /var/log/messages $linuxdir/messages.txt >> $logfile 2>&1

# networking
route &> $linuxdir/route.txt
ifconfig -a &> $linuxdir/ifconfig.txt
if [ -d /etc/sysconfig/network-scripts ] ; then
	for script in `ls /etc/sysconfig/network-scripts`
	do
		echo "File: $script" >> $linuxdir/network-devices.txt
		cat /etc/sysconfig/network-scripts/$script >> $linuxdir/network-devices.txt
		echo -e "\n" > $linuxdir/network-devices.txt
	done
fi
iptables -L &> $linuxdir/iptables.txt

# libraries
ls /usr/lib &> $linuxdir/usr-lib.txt
ls /usr/local/lib &> $linuxdir/usr-local-lib.txt
ls /usr/lib64 &> $linuxdir/usr-lib64.txt
ls /usr/local/lib64 &> $linuxdir/usr-local-lib64.txt

# kernel modules loaded and available
lsmod &> $linuxdir/lsmod.txt
find /lib/modules &> $linuxdir/lib-modules.txt

# common place for sources
ls /usr/src &> $linuxdir/usr-src.txt

# packets installed (redhat only)
rpm -qa &> $linuxdir/rpm-qa.txt

##### WANPIPE ######

# get hwprobe to see what is installed
wanrouter hwprobe verbose &> $wandir/hwprobe.txt
wanrouter version &> $wandir/version.txt
cat /var/log/wanrouter &> $wandir/wanrouter-log.txt
cp -r /etc/wanpipe $wandir/etc-wanpipe >> $logfile 2>&1
cat /usr/include/wanpipe/wanpipe_version.h &> $wandir/wanpipe_version.h

##### Asterisk #####
cp -r /etc/asterisk $astdir/etc-asterisk >> $logfile 2>&1 
cp /var/log/asterisk/messages $astdir/messages.txt >> $logfile 2>&1
cp /var/log/asterisk/full $astdir/full.txt >> $logfile 2>&1
cp /etc/zaptel.conf $astdir/ >> $logfile 2>&1
cp /etc/dahdi/system.conf $astdir/ >> $logfile 2>&1
asterisk -rx 'core show version' $astdir &> $astdir/core-show-version.txt
asterisk -rx 'show version' $astdir &> $astdir/show-version.txt
asterisk -rx 'zap show channels' &> $astdir/zap-show-channels.txt
asterisk -rx 'dahdi show channels' &> $astdir/dahdi-show-channels.txt

##### FreeSWITCH #####
cp -r /usr/local/freeswitch/conf $fsdir/conf >> $logfile 2>&1
cp -r /usr/local/freeswitch/log  $fsdir/log >> $logfile 2>&1

##### nsg #####
cp -r /usr/local/nsg/conf $nsgdir/conf >> $logfile 2>&1
#cp -r /usr/local/nsg/log/ $nsgdir/log >> $logfile 2>&1
mkdir $fsdir/log
cp -r /usr/local/nsg/log/sangomagw.log*  $fsdir/log/ >> $logfile 2>&1
cp -r /usr/local/nsg/log/freeswitch.*  $fsdir/log/ >> $logfile 2>&1



tar -C $debugdir/.. -czf $output `basename $debugdir`

echo "Done!, please email $output to techdesk@sangoma.com! You may want to delete $debugdir after this."

