command_monitor.sh 8.2 KB

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