#!/bin/bash # Hidden: Source # Server Management Script # Author: Daniel Gibbs # Website: http://danielgibbs.co.uk # Version: 070913 #### Variables #### # Notification Email # (on|off) emailnotification="off" email="email@example.com" # Start vars defaultmap="hdn_docks" port="27015" sourcetvport="27020" clientport="27005" parms="-game hidden +map ${defaultmap} -port ${port} +tv_port ${sourcetvport} +clientport ${clientport}" #### Advanced Variables #### # Steam appid="4020" # Directorys rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" selfname="$0" filesdir="${rootdir}/serverfiles" servercfg="${filesdir}/hidden/cfg/server.cfg" # Server Details servicename="hs-server" gamename="Hidden: Source" engine="source" 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'` # 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" ##### Script ##### # Do not edit # unless you know # what you are doing fn_rootcheck(){ if [ `whoami` = "root" ];then echo -e "[\e[0;31m FAIL \e[0;39m] Script will not run as root!" exit fi } fn_syscheck(){ if [ ! -e ${filesdir} ];then echo -e "[\e[0;31m FAIL \e[0;39m] Cannot access ${filesdir}: No such directory" exit fi } fn_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(){ # already running check pidwc=`screen -ls |grep ${servicename} |awk -F . '{print $1}'|awk '{print $1}'|wc -l` if [ ${pidwc} -eq 1 ];then fn_serverquery echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} exit # 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=$( /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 ${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 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 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 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}" ${filesdir}/srcds_run ${parms} -debug } 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-hlds(){ 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 ${filesdir} ./steam -command update -game episode1 -verify_all -retry -dir ${filesdir} } fn_details(){ echo "" echo "${gamename} Server Details" echo "============================" echo "" echo "${servername} Ports" 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') queryport=$((${gameport} + 1)) gsqueryport=$(grep OldQueryPortNumber= ${systemdir}/${ini}|sed 's/\OldQueryPortNumber=//g') udplinkport=$((${gameport} + 2)) 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') 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 echo "OUTBOUND Master Server port 28900/28902 TCP/UDP n/a" fi if [ "${appid}" ];then if [ "${appid}" == "223250" ];then echo "OUTBOUND Steam Port 20610 UDP n/a" else echo "OUTBOUND Steam Port 20660 UDP n/a" fi 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 echo "" } # ## Installer # fn_header(){ clear echo "=================================" echo "${gamename}" echo "Server Linux Installer" echo "by Daniel Gibbs" echo "http://danielgibbs.co.uk" echo "=================================" echo "" } fn_hldsdl(){ echo "" echo "Installing hldsupdatetool" echo "=================================" cd ${filesdir} if [ ! -f steam ];then wget http://danielgibbs.co.uk/wp-content/uploads/steam tar --verbose -zxf steamcmd_linux.tar.gz rm -v steamcmd_linux.tar.gz chmod +x steam else echo "hldsupdatetool already installed!" fi ./steam -verify_all -retry } fn_retryinstall(){ while true; do read -p "Retry install? [y/N]" yn case $yn in [Yy]* ) fn_install;; [Nn]* ) echo Exiting; exit ;; * ) echo "Please answer yes or no.";; esac done } fn_filesdl(){ echo "" echo "Downloading ${gamename} Server Files" echo "============================" cd ${rootdir} mkdir ${filesdir} cd ${filesdir} if [ ! -f hsb4b-full.zip ];then wget http://www.hidden-source.com/downloads/hsb4b-full.zip else echo "hsb4b-full.zip already downloaded!" fi echo "Running MD5 checksum to verify hsb4b-full.zip" sleep 1 echo "MD5 checksum: 4aa558231d5f945f78ab55364a16dd60" md5check=`md5sum hsb4b-full.zip| awk '{print $1;}'` echo "File returned: ${md5check}" if [ "${md5check}" != "4aa558231d5f945f78ab55364a16dd60" ];then echo "MD5 checksum: FAILED!" read -p "Retry download? [y/N]" yn case $yn in [Yy]* ) rm -f hsb4b-full.zip; fn_filesdl;; [Nn]* ) echo Exiting; exit ;; * ) echo "Please answer yes or no.";; esac fi echo "MD5 checksum: PASSED" echo "" if [ ! -f hidden-beta4b1-server.zip ];then wget http://www.hidden-source.com/downloads/hidden-beta4b1-server.zip else echo "hidden-beta4b1-server.zip already downloaded!" fi echo "Running MD5 checksum to verify hidden-beta4b1-server.zip" sleep 1 echo "MD5 checksum: 48052ea54f9638b15fe65d23f6008feb" md5check=`md5sum hidden-beta4b1-server.zip| awk '{print $1;}'` echo "File returned: ${md5check}" if [ "${md5check}" != "48052ea54f9638b15fe65d23f6008feb" ];then echo "MD5 checksum: FAILED!" read -p "Retry download? [y/N]" yn case $yn in [Yy]* ) rm -f hidden-beta4b1-server.zip; fn_filesdl;; [Nn]* ) echo Exiting; exit ;; * ) echo "Please answer yes or no.";; esac fi echo "MD5 checksum: PASSED" echo "" } fn_install(){ fn_rootcheck fn_header if [ -d ${filesdir} ];then echo "${gamename} Server is already installed here:" pwd 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 fi fn_header echo "Install Directory:" pwd 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_header echo "" echo "Installing ${gamename} Server" echo "=================================" mkdir -v ${filesdir} mkdir -v ${rootdir}/log mkdir -v ${logdir} fn_hldsdl echo "Downloading episode 1 game assets" echo "=================================" sleep 2 ./steam -command update -game episode1 -verify_all -retry -dir ${filesdir} fn_filesdl unzip hsb4b-full.zip unzip hidden-beta4b1-server.zip echo "" echo "=================================" while true; do read -p "Was the install successful? [y/N]" yn case $yn in [Yy]* ) break;; [Nn]* ) fn_retryinstall ;; * ) echo "Please answer yes or no.";; esac done fn_header echo "Configuring ${gamename} Server" echo "=================================" read -p "Enter server name: " servername read -p "Enter rcon password: " rconpass sleep 1 echo "" echo "Creating server.cfg" touch ${servercfg} { echo -e "// server name" echo -e "hostname \"${servername}\"" echo -e "" echo -e "// rcon passsword" echo -e "rcon_password \"${rconpass}\"" echo -e "" echo -e "// Server password" echo -e "sv_password \"\"" echo -e "" echo -e "// server logging" echo -e "log on" echo -e "sv_logbans 1" echo -e "sv_logecho 1" echo -e "sv_logfile 1" echo -e "sv_log_onefile 0" }|tee ${servercfg} > /dev/null 2>&1 sleep 1 echo "" echo "Creating log directory" mkdir -v ${logdir} echo "Setting up logging" if [ ! -h ${rootdir}/log/server ]; then ln -sv ${gamelogdir} ${rootdir}/log/server else echo "Symbolic link ${gamelogdir} => ${rootdir}/log/server already exists!" fi sleep 1 fn_header fn_details echo "=================================" echo "Install Complete!" echo "" echo "To start server type:" echo "${selfname} start" echo "" } case "$1" in start) fn_startserver;; stop) fn_stopserver;; restart) fn_restartserver;; update) fn_stopserver fn_updateserver-hlds fn_startserver;; monitor) fn_monitorserver;; install) fn_install;; debug) fn_debugserver;; email-test) fn_emailtest;; details) fn_details;; *) echo "Usage: $0 {start|stop|restart|update|monitor|install|debug|email-test|details}" exit 1;; esac exit