command_monitor.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 modulename="MONITOR"
  9. local commandaction="Monitor"
  10. local function_selfname=$(basename "$(readlink -f "${BASH_SOURCE[0]}")")
  11. fn_monitor_check_lockfile(){
  12. # Monitor does not run it lockfile is not found.
  13. if [ ! -f "${rootdir}/${lockselfname}" ]; then
  14. fn_print_dots "Checking lockfile: "
  15. fn_print_checking_eol
  16. fn_script_log_info "Checking lockfile: CHECKING"
  17. fn_sleep_time
  18. fn_print_error "Checking lockfile: No lockfile found: "
  19. fn_print_error_eol_nl
  20. fn_script_log_error "Checking lockfile: No lockfile found: ERROR"
  21. fn_sleep_time
  22. echo -e "* Start ${selfname} to run monitor."
  23. core_exit.sh
  24. fi
  25. # Fix if lockfile is not unix time or contains letters
  26. if [[ "$(cat "${rootdir}/${lockselfname}")" =~ [A-Za-z] ]]; then
  27. date '+%s' > "${rootdir}/${lockselfname}"
  28. fi
  29. }
  30. fn_monitor_check_update(){
  31. # Monitor will check if update is already running.
  32. if [ "$(pgrep "${selfname} update" | wc -l)" != "0" ]; then
  33. fn_print_dots "Checking active updates: "
  34. fn_print_checking_eol
  35. fn_script_log_info "Checking active updates: CHECKING"
  36. fn_sleep_time
  37. fn_print_error_nl "Checking active updates: SteamCMD is currently checking for updates: "
  38. fn_print_error_eol
  39. fn_script_log_error "Checking active updates: SteamCMD is currently checking for updates: ERROR"
  40. fn_sleep_time
  41. core_exit.sh
  42. fi
  43. }
  44. fn_monitor_check_session(){
  45. fn_print_dots "Checking session: "
  46. fn_print_checking_eol
  47. fn_script_log_info "Checking session: CHECKING"
  48. fn_sleep_time
  49. # uses status var from check_status.sh
  50. if [ "${status}" != "0" ]; then
  51. fn_print_ok "Checking session: "
  52. fn_print_ok_eol_nl
  53. fn_script_log_pass "Checking session: OK"
  54. fn_sleep_time
  55. else
  56. fn_print_error "Checking session: "
  57. fn_print_fail_eol_nl
  58. fn_script_log_fatal "Checking session: FAIL"
  59. fn_sleep_time
  60. alert="restart"
  61. alert.sh
  62. fn_script_log_info "Checking session: Monitor is restarting ${selfname}"
  63. command_restart.sh
  64. core_exit.sh
  65. fi
  66. }
  67. fn_monitor_check_queryport(){
  68. # Monitor will check queryport is set before continuing.
  69. if [ -z "${queryport}" ]||[ "${queryport}" == "0" ]; then
  70. fn_print_dots "Checking port: "
  71. fn_print_checking_eol
  72. fn_script_log_info "Checking session: CHECKING"
  73. fn_sleep_time
  74. fn_print_error "Checking port: Unable to query as queryport is not set: "
  75. fn_print_error_eol_nl
  76. fn_script_log_error "Checking port: Unable to query as queryport is not set: ERROR"
  77. fn_sleep_time
  78. core_exit.sh
  79. fi
  80. }
  81. fn_query_gsquery(){
  82. if [ ! -f "${functionsdir}/query_gsquery.py" ]; then
  83. fn_fetch_file_github "lgsm/functions" "query_gsquery.py" "${functionsdir}" "chmodx" "norun" "noforce" "nomd5"
  84. fi
  85. "${functionsdir}"/query_gsquery.py -a "${ip}" -p "${queryport}" -e "${querytype}" > /dev/null 2>&1
  86. querystatus="$?"
  87. }
  88. fn_query_tcp(){
  89. bash -c 'exec 3<> /dev/tcp/'${ip}'/'${queryport}'' > /dev/null 2>&1
  90. querystatus="$?"
  91. }
  92. fn_monitor_query(){
  93. # Will loop and query up to 5 times every 15 seconds.
  94. # Query will wait up to 60 seconds to confirm server is down as server can become non-responsive during map changes.
  95. totalseconds=0
  96. for queryattempt in {1..5}; do
  97. fn_print_dots "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  98. fn_print_querying_eol
  99. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : QUERYING"
  100. fn_sleep_time
  101. # querydelay
  102. if [ "$(cat "${rootdir}/${lockselfname}")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then
  103. fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  104. fn_print_delay_eol_nl
  105. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : DELAY"
  106. fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago"
  107. fn_sleep_time
  108. monitorpass=1
  109. core_exit.sh
  110. # will use query method selected in fn_monitor_loop
  111. # gamedig
  112. elif [ "${querymethod}" == "gamedig" ]; then
  113. query_gamedig.sh
  114. # gsquery
  115. elif [ "${querymethod}" == "gsquery" ]; then
  116. fn_query_gsquery
  117. #tcp query
  118. elif [ "${querymethod}" == "tcp" ]; then
  119. fn_query_tcp
  120. fi
  121. if [ "${querystatus}" == "0" ]; then
  122. # Server query OK.
  123. fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  124. fn_print_ok_eol_nl
  125. fn_script_log_pass "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: OK"
  126. fn_sleep_time
  127. monitorpass=1
  128. # send LinuxGSM stats if monitor is OK.
  129. if [ "${stats}" == "on" ]||[ "${stats}" == "y" ]; then
  130. info_stats.sh
  131. fi
  132. if [ "${querystatus}" == "0" ]; then
  133. # Add query data to log.
  134. if [ "${gdname}" ]; then
  135. fn_script_log_info "Server name: ${gdname}"
  136. fi
  137. if [ "${gdplayers}" ]; then
  138. fn_script_log_info "Players: ${gdplayers}/${gdmaxplayers}"
  139. fi
  140. if [ "${gdmap}" ]; then
  141. fn_script_log_info "Map: ${gdmap}"
  142. fi
  143. if [ "${gdgamemode}" ]; then
  144. fn_script_log_info "Game Mode: ${gdgamemode}"
  145. fi
  146. fi
  147. core_exit.sh
  148. else
  149. # Server query FAIL.
  150. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  151. fn_print_fail_eol
  152. fn_script_log_warn "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: FAIL"
  153. fn_sleep_time
  154. # Monitor will try gamedig (if supported) for first 30s then gsquery before restarting.
  155. if [ "${querymethod}" == "gsquery" ]||[ "${querymethod}" == "tcp" ]; then
  156. # gsquery will fail if longer than 60s
  157. if [ "${totalseconds}" -ge "59" ]; then
  158. # Monitor will FAIL if over 60s and trigger gane server reboot.
  159. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  160. fn_print_fail_eol_nl
  161. fn_script_log_warn "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: FAIL"
  162. fn_sleep_time
  163. # Send alert if enabled.
  164. alert="restartquery"
  165. alert.sh
  166. command_restart.sh
  167. core_exit.sh
  168. fi
  169. elif [ "${querymethod}" == "gamedig" ]; then
  170. # gamedig will fail and try gsquery if longer than 30s
  171. if [ "${totalseconds}" -ge "29" ]; then
  172. break
  173. fi
  174. fi
  175. # Second counter will wait for 15s before breaking loop.
  176. for seconds in {1..15}; do
  177. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: WAIT"
  178. totalseconds=$((totalseconds + 1))
  179. if [ "${seconds}" == "15" ]; then
  180. break
  181. fi
  182. done
  183. fi
  184. done
  185. }
  186. fn_monitor_loop(){
  187. # loop though query methods selected by querymode.
  188. totalseconds=0
  189. if [ "${querymode}" == "2" ]; then
  190. local query_methods_array=( gamedig gsquery )
  191. elif [ "${querymode}" == "3" ]; then
  192. local query_methods_array=( gamedig )
  193. elif [ "${querymode}" == "4" ]; then
  194. local query_methods_array=( gsquery )
  195. elif [ "${querymode}" == "5" ]; then
  196. local query_methods_array=( tcp )
  197. fi
  198. for querymethod in "${query_methods_array[@]}"
  199. do
  200. # Will check if gamedig is installed and bypass if not.
  201. if [ "${querymethod}" == "gamedig" ]; then
  202. if [ "$(command -v gamedig 2>/dev/null)" ]&&[ "$(command -v jq 2>/dev/null)" ]; then
  203. if [ -z "${monitorpass}" ]; then
  204. fn_monitor_query
  205. fi
  206. else
  207. fn_script_log_info "gamedig is not installed"
  208. fn_script_log_info "https://docs.linuxgsm.com/requirements/gamedig"
  209. fi
  210. else
  211. # will not query if query already passed.
  212. if [ -z "${monitorpass}" ]; then
  213. fn_monitor_query
  214. fi
  215. fi
  216. done
  217. }
  218. monitorflag=1
  219. check.sh
  220. logs.sh
  221. info_config.sh
  222. info_parms.sh
  223. # query pre-checks
  224. fn_monitor_check_lockfile
  225. fn_monitor_check_update
  226. fn_monitor_check_session
  227. # Monitor will not continue if session only check.
  228. if [ "${querymode}" != "1" ]; then
  229. fn_monitor_check_queryport
  230. # Add a querydelay of 1 min if var missing.
  231. if [ -z "${querydelay}" ]; then
  232. querydelay="1"
  233. fi
  234. fn_monitor_loop
  235. fi
  236. core_exit.sh