command_monitor.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_script_log_warn "Checking port: Unable to query rconport, rcon not enabled: WARN"
  69. else
  70. fn_print_error "Checking port: Unable to query queryport is not set: "
  71. fn_script_log_error "Checking port: Unable to query as queryport is not set: ERROR"
  72. fi
  73. core_exit.sh
  74. fi
  75. }
  76. fn_query_gsquery(){
  77. if [ ! -f "${functionsdir}/query_gsquery.py" ]; then
  78. fn_fetch_file_github "lgsm/functions" "query_gsquery.py" "${functionsdir}" "chmodx" "norun" "noforce" "nomd5"
  79. fi
  80. "${functionsdir}"/query_gsquery.py -a "${ip}" -p "${queryport}" -e "${querytype}" > /dev/null 2>&1
  81. querystatus="$?"
  82. }
  83. fn_query_tcp(){
  84. bash -c 'exec 3<> /dev/tcp/'${ip}'/'${queryport}'' > /dev/null 2>&1
  85. querystatus="$?"
  86. }
  87. fn_monitor_query(){
  88. # Will loop and query up to 5 times every 15 seconds.
  89. # Query will wait up to 60 seconds to confirm server is down as server can become non-responsive during map changes.
  90. totalseconds=0
  91. for queryattempt in {1..5}; do
  92. fn_print_dots "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  93. fn_print_querying_eol
  94. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : QUERYING"
  95. # querydelay
  96. if [ "$(cat "${lockdir}/${selfname}.lock")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then
  97. fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  98. fn_print_delay_eol_nl
  99. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : DELAY"
  100. fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago"
  101. fn_script_log_info "Server started: $(date -d @$(cat lgsm/lock/bmdmserver.lock))"
  102. fn_script_log_info "Current time: $(date)"
  103. monitorpass=1
  104. core_exit.sh
  105. # will use query method selected in fn_monitor_loop
  106. # gamedig
  107. elif [ "${querymethod}" == "gamedig" ]; then
  108. query_gamedig.sh
  109. # gsquery
  110. elif [ "${querymethod}" == "gsquery" ]; then
  111. fn_query_gsquery
  112. #tcp query
  113. elif [ "${querymethod}" == "tcp" ]; then
  114. fn_query_tcp
  115. fi
  116. if [ "${querystatus}" == "0" ]; then
  117. # Server query OK.
  118. fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  119. fn_print_ok_eol_nl
  120. fn_script_log_pass "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: OK"
  121. monitorpass=1
  122. if [ "${querystatus}" == "0" ]; then
  123. # Add query data to log.
  124. if [ "${gdname}" ]; then
  125. fn_script_log_info "Server name: ${gdname}"
  126. fi
  127. if [ "${gdplayers}" ]; then
  128. fn_script_log_info "Players: ${gdplayers}/${gdmaxplayers}"
  129. fi
  130. if [ "${gdbots}" ]; then
  131. fn_script_log_info "Bots: ${gdbots}"
  132. fi
  133. if [ "${gdmap}" ]; then
  134. fn_script_log_info "Map: ${gdmap}"
  135. fi
  136. if [ "${gdgamemode}" ]; then
  137. fn_script_log_info "Game Mode: ${gdgamemode}"
  138. fi
  139. # send LinuxGSM stats if monitor is OK.
  140. if [ "${stats}" == "on" ]||[ "${stats}" == "y" ]; then
  141. info_stats.sh
  142. fi
  143. fi
  144. core_exit.sh
  145. else
  146. # Server query FAIL.
  147. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  148. fn_print_fail_eol
  149. fn_script_log_warn "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: FAIL"
  150. # Monitor will try gamedig (if supported) for first 30s then gsquery before restarting.
  151. if [ "${querymethod}" == "gsquery" ]||[ "${querymethod}" == "tcp" ]; then
  152. # gsquery will fail if longer than 60s
  153. if [ "${totalseconds}" -ge "59" ]; then
  154. # Monitor will FAIL if over 60s and trigger gane server reboot.
  155. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  156. fn_print_fail_eol_nl
  157. fn_script_log_warn "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: FAIL"
  158. # Send alert if enabled.
  159. alert="restartquery"
  160. alert.sh
  161. command_restart.sh
  162. core_exit.sh
  163. fi
  164. elif [ "${querymethod}" == "gamedig" ]; then
  165. # gamedig will fail and try gsquery if longer than 30s
  166. if [ "${totalseconds}" -ge "29" ]; then
  167. break
  168. fi
  169. fi
  170. # Second counter will wait for 15s before breaking loop.
  171. for seconds in {1..15}; do
  172. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: ${cyan}WAIT${default}"
  173. sleep 0.5
  174. totalseconds=$((totalseconds + 1))
  175. if [ "${seconds}" == "15" ]; then
  176. break
  177. fi
  178. done
  179. fi
  180. done
  181. }
  182. fn_monitor_loop(){
  183. # loop though query methods selected by querymode.
  184. totalseconds=0
  185. if [ "${querymode}" == "2" ]; then
  186. local query_methods_array=( gamedig gsquery )
  187. elif [ "${querymode}" == "3" ]; then
  188. local query_methods_array=( gamedig )
  189. elif [ "${querymode}" == "4" ]; then
  190. local query_methods_array=( gsquery )
  191. elif [ "${querymode}" == "5" ]; then
  192. local query_methods_array=( tcp )
  193. fi
  194. for querymethod in "${query_methods_array[@]}"
  195. do
  196. # Will check if gamedig is installed and bypass if not.
  197. if [ "${querymethod}" == "gamedig" ]; then
  198. if [ "$(command -v gamedig 2>/dev/null)" ]&&[ "$(command -v jq 2>/dev/null)" ]; then
  199. if [ -z "${monitorpass}" ]; then
  200. fn_monitor_query
  201. fi
  202. else
  203. fn_script_log_info "gamedig is not installed"
  204. fn_script_log_info "https://docs.linuxgsm.com/requirements/gamedig"
  205. fi
  206. else
  207. # will not query if query already passed.
  208. if [ -z "${monitorpass}" ]; then
  209. fn_monitor_query
  210. fi
  211. fi
  212. done
  213. }
  214. monitorflag=1
  215. check.sh
  216. core_logs.sh
  217. info_config.sh
  218. info_parms.sh
  219. # query pre-checks
  220. fn_monitor_check_lockfile
  221. fn_monitor_check_update
  222. fn_monitor_check_session
  223. # Monitor will not continue if session only check.
  224. if [ "${querymode}" != "1" ]; then
  225. fn_monitor_check_queryport
  226. # Add a querydelay of 1 min if var missing.
  227. if [ -z "${querydelay}" ]; then
  228. querydelay="1"
  229. fi
  230. fn_monitor_loop
  231. fi
  232. core_exit.sh