diff --git a/Mumble/mumbleserver b/Mumble/mumbleserver index 5906844ff..1c05a8598 100644 --- a/Mumble/mumbleserver +++ b/Mumble/mumbleserver @@ -3,7 +3,7 @@ # Server Management Script # Author: Daniel Gibbs # Website: http://danielgibbs.co.uk -# Version: 221113 +# Version: 250814 ### Variables #### @@ -19,57 +19,96 @@ servicename="mumble-server" # Directorys rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" selfname="$0" -filesdir="${rootdir}/murmur" +lockselfname=$(echo ".${servicename}.lock") +filesdir="${rootdir}/serverfiles" ini="murmur.ini" +executable="./murmur.x86 -fg -ini ${ini}" backupdir="backups" # Logging -logdir="${rootdir}/log" -logfile="${logdir}/${servicename}.log" -logfiledate="${logdir}/${servicename}-$( date '+%d-%m-%Y-%H-%M-%S').log" logdays="7" +logdir="${rootdir}/log" +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 ##### # 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_scriptlog(){ + echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: '$1'" >> ${scriptlog} } -fn_syscheck(){ -if [ ! -e ${filesdir} ];then - echo -e "[\e[0;31m FAIL \e[0;39m] ${servicename}: cannot access ${filesdir}: No such directory" +# [ FAIL ] +fn_printfail(){ + echo -en "\r\033[K[\e[0;31m FAIL \e[0;39m] $@" +} + +fn_printfailnl(){ + echo -e "\r\033[K[\e[0;31m FAIL \e[0;39m] $@" +} + +fn_printok(){ + echo -en "\r\033[K[\e[0;32m OK \e[0;39m] $@" +} + +# [ OK ] +fn_printoknl(){ + echo -e "\r\033[K[\e[0;32m OK \e[0;39m] $@" +} + +fn_printinfo(){ + echo -en "\r\033[K[\e[0;36m INFO \e[0;39m] $@" +} + +fn_printinfonl(){ + echo -e "\r\033[K[\e[0;36m INFO \e[0;39m] $@" +} + +# [ INFO ] +fn_printokinfonl(){ + echo -e "\r\033[K[\e[0;36m INFO \e[0;39m] $@" +} + +fn_printwarn(){ + echo -en "\r\033[K[\e[1;33m WARN \e[0;39m] $@" +} + +fn_printwarnnl(){ + echo -e "\r\033[K[\e[1;33m WARN \e[0;39m] $@" +} + +# [ .... ] +fn_printdots(){ + echo -en "\r\033[K[ .... ] $@" +} + +fn_rootcheck(){ +if [ `whoami` = "root" ]; then + fn_printfailnl "Script will not run as root!" exit fi } -fn_runcheck(){ -# already running check -pidwc=$(screen -ls |grep -E "^${servicename}:" |awk -F . '{print $1}'|awk '{print $1}'|wc -l) -if [ ${pidwc} -eq 1 ];then - echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} is already running" - echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} is already running" >> ${logfile} - exit -# 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}: ${servername} stopping all ${pidwc} instances" - echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} stopping all ${pidwc} instances" >> ${logfile} - fn_stopserver +fn_syscheck(){ +if [ ! -e "${filesdir}" ]; then + fn_printfailnl "Cannot access ${systemdir}: No such directory" exit fi } fn_inicheck(){ if [ ! -e ${filesdir}/${ini} ]; then - echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: ${filesdir}/${ini} is missing" - echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} ${filesdir}/${ini} is missing" >> ${logfile} + fn_printwarnnl "${servicename}: ${filesdir}/${ini} is missing" + fn_scriptlog "${servername} ${filesdir}/${ini} is missing" >> ${scriptlog} fi } @@ -89,34 +128,33 @@ 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 -pidwc=$(screen -ls |grep -E "^${servicename}:" |awk -F . '{print $1}'|awk '{print $1}'|wc -l) -if [ ${pidwc} -eq 1 ];then +tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|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;; + [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} +fn_printdots "Starting backup ${servicename}: ${servername}" +sleep 1 +fn_printok "Starting backup ${servicename}: ${servername}" sleep 1 +fn_scriptlog "Backup started" echo -en "\n" -cd ${rootdir} -mkdir ${backupdir} > /dev/null 2>&1 -tar -cvzf ${backupdir}/${backupname}.tar.gz --exclude ${backupdir} * -echo -en "\r${servicename} backup complete" -echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: backup complete" >> ${scriptlog} +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" } fn_distro(){ @@ -142,7 +180,7 @@ days=$(( uptime/60/60/24 )) } fn_load(){ -load=$(uptime | awk -F 'load average' '{ print $2 }') +load=$(uptime|awk -F 'load average' '{ print $2 }') } fn_emailnotification(){ @@ -150,150 +188,275 @@ 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} + echo -e "========================================\n${servicename} details\n========================================\n" + echo -e "Service: ${servicename}" + echo -e "Server: ${servername}" + echo -e "Failure reason: ${failurereason}" + echo -e "Action Taken: ${actiontaken}\n" + echo -e "========================================\nServer details\n========================================\n" + echo -e "Date: $(date)" + echo -e "Distro: ${os}" + echo -e "Arch: ${arch}" + echo -e "Kernel: ${kernel}" + echo -e "Hostname: $HOSTNAME" + echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m" + echo -e "Avg Load${load}\n" + echo -e "========================================\nLogs\n========================================\n" + echo -e "Script log\n===================\n" +}|tee "${scriptlogdir}/${servicename}-email.log" > /dev/null 2>&1 +tail -25 "${scriptlog}" >> "${emaillog}" +if [ ! -z "${consolelog}" ]; then + echo -e "\n\nConsole log\n====================\n" >> "${emaillog}" + tail -25 "${consolelog}" >> "${emaillog}" +fi +if [ ! -z "${gamelogdir}" ]; then + echo -e "\n\nServer log\n====================\n" >> "${emaillog}" + tail "${gamelogdir}"/*|grep -v "==>"|sed '/^$/d'|tail -25 >> "${emaillog}" +fi +mail -s "${subject}" ${email} < "${emaillog}" +fn_printinfo "Sent email notification to ${email}" +sleep 1 +echo -en "\n" +fn_scriptlog "Sent email notification to ${email}" } 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 +fn_scriptlog "Emailing test notification" +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} + fn_printfailnl "Email notification not enabled" + fn_scriptlog "Email notification not enabled" +fi +sleep 1 +echo -en "\n" +} + +fn_monitorserver(){ +fn_rootcheck +fn_syscheck +if [ ! -f ${lockselfname} ]; then + fn_printinfo "Monitoring ${servicename}: No lock file found: Monitor disabled" + sleep 1 + echo -en "\n" + exit +fi +fn_printdots "Monitoring ${servicename}: ${servername}" +sleep 1 +fn_scriptlog "Monitoring ${servername}" +fn_printdots "Monitoring ${servicename}: Checking session: CHECKING" +sleep 1 +fn_scriptlog "Checking session: CHECKING" +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" + sleep 1 + echo -en "\n" + fn_scriptlog "Checking session: OK" + 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 } 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} +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" + sleep 1 + fn_scriptlog "Starting log cleaner" + sleep 1 + echo -en "\n" + fn_printinfo "Removing logs older than ${logdays} days" + sleep 1 + echo -en "\n" + fn_scriptlog "Removing logs older than ${logdays} days" + sleep 1 + 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 {} \; + fn_printok "Log cleaner removed ${count} log files" + sleep 1 + echo -en "\n" + fn_scriptlog "Log cleaner removed ${count} log files" 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_scriptlog "Restarting ${servername}" +fn_stopserver +fn_startserver +} + +fn_stopserver(){ +fn_rootcheck +fn_syscheck +pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l) +pidwc=$(screen -ls |grep -E "^${servicename}:" |awk -F . '{print $1}'|awk '{print $1}'|wc -l) + +fn_printdots "Stopping ${servicename}: ${servername}" +sleep 1 +fn_scriptlog "Stopping ${servername}" +if [ "${pid}" == "0" ]; then + fn_printfail "Stopping ${servicename}: ${servername} is already stopped" + fn_scriptlog "${servername} is already stopped" +else + #Kill murmur process that spawns separate to tmux process + for s in `tmux list-sessions -F '#{session_name}'` ; do + for pid in `tmux list-panes -s -F '#{pane_pid}' -t "$s"` ; do + kill $pid + done + tmux kill-session -t ${servicename} + fn_printok "Stopping ${servicename}: ${servername}" + fn_scriptlog "Stopped ${servername}" +done +fi +# Remove lock file +rm -f ${lockselfname} +sleep 1 +echo -en "\n" } fn_startserver(){ if [ ! -d ${logdir} ];then mkdir ${logdir} + mkdir ${scriptlogdir} + mkdir ${consolelogdir} echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} Creating log directory ${logdir}" - echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} Creating log directory ${logdir}" >> ${logfile} + fn_scriptlog " Creating log directory ${logdir}" >> ${scriptlog} fi fn_rootcheck fn_syscheck -fn_runcheck -fn_inicheck fn_logmanager -cd ${filesdir} -if [ -f ${logdir} ];then - cp ${logfile} ${logfiledate} +tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l) +if [ ${tmuxwc} -eq 0 ]; then + mv "${scriptlog}" "${scriptlogdate}" + mv "${consolelog}" "${consolelogdate}" fi -echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}" -echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Starting ${servername}" >> ${logfile} -screen -d -m -S ${servicename} ./murmur.x86 -fg -ini ${ini} +fn_printdots "Starting ${servicename}: ${servername}" sleep 1 -pidwc=$(screen -ls |grep -E "^${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}: ${servername} failed to start" - echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: ${servername} failed to start" >> ${logfile} +fn_scriptlog "Starting ${servername}" +if [ ${tmuxwc} -eq 1 ]; then + fn_printinfo "Starting ${servicename}: ${servername} is already running" + sleep 1 + echo -en "\n" + fn_scriptlog "${servername} is already running" + exit fi -} - -fn_stopserver(){ -fn_rootcheck -fn_syscheck -pid=$(screen -ls |grep -E "^${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} +# Create lock file +date > ${lockselfname} +cd ${filesdir} +tmux new-session -d -s ${servicename} "${executable}" +tmux pipe-pane -o -t ${servicename} "exec cat >> '${consolelog}'" +sleep 1 +tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l) +if [ ${tmuxwc} -eq 0 ]; then + fn_printfail "Starting ${servicename}: Failed to start ${servername}" + fn_scriptlog "failed to start ${servername}" 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} - pkill -P `screen -ls |grep -E "^${servicename}:" |awk -F . '{print $1}'|awk '{print $1}'` - screen -wipe > /dev/null 2>&1 + fn_printok "Starting ${servicename}: ${servername}" + fn_scriptlog "Started ${servername}" fi +sleep 1 +echo -en "\n" } fn_debugserver(){ fn_rootcheck fn_syscheck -cd ${filesdir} -if [ -f ${logdir} ];then - cp ${logfile} ${logfiledate} -fi -echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername} Debug mode!" -sleep 0.5 +echo "" +echo "${gamename} Debug" +echo "============================" +echo "" echo -e "Use for identifying server issues only!" -sleep 2 +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 "" +echo "Start parameters:" +echo ${executable} +echo "" 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 ;; + [Nn]* ) echo Exiting; return 1;; * ) echo "Please answer yes or no.";; esac done fn_stopserver -echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: Enter CTRL+C to drop out of debug mode" -sleep 3 -echo -e "[\e[0;32m OK \e[0;39m] Starting ${servicename}: ${servername}" +fn_printdots "Starting debug mode ${servicename}: ${servername}" +sleep 1 +fn_printok "Starting debug mode ${servicename}: ${servername}" +sleep 1 +fn_scriptlog "Started debug mode ${servername}" +echo -en "\n" +cd ${filesdir} ./murmur.x86 -fg -ini ${ini} } -fn_monitorserver(){ +fn_console(){ fn_rootcheck fn_syscheck -fn_logmanager -echo -e "[\e[0;32m OK \e[0;39m] Checking ${servicename}: ${servername}" -echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Checking ${servername}" >> ${logfile} -fn_runcheck -echo -e "[\e[1;33m WARN \e[0;39m] ${servicename}: Monitor detected ${servername} had stopped!" -echo -e "$( date '+%b %d %H:%M:%S') ${servicename}: Monitor detected ${servername} had stopped!" >> ${logfile} -if [ "${emailnotification}" = "on" ];then - subject="${servicename} Monitor - Starting ${servername}" - failurereason="${servername} had stopped" - actiontaken="restarted ${servername}" - fn_emailnotification +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 -E "^${servicename}:"|wc -l) +if [ ${tmuxwc} -eq 1 ]; then + fn_printoknl "Starting ${servicename} console" + sleep 1 + fn_scriptlog "Console accessed" + 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 -fn_startserver } case "$1" in @@ -305,14 +468,16 @@ case "$1" in fn_restartserver;; monitor) fn_monitorserver;; - debug) - fn_debugserver;; - backup) - fn_backupserver;; email-test) fn_emailtest;; + backup) + fn_backupserver;; + console) + fn_console;; + debug) + fn_debugserver;; *) echo "Usage: $0 {start|stop|restart|monitor|debug|backup|email-test}" exit 1;; esac -exit +exit \ No newline at end of file