From d96829e128dc4035caa8bdb7d344bcb7f3dde602 Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Tue, 2 Dec 2014 11:16:39 +0000 Subject: [PATCH] added several functions --- functions/fn_autoip | 22 +++++++++++ functions/fn_backupserver | 48 +++++++++++++++++++++++ functions/fn_console | 43 +++++++++++++++++++++ functions/fn_logmanager | 47 +++++++++++++++++++++++ functions/fn_monitorserver | 55 +++++++++++++++++++++++++++ functions/fn_serverconfig | 24 +++++++++--- functions/fn_serverquery | 78 ++++++++++++++++++++++++++++++++++++++ functions/fn_startserver | 52 +++++++++++++++++++++++++ functions/fn_stopserver | 24 ++++++++++++ 9 files changed, 388 insertions(+), 5 deletions(-) create mode 100644 functions/fn_autoip create mode 100644 functions/fn_backupserver create mode 100644 functions/fn_console create mode 100644 functions/fn_logmanager create mode 100644 functions/fn_monitorserver create mode 100644 functions/fn_serverquery create mode 100644 functions/fn_startserver create mode 100644 functions/fn_stopserver diff --git a/functions/fn_autoip b/functions/fn_autoip new file mode 100644 index 000000000..b626276ce --- /dev/null +++ b/functions/fn_autoip @@ -0,0 +1,22 @@ +#!/bin/bash +# LGSM fn_autoip function +# Author: Daniel Gibbs +# Website: http://danielgibbs.co.uk +# Version: 011214 + +# Identifies the server interface IP +# If multiple interfaces this will need to be set manually + +getip=$(ip -o -4 addr|awk '{print $4 }'|grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}'|grep -v 127.0.0) +getipwc=$(ip -o -4 addr|awk '{print $4 }'|grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}'|grep -vc 127.0.0) +if [ "${ip}" == "0.0.0.0" ]||[ "${ip}" == "" ]; then + if [ "${getipwc}" -ge "2" ]; then + fn_printwarn "Multiple active network interfaces.\n\n" + echo -en "Manually specify the IP you want to use within the ${selfname} script.\n" + echo -en "Set ip=\"0.0.0.0\" to one of the following:\n" + echo -en "${getip}\n" + exit + else + ip=${getip} + fi +fi \ No newline at end of file diff --git a/functions/fn_backupserver b/functions/fn_backupserver new file mode 100644 index 000000000..5048aa83f --- /dev/null +++ b/functions/fn_backupserver @@ -0,0 +1,48 @@ +#!/bin/bash +# LGSM fn_backupserver function +# Author: Daniel Gibbs +# Website: http://danielgibbs.co.uk +# Version: 011214 + +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|grep -Ec "^${servicename}:") +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 +fn_printdots "Starting backup ${servicename}: ${servername}" +sleep 1 +fn_printok "Starting backup ${servicename}: ${servername}" +fn_scriptlog "Backup started" +sleep 1 +echo -en "\n" +cd "${rootdir}" +mkdir -pv "${backupdir}" > /dev/null 2>&1 +tar -cvzf "${backupdir}/${backupname}.tar.gz" --exclude "${backupdir}" ./* +echo -en "\r\033[K${servicename} Backup complete" +fn_scriptlog "Backup complete" \ No newline at end of file diff --git a/functions/fn_console b/functions/fn_console new file mode 100644 index 000000000..9d943411c --- /dev/null +++ b/functions/fn_console @@ -0,0 +1,43 @@ +#!/bin/bash +# LGSM fn_console function +# Author: Daniel Gibbs +# Website: http://danielgibbs.co.uk +# Version: 011214 + +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;; + * ) echo "Please answer yes or no.";; +esac +done +fn_printdots "Starting ${servicename} console" +sleep 1 +tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -Ec "^${servicename}:") +if [ "${tmuxwc}" -eq 1 ]; then + fn_printoknl "Starting ${servicename} console" + fn_scriptlog "Console accessed" + sleep 1 + tmux attach-session -t ${servicename} +else + fn_printfailnl "Starting ${servicename} console: ${servername} not running" + 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 \ No newline at end of file diff --git a/functions/fn_logmanager b/functions/fn_logmanager new file mode 100644 index 000000000..71c108245 --- /dev/null +++ b/functions/fn_logmanager @@ -0,0 +1,47 @@ +#!/bin/bash +# LGSM fn_logmanager function +# Author: Daniel Gibbs +# Website: http://danielgibbs.co.uk +# Version: 011214 + +if [ ! -e "${consolelog}" ]; then + touch "${consolelog}" +fi +# log manager will active if finds logs older than ${logdays} +if [ $(find "${scriptlogdir}"/* -mtime +${logdays}|wc -l) -ne "0" ]; then + fn_printdots "Starting log cleaner" + sleep 1 + fn_printok "Starting log cleaner" + fn_scriptlog "Starting log cleaner" + sleep 1 + echo -en "\n" + fn_printinfo "Removing logs older than ${logdays} days" + fn_scriptlog "Removing logs older than ${logdays} days" + sleep 1 + echo -en "\n" + if [ "${engine}" == "unreal2" ]; then + find "${gamelogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}" + fi + find "${scriptlogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}" + find "${consolelogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}" + if [ "${engine}" == "unreal2" ]; then + gamecount=$(find "${scriptlogdir}"/* -mtime +${logdays}|wc -l) + fi + scriptcount=$(find "${scriptlogdir}"/* -mtime +${logdays}|wc -l) + consolecount=$(find "${consolelogdir}"/* -mtime +${logdays}|wc -l) + count=$((${scriptcount} + ${consolecount})) + if [ "${engine}" == "unreal2" ]; then + count=$((${scriptcount} + ${consolecount} + ${gamecount})) + else + count=$((${scriptcount} + ${consolecount})) + fi + if [ "${engine}" == "unreal2" ]; then + find "${gamelogdir}"/* -mtime +${logdays} -exec rm {} \; + fi + find "${scriptlogdir}"/* -mtime +${logdays} -exec rm {} \; + find "${consolelogdir}"/* -mtime +${logdays} -exec rm {} \; + fn_printok "Log cleaner removed ${count} log files" + fn_scriptlog "Log cleaner removed ${count} log files" + sleep 1 + echo -en "\n" +fi \ No newline at end of file diff --git a/functions/fn_monitorserver b/functions/fn_monitorserver new file mode 100644 index 000000000..35dfa646d --- /dev/null +++ b/functions/fn_monitorserver @@ -0,0 +1,55 @@ +#!/bin/bash +# LGSM fn_monitorserver function +# Author: Daniel Gibbs +# Website: http://danielgibbs.co.uk +# Version: 011214 + +fn_rootcheck +fn_syscheck +fn_autoip +fn_printdots "Monitoring ${servicename}: ${servername}" +fn_scriptlog "Monitoring ${servername}" +sleep 1 +if [ ! -f "${lockselfname}" ]; then + fn_printinfo "Monitoring ${servicename}: Monitor disabled: No lock file found" + fn_scriptlog "Monitor disabled: No lock file found" + sleep 1 + echo -en "\n" + echo "To enable monitor run ${selfname} start" + exit +fi +updatecheck=$(ps -ef|grep "${selfname} update"|grep -v grep|wc -l) +if [ "${updatecheck}" = "0" ]; then + fn_printdots "Monitoring ${servicename}: Checking session: CHECKING" + fn_scriptlog "Checking session: CHECKING" + sleep 1 + tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l) + if [ "${tmuxwc}" -eq 1 ]; then + fn_printok "Monitoring ${servicename}: Checking session: OK" + fn_scriptlog "Checking session: OK" + sleep 1 + echo -en "\n" + fn_serverquery + exit + else + fn_printfail "Monitoring ${servicename}: Checking session: FAIL" + fn_scriptlog "Checking session: FAIL" + sleep 1 + echo -en "\n" + if [ "${emailnotification}" = "on" ]; then + subject="${servicename} Monitor - Starting ${servername}" + failurereason="${servicename} process not running" + actiontaken="${servicename} has been restarted" + fn_emailnotification + fi + fn_scriptlog "Monitor is starting ${servername}" + fn_startserver + fi +else + fn_printinfonl "Monitoring ${servicename}: Detected SteamCMD is checking for updates" + fn_scriptlog "Detected SteamCMD is checking for updates" + sleep 1 + fn_printinfonl "Monitoring ${servicename}: When updates complete ${servicename} will start" + fn_scriptlog "When updates complete ${servicename} will start" + sleep 1 +fi \ No newline at end of file diff --git a/functions/fn_serverconfig b/functions/fn_serverconfig index ec3d5fad5..7cdf01521 100644 --- a/functions/fn_serverconfig +++ b/functions/fn_serverconfig @@ -9,7 +9,18 @@ echo "" echo "Loading Configs" echo "=================================" sleep 1 -cp -v lgsm-default.cfg ${servercfgfullpath} +cp -v lgsm-default.cfg "${servercfgfullpath}" +sleep 1 +echo "" +} + +fn_jc2config(){ +echo "" +echo "Loading Configs" +echo "=================================" +sleep 1 +cd "${filesdir}" +cp -v default_config.lua config.lua sleep 1 echo "" } @@ -20,7 +31,7 @@ echo "Loading Configs" echo "=================================" sleep 1 echo "creating ${servicename}.cfg config file." -cp -v lgsm-default.cfg ${servercfgfullpath} +cp -v lgsm-default.cfg "${servercfgfullpath}" sleep 1 echo "creating server.cfg." touch "${defaultcfg}" @@ -33,15 +44,15 @@ echo "" echo "Configuring ${gamename} Server" echo "=================================" sleep 1 -if [ -z ${autoinstall} ]; then +if [ -z "${autoinstall}" ]; then read -p "Enter server name: " servername read -p "Enter rcon password: " rconpass else servername="${servicename}" rconpass="${rconpass}" fi -sed -i "s/\"hostname\"/\"${servername}\"/g" ${servercfgfullpath} -sed -i "s/\"rconpassword\"/\"${rconpass}\"/g" ${servercfgfullpath} +sed -i "s/\"hostname\"/\"${servername}\"/g" "${servercfgfullpath}" +sed -i "s/\"rconpassword\"/\"${rconpass}\"/g" "${servercfgfullpath}" echo "" } @@ -53,6 +64,9 @@ cd "${servercfgdir}" if [ "${gamename}" == "ARMA 3" ]; then wget --no-check-certificate -nv -N https://raw.githubusercontent.com/dgibbs64/linuxgameservers/master/Arma3/cfg/lgsm-default.cfg fn_arma3config +elif [ "${gamename}" == "Just Cause 2" ]; then + echo "lgsm-default.cfg not required" + fn_jc2config elif [ "${gamename}" == "Counter Strike: Source" ]; then wget --no-check-certificate -nv -N https://raw.githubusercontent.com/dgibbs64/linuxgameservers/master/CounterStrikeSource/cfg/lgsm-default.cfg fn_sourceconfig diff --git a/functions/fn_serverquery b/functions/fn_serverquery new file mode 100644 index 000000000..e52f6590f --- /dev/null +++ b/functions/fn_serverquery @@ -0,0 +1,78 @@ +#!/bin/bash +# LGSM fn_serverquery function +# Author: Daniel Gibbs +# Website: http://danielgibbs.co.uk +# Version: 011214 + +# uses gsquery.py to directly query the server +# detects if the server locks up +if [ -f gsquery.py ]; then + if [ "${engine}" == "unreal" ]||[ "${engine}" == "unreal2" ]; then + gameport=$(grep Port= "${systemdir}/${ini}"|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g') + port=$((${gameport} + 1)) + elif [ "${engine}" == "spark" ]; then + port=$((${port} + 1)) + elif [ "${engine}" == "realvirtuality" ]; then + port=${queryport} + fi + fn_printinfo "Monitoring ${servicename}: Detected gsquery.py" + fn_scriptlog "Detected gsquery.py" + sleep 1 + fn_printdots "Monitoring ${servicename}: Querying port: ${ip}:${port}: QUERYING" + fn_scriptlog "Querying port: ${ip}:${port}: QUERYING" + sleep 1 + serverquery=$(./gsquery.py -a ${ip} -p ${port} -e ${engine} 2>&1) + exitcode=$? + if [ "${exitcode}" == "1" ]||[ "${exitcode}" == "2" ]||[ "${exitcode}" == "3" ]||[ "${exitcode}" == "4" ]; then + fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: ${serverquery}" + sleep 1 + echo -en "\n" + fn_scriptlog "Querying port: ${ip}:${port}: ${serverquery}" + if [[ -z "${secondquery}" ]]; then + fn_printinfo "Monitoring ${servicename}: Waiting 30 seconds to re-query" + fn_scriptlog "Waiting 30 seconds to re-query" + sleep 30 + secondquery=1 + fn_serverquery + fi + if [ "${emailnotification}" = "on" ]; then + subject="${servicename} Monitor - Starting ${servername}" + failurereason="Failed to query ${servicename}: ${serverquery}" + actiontaken="restarted ${servicename}" + fn_emailnotification + fi + fn_restartserver + exit + elif [ "${exitcode}" == "0" ]; then + fn_printok "Monitoring ${servicename}: Querying port: ${ip}:${port}: OK" + fn_scriptlog "Querying port: ${ip}:${port}: OK" + sleep 1 + echo -en "\n" + exit + elif [ "${exitcode}" == "126" ]; then + fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: ERROR: ./gsquery.py: Permission denied" + fn_scriptlog "Querying port: ${ip}:${port}: ./gsquery.py: Permission denied" + sleep 1 + echo -en "\n" + echo "Attempting to resolve automatically" + chmod +x -v gsquery.py + exitcode=$? + if [ "${exitcode}" == "0" ]; then + fn_serverquery + else + echo -en "\nUnable to resolve automatically. Please manually fix permissions\n" + owner=$(ls -al gsquery.py|awk '{ print $3 }') + echo "As user ${owner} or root run the following command" + whoami=$(whoami) + echo -en "\nchown ${whoami}:${whoami} gsquery.py\n\n" + exit + fi + else + fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: UNKNOWN ERROR" + fn_scriptlog "Querying port: ${ip}:${port}: UNKNOWN ERROR" + sleep 1 + echo -en "\n" + ./gsquery.py -a ${ip} -p ${port} -e ${engine} + exit + fi +fi \ No newline at end of file diff --git a/functions/fn_startserver b/functions/fn_startserver new file mode 100644 index 000000000..72924f8e9 --- /dev/null +++ b/functions/fn_startserver @@ -0,0 +1,52 @@ +#!/bin/bash +# LGSM fn_startserver function +# Author: Daniel Gibbs +# Website: http://danielgibbs.co.uk +# Version: 011214 + +fn_rootcheck +fn_syscheck +fn_autoip +fn_parms +fn_logmanager +tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -Ec "^${servicename}:") +if [ "${tmuxwc}" -eq 0 ]; then + fn_scriptlog "Rotating log files" + if [ "${engine}" == "unreal2" ]; then + mv "${gamelog}" "${gamelogdate}" + fi + mv "${scriptlog}" "${scriptlogdate}" + mv "${consolelog}" "${consolelogdate}" +fi +fn_printdots "Starting ${servicename}: ${servername}" +fn_scriptlog "Starting ${servername}" +sleep 1 +if [ "${tmuxwc}" -eq 1 ]; then + fn_printinfo "Starting ${servicename}: ${servername} is already running" + fn_scriptlog "${servername} is already running" + sleep 1 + echo -en "\n" + exit +fi +# Create lock file +date > "${rootdir}/${lockselfname}" +cd "${executabledir}" +tmux new-session -d -s ${servicename} "${executable} ${parms}" 2> "${scriptlogdir}/.${servicename}-tmux-error.tmp" +tmux pipe-pane -o -t ${servicename} "exec cat >> '${consolelog}'" +sleep 1 +tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:") +if [ "${tmuxwc}" -eq 0 ]; then + fn_printfailnl "Starting ${servicename}: Failed to start ${servername}" + echo -en " Check log files: ${rootdir}/log" + fn_scriptlog "failed to start ${servername}" + if [ -a "${scriptlogdir}/.${servicename}-tmux-error.tmp" ]; then + fn_scriptlog "tmux returned the following error" + cat "${scriptlogdir}/.${servicename}-tmux-error.tmp" >> "${scriptlog}" + fi +else + fn_printok "Starting ${servicename}: ${servername}" + fn_scriptlog "Started ${servername}" +fi +rm "${scriptlogdir}/.${servicename}-tmux-error.tmp" +sleep 1 +echo -en "\n" \ No newline at end of file diff --git a/functions/fn_stopserver b/functions/fn_stopserver new file mode 100644 index 000000000..6e77ca3ff --- /dev/null +++ b/functions/fn_stopserver @@ -0,0 +1,24 @@ +#!/bin/bash +# LGSM fn_stopserver function +# Author: Daniel Gibbs +# Website: http://danielgibbs.co.uk +# Version: 011214 + +fn_rootcheck +fn_syscheck +pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:") +fn_printdots "Stopping ${servicename}: ${servername}" +fn_scriptlog "Stopping ${servername}" +sleep 1 +if [ "${pid}" == "0" ]; then + fn_printfail "Stopping ${servicename}: ${servername} is already stopped" + fn_scriptlog "${servername} is already stopped" +else + tmux kill-session -t ${servicename} + fn_printok "Stopping ${servicename}: ${servername}" + fn_scriptlog "Stopped ${servername}" +fi +# Remove lock file +rm -f "${lockselfname}" +sleep 1 +echo -en "\n" \ No newline at end of file