Browse Source

Major update 011013

New Feature: Backup
New Feature: Console
Depreciated: SCREEN command
Added: Tmux to replace SCREEN
Improved: Logging
Improved: UI changes
pull/8/merge
Daniel Gibbs 12 years ago
parent
commit
e2165fd7a2
  1. 525
      CounterStrike/csserver
  2. 525
      CounterStrikeGlobalOffensive/csgoserver
  3. 519
      CounterStrikeSource/cssserver
  4. 516
      DayOfDefeatSource/dodsserver
  5. 520
      HalfLife2Deathmatch/hl2dmserver
  6. 666
      KillingFloor/kfserver
  7. 518
      Left4Dead2/l4d2server
  8. 702
      RedOrchestra/roserver
  9. 520
      TeamFortress2/tf2server
  10. 515
      TheHiddenSource/thsserver
  11. 578
      UnrealTournament2004/ut2k4server
  12. 575
      UnrealTournament99/ut99server

525
CounterStrike/csserver

@ -3,18 +3,19 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
# Notification Email # Notification Email
# (on|off) # (on|off)
emailnotification="off" emailnotification="on"
email="[email protected]" email="[email protected]"
# Start vars # Start vars
defaultmap="de_aztec" defaultmap="de_aztec"
port="27015" port="27015"
clientport="27005" clientport="27005"
parms="-game cstrike +map ${defaultmap} -strictportbind -port ${port} +clientport ${clientport}" parms="-game cstrike +map ${defaultmap} -strictportbind -port ${port} +clientport ${clientport}"
@ -28,20 +29,28 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
servercfg="${filesdir}/cstrike/server.cfg" servercfg="${filesdir}/cstrike/server.cfg"
backupdir="${rootdir}/backups"
# Server names # Server names
servicename="cs-server" servicename="cs-server"
gamename="Counter Strike 1.6" gamename="Counter Strike 1.6"
engine="goldsource" engine="goldsource"
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'` servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'` rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
# Logging # Logging
logdir="${rootdir}/log/script"
gamelogdir="${filesdir}/cstrike/logs"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
gamelogdir="${filesdir}/cstrike/logs"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
##### Script ##### ##### Script #####
# Do not edit # Do not edit
@ -62,49 +71,184 @@ if [ ! -e ${filesdir} ];then
fi fi
} }
fn_serverquery(){ fn_runcheck(){
# uses serverquery.py to directly query the server tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
# detects if the server locks up if [ ${tmuxwc} -eq 1 ];then
if [ -f serverquery.py ];then fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected" echo -en "\r[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING" sleep 0.5
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile} echo -en "\n"
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1` exit
sleep 1 fi
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then }
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} fn_logmanager(){
sleep 1 # log manager will active if finds logs older than ${logdays}
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!" if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile} 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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${filesdir}
./hlds_run ${parms} -debug
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1 sleep 1
fn_restartserver tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} sleep 0.5
sleep 1 while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_runcheck(){ fn_details(){
# already running check echo ""
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo "${gamename} Server Details"
if [ ${pidwc} -eq 1 ];then echo "============================"
fn_serverquery echo ""
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" echo "${servername} Ports"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} echo "======================="
exit servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
# multiple of same instance check rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
elif [ ${pidwc} -ge 2 ];then echo "Ports the server is currently using"
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile} echo "DIRECTION DESCRIPTION PORT"
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}" echo "INBOUND Game/Rcon Port ${port}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile} if [ "${engine}" = "source" ];then
fn_stopserver echo "INBOUND SourceTV Port ${sourcetvport}"
exit
fi fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
echo ""
} }
fn_distro(){ fn_distro(){
@ -138,7 +282,13 @@ fn_distro
fn_uptime fn_uptime
fn_load fn_load
{ {
echo -e "=========================================\nServer information\n=========================================\n" 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 "Date: $(date)"
echo -e "Distro: ${os}" echo -e "Distro: ${os}"
echo -e "Arch: ${arch}" echo -e "Arch: ${arch}"
@ -146,139 +296,95 @@ echo -e "Kernel: ${kernel}"
echo -e "Hostname: $HOSTNAME" echo -e "Hostname: $HOSTNAME"
echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m" echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
echo -e "Avg Load${load}\n" echo -e "Avg Load${load}\n"
echo -e "=========================================\n${servicename} statistics\n=========================================\n" echo -e "========================================\nLogs\n========================================\n"
echo -e "Service: ${servicename}" echo -e "Script log\n===================\n"
echo -e "Server: ${servername}" }|tee ${scriptlogdir}/${servicename}-email.log > /dev/null 2>&1
echo -e "Failure reason: ${failurereason}" tail -25 ${scriptlog} >> ${emaillog}
echo -e "Action Taken: ${actiontaken}\n" if [ ! -z ${consolelog} ];then
echo -e "=========================================\n${servicename} log summary\n=========================================\n" echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
}|tee ${logdir}/${servicename}-email.log > /dev/null 2>&1 tail -25 ${consolelog} >> ${emaillog}
tail -50 ${logfile} >> /${logdir}/${servicename}-email.log fi
mail -s "${subject}" ${email} < /${logdir}/${servicename}-email.log if [ ! -z ${gamelogdir} ];then
echo -e "[\e[0;36m INFO \e[0;39m] Sent email notification to ${email}" echo -e "\n\nServer log\n====================\n" >> ${emaillog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile} 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_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${scriptlog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Email Test Notification - Testing ${servername}" subject="${servicename} Email Test Notification - Testing ${servername}"
failurereason="Testing ${servicename} email notification" failurereason="Testing ${servicename} email notification"
actiontaken="Sent test email...hello is this thing on?" actiontaken="Sent test email...hello is this thing on?"
fn_emailnotification fn_emailnotification
else else
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo -e "[\e[0;31m FAIL \e[0;39m] Email notification not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notification not enabled" >> ${scriptlog}
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 fi
sleep 0.5
echo -en "\n"
} }
fn_restartserver(){ fn_serverquery(){
echo -e "[\e[0;32m OK \e[0;39m] Restarting ${servicename}: ${servername}" # uses serverquery.py to directly query the server
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${logfile} # detects if the server locks up
fn_stopserver if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1 sleep 1
fn_startserver if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
} echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
fn_startserver(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ./hlds_run ${parms}
sleep 1 sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
if [ ${pidwc} -eq 0 ];then echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}" sleep 1
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile} fn_restartserver
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${logfile}
else else
echo -e "[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5
echo -e ""
echo -e "Use for identifying server issues only!"
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1 sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" fi
sleep 3 fi
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
./hlds_run ${parms} -debug
} }
fn_monitorserver(){ fn_monitorserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager fn_logmanager
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo -en "[ .... ] Monitor is checking ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is checking ${servername}" >> ${scriptlog}
sleep 0.5
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then if [ "${updatecheck}" = "0" ];then
fn_runcheck fn_runcheck
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo -en "\r[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}" subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped" failurereason="${servicename} process was not running"
actiontaken="restarted ${servername}" actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification fn_emailnotification
fi fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver fn_startserver
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi fi
} }
@ -286,80 +392,60 @@ fn_updateserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${rootdir} cd ${rootdir}
cd steamcmd cd steamcmd
./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile} ./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${scriptlog}
} }
fn_details(){ fn_restartserver(){
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
echo "${gamename} Server Details" fn_stopserver
echo "============================" fn_startserver
echo "" }
echo "${servername} Ports"
echo "=======================" fn_stopserver(){
if [ "${engine}" = "unreal2" ];then fn_rootcheck
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') fn_syscheck
queryport=$((${gameport} + 1)) pid=$(tmux list-sessions 2>&1|awk '{print $1}')
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') echo -en "[ .... ] Stopping ${servicename}: ${servername}"
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') sleep 0.5
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') if [ "${pid}" == "failed" ];then
echo "Ports the server is currently using" echo -en "\r[\e[0;31m FAIL \e[0;39m] Stopping ${servicename}: ${servername} is already stopped"
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${scriptlog}
echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi
if [ "${appid}" == "215360" ];then
echo "OUTBOUND Master Server port 28852 TCP/UDP n/a"
else else
echo "OUTBOUND Master Server port 28900/28902 TCP/UDP n/a" tmux kill-session -t ${servicename}
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}
fi fi
if [ "${appid}" ];then sleep 0.5
if [ "${appid}" == "223250" ];then echo -en "\n"
echo "OUTBOUND Steam Port 20610 UDP n/a" }
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${filesdir}
tmux new-session -d -s ${servicename} "./hlds_run ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${scriptlog}
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
fi echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername}" >> ${scriptlog}
fi
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}"
echo ""
echo "${servername} WebAdmin"
echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}"
echo ""
echo "Config file"
echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo ""
echo "DIRECTION DESCRIPTION PORT"
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then
echo "INBOUND SourceTV Port ${sourcetvport}"
fi fi
echo "OUTBOUND Client Port ${clientport}" sleep 0.5
echo "" echo -en "\n"
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
fi
echo ""
} }
# #
@ -440,8 +526,6 @@ echo "================================="
rm -f ${servercfg} rm -f ${servercfg}
cd ${rootdir}/steamcmd cd ${rootdir}/steamcmd
mkdir -v ${filesdir} mkdir -v ${filesdir}
mkdir -v ${rootdir}/log
mkdir -v ${logdir}
touch install.txt touch install.txt
chmod 0600 install.txt chmod 0600 install.txt
echo "login anonymous" > install.txt echo "login anonymous" > install.txt
@ -482,6 +566,7 @@ echo "================================="
echo -e "pausable 0" echo -e "pausable 0"
echo -e "" echo -e ""
echo -e "// default server name. Change to \"Bob's Server\", etc." echo -e "// default server name. Change to \"Bob's Server\", etc."
echo -e "hostname \"${servername}\"" echo -e "hostname \"${servername}\""
echo -e "" echo -e ""
echo -e "// maximum client movement speed " echo -e "// maximum client movement speed "
@ -512,7 +597,11 @@ echo "================================="
}|tee ${servercfg} > /dev/null 2>&1 }|tee ${servercfg} > /dev/null 2>&1
sleep 1 sleep 1
echo "" echo ""
echo "Setting up logging" echo "Creating log directorys"
mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
if [ ! -h ${rootdir}/log/server ]; then if [ ! -h ${rootdir}/log/server ]; then
ln -sv ${gamelogdir} ${rootdir}/log/server ln -sv ${gamelogdir} ${rootdir}/log/server
else else
@ -548,16 +637,20 @@ case "$1" in
fn_startserver;; fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install)
fn_install;;
debug)
fn_debugserver;;
email-test) email-test)
fn_emailtest;; fn_emailtest;;
details) details)
fn_details;; fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug)
fn_debugserver;;
install)
fn_install;;
*) *)
echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install}"
exit 1;; exit 1;;
esac esac
exit exit

525
CounterStrikeGlobalOffensive/csgoserver

@ -3,14 +3,14 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
# Notification Email # Notification Email
# (on|off) # (on|off)
emailnotification="off" emailnotification="on"
email="[email protected]" email="[email protected]"
# Start vars # Start vars
defaultmap="de_dust" defaultmap="de_dust"
@ -29,20 +29,28 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
servercfg="${filesdir}/csgo/cfg/server.cfg" servercfg="${filesdir}/csgo/cfg/server.cfg"
backupdir="${rootdir}/backups"
# Server names # Server Details
servicename="csgo-server" servicename="csgo-server"
gamename="Counter Strike: Global Offensive" gamename="Counter Strike: Global Offensive"
engine="source" engine="source"
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'` servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'` rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
# Logging # Logging
logdir="${rootdir}/log/script"
gamelogdir="${filesdir}/csgo/logs"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
gamelogdir="${filesdir}/csgo/logs"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
##### Script ##### ##### Script #####
# Do not edit # Do not edit
@ -63,49 +71,184 @@ if [ ! -e ${filesdir} ];then
fi fi
} }
fn_serverquery(){ fn_runcheck(){
# uses serverquery.py to directly query the server tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
# detects if the server locks up if [ ${tmuxwc} -eq 1 ];then
if [ -f serverquery.py ];then fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected" echo -en "\r[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING" sleep 0.5
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile} echo -en "\n"
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1` exit
sleep 1 fi
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then }
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} fn_logmanager(){
sleep 1 # log manager will active if finds logs older than ${logdays}
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!" if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile} 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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${filesdir}
./srcds_run ${parms} -debug
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1 sleep 1
fn_restartserver tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} sleep 0.5
sleep 1 while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_runcheck(){ fn_details(){
# already running check echo ""
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo "${gamename} Server Details"
if [ ${pidwc} -eq 1 ];then echo "============================"
fn_serverquery echo ""
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" echo "${servername} Ports"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} echo "======================="
exit servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
# multiple of same instance check rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
elif [ ${pidwc} -ge 2 ];then echo "Ports the server is currently using"
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile} echo "DIRECTION DESCRIPTION PORT"
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}" echo "INBOUND Game/Rcon Port ${port}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile} if [ "${engine}" = "source" ];then
fn_stopserver echo "INBOUND SourceTV Port ${sourcetvport}"
exit
fi fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
echo ""
} }
fn_distro(){ fn_distro(){
@ -139,7 +282,13 @@ fn_distro
fn_uptime fn_uptime
fn_load fn_load
{ {
echo -e "=========================================\nServer information\n=========================================\n" 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 "Date: $(date)"
echo -e "Distro: ${os}" echo -e "Distro: ${os}"
echo -e "Arch: ${arch}" echo -e "Arch: ${arch}"
@ -147,139 +296,95 @@ echo -e "Kernel: ${kernel}"
echo -e "Hostname: $HOSTNAME" echo -e "Hostname: $HOSTNAME"
echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m" echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
echo -e "Avg Load${load}\n" echo -e "Avg Load${load}\n"
echo -e "=========================================\n${servicename} statistics\n=========================================\n" echo -e "========================================\nLogs\n========================================\n"
echo -e "Service: ${servicename}" echo -e "Script log\n===================\n"
echo -e "Server: ${servername}" }|tee ${scriptlogdir}/${servicename}-email.log > /dev/null 2>&1
echo -e "Failure reason: ${failurereason}" tail -25 ${scriptlog} >> ${emaillog}
echo -e "Action Taken: ${actiontaken}\n" if [ ! -z ${consolelog} ];then
echo -e "=========================================\n${servicename} log summary\n=========================================\n" echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
}|tee ${logdir}/${servicename}-email.log > /dev/null 2>&1 tail -25 ${consolelog} >> ${emaillog}
tail -50 ${logfile} >> /${logdir}/${servicename}-email.log fi
mail -s "${subject}" ${email} < /${logdir}/${servicename}-email.log if [ ! -z ${gamelogdir} ];then
echo -e "[\e[0;36m INFO \e[0;39m] Sent email notification to ${email}" echo -e "\n\nServer log\n====================\n" >> ${emaillog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile} 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_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${scriptlog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Email Test Notification - Testing ${servername}" subject="${servicename} Email Test Notification - Testing ${servername}"
failurereason="Testing ${servicename} email notification" failurereason="Testing ${servicename} email notification"
actiontaken="Sent test email...hello is this thing on?" actiontaken="Sent test email...hello is this thing on?"
fn_emailnotification fn_emailnotification
else else
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo -e "[\e[0;31m FAIL \e[0;39m] Email notification not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notification not enabled" >> ${scriptlog}
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 fi
sleep 0.5
echo -en "\n"
} }
fn_restartserver(){ fn_serverquery(){
echo -e "[\e[0;32m OK \e[0;39m] Restarting ${servicename}: ${servername}" # uses serverquery.py to directly query the server
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${logfile} # detects if the server locks up
fn_stopserver if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1 sleep 1
fn_startserver if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
} echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
fn_startserver(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ${filesdir}/srcds_run ${parms}
sleep 1 sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
if [ ${pidwc} -eq 0 ];then echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}" sleep 1
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile} fn_restartserver
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${logfile}
else else
echo -e "[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5
echo -e ""
echo -e "Use for identifying server issues only!"
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1 sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" fi
sleep 3 fi
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
${filesdir}/srcds_run ${parms} -debug
} }
fn_monitorserver(){ fn_monitorserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager fn_logmanager
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo -en "[ .... ] Monitor is checking ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is checking ${servername}" >> ${scriptlog}
sleep 0.5
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then if [ "${updatecheck}" = "0" ];then
fn_runcheck fn_runcheck
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo -en "\r[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}" subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped" failurereason="${servicename} process was not running"
actiontaken="restarted ${servername}" actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification fn_emailnotification
fi fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver fn_startserver
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi fi
} }
@ -287,80 +392,60 @@ fn_updateserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${rootdir} cd ${rootdir}
cd steamcmd cd steamcmd
./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile} ./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${scriptlog}
} }
fn_details(){ fn_restartserver(){
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
echo "${gamename} Server Details" fn_stopserver
echo "============================" fn_startserver
echo "" }
echo "${servername} Ports"
echo "=======================" fn_stopserver(){
if [ "${engine}" = "unreal2" ];then fn_rootcheck
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') fn_syscheck
queryport=$((${gameport} + 1)) pid=$(tmux list-sessions 2>&1|awk '{print $1}')
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') echo -en "[ .... ] Stopping ${servicename}: ${servername}"
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') sleep 0.5
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') if [ "${pid}" == "failed" ];then
echo "Ports the server is currently using" echo -en "\r[\e[0;31m FAIL \e[0;39m] Stopping ${servicename}: ${servername} is already stopped"
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${scriptlog}
echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi
if [ "${appid}" == "215360" ];then
echo "OUTBOUND Master Server port 28852 TCP/UDP n/a"
else else
echo "OUTBOUND Master Server port 28900/28902 TCP/UDP n/a" tmux kill-session -t ${servicename}
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}
fi fi
if [ "${appid}" ];then sleep 0.5
if [ "${appid}" == "223250" ];then echo -en "\n"
echo "OUTBOUND Steam Port 20610 UDP n/a" }
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${filesdir}
tmux new-session -d -s ${servicename} "./srcds_run ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${scriptlog}
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" 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 fi
fi sleep 0.5
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}" echo -en "\n"
echo ""
echo "${servername} WebAdmin"
echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}"
echo ""
echo "Config file"
echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo ""
echo "DIRECTION DESCRIPTION PORT"
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then
echo "INBOUND SourceTV Port ${sourcetvport}"
fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
fi
echo ""
} }
# #
@ -440,8 +525,6 @@ echo "Installing ${gamename} Server"
echo "=================================" echo "================================="
cd ${rootdir}/steamcmd cd ${rootdir}/steamcmd
mkdir -v ${filesdir} mkdir -v ${filesdir}
mkdir -v ${rootdir}/log
mkdir -v ${logdir}
touch install.txt touch install.txt
chmod 0600 install.txt chmod 0600 install.txt
echo "login anonymous" > install.txt echo "login anonymous" > install.txt
@ -488,7 +571,11 @@ echo "================================="
}|tee ${servercfg} > /dev/null 2>&1 }|tee ${servercfg} > /dev/null 2>&1
sleep 1 sleep 1
echo "" echo ""
echo "Setting up logging" echo "Creating log directorys"
mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
if [ ! -h ${rootdir}/log/server ]; then if [ ! -h ${rootdir}/log/server ]; then
ln -sv ${gamelogdir} ${rootdir}/log/server ln -sv ${gamelogdir} ${rootdir}/log/server
else else
@ -524,16 +611,20 @@ case "$1" in
fn_startserver;; fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install)
fn_install;;
debug)
fn_debugserver;;
email-test) email-test)
fn_emailtest;; fn_emailtest;;
details) details)
fn_details;; fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug)
fn_debugserver;;
install)
fn_install;;
*) *)
echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install}"
exit 1;; exit 1;;
esac esac
exit exit

519
CounterStrikeSource/cssserver

@ -3,7 +3,7 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
@ -29,21 +29,27 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
servercfg="${filesdir}/cstrike/cfg/server.cfg" servercfg="${filesdir}/cstrike/cfg/server.cfg"
backupdir="${rootdir}/backups"
# Server Details # Server Details
servicename="css-server" servicename="css-server"
gamename="Counter Strike: Source" gamename="Counter Strike: Source"
engine="source" engine="source"
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'` servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'` rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
# Logging # Logging
logdir="${rootdir}/log/script"
gamelogdir="${filesdir}/cstrike/logs"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
gamelogdir="${filesdir}/cstrike/logs"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
##### Script ##### ##### Script #####
# Do not edit # Do not edit
@ -64,49 +70,184 @@ if [ ! -e ${filesdir} ];then
fi fi
} }
fn_serverquery(){ fn_runcheck(){
# uses serverquery.py to directly query the server tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
# detects if the server locks up if [ ${tmuxwc} -eq 1 ];then
if [ -f serverquery.py ];then fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected" echo -en "\r[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING" sleep 0.5
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile} echo -en "\n"
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1` exit
sleep 1 fi
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then }
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} fn_logmanager(){
sleep 1 # log manager will active if finds logs older than ${logdays}
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!" if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile} 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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${filesdir}
./srcds_run ${parms} -debug
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1 sleep 1
fn_restartserver tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} sleep 0.5
sleep 1 while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_runcheck(){ fn_details(){
# already running check echo ""
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo "${gamename} Server Details"
if [ ${pidwc} -eq 1 ];then echo "============================"
fn_serverquery echo ""
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" echo "${servername} Ports"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} echo "======================="
exit servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
# multiple of same instance check rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
elif [ ${pidwc} -ge 2 ];then echo "Ports the server is currently using"
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile} echo "DIRECTION DESCRIPTION PORT"
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}" echo "INBOUND Game/Rcon Port ${port}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile} if [ "${engine}" = "source" ];then
fn_stopserver echo "INBOUND SourceTV Port ${sourcetvport}"
exit
fi fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
echo ""
} }
fn_distro(){ fn_distro(){
@ -140,7 +281,13 @@ fn_distro
fn_uptime fn_uptime
fn_load fn_load
{ {
echo -e "=========================================\nServer information\n=========================================\n" 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 "Date: $(date)"
echo -e "Distro: ${os}" echo -e "Distro: ${os}"
echo -e "Arch: ${arch}" echo -e "Arch: ${arch}"
@ -148,139 +295,95 @@ echo -e "Kernel: ${kernel}"
echo -e "Hostname: $HOSTNAME" echo -e "Hostname: $HOSTNAME"
echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m" echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
echo -e "Avg Load${load}\n" echo -e "Avg Load${load}\n"
echo -e "=========================================\n${servicename} statistics\n=========================================\n" echo -e "========================================\nLogs\n========================================\n"
echo -e "Service: ${servicename}" echo -e "Script log\n===================\n"
echo -e "Server: ${servername}" }|tee ${scriptlogdir}/${servicename}-email.log > /dev/null 2>&1
echo -e "Failure reason: ${failurereason}" tail -25 ${scriptlog} >> ${emaillog}
echo -e "Action Taken: ${actiontaken}\n" if [ ! -z ${consolelog} ];then
echo -e "=========================================\n${servicename} log summary\n=========================================\n" echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
}|tee ${logdir}/${servicename}-email.log > /dev/null 2>&1 tail -25 ${consolelog} >> ${emaillog}
tail -50 ${logfile} >> /${logdir}/${servicename}-email.log fi
mail -s "${subject}" ${email} < /${logdir}/${servicename}-email.log if [ ! -z ${gamelogdir} ];then
echo -e "[\e[0;36m INFO \e[0;39m] Sent email notification to ${email}" echo -e "\n\nServer log\n====================\n" >> ${emaillog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile} 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_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${scriptlog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Email Test Notification - Testing ${servername}" subject="${servicename} Email Test Notification - Testing ${servername}"
failurereason="Testing ${servicename} email notification" failurereason="Testing ${servicename} email notification"
actiontaken="Sent test email...hello is this thing on?" actiontaken="Sent test email...hello is this thing on?"
fn_emailnotification fn_emailnotification
else else
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo -e "[\e[0;31m FAIL \e[0;39m] Email notification not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notification not enabled" >> ${scriptlog}
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 fi
sleep 0.5
echo -en "\n"
} }
fn_restartserver(){ fn_serverquery(){
echo -e "[\e[0;32m OK \e[0;39m] Restarting ${servicename}: ${servername}" # uses serverquery.py to directly query the server
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${logfile} # detects if the server locks up
fn_stopserver if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1 sleep 1
fn_startserver if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
} echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
fn_startserver(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ${filesdir}/srcds_run ${parms}
sleep 1 sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
if [ ${pidwc} -eq 0 ];then echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}" sleep 1
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile} fn_restartserver
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${logfile}
else else
echo -e "[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5
echo -e ""
echo -e "Use for identifying server issues only!"
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1 sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" fi
sleep 3 fi
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
${filesdir}/srcds_run ${parms} -debug
} }
fn_monitorserver(){ fn_monitorserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager fn_logmanager
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo -en "[ .... ] Monitor is checking ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is checking ${servername}" >> ${scriptlog}
sleep 0.5
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then if [ "${updatecheck}" = "0" ];then
fn_runcheck fn_runcheck
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo -en "\r[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}" subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped" failurereason="${servicename} process was not running"
actiontaken="restarted ${servername}" actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification fn_emailnotification
fi fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver fn_startserver
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi fi
} }
@ -288,80 +391,60 @@ fn_updateserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${rootdir} cd ${rootdir}
cd steamcmd cd steamcmd
./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile} ./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${scriptlog}
} }
fn_details(){ fn_restartserver(){
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
echo "${gamename} Server Details" fn_stopserver
echo "============================" fn_startserver
echo "" }
echo "${servername} Ports"
echo "=======================" fn_stopserver(){
if [ "${engine}" = "unreal2" ];then fn_rootcheck
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') fn_syscheck
queryport=$((${gameport} + 1)) pid=$(tmux list-sessions 2>&1|awk '{print $1}')
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') echo -en "[ .... ] Stopping ${servicename}: ${servername}"
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') sleep 0.5
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') if [ "${pid}" == "failed" ];then
echo "Ports the server is currently using" echo -en "\r[\e[0;31m FAIL \e[0;39m] Stopping ${servicename}: ${servername} is already stopped"
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${scriptlog}
echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi
if [ "${appid}" == "215360" ];then
echo "OUTBOUND Master Server port 28852 TCP/UDP n/a"
else else
echo "OUTBOUND Master Server port 28900/28902 TCP/UDP n/a" tmux kill-session -t ${servicename}
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}
fi fi
if [ "${appid}" ];then sleep 0.5
if [ "${appid}" == "223250" ];then echo -en "\n"
echo "OUTBOUND Steam Port 20610 UDP n/a" }
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${filesdir}
tmux new-session -d -s ${servicename} "./srcds_run ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${scriptlog}
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" 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 fi
fi sleep 0.5
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}" echo -en "\n"
echo ""
echo "${servername} WebAdmin"
echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}"
echo ""
echo "Config file"
echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo ""
echo "DIRECTION DESCRIPTION PORT"
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then
echo "INBOUND SourceTV Port ${sourcetvport}"
fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
fi
echo ""
} }
# #
@ -441,8 +524,6 @@ echo "Installing ${gamename} Server"
echo "=================================" echo "================================="
cd ${rootdir}/steamcmd cd ${rootdir}/steamcmd
mkdir -v ${filesdir} mkdir -v ${filesdir}
mkdir -v ${rootdir}/log
mkdir -v ${logdir}
touch install.txt touch install.txt
chmod 0600 install.txt chmod 0600 install.txt
echo "login anonymous" > install.txt echo "login anonymous" > install.txt
@ -489,7 +570,11 @@ echo "================================="
}|tee ${servercfg} > /dev/null 2>&1 }|tee ${servercfg} > /dev/null 2>&1
sleep 1 sleep 1
echo "" echo ""
echo "Setting up logging" echo "Creating log directorys"
mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
if [ ! -h ${rootdir}/log/server ]; then if [ ! -h ${rootdir}/log/server ]; then
ln -sv ${gamelogdir} ${rootdir}/log/server ln -sv ${gamelogdir} ${rootdir}/log/server
else else
@ -525,16 +610,20 @@ case "$1" in
fn_startserver;; fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install)
fn_install;;
debug)
fn_debugserver;;
email-test) email-test)
fn_emailtest;; fn_emailtest;;
details) details)
fn_details;; fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug)
fn_debugserver;;
install)
fn_install;;
*) *)
echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install}"
exit 1;; exit 1;;
esac esac
exit exit

516
DayOfDefeatSource/dodsserver

@ -3,7 +3,7 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
@ -29,6 +29,7 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
servercfg="${filesdir}/dod/cfg/server.cfg" servercfg="${filesdir}/dod/cfg/server.cfg"
backupdir="${rootdir}/backups"
# Server Details # Server Details
servicename="dods-server" servicename="dods-server"
@ -38,11 +39,17 @@ servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'` rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
# Logging # Logging
logdir="${rootdir}/log/script"
gamelogdir="${filesdir}/dod/logs"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
gamelogdir="${filesdir}/dod/logs"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
##### Script ##### ##### Script #####
# Do not edit # Do not edit
@ -63,49 +70,184 @@ if [ ! -e ${filesdir} ];then
fi fi
} }
fn_serverquery(){ fn_runcheck(){
# uses serverquery.py to directly query the server tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
# detects if the server locks up if [ ${tmuxwc} -eq 1 ];then
if [ -f serverquery.py ];then fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected" echo -en "\r[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING" sleep 0.5
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile} echo -en "\n"
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1` exit
sleep 1 fi
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then }
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} fn_logmanager(){
sleep 1 # log manager will active if finds logs older than ${logdays}
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!" if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile} 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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${filesdir}
./srcds_run ${parms} -debug
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1 sleep 1
fn_restartserver tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} sleep 0.5
sleep 1 while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_runcheck(){ fn_details(){
# already running check echo ""
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo "${gamename} Server Details"
if [ ${pidwc} -eq 1 ];then echo "============================"
fn_serverquery echo ""
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" echo "${servername} Ports"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} echo "======================="
exit servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
# multiple of same instance check rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
elif [ ${pidwc} -ge 2 ];then echo "Ports the server is currently using"
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile} echo "DIRECTION DESCRIPTION PORT"
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}" echo "INBOUND Game/Rcon Port ${port}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile} if [ "${engine}" = "source" ];then
fn_stopserver echo "INBOUND SourceTV Port ${sourcetvport}"
exit
fi fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
echo ""
} }
fn_distro(){ fn_distro(){
@ -139,7 +281,13 @@ fn_distro
fn_uptime fn_uptime
fn_load fn_load
{ {
echo -e "=========================================\nServer information\n=========================================\n" 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 "Date: $(date)"
echo -e "Distro: ${os}" echo -e "Distro: ${os}"
echo -e "Arch: ${arch}" echo -e "Arch: ${arch}"
@ -147,139 +295,93 @@ echo -e "Kernel: ${kernel}"
echo -e "Hostname: $HOSTNAME" echo -e "Hostname: $HOSTNAME"
echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m" echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
echo -e "Avg Load${load}\n" echo -e "Avg Load${load}\n"
echo -e "=========================================\n${servicename} statistics\n=========================================\n" echo -e "========================================\nLogs\n========================================\n"
echo -e "Service: ${servicename}" echo -e "Script log\n===================\n"
echo -e "Server: ${servername}" }|tee ${scriptlogdir}/${servicename}-email.log > /dev/null 2>&1
echo -e "Failure reason: ${failurereason}" tail -25 ${scriptlog} >> ${emaillog}
echo -e "Action Taken: ${actiontaken}\n" echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
echo -e "=========================================\n${servicename} log summary\n=========================================\n" tail -25 ${consolelog} >> ${emaillog}
}|tee ${logdir}/${servicename}-email.log > /dev/null 2>&1 if [ ! -z ${gamelogdir} ];then
tail -50 ${logfile} >> /${logdir}/${servicename}-email.log echo -e "\n\nServer log\n====================\n" >> ${emaillog}
mail -s "${subject}" ${email} < /${logdir}/${servicename}-email.log tail ${gamelogdir}/*|grep -v "==>"|sed '/^$/d' >> ${emaillog}
echo -e "[\e[0;36m INFO \e[0;39m] Sent email notification to ${email}" fi
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile} 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_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${scriptlog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Email Test Notification - Testing ${servername}" subject="${servicename} Email Test Notification - Testing ${servername}"
failurereason="Testing ${servicename} email notification" failurereason="Testing ${servicename} email notification"
actiontaken="Sent test email...hello is this thing on?" actiontaken="Sent test email...hello is this thing on?"
fn_emailnotification fn_emailnotification
else else
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo -e "[\e[0;31m FAIL \e[0;39m] Email notification not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notification not enabled" >> ${scriptlog}
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 fi
sleep 0.5
echo -en "\n"
} }
fn_restartserver(){ fn_serverquery(){
echo -e "[\e[0;32m OK \e[0;39m] Restarting ${servicename}: ${servername}" # uses serverquery.py to directly query the server
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${logfile} # detects if the server locks up
fn_stopserver if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1 sleep 1
fn_startserver if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
} echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
fn_startserver(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ${filesdir}/srcds_run ${parms}
sleep 1 sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
if [ ${pidwc} -eq 0 ];then echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}" sleep 1
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile} fn_restartserver
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${logfile}
else else
echo -e "[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5
echo -e ""
echo -e "Use for identifying server issues only!"
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1 sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" fi
sleep 3 fi
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
${filesdir}/srcds_run ${parms} -debug
} }
fn_monitorserver(){ fn_monitorserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager fn_logmanager
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo -en "[ .... ] Monitor is checking ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is checking ${servername}" >> ${scriptlog}
sleep 0.5
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then if [ "${updatecheck}" = "0" ];then
fn_runcheck fn_runcheck
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo -en "\r[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}" subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped" failurereason="${servicename} process was not running"
actiontaken="restarted ${servername}" actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification fn_emailnotification
fi fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver fn_startserver
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi fi
} }
@ -287,80 +389,60 @@ fn_updateserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${rootdir} cd ${rootdir}
cd steamcmd cd steamcmd
./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile} ./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${scriptlog}
} }
fn_details(){ fn_restartserver(){
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
echo "${gamename} Server Details" fn_stopserver
echo "============================" fn_startserver
echo "" }
echo "${servername} Ports"
echo "=======================" fn_stopserver(){
if [ "${engine}" = "unreal2" ];then fn_rootcheck
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') fn_syscheck
queryport=$((${gameport} + 1)) pid=$(tmux list-sessions 2>&1|awk '{print $1}')
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') echo -en "[ .... ] Stopping ${servicename}: ${servername}"
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') sleep 0.5
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') if [ "${pid}" == "failed" ];then
echo "Ports the server is currently using" echo -en "\r[\e[0;31m FAIL \e[0;39m] Stopping ${servicename}: ${servername} is already stopped"
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${scriptlog}
echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi
if [ "${appid}" == "215360" ];then
echo "OUTBOUND Master Server port 28852 TCP/UDP n/a"
else else
echo "OUTBOUND Master Server port 28900/28902 TCP/UDP n/a" tmux kill-session -t ${servicename}
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}
fi fi
if [ "${appid}" ];then sleep 0.5
if [ "${appid}" == "223250" ];then echo -en "\n"
echo "OUTBOUND Steam Port 20610 UDP n/a" }
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${filesdir}
tmux new-session -d -s ${servicename} "./srcds_run ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${scriptlog}
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
fi echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername}" >> ${scriptlog}
fi
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}"
echo ""
echo "${servername} WebAdmin"
echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}"
echo ""
echo "Config file"
echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo ""
echo "DIRECTION DESCRIPTION PORT"
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then
echo "INBOUND SourceTV Port ${sourcetvport}"
fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
fi fi
echo "" sleep 0.5
echo -en "\n"
} }
# #
@ -440,8 +522,6 @@ echo "Installing ${gamename} Server"
echo "=================================" echo "================================="
cd ${rootdir}/steamcmd cd ${rootdir}/steamcmd
mkdir -v ${filesdir} mkdir -v ${filesdir}
mkdir -v ${rootdir}/log
mkdir -v ${logdir}
touch install.txt touch install.txt
chmod 0600 install.txt chmod 0600 install.txt
echo "login anonymous" > install.txt echo "login anonymous" > install.txt
@ -488,7 +568,11 @@ echo "================================="
}|tee ${servercfg} > /dev/null 2>&1 }|tee ${servercfg} > /dev/null 2>&1
sleep 1 sleep 1
echo "" echo ""
echo "Setting up logging" echo "Creating log directorys"
mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
if [ ! -h ${rootdir}/log/server ]; then if [ ! -h ${rootdir}/log/server ]; then
ln -sv ${gamelogdir} ${rootdir}/log/server ln -sv ${gamelogdir} ${rootdir}/log/server
else else
@ -524,16 +608,20 @@ case "$1" in
fn_startserver;; fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install)
fn_install;;
debug)
fn_debugserver;;
email-test) email-test)
fn_emailtest;; fn_emailtest;;
details) details)
fn_details;; fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug)
fn_debugserver;;
install)
fn_install;;
*) *)
echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install}"
exit 1;; exit 1;;
esac esac
exit exit

520
HalfLife2Deathmatch/hl2dmserver

@ -3,7 +3,7 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
@ -29,22 +29,27 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
servercfg="${filesdir}/hl2mp/cfg/server.cfg" servercfg="${filesdir}/hl2mp/cfg/server.cfg"
backupdir="${rootdir}/backups"
# Server Details # Server Details
servicename="hl2dm-server" servicename="hl2dm-server"
gamename="Half Life 2: Deathmatch" gamename="Half Life 2: Deathmatch"
engine="source" engine="source"
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'` servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'` rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
# Logging # Logging
logdir="${rootdir}/log/script"
gamelogdir="${filesdir}/hl2mp/logs"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
gamelogdir="${filesdir}/hl2mp/logs"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
##### Script ##### ##### Script #####
# Do not edit # Do not edit
@ -65,49 +70,184 @@ if [ ! -e ${filesdir} ];then
fi fi
} }
fn_serverquery(){ fn_runcheck(){
# uses serverquery.py to directly query the server tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
# detects if the server locks up if [ ${tmuxwc} -eq 1 ];then
if [ -f serverquery.py ];then fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected" echo -en "\r[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING" sleep 0.5
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile} echo -en "\n"
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1` exit
sleep 1 fi
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then }
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} fn_logmanager(){
sleep 1 # log manager will active if finds logs older than ${logdays}
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!" if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile} 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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${filesdir}
./srcds_run ${parms} -debug
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1 sleep 1
fn_restartserver tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} sleep 0.5
sleep 1 while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_runcheck(){ fn_details(){
# already running check echo ""
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo "${gamename} Server Details"
if [ ${pidwc} -eq 1 ];then echo "============================"
fn_serverquery echo ""
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" echo "${servername} Ports"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} echo "======================="
exit servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
# multiple of same instance check rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
elif [ ${pidwc} -ge 2 ];then echo "Ports the server is currently using"
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile} echo "DIRECTION DESCRIPTION PORT"
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}" echo "INBOUND Game/Rcon Port ${port}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile} if [ "${engine}" = "source" ];then
fn_stopserver echo "INBOUND SourceTV Port ${sourcetvport}"
exit
fi fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
echo ""
} }
fn_distro(){ fn_distro(){
@ -141,7 +281,13 @@ fn_distro
fn_uptime fn_uptime
fn_load fn_load
{ {
echo -e "=========================================\nServer information\n=========================================\n" 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 "Date: $(date)"
echo -e "Distro: ${os}" echo -e "Distro: ${os}"
echo -e "Arch: ${arch}" echo -e "Arch: ${arch}"
@ -149,139 +295,95 @@ echo -e "Kernel: ${kernel}"
echo -e "Hostname: $HOSTNAME" echo -e "Hostname: $HOSTNAME"
echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m" echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
echo -e "Avg Load${load}\n" echo -e "Avg Load${load}\n"
echo -e "=========================================\n${servicename} statistics\n=========================================\n" echo -e "========================================\nLogs\n========================================\n"
echo -e "Service: ${servicename}" echo -e "Script log\n===================\n"
echo -e "Server: ${servername}" }|tee ${scriptlogdir}/${servicename}-email.log > /dev/null 2>&1
echo -e "Failure reason: ${failurereason}" tail -25 ${scriptlog} >> ${emaillog}
echo -e "Action Taken: ${actiontaken}\n" if [ ! -z ${consolelog} ];then
echo -e "=========================================\n${servicename} log summary\n=========================================\n" echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
}|tee ${logdir}/${servicename}-email.log > /dev/null 2>&1 tail -25 ${consolelog} >> ${emaillog}
tail -50 ${logfile} >> /${logdir}/${servicename}-email.log fi
mail -s "${subject}" ${email} < /${logdir}/${servicename}-email.log if [ ! -z ${gamelogdir} ];then
echo -e "[\e[0;36m INFO \e[0;39m] Sent email notification to ${email}" echo -e "\n\nServer log\n====================\n" >> ${emaillog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile} 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_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${scriptlog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Email Test Notification - Testing ${servername}" subject="${servicename} Email Test Notification - Testing ${servername}"
failurereason="Testing ${servicename} email notification" failurereason="Testing ${servicename} email notification"
actiontaken="Sent test email...hello is this thing on?" actiontaken="Sent test email...hello is this thing on?"
fn_emailnotification fn_emailnotification
else else
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo -e "[\e[0;31m FAIL \e[0;39m] Email notification not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notification not enabled" >> ${scriptlog}
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 fi
sleep 0.5
echo -en "\n"
} }
fn_restartserver(){ fn_serverquery(){
echo -e "[\e[0;32m OK \e[0;39m] Restarting ${servicename}: ${servername}" # uses serverquery.py to directly query the server
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${logfile} # detects if the server locks up
fn_stopserver if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1 sleep 1
fn_startserver if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
} echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
fn_startserver(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ${filesdir}/srcds_run ${parms}
sleep 1 sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
if [ ${pidwc} -eq 0 ];then echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}" sleep 1
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile} fn_restartserver
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${logfile}
else else
echo -e "[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5
echo -e ""
echo -e "Use for identifying server issues only!"
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1 sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" fi
sleep 3 fi
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
${filesdir}/srcds_run ${parms} -debug
} }
fn_monitorserver(){ fn_monitorserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager fn_logmanager
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo -en "[ .... ] Monitor is checking ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is checking ${servername}" >> ${scriptlog}
sleep 0.5
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then if [ "${updatecheck}" = "0" ];then
fn_runcheck fn_runcheck
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo -en "\r[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}" subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped" failurereason="${servicename} process was not running"
actiontaken="restarted ${servername}" actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification fn_emailnotification
fi fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver fn_startserver
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi fi
} }
@ -289,80 +391,60 @@ fn_updateserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${rootdir} cd ${rootdir}
cd steamcmd cd steamcmd
./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile} ./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${scriptlog}
} }
fn_details(){ fn_restartserver(){
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
echo "${gamename} Server Details" fn_stopserver
echo "============================" fn_startserver
echo "" }
echo "${servername} Ports"
echo "=======================" fn_stopserver(){
if [ "${engine}" = "unreal2" ];then fn_rootcheck
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') fn_syscheck
queryport=$((${gameport} + 1)) pid=$(tmux list-sessions 2>&1|awk '{print $1}')
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') echo -en "[ .... ] Stopping ${servicename}: ${servername}"
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') sleep 0.5
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') if [ "${pid}" == "failed" ];then
echo "Ports the server is currently using" echo -en "\r[\e[0;31m FAIL \e[0;39m] Stopping ${servicename}: ${servername} is already stopped"
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${scriptlog}
echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi
if [ "${appid}" == "215360" ];then
echo "OUTBOUND Master Server port 28852 TCP/UDP n/a"
else else
echo "OUTBOUND Master Server port 28900/28902 TCP/UDP n/a" tmux kill-session -t ${servicename}
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}
fi fi
if [ "${appid}" ];then sleep 0.5
if [ "${appid}" == "223250" ];then echo -en "\n"
echo "OUTBOUND Steam Port 20610 UDP n/a" }
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${filesdir}
tmux new-session -d -s ${servicename} "./srcds_run ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${scriptlog}
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" 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 fi
fi sleep 0.5
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}" echo -en "\n"
echo ""
echo "${servername} WebAdmin"
echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}"
echo ""
echo "Config file"
echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo ""
echo "DIRECTION DESCRIPTION PORT"
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then
echo "INBOUND SourceTV Port ${sourcetvport}"
fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
fi
echo ""
} }
# #
@ -442,8 +524,6 @@ echo "Installing ${gamename} Server"
echo "=================================" echo "================================="
cd ${rootdir}/steamcmd cd ${rootdir}/steamcmd
mkdir -v ${filesdir} mkdir -v ${filesdir}
mkdir -v ${rootdir}/log
mkdir -v ${logdir}
touch install.txt touch install.txt
chmod 0600 install.txt chmod 0600 install.txt
echo "login anonymous" > install.txt echo "login anonymous" > install.txt
@ -490,7 +570,11 @@ echo "================================="
}|tee ${servercfg} > /dev/null 2>&1 }|tee ${servercfg} > /dev/null 2>&1
sleep 1 sleep 1
echo "" echo ""
echo "Setting up logging" echo "Creating log directorys"
mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
if [ ! -h ${rootdir}/log/server ]; then if [ ! -h ${rootdir}/log/server ]; then
ln -sv ${gamelogdir} ${rootdir}/log/server ln -sv ${gamelogdir} ${rootdir}/log/server
else else
@ -526,16 +610,20 @@ case "$1" in
fn_startserver;; fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install)
fn_install;;
debug)
fn_debugserver;;
email-test) email-test)
fn_emailtest;; fn_emailtest;;
details) details)
fn_details;; fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug)
fn_debugserver;;
install)
fn_install;;
*) *)
echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install}"
exit 1;; exit 1;;
esac esac
exit exit

666
KillingFloor/kfserver

@ -3,7 +3,7 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
@ -24,6 +24,7 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
systemdir="${filesdir}/System" systemdir="${filesdir}/System"
backupdir="${rootdir}/backups"
compressedmapsdir="${rootdir}/Maps-Compressed" compressedmapsdir="${rootdir}/Maps-Compressed"
# Server names # Server names
@ -34,10 +35,16 @@ ini="${servicename}.ini"
servername=`grep -s ServerName= ${systemdir}/${ini} | sed 's/ServerName=//g'` servername=`grep -s ServerName= ${systemdir}/${ini} | sed 's/ServerName=//g'`
# Logging # Logging
logdir="${rootdir}/log"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
# Start vars # Start vars
defaultmap="KF-BioticsLab.rom" defaultmap="KF-BioticsLab.rom"
@ -62,244 +69,17 @@ if [ ! -e ${systemdir} ];then
fi fi
} }
fn_serverquery(){
# uses serverquery.py to directly query the server
# detects if the server locks up
if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile}
sleep 1
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile}
sleep 1
fn_restartserver
else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile}
sleep 1
fi
fi
}
fn_runcheck(){ fn_runcheck(){
# already running check tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` if [ ${tmuxwc} -eq 1 ];then
if [ ${pidwc} -eq 1 ];then
fn_serverquery fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" 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" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
exit
# multiple of same instance check
elif [ ${pidwc} -ge 2 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile}
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile}
fn_stopserver
exit
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] Sent email notification to ${email}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile}
}
fn_emailtest(){
fn_rootcheck
fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing 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] Email notificaion not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: 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(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${systemdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ./ucc-bin ${parms}
sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l`
if [ ${pidwc} -eq 0 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile}
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is 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}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5 sleep 0.5
echo -e "" echo -en "\n"
echo -e "Use for identifying server issues only!" exit
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode"
sleep 3
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
cd ${systemdir}
./ucc-bin ${parms}
}
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}
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then
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}
if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped"
actiontaken="restarted ${servername}"
fn_emailnotification
fi
fn_startserver
else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile}
fi
}
fn_updateserver(){
fn_rootcheck
fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile}
cd ${rootdir}
cd steamcmd
./steamcmd.sh +login ${steamuser} ${steampass} +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile}
if [ $(grep Denied ${logfile}|wc -l) -ge 1 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: STEAMAUTH error detected!: Server update failed"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: STEAMAUTH error detected!: Server update failed" >> ${logfile}
if [ "${emailnotification}" = "on" ];then
mail -s "${servername}: Steam Auth Failure" ${email}< ${logfile}
fi
fi fi
} }
fn_compressmaps(){ fn_compressmaps(){
fn_rootcheck fn_rootcheck
clear clear
@ -311,7 +91,7 @@ pwd
echo "" echo ""
echo "Compressed maps saved to:" echo "Compressed maps saved to:"
echo "" echo ""
echo "$compressedmapsdir" echo "${compressedmapsdir}"
echo "" echo ""
while true; do while true; do
read -p "Start compression [y/N]" yn read -p "Start compression [y/N]" yn
@ -321,11 +101,11 @@ while true; do
* ) echo "Please answer yes or no.";; * ) echo "Please answer yes or no.";;
esac esac
done done
mkdir $compressedmapsdir mkdir ${compressedmapsdir}
rm -rfv ${filesdir}/Maps/*.uz2 rm -rfv ${filesdir}/Maps/*.uz2
cd ${systemdir} cd ${systemdir}
./ucc-bin compress ../Maps/* --nohomedir ./ucc-bin compress ../Maps/* --nohomedir
mv -fv ${filesdir}/Maps/*.uz2 $compressedmapsdir mv -fv ${filesdir}/Maps/*.uz2 ${compressedmapsdir}
} }
fn_steamguard(){ fn_steamguard(){
@ -362,6 +142,144 @@ echo "Complete!"
cd ${rootdir} cd ${rootdir}
} }
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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${systemdir}
./ucc-bin ${parms}
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1
tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else
echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
sleep 0.5
while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
}
fn_details(){ fn_details(){
echo "" echo ""
echo "${gamename} Server Details" echo "${gamename} Server Details"
@ -369,19 +287,28 @@ echo "============================"
echo "" echo ""
echo "${servername} Ports" echo "${servername} Ports"
echo "=======================" echo "======================="
if [ "${engine}" = "unreal2" ];then
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g')
queryport=$((${gameport} + 1)) queryport=$((${gameport} + 1))
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g')
udplinkport=$((${gameport} + 2))
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g')
if [ "${engine}" == "unreal" ];then
webadminuser=$(grep AdminUsername= ${systemdir}/${ini}|sed 's/\AdminUsername=//g')
else
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g')
fi
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g')
echo "Ports the server is currently using" echo "Ports the server is currently using"
echo "" echo ""
echo "DIRECTION DESCRIPTION PORT INI VARIABLE" echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}" echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a" echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then if [ "${engine}" == "unreal" ];then
echo "OUTBOUND UdpLink Port (random) ${udplinkport}+ UDP n/a"
fi
if [ "${engine}" != "unreal" ] && [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}" echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi fi
if [ "${appid}" == "215360" ];then if [ "${appid}" == "215360" ];then
@ -395,41 +322,229 @@ if [ "${engine}" = "unreal2" ];then
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" echo "OUTBOUND Steam Port 20660 UDP n/a"
fi fi
fi fi
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}" echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}"
echo "" echo ""
echo "${servername} WebAdmin" echo "${servername} WebAdmin"
echo "=======================" echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}" echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}" echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}" echo "WebAdmin Password: ${webadminpass}"
echo "" echo ""
echo "Config file" echo "Config file"
echo "${systemdir}/${ini}" echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo "" echo ""
echo "DIRECTION DESCRIPTION PORT" }
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then fn_distro(){
echo "INBOUND SourceTV Port ${sourcetvport}" 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 fi
echo "OUTBOUND Client Port ${clientport}" }
echo ""
echo "You can change ports by editing the" fn_uptime(){
echo "start parameters in ${selfname}" uptime=$(</proc/uptime)
echo "" uptime=${uptime%%.*}
echo "${servername} Details" minutes=$(( uptime/60%60 ))
echo "=======================" hours=$(( uptime/60/60%24 ))
echo "Server name: ${servername}" days=$(( uptime/60/60/24 ))
echo "Rcon Password: ${rcon}" }
echo ""
echo "Config file:" fn_load(){
echo "${servercfg}" load=$(uptime | awk -F 'load average' '{ print $2 }')
}
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 fi
echo "" 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}
fi
sleep 0.5
echo -en "\n"
}
fn_serverquery(){
# uses serverquery.py to directly query the server
# detects if the server locks up
if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
sleep 1
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
sleep 1
fn_restartserver
else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
sleep 1
fi
fi
}
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
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then
fn_runcheck
echo -en "\r[\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!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servicename} process was not running"
actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification
fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver
else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi
}
fn_updateserver(){
fn_rootcheck
fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${rootdir}
cd steamcmd
./steamcmd.sh +login ${steamuser} ${steampass} +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile}
if [ $(grep Denied ${logfile}|wc -l) -ge 1 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: STEAMAUTH error detected!: Server update failed"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: STEAMAUTH error detected!: Server update failed" >> ${logfile}
if [ "${emailnotification}" = "on" ];then
mail -s "${servername}: Steam Auth Failure" ${email}< ${logfile}
fi
fi
}
fn_restartserver(){
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
fn_stopserver
fn_startserver
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=$(tmux list-sessions 2>&1|awk '{print $1}')
echo -en "[ .... ] Stopping ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
sleep 0.5
if [ "${pid}" == "failed" ];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
tmux kill-session -t ${servicename}
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}
fi
sleep 0.5
echo -en "\n"
}
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${systemdir}
tmux new-session -d -s ${servicename} "./ucc-bin ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${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"
} }
# #
@ -534,8 +649,11 @@ echo "================================="
echo "Copying ${systemdir}/default.ini to ${systemdir}/${ini}" echo "Copying ${systemdir}/default.ini to ${systemdir}/${ini}"
cp ${systemdir}/Default.ini ${systemdir}/${ini} cp ${systemdir}/Default.ini ${systemdir}/${ini}
sleep 1 sleep 1
echo "Creating log directory" echo "Creating log directorys"
mkdir -v ${logdir} mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
sleep 1 sleep 1
echo "Applying WebAdmin ROOst.css fix!" echo "Applying WebAdmin ROOst.css fix!"
echo "http://forums.tripwireinteractive.com/showpost.php?p=585435&postcount=13" echo "http://forums.tripwireinteractive.com/showpost.php?p=585435&postcount=13"
@ -585,21 +703,25 @@ case "$1" in
fn_startserver;; fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install) email-test)
fn_install;; fn_emailtest;;
details)
fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug) debug)
fn_debugserver;; fn_debugserver;;
install)
fn_install;;
map-compressor) map-compressor)
fn_compressmaps;; fn_compressmaps;;
steam-auth) steam-auth)
fn_updateserver fn_updateserver
fn_steamguard;; fn_steamguard;;
email-test)
fn_emailtest;;
details)
fn_details;;
*) *)
echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|map-compressor|steam-auth|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install|map-compressor|steam-auth}"
exit 1;; exit 1;;
esac esac
exit exit

518
Left4Dead2/l4d2server

@ -3,7 +3,7 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
@ -29,6 +29,7 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
servercfg="${filesdir}/left4dead2/cfg/server.cfg" servercfg="${filesdir}/left4dead2/cfg/server.cfg"
backupdir="${rootdir}/backups"
# Server Details # Server Details
servicename="l4d2-server" servicename="l4d2-server"
@ -38,11 +39,17 @@ servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'` rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
# Logging # Logging
logdir="${rootdir}/log/script"
gamelogdir="${filesdir}/left4dead2/logs"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
gamelogdir="${filesdir}/left4dead2/logs"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
##### Script ##### ##### Script #####
# Do not edit # Do not edit
@ -63,49 +70,184 @@ if [ ! -e ${filesdir} ];then
fi fi
} }
fn_serverquery(){ fn_runcheck(){
# uses serverquery.py to directly query the server tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
# detects if the server locks up if [ ${tmuxwc} -eq 1 ];then
if [ -f serverquery.py ];then fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected" echo -en "\r[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING" sleep 0.5
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile} echo -en "\n"
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1` exit
sleep 1 fi
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then }
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} fn_logmanager(){
sleep 1 # log manager will active if finds logs older than ${logdays}
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!" if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile} 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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${filesdir}
./srcds_run ${parms} -debug
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1 sleep 1
fn_restartserver tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} sleep 0.5
sleep 1 while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_runcheck(){ fn_details(){
# already running check echo ""
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo "${gamename} Server Details"
if [ ${pidwc} -eq 1 ];then echo "============================"
fn_serverquery echo ""
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" echo "${servername} Ports"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} echo "======================="
exit servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
# multiple of same instance check rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
elif [ ${pidwc} -ge 2 ];then echo "Ports the server is currently using"
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile} echo "DIRECTION DESCRIPTION PORT"
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}" echo "INBOUND Game/Rcon Port ${port}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile} if [ "${engine}" = "source" ];then
fn_stopserver echo "INBOUND SourceTV Port ${sourcetvport}"
exit
fi fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
echo ""
} }
fn_distro(){ fn_distro(){
@ -139,7 +281,13 @@ fn_distro
fn_uptime fn_uptime
fn_load fn_load
{ {
echo -e "=========================================\nServer information\n=========================================\n" 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 "Date: $(date)"
echo -e "Distro: ${os}" echo -e "Distro: ${os}"
echo -e "Arch: ${arch}" echo -e "Arch: ${arch}"
@ -147,139 +295,95 @@ echo -e "Kernel: ${kernel}"
echo -e "Hostname: $HOSTNAME" echo -e "Hostname: $HOSTNAME"
echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m" echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
echo -e "Avg Load${load}\n" echo -e "Avg Load${load}\n"
echo -e "=========================================\n${servicename} statistics\n=========================================\n" echo -e "========================================\nLogs\n========================================\n"
echo -e "Service: ${servicename}" echo -e "Script log\n===================\n"
echo -e "Server: ${servername}" }|tee ${scriptlogdir}/${servicename}-email.log > /dev/null 2>&1
echo -e "Failure reason: ${failurereason}" tail -25 ${scriptlog} >> ${emaillog}
echo -e "Action Taken: ${actiontaken}\n" if [ ! -z ${consolelog} ];then
echo -e "=========================================\n${servicename} log summary\n=========================================\n" echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
}|tee ${logdir}/${servicename}-email.log > /dev/null 2>&1 tail -25 ${consolelog} >> ${emaillog}
tail -50 ${logfile} >> /${logdir}/${servicename}-email.log fi
mail -s "${subject}" ${email} < /${logdir}/${servicename}-email.log if [ ! -z ${gamelogdir} ];then
echo -e "[\e[0;36m INFO \e[0;39m] Sent email notification to ${email}" echo -e "\n\nServer log\n====================\n" >> ${emaillog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile} 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_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${scriptlog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Email Test Notification - Testing ${servername}" subject="${servicename} Email Test Notification - Testing ${servername}"
failurereason="Testing ${servicename} email notification" failurereason="Testing ${servicename} email notification"
actiontaken="Sent test email...hello is this thing on?" actiontaken="Sent test email...hello is this thing on?"
fn_emailnotification fn_emailnotification
else else
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo -e "[\e[0;31m FAIL \e[0;39m] Email notification not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notification not enabled" >> ${scriptlog}
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 fi
sleep 0.5
echo -en "\n"
} }
fn_restartserver(){ fn_serverquery(){
echo -e "[\e[0;32m OK \e[0;39m] Restarting ${servicename}: ${servername}" # uses serverquery.py to directly query the server
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${logfile} # detects if the server locks up
fn_stopserver if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1 sleep 1
fn_startserver if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
} echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
fn_startserver(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ${filesdir}/srcds_run ${parms}
sleep 1 sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
if [ ${pidwc} -eq 0 ];then echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}" sleep 1
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile} fn_restartserver
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${logfile}
else else
echo -e "[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5
echo -e ""
echo -e "Use for identifying server issues only!"
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1 sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" fi
sleep 3 fi
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
${filesdir}/srcds_run ${parms} -debug
} }
fn_monitorserver(){ fn_monitorserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager fn_logmanager
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo -en "[ .... ] Monitor is checking ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is checking ${servername}" >> ${scriptlog}
sleep 0.5
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then if [ "${updatecheck}" = "0" ];then
fn_runcheck fn_runcheck
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo -en "\r[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}" subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped" failurereason="${servicename} process was not running"
actiontaken="restarted ${servername}" actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification fn_emailnotification
fi fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver fn_startserver
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi fi
} }
@ -287,80 +391,60 @@ fn_updateserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${rootdir} cd ${rootdir}
cd steamcmd cd steamcmd
./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile} ./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${scriptlog}
} }
fn_details(){ fn_restartserver(){
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
echo "${gamename} Server Details" fn_stopserver
echo "============================" fn_startserver
echo "" }
echo "${servername} Ports"
echo "=======================" fn_stopserver(){
if [ "${engine}" = "unreal2" ];then fn_rootcheck
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') fn_syscheck
queryport=$((${gameport} + 1)) pid=$(tmux list-sessions 2>&1|awk '{print $1}')
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') echo -en "[ .... ] Stopping ${servicename}: ${servername}"
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') sleep 0.5
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') if [ "${pid}" == "failed" ];then
echo "Ports the server is currently using" echo -en "\r[\e[0;31m FAIL \e[0;39m] Stopping ${servicename}: ${servername} is already stopped"
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${scriptlog}
echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi
if [ "${appid}" == "215360" ];then
echo "OUTBOUND Master Server port 28852 TCP/UDP n/a"
else else
echo "OUTBOUND Master Server port 28900/28902 TCP/UDP n/a" tmux kill-session -t ${servicename}
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}
fi fi
if [ "${appid}" ];then sleep 0.5
if [ "${appid}" == "223250" ];then echo -en "\n"
echo "OUTBOUND Steam Port 20610 UDP n/a" }
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${filesdir}
tmux new-session -d -s ${servicename} "./srcds_run ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${scriptlog}
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
fi echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername}" >> ${scriptlog}
fi
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}"
echo ""
echo "${servername} WebAdmin"
echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}"
echo ""
echo "Config file"
echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo ""
echo "DIRECTION DESCRIPTION PORT"
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then
echo "INBOUND SourceTV Port ${sourcetvport}"
fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
fi fi
echo "" sleep 0.5
echo -en "\n"
} }
# #
@ -440,8 +524,6 @@ echo "Installing ${gamename} Server"
echo "=================================" echo "================================="
cd ${rootdir}/steamcmd cd ${rootdir}/steamcmd
mkdir -v ${filesdir} mkdir -v ${filesdir}
mkdir -v ${rootdir}/log
mkdir -v ${logdir}
touch install.txt touch install.txt
chmod 0600 install.txt chmod 0600 install.txt
echo "login anonymous" > install.txt echo "login anonymous" > install.txt
@ -488,7 +570,11 @@ echo "================================="
}|tee ${servercfg} > /dev/null 2>&1 }|tee ${servercfg} > /dev/null 2>&1
sleep 1 sleep 1
echo "" echo ""
echo "Setting up logging" echo "Creating log directorys"
mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
if [ ! -h ${rootdir}/log/server ]; then if [ ! -h ${rootdir}/log/server ]; then
ln -sv ${gamelogdir} ${rootdir}/log/server ln -sv ${gamelogdir} ${rootdir}/log/server
else else
@ -524,16 +610,20 @@ case "$1" in
fn_startserver;; fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install)
fn_install;;
debug)
fn_debugserver;;
email-test) email-test)
fn_emailtest;; fn_emailtest;;
details) details)
fn_details;; fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug)
fn_debugserver;;
install)
fn_install;;
*) *)
echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install}"
exit 1;; exit 1;;
esac esac
exit exit

702
RedOrchestra/roserver

@ -3,7 +3,7 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
@ -15,6 +15,7 @@ email="[email protected]"
# Steam login # Steam login
steamuser="username" steamuser="username"
steampass="password" steampass="password"
# Steam # Steam
appid="223250" appid="223250"
@ -23,6 +24,7 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
systemdir="${filesdir}/system" systemdir="${filesdir}/system"
backupdir="${rootdir}/backups"
compressedmapsdir="${rootdir}/Maps-Compressed" compressedmapsdir="${rootdir}/Maps-Compressed"
# Server names # Server names
@ -33,10 +35,16 @@ ini="${servicename}.ini"
servername=`grep -s ServerName= ${systemdir}/${ini} | sed 's/ServerName=//g'` servername=`grep -s ServerName= ${systemdir}/${ini} | sed 's/ServerName=//g'`
# Logging # Logging
logdir="${rootdir}/log"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
# Start vars # Start vars
defaultmap="RO-Arad.rom" defaultmap="RO-Arad.rom"
@ -61,244 +69,17 @@ if [ ! -e ${systemdir} ];then
fi fi
} }
fn_serverquery(){
# uses serverquery.py to directly query the server
# detects if the server locks up
if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile}
sleep 1
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile}
sleep 1
fn_restartserver
else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile}
sleep 1
fi
fi
}
fn_runcheck(){ fn_runcheck(){
# already running check tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` if [ ${tmuxwc} -eq 1 ];then
if [ ${pidwc} -eq 1 ];then
fn_serverquery fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" 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" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
exit
# multiple of same instance check
elif [ ${pidwc} -ge 2 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile}
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile}
fn_stopserver
exit
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] Sent email notification to ${email}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile}
}
fn_emailtest(){
fn_rootcheck
fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing 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] Email notificaion not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: 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(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${systemdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ./ucc-bin ${parms}
sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l`
if [ ${pidwc} -eq 0 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile}
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is 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}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5 sleep 0.5
echo -e "" echo -en "\n"
echo -e "Use for identifying server issues only!" exit
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode"
sleep 3
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
cd ${systemdir}
./ucc-bin ${parms}
}
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}
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then
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}
if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped"
actiontaken="restarted ${servername}"
fn_emailnotification
fi
fn_startserver
else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile}
fi
}
fn_updateserver(){
fn_rootcheck
fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile}
cd ${rootdir}
cd steamcmd
./steamcmd.sh +login ${steamuser} ${steampass} +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile}
if [ $(grep Denied ${logfile}|wc -l) -ge 1 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: STEAMAUTH error detected!: Server update failed"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: STEAMAUTH error detected!: Server update failed" >> ${logfile}
if [ "${emailnotification}" = "on" ];then
mail -s "${servername}: Steam Auth Failure" ${email}< ${logfile}
fi
fi fi
} }
fn_compressmaps(){ fn_compressmaps(){
fn_rootcheck fn_rootcheck
clear clear
@ -310,7 +91,7 @@ pwd
echo "" echo ""
echo "Compressed maps saved to:" echo "Compressed maps saved to:"
echo "" echo ""
echo "$compressedmapsdir" echo "${compressedmapsdir}"
echo "" echo ""
while true; do while true; do
read -p "Start compression [y/N]" yn read -p "Start compression [y/N]" yn
@ -320,11 +101,11 @@ while true; do
* ) echo "Please answer yes or no.";; * ) echo "Please answer yes or no.";;
esac esac
done done
mkdir $compressedmapsdir mkdir ${compressedmapsdir}
rm -rfv ${filesdir}/Maps/*.uz2 rm -rfv ${filesdir}/Maps/*.uz2
cd ${systemdir} cd ${systemdir}
./ucc-bin compress ../Maps/* --nohomedir ./ucc-bin compress ../Maps/* --nohomedir
mv -fv ${filesdir}/Maps/*.uz2 $compressedmapsdir mv -fv ${filesdir}/Maps/*.uz2 ${compressedmapsdir}
} }
fn_steamguard(){ fn_steamguard(){
@ -361,6 +142,144 @@ echo "Complete!"
cd ${rootdir} cd ${rootdir}
} }
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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${systemdir}
./ucc-bin ${parms}
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1
tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else
echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
sleep 0.5
while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
}
fn_details(){ fn_details(){
echo "" echo ""
echo "${gamename} Server Details" echo "${gamename} Server Details"
@ -368,19 +287,28 @@ echo "============================"
echo "" echo ""
echo "${servername} Ports" echo "${servername} Ports"
echo "=======================" echo "======================="
if [ "${engine}" = "unreal2" ];then
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g')
queryport=$((${gameport} + 1)) queryport=$((${gameport} + 1))
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g')
udplinkport=$((${gameport} + 2))
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g')
if [ "${engine}" == "unreal" ];then
webadminuser=$(grep AdminUsername= ${systemdir}/${ini}|sed 's/\AdminUsername=//g')
else
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g')
fi
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g')
echo "Ports the server is currently using" echo "Ports the server is currently using"
echo "" echo ""
echo "DIRECTION DESCRIPTION PORT INI VARIABLE" echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}" echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a" echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then if [ "${engine}" == "unreal" ];then
echo "OUTBOUND UdpLink Port (random) ${udplinkport}+ UDP n/a"
fi
if [ "${engine}" != "unreal" ] && [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}" echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi fi
if [ "${appid}" == "215360" ];then if [ "${appid}" == "215360" ];then
@ -394,41 +322,227 @@ if [ "${engine}" = "unreal2" ];then
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" echo "OUTBOUND Steam Port 20660 UDP n/a"
fi fi
fi fi
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}" echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}"
echo "" echo ""
echo "${servername} WebAdmin" echo "${servername} WebAdmin"
echo "=======================" echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}" echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}" echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}" echo "WebAdmin Password: ${webadminpass}"
echo "" echo ""
echo "Config file" echo "Config file"
echo "${systemdir}/${ini}" echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo "" echo ""
echo "DIRECTION DESCRIPTION PORT" }
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then fn_distro(){
echo "INBOUND SourceTV Port ${sourcetvport}" 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 fi
echo "OUTBOUND Client Port ${clientport}" }
echo ""
echo "You can change ports by editing the" fn_uptime(){
echo "start parameters in ${selfname}" uptime=$(</proc/uptime)
echo "" uptime=${uptime%%.*}
echo "${servername} Details" minutes=$(( uptime/60%60 ))
echo "=======================" hours=$(( uptime/60/60%24 ))
echo "Server name: ${servername}" days=$(( uptime/60/60/24 ))
echo "Rcon Password: ${rcon}" }
echo ""
echo "Config file:" fn_load(){
echo "${servercfg}" load=$(uptime | awk -F 'load average' '{ print $2 }')
}
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}
echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
tail -25 ${consolelog} >> ${emaillog}
if [ ! -z ${gamelogdir} ];then
echo -e "\n\nServer log\n====================\n" >> ${emaillog}
tail ${gamelogdir}/*|grep -v "==>"|sed '/^$/d' >> ${emaillog}
fi fi
echo "" 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}
fi
sleep 0.5
echo -en "\n"
}
fn_serverquery(){
# uses serverquery.py to directly query the server
# detects if the server locks up
if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
sleep 1
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
sleep 1
fn_restartserver
else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
sleep 1
fi
fi
}
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
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then
fn_runcheck
echo -en "\r[\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!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servicename} process was not running"
actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification
fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver
else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi
}
fn_updateserver(){
fn_rootcheck
fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${rootdir}
cd steamcmd
./steamcmd.sh +login ${steamuser} ${steampass} +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile}
if [ $(grep Denied ${logfile}|wc -l) -ge 1 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: STEAMAUTH error detected!: Server update failed"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: STEAMAUTH error detected!: Server update failed" >> ${logfile}
if [ "${emailnotification}" = "on" ];then
mail -s "${servername}: Steam Auth Failure" ${email}< ${logfile}
fi
fi
}
fn_restartserver(){
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
fn_stopserver
fn_startserver
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=$(tmux list-sessions 2>&1|awk '{print $1}')
echo -en "[ .... ] Stopping ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
sleep 0.5
if [ "${pid}" == "failed" ];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
tmux kill-session -t ${servicename}
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}
fi
sleep 0.5
echo -en "\n"
}
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${systemdir}
tmux new-session -d -s ${servicename} "./ucc-bin ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${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"
} }
# #
@ -529,12 +643,38 @@ echo "================================="
fn_header fn_header
echo "Configuring ${gamename} Server" echo "Configuring ${gamename} Server"
echo "=================================" echo "================================="
sleep 1 sleep 1
echo "Copying ${systemdir}/default.ini to ${systemdir}/${ini}" echo "Copying ${systemdir}/default.ini to ${systemdir}/${ini}"
cp ${systemdir}/default.ini ${systemdir}/${ini} cp ${systemdir}/default.ini ${systemdir}/${ini}
sleep 1 sleep 1
echo "Creating log directory"
mkdir -v ${logdir} echo "Creating log directorys"
mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
sleep 1 sleep 1
echo "Applying WebAdmin ROOst.css fix!" echo "Applying WebAdmin ROOst.css fix!"
echo "http://forums.tripwireinteractive.com/showpost.php?p=585435&postcount=13" echo "http://forums.tripwireinteractive.com/showpost.php?p=585435&postcount=13"
@ -543,16 +683,26 @@ echo "================================="
sleep 1 sleep 1
echo "Applying WebAdmin CharSet fix!" echo "Applying WebAdmin CharSet fix!"
echo "http://forums.tripwireinteractive.com/showpost.php?p=442340&postcount=1" echo "http://forums.tripwireinteractive.com/showpost.php?p=442340&postcount=1"
sed -i 's/CharSet="iso-8859-1"/CharSet="utf-8"/g' ${systemdir}/uweb.int sed -i 's/CharSet="iso-8859-1"/CharSet="utf-8"/g' ${systemdir}/UWeb.int
sleep 1 sleep 1
echo "Setting WebAdmin username and password" echo "Setting WebAdmin username and password"
sed -i 's/AdminName=/AdminName=admin/g' ${systemdir}/${ini} sed -i 's/AdminName=/AdminName=admin/g' ${systemdir}/${ini}
sed -i 's/AdminPassword=/AdminPassword=ropass/g' ${systemdir}/${ini} sed -i 's/AdminPassword=/AdminPassword=kfpass/g' ${systemdir}/${ini}
sleep 1 sleep 1
echo "Enabling WebAdmin" echo "Enabling WebAdmin"
sed -i 's/bEnabled=False/bEnabled=True/g' ${systemdir}/${ini} sed -i 's/bEnabled=False/bEnabled=True/g' ${systemdir}/${ini}
sleep 1 sleep 1
echo "Forcing server to start to get ports/server name to display correctly" echo "Forcing server to start to get ports/server name to display correctly"
sleep 1 sleep 1
cd ${rootdir} cd ${rootdir}
${selfname} start ${selfname} start
@ -584,21 +734,25 @@ case "$1" in
fn_startserver;; fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install) email-test)
fn_install;; fn_emailtest;;
details)
fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug) debug)
fn_debugserver;; fn_debugserver;;
install)
fn_install;;
map-compressor) map-compressor)
fn_compressmaps;; fn_compressmaps;;
steam-auth) steam-auth)
fn_updateserver fn_updateserver
fn_steamguard;; fn_steamguard;;
email-test)
fn_emailtest;;
details)
fn_details;;
*) *)
echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|map-compressor|steam-auth|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install|map-compressor|steam-auth}"
exit 1;; exit 1;;
esac esac
exit exit

520
TeamFortress2/tf2server

@ -3,7 +3,7 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
@ -29,21 +29,28 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
servercfg="${filesdir}/tf/cfg/server.cfg" servercfg="${filesdir}/tf/cfg/server.cfg"
backupdir="${rootdir}/backups"
# Server Details # Server Details
servicename="tf2-server" servicename="tf2-server"
engine="source"
gamename="Team Fortress 2" gamename="Team Fortress 2"
engine="source" engine="source"
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'` servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'` rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
# Logging # Logging
logdir="${rootdir}/log/script"
gamelogdir="${filesdir}/tf/logs"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
gamelogdir="${filesdir}/tf/logs"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
##### Script ##### ##### Script #####
# Do not edit # Do not edit
@ -64,49 +71,184 @@ if [ ! -e ${filesdir} ];then
fi fi
} }
fn_serverquery(){ fn_runcheck(){
# uses serverquery.py to directly query the server tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
# detects if the server locks up if [ ${tmuxwc} -eq 1 ];then
if [ -f serverquery.py ];then fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected" echo -en "\r[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING" sleep 0.5
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile} echo -en "\n"
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1` exit
sleep 1 fi
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then }
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} fn_logmanager(){
sleep 1 # log manager will active if finds logs older than ${logdays}
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!" if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile} 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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${filesdir}
./srcds_run ${parms} -debug
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1 sleep 1
fn_restartserver tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} sleep 0.5
sleep 1 while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_runcheck(){ fn_details(){
# already running check echo ""
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo "${gamename} Server Details"
if [ ${pidwc} -eq 1 ];then echo "============================"
fn_serverquery echo ""
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" echo "${servername} Ports"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} echo "======================="
exit servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
# multiple of same instance check rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
elif [ ${pidwc} -ge 2 ];then echo "Ports the server is currently using"
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile} echo "DIRECTION DESCRIPTION PORT"
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}" echo "INBOUND Game/Rcon Port ${port}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile} if [ "${engine}" = "source" ];then
fn_stopserver echo "INBOUND SourceTV Port ${sourcetvport}"
exit
fi fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
echo ""
} }
fn_distro(){ fn_distro(){
@ -140,7 +282,13 @@ fn_distro
fn_uptime fn_uptime
fn_load fn_load
{ {
echo -e "=========================================\nServer information\n=========================================\n" 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 "Date: $(date)"
echo -e "Distro: ${os}" echo -e "Distro: ${os}"
echo -e "Arch: ${arch}" echo -e "Arch: ${arch}"
@ -148,139 +296,95 @@ echo -e "Kernel: ${kernel}"
echo -e "Hostname: $HOSTNAME" echo -e "Hostname: $HOSTNAME"
echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m" echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
echo -e "Avg Load${load}\n" echo -e "Avg Load${load}\n"
echo -e "=========================================\n${servicename} statistics\n=========================================\n" echo -e "========================================\nLogs\n========================================\n"
echo -e "Service: ${servicename}" echo -e "Script log\n===================\n"
echo -e "Server: ${servername}" }|tee ${scriptlogdir}/${servicename}-email.log > /dev/null 2>&1
echo -e "Failure reason: ${failurereason}" tail -25 ${scriptlog} >> ${emaillog}
echo -e "Action Taken: ${actiontaken}\n" if [ ! -z ${consolelog} ];then
echo -e "=========================================\n${servicename} log summary\n=========================================\n" echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
}|tee ${logdir}/${servicename}-email.log > /dev/null 2>&1 tail -25 ${consolelog} >> ${emaillog}
tail -50 ${logfile} >> /${logdir}/${servicename}-email.log fi
mail -s "${subject}" ${email} < /${logdir}/${servicename}-email.log if [ ! -z ${gamelogdir} ];then
echo -e "[\e[0;36m INFO \e[0;39m] Sent email notification to ${email}" echo -e "\n\nServer log\n====================\n" >> ${emaillog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile} 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_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${scriptlog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Email Test Notification - Testing ${servername}" subject="${servicename} Email Test Notification - Testing ${servername}"
failurereason="Testing ${servicename} email notification" failurereason="Testing ${servicename} email notification"
actiontaken="Sent test email...hello is this thing on?" actiontaken="Sent test email...hello is this thing on?"
fn_emailnotification fn_emailnotification
else else
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo -e "[\e[0;31m FAIL \e[0;39m] Email notification not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notification not enabled" >> ${scriptlog}
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 fi
sleep 0.5
echo -en "\n"
} }
fn_restartserver(){ fn_serverquery(){
echo -e "[\e[0;32m OK \e[0;39m] Restarting ${servicename}: ${servername}" # uses serverquery.py to directly query the server
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${logfile} # detects if the server locks up
fn_stopserver if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1 sleep 1
fn_startserver if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
} echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
fn_startserver(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ${filesdir}/srcds_run ${parms}
sleep 1 sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
if [ ${pidwc} -eq 0 ];then echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}" sleep 1
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile} fn_restartserver
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${logfile}
else else
echo -e "[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5
echo -e ""
echo -e "Use for identifying server issues only!"
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1 sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" fi
sleep 3 fi
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
${filesdir}/srcds_run ${parms} -debug
} }
fn_monitorserver(){ fn_monitorserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager fn_logmanager
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo -en "[ .... ] Monitor is checking ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is checking ${servername}" >> ${scriptlog}
sleep 0.5
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then if [ "${updatecheck}" = "0" ];then
fn_runcheck fn_runcheck
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo -en "\r[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}" subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped" failurereason="${servicename} process was not running"
actiontaken="restarted ${servername}" actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification fn_emailnotification
fi fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver fn_startserver
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi fi
} }
@ -288,80 +392,60 @@ fn_updateserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${rootdir} cd ${rootdir}
cd steamcmd cd steamcmd
./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${logfile} ./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${scriptlog}
} }
fn_details(){ fn_restartserver(){
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
echo "${gamename} Server Details" fn_stopserver
echo "============================" fn_startserver
echo "" }
echo "${servername} Ports"
echo "=======================" fn_stopserver(){
if [ "${engine}" = "unreal2" ];then fn_rootcheck
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') fn_syscheck
queryport=$((${gameport} + 1)) pid=$(tmux list-sessions 2>&1|awk '{print $1}')
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') echo -en "[ .... ] Stopping ${servicename}: ${servername}"
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') sleep 0.5
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') if [ "${pid}" == "failed" ];then
echo "Ports the server is currently using" echo -en "\r[\e[0;31m FAIL \e[0;39m] Stopping ${servicename}: ${servername} is already stopped"
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${scriptlog}
echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi
if [ "${appid}" == "215360" ];then
echo "OUTBOUND Master Server port 28852 TCP/UDP n/a"
else else
echo "OUTBOUND Master Server port 28900/28902 TCP/UDP n/a" tmux kill-session -t ${servicename}
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}
fi fi
if [ "${appid}" ];then sleep 0.5
if [ "${appid}" == "223250" ];then echo -en "\n"
echo "OUTBOUND Steam Port 20610 UDP n/a" }
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${filesdir}
tmux new-session -d -s ${servicename} "./srcds_run ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${scriptlog}
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" 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 fi
fi sleep 0.5
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}" echo -en "\n"
echo ""
echo "${servername} WebAdmin"
echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}"
echo ""
echo "Config file"
echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo ""
echo "DIRECTION DESCRIPTION PORT"
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then
echo "INBOUND SourceTV Port ${sourcetvport}"
fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
fi
echo ""
} }
# #
@ -441,8 +525,6 @@ echo "Installing ${gamename} Server"
echo "=================================" echo "================================="
cd ${rootdir}/steamcmd cd ${rootdir}/steamcmd
mkdir -v ${filesdir} mkdir -v ${filesdir}
mkdir -v ${rootdir}/log
mkdir -v ${logdir}
touch install.txt touch install.txt
chmod 0600 install.txt chmod 0600 install.txt
echo "login anonymous" > install.txt echo "login anonymous" > install.txt
@ -489,7 +571,11 @@ echo "================================="
}|tee ${servercfg} > /dev/null 2>&1 }|tee ${servercfg} > /dev/null 2>&1
sleep 1 sleep 1
echo "" echo ""
echo "Setting up logging" echo "Creating log directorys"
mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
if [ ! -h ${rootdir}/log/server ]; then if [ ! -h ${rootdir}/log/server ]; then
ln -sv ${gamelogdir} ${rootdir}/log/server ln -sv ${gamelogdir} ${rootdir}/log/server
else else
@ -525,16 +611,20 @@ case "$1" in
fn_startserver;; fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install)
fn_install;;
debug)
fn_debugserver;;
email-test) email-test)
fn_emailtest;; fn_emailtest;;
details) details)
fn_details;; fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug)
fn_debugserver;;
install)
fn_install;;
*) *)
echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install}"
exit 1;; exit 1;;
esac esac
exit exit

515
TheHiddenSource/thsserver

@ -1,9 +1,9 @@
#!/bin/bash #!/bin/bash
# The Hidden: Source # Hidden: Source
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 070913 # Version: 011013
#### Variables #### #### Variables ####
@ -29,19 +29,22 @@ filesdir="${rootdir}/serverfiles"
servercfg="${filesdir}/hidden/cfg/server.cfg" servercfg="${filesdir}/hidden/cfg/server.cfg"
# Server Details # Server Details
servicename="ths-server" servicename="hs-server"
gamename="The Hidden: Source" gamename="Hidden: Source"
engine="source" engine="source"
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'` servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'` rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
# Logging # Logging
logdir="${rootdir}/log/script"
gamelogdir="${filesdir}/hidden/logs"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
gamelogdir="${filesdir}/hidden/logs"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
##### Script ##### ##### Script #####
# Do not edit # Do not edit
@ -62,49 +65,184 @@ if [ ! -e ${filesdir} ];then
fi fi
} }
fn_serverquery(){ fn_runcheck(){
# uses serverquery.py to directly query the server tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
# detects if the server locks up if [ ${tmuxwc} -eq 1 ];then
if [ -f serverquery.py ];then fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected" echo -en "\r[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING" sleep 0.5
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile} echo -en "\n"
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1` exit
sleep 1 fi
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then }
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} fn_logmanager(){
sleep 1 # log manager will active if finds logs older than ${logdays}
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!" if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile} 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}
find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -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_debugserver(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Debug"
echo "============================"
echo ""
echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
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_stopserver
echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${filesdir}
./srcds_run ${parms} -debug
}
fn_console(){
fn_rootcheck
fn_syscheck
echo ""
echo "${gamename} Console"
echo "============================"
echo ""
echo "Press \"CTRL+b d\" to exit console"
echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo ""
while true; do
read -p "Continue? [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1 sleep 1
fn_restartserver tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile} sleep 0.5
sleep 1 while true; do
read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
}
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
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];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 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_runcheck(){ fn_details(){
# already running check echo ""
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo "${gamename} Server Details"
if [ ${pidwc} -eq 1 ];then echo "============================"
fn_serverquery echo ""
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" echo "${servername} Ports"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} echo "======================="
exit servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
# multiple of same instance check rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
elif [ ${pidwc} -ge 2 ];then echo "Ports the server is currently using"
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile} echo "DIRECTION DESCRIPTION PORT"
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}" echo "INBOUND Game/Rcon Port ${port}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile} if [ "${engine}" = "source" ];then
fn_stopserver echo "INBOUND SourceTV Port ${sourcetvport}"
exit
fi fi
echo "OUTBOUND Client Port ${clientport}"
echo ""
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
echo ""
} }
fn_distro(){ fn_distro(){
@ -138,7 +276,13 @@ fn_distro
fn_uptime fn_uptime
fn_load fn_load
{ {
echo -e "=========================================\nServer information\n=========================================\n" 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 "Date: $(date)"
echo -e "Distro: ${os}" echo -e "Distro: ${os}"
echo -e "Arch: ${arch}" echo -e "Arch: ${arch}"
@ -146,227 +290,156 @@ echo -e "Kernel: ${kernel}"
echo -e "Hostname: $HOSTNAME" echo -e "Hostname: $HOSTNAME"
echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m" echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
echo -e "Avg Load${load}\n" echo -e "Avg Load${load}\n"
echo -e "=========================================\n${servicename} statistics\n=========================================\n" echo -e "========================================\nLogs\n========================================\n"
echo -e "Service: ${servicename}" echo -e "Script log\n===================\n"
echo -e "Server: ${servername}" }|tee ${scriptlogdir}/${servicename}-email.log > /dev/null 2>&1
echo -e "Failure reason: ${failurereason}" tail -25 ${scriptlog} >> ${emaillog}
echo -e "Action Taken: ${actiontaken}\n" if [ ! -z ${consolelog} ];then
echo -e "=========================================\n${servicename} log summary\n=========================================\n" echo -e "\n\nConsole log\n====================\n" >> ${emaillog}
}|tee ${logdir}/${servicename}-email.log > /dev/null 2>&1 tail -25 ${consolelog} >> ${emaillog}
tail -50 ${logfile} >> /${logdir}/${servicename}-email.log fi
mail -s "${subject}" ${email} < /${logdir}/${servicename}-email.log if [ ! -z ${gamelogdir} ];then
echo -e "[\e[0;36m INFO \e[0;39m] Sent email notification to ${email}" echo -e "\n\nServer log\n====================\n" >> ${emaillog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile} 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_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${scriptlog}
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Email Test Notification - Testing ${servername}" subject="${servicename} Email Test Notification - Testing ${servername}"
failurereason="Testing ${servicename} email notification" failurereason="Testing ${servicename} email notification"
actiontaken="Sent test email...hello is this thing on?" actiontaken="Sent test email...hello is this thing on?"
fn_emailnotification fn_emailnotification
else else
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo -e "[\e[0;31m FAIL \e[0;39m] Email notification not enabled"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notification not enabled" >> ${scriptlog}
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 fi
sleep 0.5
echo -en "\n"
} }
fn_restartserver(){ fn_serverquery(){
echo -e "[\e[0;32m OK \e[0;39m] Restarting ${servicename}: ${servername}" # uses serverquery.py to directly query the server
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${logfile} # detects if the server locks up
fn_stopserver if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1 sleep 1
fn_startserver if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
} echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
fn_startserver(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ${filesdir}/srcds_run ${parms}
sleep 1 sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
if [ ${pidwc} -eq 0 ];then echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}" sleep 1
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile} fn_restartserver
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${logfile}
else else
echo -e "[\e[0;32m OK \e[0;39m] Stopping ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi
}
fn_debugserver(){
fn_rootcheck
fn_syscheck
cd ${filesdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!"
sleep 0.5
echo -e ""
echo -e "Use for identifying server issues only!"
while true; do
echo "If server is already running it will be stopped"
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_stopserver
sleep 1 sleep 1
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" fi
sleep 3 fi
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
${filesdir}/srcds_run ${parms} -debug
} }
fn_monitorserver(){ fn_monitorserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager fn_logmanager
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo -en "[ .... ] Monitor is checking ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is checking ${servername}" >> ${scriptlog}
sleep 0.5
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then if [ "${updatecheck}" = "0" ];then
fn_runcheck fn_runcheck
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo -en "\r[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${scriptlog}
if [ "${emailnotification}" = "on" ];then if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}" subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servername} had stopped" failurereason="${servicename} process was not running"
actiontaken="restarted ${servername}" actiontaken="restarted ${servicename}"
sleep 0.5
echo -en "\n"
fn_emailnotification fn_emailnotification
fi fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver fn_startserver
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi fi
} }
fn_updateserver-hlds(){ fn_updateserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}" echo -e "[\e[0;32m OK \e[0;39m] Updating ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Updating ${servername}" >> ${scriptlog}
cd ${filesdir} cd ${rootdir}
./steam -command update -game episode1 -verify_all -retry -dir ${filesdir} cd steamcmd
./steamcmd.sh +login anonymous +force_install_dir ${filesdir} +app_update ${appid} validate +quit |tee -a ${scriptlog}
} }
fn_details(){ fn_restartserver(){
echo "" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
echo "${gamename} Server Details" fn_stopserver
echo "============================" fn_startserver
echo "" }
echo "${servername} Ports"
echo "=======================" fn_stopserver(){
if [ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ];then fn_rootcheck
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') fn_syscheck
queryport=$((${gameport} + 1)) pid=$(tmux list-sessions 2>&1|awk '{print $1}')
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') echo -en "[ .... ] Stopping ${servicename}: ${servername}"
udplinkport=$((${gameport} + 2)) echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') sleep 0.5
if [ "${engine}" == "unreal" ];then if [ "${pid}" == "failed" ];then
webadminuser=$(grep AdminUsername= ${systemdir}/${ini}|sed 's/\AdminUsername=//g') echo -en "\r[\e[0;31m FAIL \e[0;39m] Stopping ${servicename}: ${servername} is already stopped"
else echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already stopped" >> ${scriptlog}
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g')
fi
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g')
echo "Ports the server is currently using"
echo ""
echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${engine}" == "unreal" ];then
echo "OUTBOUND UdpLink Port (random) ${udplinkport}+ UDP n/a"
fi
if [ "${engine}" != "unreal" ] && [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi
if [ "${appid}" == "215360" ];then
echo "OUTBOUND Master Server port 28852 TCP/UDP n/a"
else else
echo "OUTBOUND Master Server port 28900/28902 TCP/UDP n/a" tmux kill-session -t ${servicename}
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}
fi fi
if [ "${appid}" ];then sleep 0.5
if [ "${appid}" == "223250" ];then echo -en "\n"
echo "OUTBOUND Steam Port 20610 UDP n/a" }
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${filesdir}
tmux new-session -d -s ${servicename} "./srcds_run ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${scriptlog}
else else
echo "OUTBOUND Steam Port 20660 UDP n/a" echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
fi echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername}" >> ${scriptlog}
fi
echo "INBOUND WebAdmin ${webadminport} TCP ListenPort=${webadminport}"
echo ""
echo "${servername} WebAdmin"
echo "======================="
echo "WebAdmin URL: http://localhost:${webadminport}"
echo "WebAdmin Username: ${webadminuser}"
echo "WebAdmin Password: ${webadminpass}"
echo ""
echo "Config file"
echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo ""
echo "DIRECTION DESCRIPTION PORT"
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then
echo "INBOUND SourceTV Port ${sourcetvport}"
fi fi
echo "OUTBOUND Client Port ${clientport}" sleep 0.5
echo "" echo -en "\n"
echo "You can change ports by editing the"
echo "start parameters in ${selfname}"
echo ""
echo "${servername} Details"
echo "======================="
echo "Server name: ${servername}"
echo "Rcon Password: ${rcon}"
echo ""
echo "Config file:"
echo "${servercfg}"
fi
echo ""
} }
# #
@ -496,7 +569,9 @@ echo "Installing ${gamename} Server"
echo "=================================" echo "================================="
mkdir -v ${filesdir} mkdir -v ${filesdir}
mkdir -v ${rootdir}/log mkdir -v ${rootdir}/log
mkdir -v ${logdir} mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
fn_hldsdl fn_hldsdl
echo "Downloading episode 1 game assets" echo "Downloading episode 1 game assets"
echo "=================================" echo "================================="

578
UnrealTournament2004/ut2k4server

@ -3,7 +3,7 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
@ -17,6 +17,7 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
systemdir="${filesdir}/System" systemdir="${filesdir}/System"
backupdir="${rootdir}/backups"
compressedmapsdir="${rootdir}/Maps-Compressed" compressedmapsdir="${rootdir}/Maps-Compressed"
# Server names # Server names
@ -27,10 +28,16 @@ ini="${servicename}.ini"
servername=`grep -s ServerName= ${systemdir}/${ini} | sed 's/ServerName=//g'` servername=`grep -s ServerName= ${systemdir}/${ini} | sed 's/ServerName=//g'`
# Logging # Logging
logdir="${rootdir}/log"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
# Start vars # Start vars
defaultmap="DM-Rankin" defaultmap="DM-Rankin"
@ -55,185 +62,76 @@ if [ ! -e ${systemdir} ];then
fi fi
} }
fn_serverquery(){
# uses serverquery.py to directly query the server
# detects if the server locks up
if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile}
sleep 1
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile}
sleep 1
fn_restartserver
else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile}
sleep 1
fi
fi
}
fn_runcheck(){ fn_runcheck(){
# already running check tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` if [ ${tmuxwc} -eq 1 ];then
if [ ${pidwc} -eq 1 ];then
fn_serverquery fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" 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" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
exit sleep 0.5
# multiple of same instance check echo -en "\n"
elif [ ${pidwc} -ge 2 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile}
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile}
fn_stopserver
exit exit
fi fi
} }
fn_compressmaps(){
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] Sent email notification to ${email}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile}
}
fn_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck clear
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo "${gamename} Map Compressor"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile} echo "============================"
if [ "${emailnotification}" = "on" ];then echo "Will compress all maps in:"
subject="${servicename} Email Test Notification - Testing ${servername}" echo ""
failurereason="Testing ${servicename} email notification" pwd
actiontaken="Sent test email...hello is this thing on?" echo ""
fn_emailnotification echo "Compressed maps saved to:"
else echo ""
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo "${compressedmapsdir}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo ""
fi while true; do
read -p "Start compression [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
mkdir ${compressedmapsdir}
rm -rfv ${filesdir}/Maps/*.uz2
cd ${systemdir}
./ucc-bin compress ../Maps/* --nohomedir
mv -fv ${filesdir}/Maps/*.uz2 ${compressedmapsdir}
} }
fn_logmanager(){ fn_logmanager(){
# log manager will active if finds logs older than $logdays # log manager will active if finds logs older than ${logdays}
if [ `find ${logdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "[\e[0;32m OK \e[0;39m] Starting log cleaner" 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 "$( 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 "[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Removing logs older than ${logdays} days" >> ${scriptlog}
find ${logdir}/* -mtime +${logdays} |tee >> ${logfile} find ${scriptlogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
count=`find ${logdir}/* -mtime +${logdays}|wc -l` find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
find ${logdir}/* -mtime +${logdays} -exec rm {} \; scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -mtime +${logdays} -exec rm {} \;
echo -e "[\e[0;36m INFO \e[0;39m] Log cleaner removed ${count} log files" 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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Log cleaner removed ${count} log files" >> ${scriptlog}
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(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${systemdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ./ucc-bin ${parms}
sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l`
if [ ${pidwc} -eq 0 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile}
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is 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}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi fi
} }
fn_debugserver(){ fn_debugserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
cd ${filesdir} echo ""
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!" echo "${gamename} Debug"
sleep 0.5 echo "============================"
echo -e "" echo ""
echo -e "Use for identifying server issues only!" echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
echo ""
while true; do while true; do
echo "If server is already running it will be stopped"
read -p "Continue? [y/N]" yn read -p "Continue? [y/N]" yn
case $yn in case $yn in
[Yy]* ) break;; [Yy]* ) break;;
@ -242,67 +140,102 @@ while true; do
esac esac
done done
fn_stopserver fn_stopserver
sleep 1 echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 3 sleep 0.5
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}" echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${systemdir} cd ${systemdir}
./ucc-bin ${parms} ./ucc-bin ${parms}
} }
fn_monitorserver(){ fn_console(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager echo ""
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo "${gamename} Console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo "============================"
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` echo ""
if [ "${updatecheck}" = "0" ];then echo "Press \"CTRL+b d\" to exit console"
fn_runcheck echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${logfile} while true; do
if [ "${emailnotification}" = "on" ];then read -p "Continue? [y/N]" yn
subject="${servicename} Monitor - Starting ${servername}" case $yn in
failurereason="${servername} had stopped" [Yy]* ) break;;
actiontaken="restarted ${servername}" [Nn]* ) echo Exiting; return 1 ;;
fn_emailnotification [Nn]* ) echo Exiting; return 1 ;;
fi * ) echo "Please answer yes or no.";;
fn_startserver esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1
tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} sleep 0.5
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" while true; do
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
} }
fn_compressmaps(){ fn_backupserver(){
fn_rootcheck fn_rootcheck
clear fn_syscheck
echo "${gamename} Map Compressor" backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
echo ""
echo "${gamename} Backup"
echo "============================" echo "============================"
echo "Will compress all maps in:"
echo "" echo ""
pwd echo "The following backup will be created."
echo "" echo ""
echo "Compressed maps saved to:" echo "${backupdir}/${backupname}.tar.gz"
echo ""
echo "$compressedmapsdir"
echo "" echo ""
while true; do while true; do
read -p "Start compression [y/N]" yn read -p "Continue? [y/N]" yn
case $yn in case $yn in
[Yy]* ) break;; [Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;; [Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";; * ) echo "Please answer yes or no.";;
esac esac
done done
mkdir $compressedmapsdir tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
rm -rfv ${filesdir}/Maps/*.uz2 if [ ${tmuxwc} -eq 1 ];then
cd ${systemdir} echo -e "\e[0;31mWARNING!\e[0;39m ${servicename} is currently running"
./ucc-bin compress ../Maps/* --nohomedir while true; do
mv -fv ${filesdir}/Maps/*.uz2 $compressedmapsdir 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_details(){ fn_details(){
@ -312,19 +245,26 @@ echo "============================"
echo "" echo ""
echo "${servername} Ports" echo "${servername} Ports"
echo "=======================" echo "======================="
if [ "${engine}" = "unreal2" ];then
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g')
queryport=$((${gameport} + 1)) queryport=$((${gameport} + 1))
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g')
udplinkport=$((${gameport} + 2))
webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g') webadminport=$(grep ListenPort= ${systemdir}/${ini}|sed 's/\ListenPort=//g')
if [ "${engine}" == "unreal" ];then
webadminuser=$(grep AdminUsername= ${systemdir}/${ini}|sed 's/\AdminUsername=//g')
else
webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g') webadminuser=$(grep AdminName= ${systemdir}/${ini}|sed 's/\AdminName=//g')
fi
webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g') webadminpass=$(grep AdminPassword= ${systemdir}/${ini}|sed 's/\AdminPassword=//g')
echo "Ports the server is currently using" echo "Ports the server is currently using"
echo "" echo ""
echo "DIRECTION DESCRIPTION PORT INI VARIABLE" echo "DIRECTION DESCRIPTION PORT INI VARIABLE"
echo "INBOUND Game Port ${gameport} UDP Port=${gameport}" echo "INBOUND Game Port ${gameport} UDP Port=${gameport}"
echo "INBOUND Query Port ${queryport} UDP n/a" echo "INBOUND Query Port ${queryport} UDP n/a"
if [ "${appid}" != "223250" ];then if [ "${engine}" == "unreal" ];then
echo "OUTBOUND UdpLink Port (random) ${udplinkport}+ UDP n/a"
fi
if [ "${engine}" != "unreal" ] && [ "${appid}" != "223250" ];then
echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}" echo "INBOUND GameSpy Query Port ${gsqueryport} UDP OldQueryPortNumber=${gsqueryport}"
fi fi
if [ "${appid}" == "215360" ];then if [ "${appid}" == "215360" ];then
@ -349,30 +289,192 @@ if [ "${engine}" = "unreal2" ];then
echo "" echo ""
echo "Config file" echo "Config file"
echo "${systemdir}/${ini}" echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo "" echo ""
echo "DIRECTION DESCRIPTION PORT" }
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then fn_distro(){
echo "INBOUND SourceTV Port ${sourcetvport}" 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 fi
echo "OUTBOUND Client Port ${clientport}" }
echo ""
echo "You can change ports by editing the" fn_uptime(){
echo "start parameters in ${selfname}" uptime=$(</proc/uptime)
echo "" uptime=${uptime%%.*}
echo "${servername} Details" minutes=$(( uptime/60%60 ))
echo "=======================" hours=$(( uptime/60/60%24 ))
echo "Server name: ${servername}" days=$(( uptime/60/60/24 ))
echo "Rcon Password: ${rcon}" }
echo ""
echo "Config file:" fn_load(){
echo "${servercfg}" load=$(uptime | awk -F 'load average' '{ print $2 }')
}
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 fi
echo "" 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}
fi
sleep 0.5
echo -en "\n"
}
fn_serverquery(){
# uses serverquery.py to directly query the server
# detects if the server locks up
if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
sleep 1
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
sleep 1
fn_restartserver
else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
sleep 1
fi
fi
}
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
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then
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}
if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servicename} process was not running"
actiontaken="restarted ${servicename}"
fn_emailnotification
fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver
else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi
}
fn_restartserver(){
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
fn_stopserver
fn_startserver
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=$(tmux list-sessions 2>&1|awk '{print $1}')
echo -en "[ .... ] Stopping ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
sleep 0.5
if [ "${pid}" == "failed" ];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
tmux kill-session -t ${servicename}
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}
fi
sleep 0.5
echo -en "\n"
}
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${systemdir}
tmux new-session -d -s ${servicename} "./ucc-bin ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${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"
} }
# #
@ -488,6 +590,7 @@ echo ""
echo "Installing ${gamename} Server" echo "Installing ${gamename} Server"
echo "=================================" echo "================================="
sleep 1 sleep 1
mkdir -v ${filesdir}
cd ${filesdir} cd ${filesdir}
echo "Extracting dedicatedserver3339-bonuspack.zip" echo "Extracting dedicatedserver3339-bonuspack.zip"
sleep 1 sleep 1
@ -541,8 +644,11 @@ echo "================================="
echo "Copying ${systemdir}/ut2004.ini to ${systemdir}/${ini}" echo "Copying ${systemdir}/ut2004.ini to ${systemdir}/${ini}"
cp ${systemdir}/UT2004.ini ${systemdir}/${ini} cp ${systemdir}/UT2004.ini ${systemdir}/${ini}
sleep 1 sleep 1
echo "Creating log directory" echo "Creating log directorys"
mkdir -v ${logdir} mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
sleep 1 sleep 1
echo "Applying WebAdmin ut2003.css fix!" echo "Applying WebAdmin ut2003.css fix!"
echo "http://forums.tripwireinteractive.com/showpost.php?p=585435&postcount=13" echo "http://forums.tripwireinteractive.com/showpost.php?p=585435&postcount=13"
@ -591,18 +697,22 @@ case "$1" in
fn_restartserver;; fn_restartserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install)
fn_install;;
debug)
fn_debugserver;;
map-compressor)
fn_compressmaps;;
email-test) email-test)
fn_emailtest;; fn_emailtest;;
details) details)
fn_details;; fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug)
fn_debugserver;;
install)
fn_install;;
map-compressor)
fn_compressmaps;;
*) *)
echo "Usage: $0 {start|stop|restart|monitor|install|debug|map-compressor|email-test|details}" echo "Usage: $0 {start|stop|restart|monitor|email-test|details|backup|console|debug|install|map-compressor}"
exit 1;; exit 1;;
esac esac
exit exit

575
UnrealTournament99/ut99server

@ -3,7 +3,7 @@
# Server Management Script # Server Management Script
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk # Website: http://danielgibbs.co.uk
# Version: 310813 # Version: 011013
#### Variables #### #### Variables ####
@ -17,6 +17,7 @@ rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
selfname="$0" selfname="$0"
filesdir="${rootdir}/serverfiles" filesdir="${rootdir}/serverfiles"
systemdir="${filesdir}/System" systemdir="${filesdir}/System"
backupdir="${rootdir}/backups"
compressedmapsdir="${rootdir}/Maps-Compressed" compressedmapsdir="${rootdir}/Maps-Compressed"
# Server names # Server names
@ -27,10 +28,16 @@ ini="${servicename}.ini"
servername=`grep -s ServerName= ${systemdir}/${ini} | sed 's/ServerName=//g'` servername=`grep -s ServerName= ${systemdir}/${ini} | sed 's/ServerName=//g'`
# Logging # Logging
logdir="${rootdir}/log"
logfile="${logdir}/${servicename}.log"
logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log"
logdays="7" logdays="7"
scriptlogdir="${rootdir}/log/script"
consolelogdir="${rootdir}/log/console"
scriptlog="${scriptlogdir}/${servicename}-script.log"
consolelog="${consolelogdir}/${servicename}-console.log"
emaillog="${scriptlogdir}/${servicename}-email.log"
scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
# Start vars # Start vars
defaultmap="DM-Deck16][" defaultmap="DM-Deck16]["
@ -55,185 +62,76 @@ if [ ! -e ${systemdir} ];then
fi fi
} }
fn_serverquery(){
# uses serverquery.py to directly query the server
# detects if the server locks up
if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${logfile}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${logfile}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile}
sleep 1
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${logfile}
sleep 1
fn_restartserver
else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${logfile}
sleep 1
fi
fi
}
fn_runcheck(){ fn_runcheck(){
# already running check tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` if [ ${tmuxwc} -eq 1 ];then
if [ ${pidwc} -eq 1 ];then
fn_serverquery fn_serverquery
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" 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" >> ${logfile} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${scriptlog}
exit sleep 0.5
# multiple of same instance check echo -en "\n"
elif [ ${pidwc} -ge 2 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: ${servername} is running ${pidwc} times"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is running ${pidwc} times" >> ${logfile}
echo -e "[\e[0;32m OK \e[0;39m] ${servicename}: Stopping ${pidwc} instances of ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${pidwc} instances of ${servername}" >> ${logfile}
fn_stopserver
exit exit
fi fi
} }
fn_compressmaps(){
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] Sent email notification to ${email}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: sent email notification to ${email}" >> ${logfile}
}
fn_emailtest(){
fn_rootcheck fn_rootcheck
fn_syscheck clear
echo -e "[\e[0;32m OK \e[0;39m] Emailing test notification" echo "${gamename} Map Compressor"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Emailing test notification" >> ${logfile} echo "============================"
if [ "${emailnotification}" = "on" ];then echo "Will compress all maps in:"
subject="${servicename} Email Test Notification - Testing ${servername}" echo ""
failurereason="Testing ${servicename} email notification" pwd
actiontaken="Sent test email...hello is this thing on?" echo ""
fn_emailnotification echo "Compressed maps saved to:"
else echo ""
echo -e "[\e[0;31m FAIL \e[0;39m] Email notificaion not enabled" echo "${compressedmapsdir}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Email notificaion not enabled" >> ${logfile} echo ""
fi while true; do
read -p "Start compression [y/N]" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";;
esac
done
mkdir ${compressedmapsdir}
rm -rfv ${filesdir}/Maps/*.uz2
cd ${systemdir}
./ucc-bin compress ../Maps/* --nohomedir
mv -fv ${filesdir}/Maps/*.uz2 ${compressedmapsdir}
} }
fn_logmanager(){ fn_logmanager(){
# log manager will active if finds logs older than $logdays # log manager will active if finds logs older than ${logdays}
if [ `find ${logdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then if [ `find ${scriptlogdir}/* -mtime +${logdays} |wc -l` -ne "0" ];then
echo -e "[\e[0;32m OK \e[0;39m] Starting log cleaner" 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 "$( 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 "[\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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Removing logs older than ${logdays} days" >> ${scriptlog}
find ${logdir}/* -mtime +${logdays} |tee >> ${logfile} find ${scriptlogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
count=`find ${logdir}/* -mtime +${logdays}|wc -l` find ${consolelogdir}/* -mtime +${logdays} |tee >> ${scriptlog}
find ${logdir}/* -mtime +${logdays} -exec rm {} \; scriptcount=`find ${scriptlogdir}/* -mtime +${logdays}|wc -l`
consolecount=`find ${consolelogdir}/* -mtime +${logdays}|wc -l`
count=$((${scriptcount} + ${consolecount}))
find ${scriptlogdir}/* -mtime +${logdays} -exec rm {} \;
find ${consolelogdir}/* -mtime +${logdays} -exec rm {} \;
echo -e "[\e[0;36m INFO \e[0;39m] Log cleaner removed ${count} log files" 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} echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Log cleaner removed ${count} log files" >> ${scriptlog}
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(){
fn_rootcheck
fn_syscheck
fn_runcheck
fn_logmanager
cd ${systemdir}
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile}
cp ${logfile} ${logfiledate}
screen -d -m -S ${servicename} ./ucc-bin ${parms}
sleep 1
pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l`
if [ ${pidwc} -eq 0 ];then
echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: Failed to start ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: failed to start ${servername}" >> ${logfile}
fi
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
if [ -z ${pid} ];then
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already stopped"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is 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}
kill `screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'`
screen -wipe > /dev/null 2>&1
fi fi
} }
fn_debugserver(){ fn_debugserver(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
cd ${filesdir} echo ""
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} in debug mode!" echo "${gamename} Debug"
sleep 0.5 echo "============================"
echo -e "" echo ""
echo -e "Use for identifying server issues only!" echo -e "Use for identifying server issues only!"
echo -e "Press CTRL+c to drop out of debug mode"
echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
echo ""
while true; do while true; do
echo "If server is already running it will be stopped"
read -p "Continue? [y/N]" yn read -p "Continue? [y/N]" yn
case $yn in case $yn in
[Yy]* ) break;; [Yy]* ) break;;
@ -242,69 +140,102 @@ while true; do
esac esac
done done
fn_stopserver fn_stopserver
sleep 1 echo -en "[ .... ] Starting ${servicename}: ${servername} debug mode"
echo -e "[\e[0;36m INFO \e[0;39m] Press CTRL+C to drop out of debug mode" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername} debug mode" >> ${scriptlog}
sleep 3 sleep 0.5
echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}" echo -en "\r[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Started ${servername} debug mode" >> ${scriptlog}
sleep 0.5
echo -en "\n"
cd ${systemdir} cd ${systemdir}
./ucc-bin ${parms} ./ucc-bin ${parms}
} }
fn_monitorserver(){ fn_console(){
fn_rootcheck fn_rootcheck
fn_syscheck fn_syscheck
fn_logmanager echo ""
echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" echo "${gamename} Console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} echo "============================"
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l` echo ""
if [ "${updatecheck}" = "0" ];then echo "Press \"CTRL+b d\" to exit console"
fn_runcheck echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" echo ""
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${logfile} while true; do
if [ "${emailnotification}" = "on" ];then read -p "Continue? [y/N]" yn
subject="${servicename} Monitor - Starting ${servername}" case $yn in
failurereason="${servername} had stopped" [Yy]* ) break;;
actiontaken="restarted ${servername}" [Nn]* ) echo Exiting; return 1 ;;
fn_emailnotification [Nn]* ) echo Exiting; return 1 ;;
fi * ) echo "Please answer yes or no.";;
fn_startserver esac
done
echo -en "\r[ .... ] Starting ${servicename} console"
sleep 0.5
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
if [ ${tmuxwc} -eq 1 ];then
echo -e "\r[\e[0;32m OK \e[0;39m] Starting ${servicename} console"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} console accessed" >> ${scriptlog}
sleep 1
tmux attach-session -t $(tmux list-sessions|awk '{print $1}')
else else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!" echo -e "\r[\e[0;31m FAIL \e[0;39m] Starting ${servicename} console: ${servername} not running"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${logfile} sleep 0.5
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete" while true; do
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${logfile} read -p "Do you want to start the server? [y/N]" yn
case $yn in
[Yy]* ) fn_startserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi fi
} }
fn_compressmaps(){ fn_backupserver(){
fn_rootcheck fn_rootcheck
clear fn_syscheck
echo "${gamename} Map Compressor" backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
echo "============================"
echo "Will compress all maps in:"
echo "" echo ""
pwd echo "${gamename} Backup"
echo "============================"
echo "" echo ""
echo "Compressed maps saved to:" echo "The following backup will be created."
echo "" echo ""
echo "${compressedmapsdir}" echo "${backupdir}/${backupname}.tar.gz"
echo "" echo ""
while true; do while true; do
read -p "Start compression [y/N]" yn read -p "Continue? [y/N]" yn
case $yn in case $yn in
[Yy]* ) break;; [Yy]* ) break;;
[Nn]* ) echo Exiting; return 1 ;; [Nn]* ) echo Exiting; return 1 ;;
* ) echo "Please answer yes or no.";; * ) echo "Please answer yes or no.";;
esac esac
done done
mkdir ${compressedmapsdir} tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|wc -l)
rm -rfv ${filesdir}/Maps/*.unr.uz if [ ${tmuxwc} -eq 1 ];then
cd ${systemdir} echo -e "\e[0;31mWARNING!\e[0;39m ${servicename} is currently running"
for f in ../Maps/* while true; do
do read -p "Would you like to stop ${servicename} while running the backup? [y/N]" yn
./ucc-bin compress $f --nohomedir case $yn in
[Yy]* ) fn_stopserver;break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done done
mv -fv ${filesdir}/Maps/*.unr.uz ${compressedmapsdir} 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}' ${rootdir}
echo -en "\r${servicename} backup complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog}
} }
fn_details(){ fn_details(){
@ -314,7 +245,6 @@ echo "============================"
echo "" echo ""
echo "${servername} Ports" echo "${servername} Ports"
echo "=======================" echo "======================="
if [ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ];then
gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g')
queryport=$((${gameport} + 1)) queryport=$((${gameport} + 1))
gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g')
@ -359,30 +289,192 @@ if [ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ];then
echo "" echo ""
echo "Config file" echo "Config file"
echo "${systemdir}/${ini}" echo "${systemdir}/${ini}"
elif [ "${engine}" = "source" ]||[ "${engine}" = "goldsource" ];then
servername=`grep -s hostname ${servercfg} | sed 's/hostname //g'|sed 's/"//g'`
rcon=`grep -s rcon_password ${servercfg} | sed 's/rcon_password //g'|sed 's/"//g'`
echo "Ports the server is currently using"
echo "" echo ""
echo "DIRECTION DESCRIPTION PORT" }
echo "INBOUND Game/Rcon Port ${port}"
if [ "${engine}" = "source" ];then fn_distro(){
echo "INBOUND SourceTV Port ${sourcetvport}" 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 fi
echo "OUTBOUND Client Port ${clientport}" }
echo ""
echo "You can change ports by editing the" fn_uptime(){
echo "start parameters in ${selfname}" uptime=$(</proc/uptime)
echo "" uptime=${uptime%%.*}
echo "${servername} Details" minutes=$(( uptime/60%60 ))
echo "=======================" hours=$(( uptime/60/60%24 ))
echo "Server name: ${servername}" days=$(( uptime/60/60/24 ))
echo "Rcon Password: ${rcon}" }
echo ""
echo "Config file:" fn_load(){
echo "${servercfg}" load=$(uptime | awk -F 'load average' '{ print $2 }')
}
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 fi
echo "" 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}
fi
sleep 0.5
echo -en "\n"
}
fn_serverquery(){
# uses serverquery.py to directly query the server
# detects if the server locks up
if [ -f serverquery.py ];then
echo -e "[\e[0;36m INFO \e[0;39m] serverquery.py detected"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: serverquery.py detected" >> ${scriptlog}
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: QUERYING"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: QUERYING" >> ${scriptlog}
serverquery=`./serverquery.py 127.0.0.1 ${port} 2>&1`
sleep 1
if [ "${serverquery}" = "ERROR 1" ]||[ "${serverquery}" = "ERROR 2" ]||[ "${serverquery}" = "ERROR 3" ];then
echo -e "[\e[0;31m FAIL \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
sleep 1
echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} has locked up!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} has locked up!" >> ${scriptlog}
sleep 1
fn_restartserver
else
echo -e "[\e[0;32m OK \e[0;39m] Querying ${servicename}: ${servername}: ${serverquery}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Querying ${servername}: ${serverquery}" >> ${scriptlog}
sleep 1
fi
fi
}
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
updatecheck=`ps -ef|grep "${selfname} update"|grep -v grep|wc -l`
if [ "${updatecheck}" = "0" ];then
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}
if [ "${emailnotification}" = "on" ];then
subject="${servicename} Monitor - Starting ${servername}"
failurereason="${servicename} process was not running"
actiontaken="restarted ${servicename}"
fn_emailnotification
fi
sleep 0.5
echo -en "\n"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor is starting ${servername}" >> ${scriptlog}
fn_startserver
else
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Monitor detected ${servername} is currently checking for updates!"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} is currently checking for updates!" >> ${scriptlog}
echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} will start when updating is complete"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} will start when updating is complete" >> ${scriptlog}
fi
}
fn_restartserver(){
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Restarting ${servername}" >> ${scriptlog}
fn_stopserver
fn_startserver
}
fn_stopserver(){
fn_rootcheck
fn_syscheck
pid=$(tmux list-sessions 2>&1|awk '{print $1}')
echo -en "[ .... ] Stopping ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Stopping ${servername}" >> ${scriptlog}
sleep 0.5
if [ "${pid}" == "failed" ];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
tmux kill-session -t ${servicename}
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}
fi
sleep 0.5
echo -en "\n"
}
fn_startserver(){
fn_rootcheck
fn_syscheck
echo -en "[ .... ] Starting ${servicename}: ${servername}"
echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${scriptlog}
sleep 0.5
fn_runcheck
fn_logmanager
mv ${scriptlog} ${scriptlogdate}
mv ${consolelog} ${consolelogdate}
cd ${systemdir}
tmux new-session -d -s ${servicename} "./ucc-bin ${parms} |tee -a ${consolelog}"
tmuxwc=`tmux list-sessions 2>&1|awk '{print $1}'|wc -l`
sleep 1
if [ ${tmuxwc} -eq 0 ];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}: failed to start ${servername}" >> ${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"
} }
# #
@ -495,6 +587,7 @@ fn_filesdl
echo "" echo ""
echo "Installing ${gamename} Server" echo "Installing ${gamename} Server"
echo "=================================" echo "================================="
mkdir -v ${filesdir}
cd ${filesdir} cd ${filesdir}
tar -zxvf ut-server-436.tar.gz ut-server/ --strip-components=1 tar -zxvf ut-server-436.tar.gz ut-server/ --strip-components=1
tar jxvf UTPGPatch451.tar.bz2 tar jxvf UTPGPatch451.tar.bz2
@ -553,8 +646,11 @@ echo "================================="
echo "Setting WebAdmin port to 8076" echo "Setting WebAdmin port to 8076"
sed -i '467i\ListenPort=8076' ${systemdir}/${ini} sed -i '467i\ListenPort=8076' ${systemdir}/${ini}
sleep 1 sleep 1
echo "Creating log directory" echo "Creating log directorys"
mkdir -v ${logdir} mkdir -v ${rootdir}/log
mkdir -v ${scriptlogdir}
mkdir -v ${consolelogdir}
touch ${consolelog}
sleep 1 sleep 1
sleep 1 sleep 1
fn_header fn_header
@ -574,20 +670,31 @@ case "$1" in
fn_stopserver;; fn_stopserver;;
restart) restart)
fn_restartserver;; fn_restartserver;;
update)
fn_stopserver
fn_updateserver
fn_startserver;;
monitor) monitor)
fn_monitorserver;; fn_monitorserver;;
install)
fn_install;;
debug)
fn_debugserver;;
map-compressor)
fn_compressmaps;;
email-test) email-test)
fn_emailtest;; fn_emailtest;;
details) details)
fn_details;; fn_details;;
backup)
fn_backupserver;;
console)
fn_console;;
debug)
fn_debugserver;;
install)
fn_install;;
map-compressor)
fn_compressmaps;;
steam-auth)
fn_updateserver
fn_steamguard;;
*) *)
echo "Usage: $0 {start|stop|restart|monitor|install|debug|map-compressor|email-test|details}" echo "Usage: $0 {start|stop|restart|update|monitor|email-test|details|backup|console|debug|install|map-compressor|steam-auth}"
exit 1;; exit 1;;
esac esac
exit exit
Loading…
Cancel
Save