mumbleserver 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #!/bin/bash
  2. # Mumble
  3. # Server Management Script
  4. # Author: Daniel Gibbs
  5. # Website: http://danielgibbs.co.uk
  6. # Version: 250814
  7. ### Variables ####
  8. # Notification Email
  9. # (on|off)
  10. emailnotification="off"
  11. email="email@example.com"
  12. # Server Details
  13. servername="Mumble"
  14. servicename="mumble-server"
  15. # Directorys
  16. rootdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  17. selfname="$0"
  18. lockselfname=$(echo ".${servicename}.lock")
  19. filesdir="${rootdir}/serverfiles"
  20. ini="murmur.ini"
  21. executable="./murmur.x86 -fg -ini ${ini}"
  22. backupdir="backups"
  23. # Logging
  24. logdays="7"
  25. logdir="${rootdir}/log"
  26. scriptlogdir="${rootdir}/log/script"
  27. consolelogdir="${rootdir}/log/console"
  28. scriptlog="${scriptlogdir}/${servicename}-script.log"
  29. consolelog="${consolelogdir}/${servicename}-console.log"
  30. emaillog="${scriptlogdir}/${servicename}-email.log"
  31. scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
  32. consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
  33. ##### Script #####
  34. # Do not edit
  35. # unless you know
  36. # what you are doing
  37. fn_scriptlog(){
  38. echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: '$1'" >> ${scriptlog}
  39. }
  40. # [ FAIL ]
  41. fn_printfail(){
  42. echo -en "\r\033[K[\e[0;31m FAIL \e[0;39m] $@"
  43. }
  44. fn_printfailnl(){
  45. echo -e "\r\033[K[\e[0;31m FAIL \e[0;39m] $@"
  46. }
  47. fn_printok(){
  48. echo -en "\r\033[K[\e[0;32m OK \e[0;39m] $@"
  49. }
  50. # [ OK ]
  51. fn_printoknl(){
  52. echo -e "\r\033[K[\e[0;32m OK \e[0;39m] $@"
  53. }
  54. fn_printinfo(){
  55. echo -en "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  56. }
  57. fn_printinfonl(){
  58. echo -e "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  59. }
  60. # [ INFO ]
  61. fn_printokinfonl(){
  62. echo -e "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  63. }
  64. fn_printwarn(){
  65. echo -en "\r\033[K[\e[1;33m WARN \e[0;39m] $@"
  66. }
  67. fn_printwarnnl(){
  68. echo -e "\r\033[K[\e[1;33m WARN \e[0;39m] $@"
  69. }
  70. # [ .... ]
  71. fn_printdots(){
  72. echo -en "\r\033[K[ .... ] $@"
  73. }
  74. fn_rootcheck(){
  75. if [ `whoami` = "root" ]; then
  76. fn_printfailnl "Script will not run as root!"
  77. exit
  78. fi
  79. }
  80. fn_syscheck(){
  81. if [ ! -e "${filesdir}" ]; then
  82. fn_printfailnl "Cannot access ${systemdir}: No such directory"
  83. exit
  84. fi
  85. }
  86. fn_inicheck(){
  87. if [ ! -e ${filesdir}/${ini} ]; then
  88. fn_printwarnnl "${servicename}: ${filesdir}/${ini} is missing"
  89. fn_scriptlog "${servername} ${filesdir}/${ini} is missing" >> ${scriptlog}
  90. fi
  91. }
  92. fn_backupserver(){
  93. fn_rootcheck
  94. fn_syscheck
  95. backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
  96. echo ""
  97. echo "${gamename} Backup"
  98. echo "============================"
  99. echo ""
  100. echo "The following backup will be created."
  101. echo ""
  102. echo "${backupdir}/${backupname}.tar.gz"
  103. echo ""
  104. while true; do
  105. read -p "Continue? [y/N]" yn
  106. case $yn in
  107. [Yy]* ) break;;
  108. [Nn]* ) echo Exiting; return 1;;
  109. * ) echo "Please answer yes or no.";;
  110. esac
  111. done
  112. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  113. if [ ${tmuxwc} -eq 1 ]; then
  114. echo -e "\e[0;31mWARNING!\e[0;39m ${servicename} is currently running"
  115. while true; do
  116. read -p "Would you like to stop ${servicename} while running the backup? [y/N]" yn
  117. case $yn in
  118. [Yy]* ) fn_stopserver; break;;
  119. [Nn]* ) break;;
  120. * ) echo "Please answer yes or no.";;
  121. esac
  122. done
  123. fi
  124. fn_printdots "Starting backup ${servicename}: ${servername}"
  125. sleep 1
  126. fn_printok "Starting backup ${servicename}: ${servername}"
  127. sleep 1
  128. fn_scriptlog "Backup started"
  129. echo -en "\n"
  130. cd "${rootdir}"
  131. mkdir -pv "${backupdir}" > /dev/null 2>&1
  132. tar -cvzf "${backupdir}/${backupname}.tar.gz" --exclude "${backupdir}" *
  133. echo -en "\r\033[K${servicename} Backup complete"
  134. fn_scriptlog "Backup complete"
  135. }
  136. fn_distro(){
  137. arch=$(uname -m)
  138. kernel=$(uname -r)
  139. if [ -f /etc/lsb-release ]; then
  140. os=$(lsb_release -s -d)
  141. elif [ -f /etc/debian_version ]; then
  142. os="Debian $(cat /etc/debian_version)"
  143. elif [ -f /etc/redhat-release ]; then
  144. os=$(cat /etc/redhat-release)
  145. else
  146. os="$(uname -s) $(uname -r)"
  147. fi
  148. }
  149. fn_uptime(){
  150. uptime=$(</proc/uptime)
  151. uptime=${uptime%%.*}
  152. minutes=$(( uptime/60%60 ))
  153. hours=$(( uptime/60/60%24 ))
  154. days=$(( uptime/60/60/24 ))
  155. }
  156. fn_load(){
  157. load=$(uptime|awk -F 'load average' '{ print $2 }')
  158. }
  159. fn_emailnotification(){
  160. fn_distro
  161. fn_uptime
  162. fn_load
  163. {
  164. echo -e "========================================\n${servicename} details\n========================================\n"
  165. echo -e "Service: ${servicename}"
  166. echo -e "Server: ${servername}"
  167. echo -e "Failure reason: ${failurereason}"
  168. echo -e "Action Taken: ${actiontaken}\n"
  169. echo -e "========================================\nServer details\n========================================\n"
  170. echo -e "Date: $(date)"
  171. echo -e "Distro: ${os}"
  172. echo -e "Arch: ${arch}"
  173. echo -e "Kernel: ${kernel}"
  174. echo -e "Hostname: $HOSTNAME"
  175. echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
  176. echo -e "Avg Load${load}\n"
  177. echo -e "========================================\nLogs\n========================================\n"
  178. echo -e "Script log\n===================\n"
  179. }|tee "${scriptlogdir}/${servicename}-email.log" > /dev/null 2>&1
  180. tail -25 "${scriptlog}" >> "${emaillog}"
  181. if [ ! -z "${consolelog}" ]; then
  182. echo -e "\n\nConsole log\n====================\n" >> "${emaillog}"
  183. tail -25 "${consolelog}" >> "${emaillog}"
  184. fi
  185. if [ ! -z "${gamelogdir}" ]; then
  186. echo -e "\n\nServer log\n====================\n" >> "${emaillog}"
  187. tail "${gamelogdir}"/*|grep -v "==>"|sed '/^$/d'|tail -25 >> "${emaillog}"
  188. fi
  189. mail -s "${subject}" ${email} < "${emaillog}"
  190. fn_printinfo "Sent email notification to ${email}"
  191. sleep 1
  192. echo -en "\n"
  193. fn_scriptlog "Sent email notification to ${email}"
  194. }
  195. fn_emailtest(){
  196. fn_rootcheck
  197. fn_syscheck
  198. fn_scriptlog "Emailing test notification"
  199. if [ "${emailnotification}" = "on" ]; then
  200. subject="${servicename} Email Test Notification - Testing ${servername}"
  201. failurereason="Testing ${servicename} email notification"
  202. actiontaken="Sent test email...hello is this thing on?"
  203. fn_emailnotification
  204. else
  205. fn_printfailnl "Email notification not enabled"
  206. fn_scriptlog "Email notification not enabled"
  207. fi
  208. sleep 1
  209. echo -en "\n"
  210. }
  211. fn_monitorserver(){
  212. fn_rootcheck
  213. fn_syscheck
  214. if [ ! -f ${lockselfname} ]; then
  215. fn_printinfo "Monitoring ${servicename}: No lock file found: Monitor disabled"
  216. sleep 1
  217. echo -en "\n"
  218. exit
  219. fi
  220. fn_printdots "Monitoring ${servicename}: ${servername}"
  221. sleep 1
  222. fn_scriptlog "Monitoring ${servername}"
  223. fn_printdots "Monitoring ${servicename}: Checking session: CHECKING"
  224. sleep 1
  225. fn_scriptlog "Checking session: CHECKING"
  226. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  227. if [ ${tmuxwc} -eq 1 ]; then
  228. fn_printok "Monitoring ${servicename}: Checking session: OK"
  229. sleep 1
  230. echo -en "\n"
  231. fn_scriptlog "Checking session: OK"
  232. exit
  233. else
  234. fn_printfail "Monitoring ${servicename}: Checking session: FAIL"
  235. fn_scriptlog "Checking session: FAIL"
  236. sleep 1
  237. echo -en "\n"
  238. if [ "${emailnotification}" = "on" ]; then
  239. subject="${servicename} Monitor - Starting ${servername}"
  240. failurereason="${servicename} process not running"
  241. actiontaken="${servicename} has been restarted"
  242. fn_emailnotification
  243. fi
  244. fn_scriptlog "Monitor is starting ${servername}"
  245. fn_startserver
  246. fi
  247. }
  248. fn_logmanager(){
  249. if [ ! -e "${consolelog}" ]; then
  250. touch "${consolelog}"
  251. fi
  252. # log manager will active if finds logs older than ${logdays}
  253. if [ `find "${scriptlogdir}"/* -mtime +${logdays}|wc -l` -ne "0" ]; then
  254. fn_printdots "Starting log cleaner"
  255. sleep 1
  256. fn_printok "Starting log cleaner"
  257. sleep 1
  258. fn_scriptlog "Starting log cleaner"
  259. sleep 1
  260. echo -en "\n"
  261. fn_printinfo "Removing logs older than ${logdays} days"
  262. sleep 1
  263. echo -en "\n"
  264. fn_scriptlog "Removing logs older than ${logdays} days"
  265. sleep 1
  266. find "${scriptlogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}"
  267. find "${consolelogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}"
  268. scriptcount=$(find "${scriptlogdir}"/* -mtime +${logdays}|wc -l)
  269. consolecount=$(find "${consolelogdir}"/* -mtime +${logdays}|wc -l)
  270. count=$((${scriptcount} + ${consolecount}))
  271. find "${scriptlogdir}"/* -mtime +${logdays} -exec rm {} \;
  272. find "${consolelogdir}"/* -mtime +${logdays} -exec rm {} \;
  273. fn_printok "Log cleaner removed ${count} log files"
  274. sleep 1
  275. echo -en "\n"
  276. fn_scriptlog "Log cleaner removed ${count} log files"
  277. fi
  278. }
  279. fn_restartserver(){
  280. fn_scriptlog "Restarting ${servername}"
  281. fn_stopserver
  282. fn_startserver
  283. }
  284. fn_stopserver(){
  285. fn_rootcheck
  286. fn_syscheck
  287. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  288. pidwc=$(screen -ls |grep -E "^${servicename}:" |awk -F . '{print $1}'|awk '{print $1}'|wc -l)
  289. fn_printdots "Stopping ${servicename}: ${servername}"
  290. sleep 1
  291. fn_scriptlog "Stopping ${servername}"
  292. if [ "${pid}" == "0" ]; then
  293. fn_printfail "Stopping ${servicename}: ${servername} is already stopped"
  294. fn_scriptlog "${servername} is already stopped"
  295. else
  296. #Kill murmur process that spawns separate to tmux process
  297. for s in `tmux list-sessions -F '#{session_name}'` ; do
  298. for pid in `tmux list-panes -s -F '#{pane_pid}' -t "$s"` ; do
  299. kill $pid
  300. done
  301. tmux kill-session -t ${servicename}
  302. fn_printok "Stopping ${servicename}: ${servername}"
  303. fn_scriptlog "Stopped ${servername}"
  304. done
  305. fi
  306. # Remove lock file
  307. rm -f ${lockselfname}
  308. sleep 1
  309. echo -en "\n"
  310. }
  311. fn_startserver(){
  312. if [ ! -d ${logdir} ];then
  313. mkdir ${logdir}
  314. mkdir ${scriptlogdir}
  315. mkdir ${consolelogdir}
  316. echo -e "[\e[0;36m INFO \e[0;39m] ${servicename}: ${servername} Creating log directory ${logdir}"
  317. fn_scriptlog " Creating log directory ${logdir}" >> ${scriptlog}
  318. fi
  319. fn_rootcheck
  320. fn_syscheck
  321. fn_logmanager
  322. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  323. if [ ${tmuxwc} -eq 0 ]; then
  324. mv "${scriptlog}" "${scriptlogdate}"
  325. mv "${consolelog}" "${consolelogdate}"
  326. fi
  327. fn_printdots "Starting ${servicename}: ${servername}"
  328. sleep 1
  329. fn_scriptlog "Starting ${servername}"
  330. if [ ${tmuxwc} -eq 1 ]; then
  331. fn_printinfo "Starting ${servicename}: ${servername} is already running"
  332. sleep 1
  333. echo -en "\n"
  334. fn_scriptlog "${servername} is already running"
  335. exit
  336. fi
  337. # Create lock file
  338. date > ${lockselfname}
  339. cd ${filesdir}
  340. tmux new-session -d -s ${servicename} "${executable}"
  341. tmux pipe-pane -o -t ${servicename} "exec cat >> '${consolelog}'"
  342. sleep 1
  343. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  344. if [ ${tmuxwc} -eq 0 ]; then
  345. fn_printfail "Starting ${servicename}: Failed to start ${servername}"
  346. fn_scriptlog "failed to start ${servername}"
  347. else
  348. fn_printok "Starting ${servicename}: ${servername}"
  349. fn_scriptlog "Started ${servername}"
  350. fi
  351. sleep 1
  352. echo -en "\n"
  353. }
  354. fn_debugserver(){
  355. fn_rootcheck
  356. fn_syscheck
  357. echo ""
  358. echo "${gamename} Debug"
  359. echo "============================"
  360. echo ""
  361. echo -e "Use for identifying server issues only!"
  362. echo -e "Press CTRL+c to drop out of debug mode"
  363. echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
  364. echo ""
  365. echo "Start parameters:"
  366. echo ${executable}
  367. echo ""
  368. while true; do
  369. read -p "Continue? [y/N]" yn
  370. case $yn in
  371. [Yy]* ) break;;
  372. [Nn]* ) echo Exiting; return 1;;
  373. * ) echo "Please answer yes or no.";;
  374. esac
  375. done
  376. fn_stopserver
  377. fn_printdots "Starting debug mode ${servicename}: ${servername}"
  378. sleep 1
  379. fn_printok "Starting debug mode ${servicename}: ${servername}"
  380. sleep 1
  381. fn_scriptlog "Started debug mode ${servername}"
  382. echo -en "\n"
  383. cd ${filesdir}
  384. ./murmur.x86 -fg -ini ${ini}
  385. }
  386. fn_console(){
  387. fn_rootcheck
  388. fn_syscheck
  389. echo ""
  390. echo "${gamename} Console"
  391. echo "============================"
  392. echo ""
  393. echo "Press \"CTRL+b d\" to exit console"
  394. echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
  395. echo ""
  396. while true; do
  397. read -p "Continue? [y/N]" yn
  398. case $yn in
  399. [Yy]* ) break;;
  400. [Nn]* ) echo Exiting; return 1;;
  401. * ) echo "Please answer yes or no.";;
  402. esac
  403. done
  404. fn_printdots "Starting ${servicename} console"
  405. sleep 1
  406. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  407. if [ ${tmuxwc} -eq 1 ]; then
  408. fn_printoknl "Starting ${servicename} console"
  409. sleep 1
  410. fn_scriptlog "Console accessed"
  411. tmux attach-session -t ${servicename}
  412. else
  413. fn_printfailnl "Starting ${servicename} console: ${servername} not running"
  414. sleep 1
  415. while true; do
  416. read -p "Do you want to start the server? [y/N]" yn
  417. case $yn in
  418. [Yy]* ) fn_startserver; break;;
  419. [Nn]* ) break;;
  420. * ) echo "Please answer yes or no.";;
  421. esac
  422. done
  423. fi
  424. }
  425. case "$1" in
  426. start)
  427. fn_startserver;;
  428. stop)
  429. fn_stopserver;;
  430. restart)
  431. fn_restartserver;;
  432. monitor)
  433. fn_monitorserver;;
  434. email-test)
  435. fn_emailtest;;
  436. backup)
  437. fn_backupserver;;
  438. console)
  439. fn_console;;
  440. debug)
  441. fn_debugserver;;
  442. *)
  443. echo "Usage: $0 {start|stop|restart|monitor|debug|backup|email-test}"
  444. exit 1;;
  445. esac
  446. exit