#!/bin/sh
#
# OTRSwatch.sh - check the database for dead SQL-Queries and
# stop them
#
# Author: dennis.schwan@poet.de
# Version 0.1 / 14.09.2006
# Version 0.2 / 14.09.2006: added User/PW for Database and corrected PATHs

#config (DB-Root Password and Time a query may live)
DBUSER=
DBPW=
TIME=120
LOG=/var/log/OTRSwatch.log
DATE=`date`
VER=0.2
MYSQLADMIN=/usr/bin/mysqladmin

if [ "$1" == "-v" ] ; then
	echo "OTRSwatch.sh Version $VER"
fi
#start
# check for dead queries
PROCESSID=`$MYSQLADMIN -u $DBUSER -p$DBPW processlist |grep Copying |awk -F"|" '{print $2}' | sed -e "s/ //"`
# if PROCESSID != 0.... determine running time
if [ $PROCESSID ] ; then
        RUN=`$MYSQLADMIN -u $DBUSER -p$DBPW processlist |grep $PROCESSID |awk -F"|" '{print $7}' | sed -e "s/ //"`
        # if RUN > TIME then kill process
        if [ $RUN -ge $TIME ] ; then
                echo "$DATE: Killing $PROCESSID" >> $LOG
                $MYSQLADMIN -u $DBUSER -p$DBPW kill $PROCESSID
		exit 0
        else
                echo "$DATE: PID $PROCESSID: Zeitschwelle nicht erreicht" >> $LOG
                exit 0
        fi
else
        #debug
        #echo "Kein toter Prozess" >> $LOG
        exit 0
fi
