command_monitor.sh 10.0 KB

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