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_firstcommand_set
  12. fn_monitor_check_lockfile(){
  13. # Monitor does not run it lockfile is not found.
  14. if [ ! -f "${lockdir}/${selfname}.lock" ]; then
  15. fn_print_dots "Checking lockfile: "
  16. fn_print_checking_eol
  17. fn_script_log_info "Checking lockfile: CHECKING"
  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. echo -e "* Start ${selfname} to run monitor."
  22. core_exit.sh
  23. fi
  24. # Fix if lockfile is not unix time or contains letters
  25. if [ -f "${lockdir}/${selfname}.lock" ]&&[[ "$(cat "${lockdir}/${selfname}.lock")" =~ [A-Za-z] ]]; then
  26. date '+%s' > "${lockdir}/${selfname}.lock"
  27. fi
  28. }
  29. fn_monitor_check_update(){
  30. # Monitor will check if update is already running.
  31. if [ "$(pgrep "${selfname} update" | wc -l)" != "0" ]; then
  32. fn_print_dots "Checking active updates: "
  33. fn_print_checking_eol
  34. fn_script_log_info "Checking active updates: CHECKING"
  35. fn_print_error_nl "Checking active updates: SteamCMD is currently checking for updates: "
  36. fn_print_error_eol
  37. fn_script_log_error "Checking active updates: SteamCMD is currently checking for updates: ERROR"
  38. core_exit.sh
  39. fi
  40. }
  41. fn_monitor_check_session(){
  42. fn_print_dots "Checking session: "
  43. fn_print_checking_eol
  44. fn_script_log_info "Checking session: CHECKING"
  45. # uses status var from check_status.sh
  46. if [ "${status}" != "0" ]; then
  47. fn_print_ok "Checking session: "
  48. fn_print_ok_eol_nl
  49. fn_script_log_pass "Checking session: OK"
  50. else
  51. fn_print_error "Checking session: "
  52. fn_print_fail_eol_nl
  53. fn_script_log_fatal "Checking session: FAIL"
  54. alert="restart"
  55. alert.sh
  56. fn_script_log_info "Checking session: Monitor is restarting ${selfname}"
  57. command_restart.sh
  58. core_exit.sh
  59. fi
  60. }
  61. fn_monitor_check_queryport(){
  62. # Monitor will check queryport is set before continuing.
  63. if [ -z "${queryport}" ]||[ "${queryport}" == "0" ]; then
  64. fn_print_dots "Checking port: "
  65. fn_print_checking_eol
  66. fn_script_log_info "Checking port: CHECKING"
  67. if [ -n "${rconenabled}" ]&&[ "${rconenabled}" != "true" ]&&[ ${shortname} == "av" ]; then
  68. fn_print_warn "Checking port: Unable to query as rconport, rcon not enabled: "
  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_script_log_error "Checking port: Unable to query as queryport is not set: ERROR"
  73. fi
  74. core_exit.sh
  75. fi
  76. }
  77. fn_query_gsquery(){
  78. if [ ! -f "${functionsdir}/query_gsquery.py" ]; then
  79. fn_fetch_file_github "lgsm/functions" "query_gsquery.py" "${functionsdir}" "chmodx" "norun" "noforce" "nomd5"
  80. fi
  81. "${functionsdir}"/query_gsquery.py -a "${ip}" -p "${queryport}" -e "${querytype}" > /dev/null 2>&1
  82. querystatus="$?"
  83. }
  84. fn_query_tcp(){
  85. bash -c 'exec 3<> /dev/tcp/'${ip}'/'${queryport}'' > /dev/null 2>&1
  86. querystatus="$?"
  87. }
  88. fn_monitor_query(){
  89. # Will loop and query up to 5 times every 15 seconds.
  90. # Query will wait up to 60 seconds to confirm server is down as server can become non-responsive during map changes.
  91. totalseconds=0
  92. for queryattempt in {1..5}; do
  93. fn_print_dots "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  94. fn_print_querying_eol
  95. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : QUERYING"
  96. # querydelay
  97. if [ "$(cat "${lockdir}/${selfname}.lock")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then
  98. fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  99. fn_print_delay_eol_nl
  100. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : DELAY"
  101. fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago"
  102. fn_script_log_info "Server started: $(date -d @$(cat "${lockdir}/${selfname}.lock"))"
  103. fn_script_log_info "Current time: $(date)"
  104. monitorpass=1
  105. core_exit.sh
  106. # will use query method selected in fn_monitor_loop
  107. # gamedig
  108. elif [ "${querymethod}" == "gamedig" ]; then
  109. query_gamedig.sh
  110. # gsquery
  111. elif [ "${querymethod}" == "gsquery" ]; then
  112. fn_query_gsquery
  113. #tcp query
  114. elif [ "${querymethod}" == "tcp" ]; then
  115. fn_query_tcp
  116. fi
  117. if [ "${querystatus}" == "0" ]; then
  118. # Server query OK.
  119. fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  120. fn_print_ok_eol_nl
  121. fn_script_log_pass "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: OK"
  122. monitorpass=1
  123. if [ "${querystatus}" == "0" ]; then
  124. # Add query data to log.
  125. if [ "${gdname}" ]; then
  126. fn_script_log_info "Server name: ${gdname}"
  127. fi
  128. if [ "${gdplayers}" ]; then
  129. fn_script_log_info "Players: ${gdplayers}/${gdmaxplayers}"
  130. fi
  131. if [ "${gdbots}" ]; then
  132. fn_script_log_info "Bots: ${gdbots}"
  133. fi
  134. if [ "${gdmap}" ]; then
  135. fn_script_log_info "Map: ${gdmap}"
  136. fi
  137. if [ "${gdgamemode}" ]; then
  138. fn_script_log_info "Game Mode: ${gdgamemode}"
  139. fi
  140. # send LinuxGSM stats if monitor is OK.
  141. if [ "${stats}" == "on" ]||[ "${stats}" == "y" ]; then
  142. info_stats.sh
  143. fi
  144. fi
  145. core_exit.sh
  146. else
  147. # Server query FAIL.
  148. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  149. fn_print_fail_eol
  150. fn_script_log_warn "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: FAIL"
  151. # Monitor will try gamedig (if supported) for first 30s then gsquery before restarting.
  152. if [ "${querymethod}" == "gsquery" ]||[ "${querymethod}" == "tcp" ]; then
  153. # gsquery will fail if longer than 60s
  154. if [ "${totalseconds}" -ge "59" ]; then
  155. # Monitor will FAIL if over 60s and trigger gane server reboot.
  156. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  157. fn_print_fail_eol_nl
  158. fn_script_log_warn "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt}: FAIL"
  159. # Send alert if enabled.
  160. alert="restartquery"
  161. alert.sh
  162. command_restart.sh
  163. fn_firstcommand_reset
  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