command_monitor.sh 8.2 KB

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