command_monitor.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/bash
  2. # LinuxGSM command_monitor.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://linuxgsm.com
  6. # Description: Monitors server by checking for running processes.
  7. # then passes to gamedig and gsquery.
  8. local commandname="MONITOR"
  9. local commandaction="Monitor"
  10. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  11. fn_monitor_loop(){
  12. # Will query up to 5 times every 15 seconds.
  13. # Query will wait up to 60 seconds to confirm server is down giving server time if changing map.
  14. for queryattempt in {1..5}; do
  15. fn_print_dots "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  16. fn_print_querying_eol
  17. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : QUERYING"
  18. sleep 0.5
  19. if [ "${querymethod}" == "gamedig" ];then
  20. query_gamedig.sh
  21. elif [ "${querymethod}" == "gsquery" ];then
  22. if [ ! -f "${functionsdir}/query_gsquery.py" ]; then
  23. fn_fetch_file_github "lgsm/functions" "query_gsquery.py" "${functionsdir}" "chmodx" "norun" "noforce" "nomd5"
  24. fi
  25. "${functionsdir}"/query_gsquery.py -a "${ip}" -p "${queryport}" -e "${engine}" > /dev/null 2>&1
  26. querystatus="$?"
  27. elif [ "${querymethod}" == "telnet" ];then
  28. bash -c 'exec 3<> /dev/tcp/'${ip}'/'${queryport}''
  29. querystatus="$?"
  30. fi
  31. if [ "${querystatus}" == "0" ]; then
  32. # Server query OK
  33. sleep 0.5
  34. fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  35. fn_print_ok_eol_nl
  36. fn_script_log_pass "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: OK"
  37. exitcode=0
  38. monitorpass=1
  39. core_exit.sh
  40. else
  41. # Server query FAIL
  42. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: FAIL"
  43. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  44. fn_print_fail_eol
  45. sleep 1
  46. # monitor try gamedig first then gsquery before restarting
  47. if [ "${querymethod}" == "gsquery" ];then
  48. if [ "${totalseconds}" -ge "59" ]; then
  49. # Server query FAIL for over 59 seconds reboot server
  50. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  51. fn_print_fail_eol_nl
  52. fn_script_log_error "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: FAIL"
  53. sleep 1
  54. # Send alert if enabled
  55. alert="restartquery"
  56. alert.sh
  57. command_restart.sh
  58. core_exit.sh
  59. fi
  60. elif [ "${querymethod}" == "gamedig" ];then
  61. if [ "${totalseconds}" -ge "29" ]; then
  62. break
  63. fi
  64. fi
  65. # Seconds counter
  66. for seconds in {1..15}; do
  67. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: WAIT"
  68. totalseconds=$((totalseconds + 1))
  69. sleep 1
  70. if [ "${seconds}" == "15" ]; then
  71. break
  72. fi
  73. done
  74. fi
  75. done
  76. }
  77. fn_monitor_check_lockfile(){
  78. # Monitor does not run it lockfile is not found
  79. if [ ! -f "${rootdir}/${lockselfname}" ]; then
  80. fn_print_error_nl "Disabled: No lockfile found"
  81. fn_script_log_error "Disabled: No lockfile found"
  82. echo " * To enable monitor run ./${selfname} start"
  83. core_exit.sh
  84. fi
  85. }
  86. fn_monitor_check_update(){
  87. # Monitor will not check if update is running.
  88. if [ "$(ps -ef|grep "${selfname} update"|grep -v grep|wc -l)" != "0" ]; then
  89. fn_print_error_nl "SteamCMD is currently checking for updates"
  90. fn_script_log_error "SteamCMD is currently checking for updates"
  91. sleep 1
  92. core_exit.sh
  93. fi
  94. }
  95. fn_monitor_check_session(){
  96. fn_print_dots "Checking session: "
  97. fn_print_checking_eol
  98. fn_script_log_info "Checking session: CHECKING"
  99. sleep 1
  100. if [ "${status}" != "0" ]; then
  101. fn_print_ok "Checking session: "
  102. fn_print_ok_eol_nl
  103. fn_script_log_pass "Checking session: OK"
  104. else
  105. if [ "${gamename}" == "TeamSpeak 3" ]; then
  106. fn_print_error "Checking session: ${ts3error}: "
  107. elif [ "${gamename}" == "Mumble" ]; then
  108. fn_print_error "Checking session: Not listening to port ${queryport}"
  109. else
  110. fn_print_error "Checking session: "
  111. fi
  112. fn_print_fail_eol_nl
  113. fn_script_log_error "Checking session: FAIL"
  114. alert="restart"
  115. alert.sh
  116. fn_script_log_info "Monitor is starting ${servername}"
  117. sleep 1
  118. command_restart.sh
  119. fi
  120. sleep 1
  121. }
  122. fn_monitor_query(){
  123. fn_script_log_info "Querying port: query enabled"
  124. # engines that work with query
  125. local allowed_engines_array=( avalanche2.0 avalanche3.0 goldsource idtech2 idtech3 idtech3_ql iw2.0 iw3.0 madness quake refractor realvirtuality source spark starbound unity3d unreal unreal2 unreal4 )
  126. for allowed_engine in "${allowed_engines_array[@]}"
  127. do
  128. if [ "${engine}" == "idtech3_ql" ]; then
  129. local engine="quakelive"
  130. elif [ "${gamename}" == "Killing Floor 2" ]; then
  131. local engine="unreal4"
  132. fi
  133. # will first attempt to use gamedig then gsquery
  134. totalseconds=0
  135. local query_methods_array=( gamedig gsquery )
  136. for query_method in "${query_methods_array[@]}"
  137. do
  138. if [ "${query_method}" == "gamedig" ]; then
  139. # will bypass gamedig if not installed
  140. if [ "$(command -v gamedig 2>/dev/null)" ]&&[ "$(command -v jq 2>/dev/null)" ]; then
  141. if [ -z "${monitorpass}" ]; then
  142. querymethod="${query_method}"
  143. fn_monitor_loop
  144. fi
  145. fi
  146. else
  147. if [ -z "${monitorpass}" ]; then
  148. querymethod="${query_method}"
  149. fn_monitor_loop
  150. fi
  151. fi
  152. done
  153. done
  154. }
  155. fn_monitor_query_telnet(){
  156. querymethod="telnet"
  157. fn_monitor_loop
  158. }
  159. monitorflag=1
  160. fn_print_dots "${servername}"
  161. sleep 1
  162. check.sh
  163. logs.sh
  164. info_config.sh
  165. info_parms.sh
  166. fn_monitor_check_lockfile
  167. fn_monitor_check_update
  168. fn_monitor_check_session
  169. # Query has to be enabled in starbound config
  170. if [ "${gamename}" == "starbound" ]; then
  171. if [ "${queryenabled}" == "true" ]; then
  172. fn_monitor_query
  173. fi
  174. elif [ "${gamename}" == "Teamspeak 3" ]; then
  175. fn_monitor_query_telnet
  176. else
  177. fn_monitor_query
  178. fi
  179. core_exit.sh