gameservergame-servergame-servershacktoberfestdedicated-game-serversgamelinuxgsmserverbashgaminglinuxmultiplayer-game-servershell
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
250 lines
8.4 KiB
250 lines
8.4 KiB
#!/bin/bash
|
|
# Teamspeak 3
|
|
# Server Management Script
|
|
# Author: Daniel Gibbs
|
|
# Website: http://danielgibbs.co.uk
|
|
# Version: 010813
|
|
|
|
#### Variables ####
|
|
|
|
# Notification Email
|
|
# (on|off)
|
|
emailnotification="off"
|
|
email="[email protected]"
|
|
|
|
# Server names
|
|
servername="Teamspeak 3 Server"
|
|
servicename="ts3-server"
|
|
|
|
# Directorys
|
|
rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
filesdir="${rootdir}/ts3"
|
|
ini="${servicename}.ini"
|
|
|
|
# Logging
|
|
logdir="${rootdir}/${servicename}-log"
|
|
logfile="${logdir}/${servicename}.log"
|
|
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
|
|
logdays="7"
|
|
|
|
##### 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 -e "[\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" >> ${logfile}
|
|
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" >> ${logfile}
|
|
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}" >> ${logfile}
|
|
echo -e "* ${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_emailnotification(){
|
|
fn_distro
|
|
fn_uptime
|
|
fn_load
|
|
{
|
|
echo -e "=========================================\nServer information\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 "=========================================\n${servicename} statistics\n=========================================\n"
|
|
echo -e "Service: ${servicename}"
|
|
echo -e "Server: ${servername}"
|
|
echo -e "Failure reason: ${failurereason}"
|
|
echo -e "Action Taken: ${actiontaken}\n"
|
|
echo -e "=========================================\n${servicename} log summary\n=========================================\n"
|
|
}|tee ${logdir}/${servicename}-email.log > /dev/null 2>&1
|
|
tail -50 ${logfile} >> /${logdir}/${servicename}-email.log
|
|
mail -s "${subject}" ${email} < /${logdir}/${servicename}-email.log
|
|
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Sent email notification to ${email}"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} sent email notification to ${email}" >> ${logfile}
|
|
}
|
|
|
|
fn_emailtest(){
|
|
fn_rootcheck
|
|
fn_syscheck
|
|
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Email test notification: ${servername}"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} email test notification" >> ${logfile}
|
|
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] ${servicename}: email notificaion not enabled"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} email notificaion not enabled" >> ${logfile}
|
|
fi
|
|
}
|
|
|
|
fn_logmanager(){
|
|
#Log manager will active if finds logs older than $logdays
|
|
if [ `find ${logdir}/* -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" >> ${logfile}
|
|
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" >> ${logfile}
|
|
find ${logdir}/* -mtime +${logdays} |tee >> ${logfile}
|
|
count=`find ${logdir}/* -mtime +${logdays}|wc -l`
|
|
find ${logdir}/* -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" >> ${logfile}
|
|
fi
|
|
}
|
|
|
|
fn_restartserver(){
|
|
echo -e "[\e[0;32m OK \e[0;39m] Restarting ${servicename}: ${servername}"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${logfile}
|
|
fn_stopserver
|
|
sleep 1
|
|
fn_startserver
|
|
}
|
|
|
|
fn_startserver(){
|
|
if [ ! -d ${logdir} ];then
|
|
mkdir ${logdir}
|
|
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} Creating log directory ${logdir}"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} Creating log directory ${logdir}" >> ${logfile}
|
|
fi
|
|
fn_rootcheck
|
|
fn_syscheck
|
|
fn_runcheck
|
|
fn_inicheck
|
|
fn_logmanager
|
|
if [ -f ${logdir} ];then
|
|
cp ${logfile} ${logfiledate}
|
|
fi
|
|
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
|
|
${filesdir}/ts3server_startscript.sh start inifile=${ini}
|
|
sleep 3
|
|
fn_status
|
|
if [ "$ts3status" = "Server seems to have died" ] || [ "$ts3status" = "No server running (ts3server.pid is missing)" ];then
|
|
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} Server is unable to start"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} Server is unable to start" >> ${logfile}
|
|
echo -e "Check ${filesdir}/logs for failure reason" |tee ${logfile}
|
|
fi
|
|
}
|
|
|
|
fn_stopserver(){
|
|
fn_rootcheck
|
|
fn_syscheck
|
|
fn_status
|
|
if [ "$ts3status" = "No server running (ts3server.pid is missing)" ];then
|
|
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} has already stopped"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} has already stopped" >> ${logfile}
|
|
else
|
|
echo -e "[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${logfile}
|
|
${filesdir}/ts3server_startscript.sh stop inifile=${ini}
|
|
fi
|
|
}
|
|
|
|
fn_monitorserver(){
|
|
fn_rootcheck
|
|
fn_syscheck
|
|
fn_logmanager
|
|
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile}
|
|
fn_runcheck
|
|
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${logfile}
|
|
ts3status=`${filesdir}/ts3server_startscript.sh status inifile=${ini}`
|
|
if [ "$ts3status" = "Server seems to have died" ];then
|
|
echo -e "[\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" >> ${logfile}
|
|
failurereason="Server seems to have died"
|
|
elif [ "$ts3status" = "No server running (ts3server.pid is missing)" ];then
|
|
echo -e "[\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)" >> ${logfile}
|
|
failurereason="No server running (ts3server.pid is missing)"
|
|
else
|
|
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} Unknown error"
|
|
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} Unknown error" >> ${logfile}
|
|
failurereason="Unknown error"
|
|
fi
|
|
fn_restartserver
|
|
if [ "${emailnotification}" = "on" ];then
|
|
subject="${servicename} Monitor - Restarting ${servername}"
|
|
actiontaken="restarted ${servername}"
|
|
fn_emailnotification
|
|
fi
|
|
}
|
|
|
|
fn_status(){
|
|
ts3status=`${filesdir}/ts3server_startscript.sh status inifile=${ini}`
|
|
}
|
|
|
|
case "$1" in
|
|
start)
|
|
fn_startserver;;
|
|
stop)
|
|
fn_stopserver;;
|
|
restart)
|
|
fn_restartserver;;
|
|
monitor)
|
|
fn_monitorserver;;
|
|
email-test)
|
|
fn_emailtest;;
|
|
*)
|
|
echo "Usage: $0 {start|stop|restart|monitor|email-test}"
|
|
exit 1;;
|
|
esac
|
|
exit
|