#!/bin/sh
# $Id$

ADMINEMAIL="set.your.email@here"
DUMPDROP=/mnt/logs/asterisk/cores
GDB_SCRIPTS=/usr/share/asterisk

PID=$1
DATE=`date -Iseconds`
HOSTNAME=`hostname`
CORE=$DUMPDROP/core.`hostname`-$DATE.$PID
BACKTRACE=$DUMPDROP/bt.`hostname`-$DATE.$PID.txt
DIR=/tmp

if [ -f $DIR/core.$PID ]; then
	mv $DIR/core.$PID $CORE
elif [ -f /tmp/core ]; then
	mv $DIR/core $CORE
elif [ -f $2/core.$PID ]; then
	mv $2/core.$PID $CORE
else
	exit;
fi

chmod 644 $CORE

gdb --batch --command=$GDB_SCRIPTS/backtrace_small.gdb /usr/sbin/asterisk $CORE > $BACKTRACE 2> /dev/null
mail -s "Asterisk on $HOSTNAME died, core dumped at $CORE" $ADMINEMAIL < $BACKTRACE

/usr/bin/gdb --batch --command=$GDB_SCRIPTS/backtrace_threads.gdb /usr/sbin/asterisk $CORE >> $BACKTRACE 2> /dev/null
