@ -3,8 +3,7 @@
# Server Management Script
# Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk
# Version: 221113
# Version: 080214
#### Variables ####
# Notification Email
@ -17,11 +16,23 @@ gamename="Teamspeak 3"
servername="Teamspeak 3 Server"
servicename="ts3-server"
# Directorys
rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fn_parms(){
parms="-game fof -strictportbind -ip ${ip} -port ${port} +clientport ${clientport} +tv_port ${sourcetvport} +map ${defaultmap} +servercfgfile ${servercfg} -maxplayers ${maxplayers}"
}
# Directories
rootdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
selfname="$(basename $0)"
lockselfname=".${servicename}.lock"
filesdir="${rootdir}/serverfiles"
ini="${servicename}.ini"
backupdir="backups"
systemdir="${filesdir}"
executabledir="${filesdir}"
executable="./ts3server_startscript.sh"
servercfgdir="${filesdir}"
servercfg="${servicename}.ini"
servercfgfullpath="${servercfgdir}/${servercfg}"
defaultcfg="${servercfgfullpath}" #
backupdir="${rootdir}/backups"
# Logging
logdays="7"
@ -35,297 +46,31 @@ scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S'
##### Script #####
# Do not edit
# unless you know
# what you are doing
fn_rootcheck(){
if [ `whoami` = "root" ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Script will not run as root!"
exit
fi
}
fn_syscheck(){
if [ ! -e ${filesdir} ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Cannot access ${filesdir}: No such directory"
exit
fi
}
fn_runcheck(){
fn_status
if [ "$ts3status" = "Server is running" ]; then
echo -en "\r[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
sleep 0.5
echo -en "\n"
exit
fi
}
fn_inicheck(){
if [ ! -e ${filesdir}/${ini} ]; then
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: ${filesdir}/${ini} is missing"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: ${servername} ${filesdir}/${ini} is missing" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Creating blank ${ini}"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Creating blank ${ini}" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${ini} can remain blank by default."
touch ${filesdir}/${ini}
fi
}
fn_distro(){
arch=$(uname -m)
kernel=$(uname -r)
if [ -f /etc/lsb-release ]; then
os=$(lsb_release -s -d)
elif [ -f /etc/debian_version ]; then
os="Debian $(cat /etc/debian_version)"
elif [ -f /etc/redhat-release ]; then
os=$(cat /etc/redhat-release)
else
os="$(uname -s) $(uname -r)"
fi
}
fn_uptime(){
uptime=$(</proc/uptime)
uptime=${uptime%%.*}
minutes=$(( uptime/60%60 ))
hours=$(( uptime/60/60%24 ))
days=$(( uptime/60/60/24 ))
}
fn_load(){
load=$(uptime | awk -F 'load average' '{ print $2 }')
}
fn_backupserver(){
fn_rootcheck
fn_syscheck
backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
echo ""
echo "${gamename} Backup"
echo "============================"
echo ""
echo "The following backup will be created."
echo ""
echo "${backupdir}/${backupname}.tar.gz"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
fn_status
if [ "$ts3status" = "Server is running" ]; then
echo -e "\e[0;31mWARNING!\e[0;39m ${servicename} is currently running"
while true; do
read -p "Would you like to stop ${servicename} while running the backup? [y/N]" yn
case $yn in
[Yy]* ) fn_stopserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi
echo -en "[ .... ] Starting ${servicename}: ${servername} backup"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} backup" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} backup"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} backup" >> ${scriptlog}
sleep 1
echo -en "\n"
cd ${rootdir}
mkdir ${backupdir} > /dev/null 2>&1
tar -cvzf ${backupdir}/${backupname}.tar.gz --exclude ${backupdir} *
echo -en "\r${servicename} backup complete"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
}
fn_emailnotification(){
fn_distro
fn_uptime
fn_load
{
echo -e "========================================\n${servicename} details\n========================================\n"
echo -e "Service: ${servicename}"
echo -e "Game: ${gamename}"
echo -e "Server: ${servername}"
echo -e "Failure reason: ${failurereason}"
echo -e "Action Taken: ${actiontaken}\n"
echo -e "========================================\nServer details\n========================================\n"
echo -e "Date: $(date)"
echo -e "Distro: ${os}"
echo -e "Arch: ${arch}"
echo -e "Kernel: ${kernel}"
echo -e "Hostname: $HOSTNAME"
echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
echo -e "Avg Load${load}\n"
echo -e "========================================\nLogs\n========================================\n"
echo -e "Script log\n===================\n"
}|tee ${scriptlogdir}/${servicename}-email.log > /dev/null 2>&1
tail -25 ${scriptlog} >> ${emaillog}
if [ ! -z ${consolelog} ];then
echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
tail -25 ${consolelog} >> ${emaillog}
fi
if [ ! -z ${gamelogdir} ];then
echo -e "\n\nServer log\n====================\n" >> ${emaillog}
tail ${gamelogdir}/*|grep -v "==>"|sed '/^$/d'|tail -25 >> ${emaillog}
fi
mail -s "${subject}" ${email} < ${emaillog}
echo -en "[\e[0;36m INFO \e[0;39m] Sent email notification to ${email}"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Sent email notification to ${email}" >> ${scriptlog}
}
fn_emailtest(){
fn_rootcheck
fn_syscheck
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then
subject="${servicename} Email Test Notification - Testing ${servername}"
failurereason="Testing ${servicename} email notification"
actiontaken="Sent test email...hello is this thing on?"
fn_emailnotification
else
echo -e "[\e[0;31m FAIL \e[0;39m] Email notification not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notification not enabled" >> ${scriptlog}
fn_runfunction(){
# Functions are downloaded and run with this function
if [ ! -f "${rootdir}/functions/${functionfile}" ]; then
cd "${rootdir}"
if [ ! -d "functions" ]; then
mkdir functions
fi
cd functions
echo -e "loading ${functionfile}...\c"
wget -N --no-check-certificate /dev/null https://raw.githubusercontent.com/dgibbs64/linuxgameservers/master/functions/${functionfile} 2>&1 | grep -F HTTP | cut -c45-
chmod +x "${functionfile}"
cd "${rootdir}"
sleep 1
fi
sleep 0.5
echo -en "\n"
source "${rootdir}/functions/${functionfile}"
}
fn_logmanager(){
#Log manager will active if finds logs older than $logdays
if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "[\e[0;32m OK \e[0;39m] Starting log cleaner"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Starting log cleaner" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] Removing logs older than ${logdays} days"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Removing logs older than ${logdays} days" >> ${scriptlog}
find ${scriptlogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
count=$(find ${scriptlogdir}/* -mtime +${logdays}|wc -l)
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
echo -e "[\e[0;36m INFO \e[0;39m] Log cleaner removed ${count} log files"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Log cleaner removed ${count} log files" >> ${scriptlog}
fi
}
fn_restartserver(){
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
fn_stopserver
fn_startserver
fn_functions(){
# Functions are defined in fn_functions.
functionfile="${FUNCNAME}"
fn_runfunction
}
fn_startserver(){
if [ ! -d ${scriptlogdir} ];then
mkdir ${rootdir}/log
mkdir ${scriptlogdir}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} Creating log directorys ${scriptlogdir}"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: ${servername} Creating log directorys ${scriptlogdir}" >> ${scriptlog}
fi
if [ ! -h ${rootdir}/log/server ]; then
ln -sv ${gamelogdir} ${rootdir}/log/server
fi
fn_rootcheck
fn_syscheck
fn_runcheck
fn_inicheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
echo -en "[ .... ] Starting ${servicename}: ${servername}"
${filesdir}/ts3server_startscript.sh start inifile=${ini} > /dev/null 2>&1
sleep 1
fn_status
if [ "$ts3status" = "Server seems to have died" ] || [ "$ts3status" = "No server running (ts3server.pid is missing)" ];then
echo -en "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename}: Failed to start ${servername}"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: ${servername} Server is unable to start" >> ${scriptlog}
echo -e "Check ${filesdir}/logs for failure reason" |tee ${scriptlog}
else
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Started ${servername}" >> ${scriptlog}
fi
sleep 0.5
echo -en "\n"
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
fn_status
echo -en "[ .... ] Stopping ${servicename}: ${servername}"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
sleep 0.5
if [ "$ts3status" = "No server running (ts3server.pid is missing)" ];then
echo -en "\r[\e[0;31m FAIL \e[0;39m] Stopping ${servicename}: ${servername} is already stopped"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${scriptlog}
else
echo -en "\r[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: Stopped ${servername}" >> ${scriptlog}
${filesdir}/ts3server_startscript.sh stop inifile=${ini} > /dev/null 2>&1
fi
echo -en "\n"
}
fn_monitorserver(){
fn_rootcheck
fn_syscheck
fn_logmanager
echo -en "[ .... ] Monitor is checking ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is checking ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
echo -en "\r[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${scriptlog}
sleep 1
fn_status
if [ "$ts3status" = "Server seems to have died" ];then
echo -en "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} Server seems to have died"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: ${servername} Server seems to have died" >> ${scriptlog}
failurereason="Server seems to have died"
echo -en "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} No server running (ts3server.pid is missing)"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: ${servername} No server running (ts3server.pid is missing)" >> ${scriptlog}
failurereason="No server running (ts3server.pid is missing)"
else
echo -en "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} Unknown error"
echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: ${servername} Unknown error" >> ${scriptlog}
failurereason="Unknown error"
fi
sleep 0.5
echo -en "\n"
if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Restarting ${servername}"
actiontaken="restarted ${servername}"
fn_emailnotification
fi
sleep 0.5
echo -en "\n"
fn_restartserver
}
fn_status(){
ts3status=$(${filesdir}/ts3server_startscript.sh status inifile=${ini})
}
fn_functions
case "$1" in
start)
fn_startserver;;
stop)
fn_stopserver;;
restart)
fn_restartserver;;
backup)
fn_backupserver;;
monitor)
fn_monitorserver;;
email-test)
fn_emailtest;;
*)
echo "Usage: $0 {start|stop|restart|backup|monitor|email-test}"
exit 1;;
esac
exit
getopt=$1
fn_getopt