command_monitor.sh 8.2 KB

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