command_monitor.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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_monitoring() {
  13. if [ -f "${lockdir}/${selfname}.lock" ]; then
  14. # Part of migration to v23.5.0. #4296
  15. rm -f "${lockdir:?}/${selfname}.lock"
  16. date '+%s' > "${lockdir:?}/${selfname}-monitoring.lock"
  17. elif [ ! -f "${lockdir}/${selfname}-monitoring.lock" ]; then
  18. # Monitor does not run if lockfile is not found.
  19. fn_print_dots "Checking lockfile: "
  20. fn_print_checking_eol
  21. fn_script_log_info "Checking lockfile: CHECKING"
  22. fn_print_error "Checking lockfile: No lockfile found: "
  23. fn_print_error_eol_nl
  24. fn_script_log_error "Checking lockfile: No lockfile found: ERROR"
  25. echo -e "* Start ${selfname} to run monitor."
  26. core_exit.sh
  27. fi
  28. }
  29. fn_monitor_check_install() {
  30. if [ "$(pgrep -fc -u "${USER}" "/bin/bash ./${selfname} install")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} i")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} auto-install")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} ai")" != "0" ]; then
  31. fn_print_dots "Checking installer: "
  32. fn_print_checking_eol
  33. fn_script_log_info "Checking installer: CHECKING"
  34. fn_print_info "Checking installer: LinuxGSM is installing: "
  35. fn_print_info_eol_nl
  36. fn_script_log_pass "Checking installer: LinuxGSM is installing"
  37. core_exit.sh
  38. fi
  39. }
  40. fn_monitor_check_debug() {
  41. if [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} debug")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} d")" != "0" ]; then
  42. fn_print_dots "Checking debug: "
  43. fn_print_checking_eol
  44. fn_print_info "Checking debug: Debug is running: "
  45. fn_print_info_eol_nl
  46. fn_script_log_pass "Checking debug: Debug is running"
  47. core_exit.sh
  48. fi
  49. }
  50. fn_monitor_check_starting() {
  51. # Remove stale lockfile.
  52. if [ -f "${lockdir}/${selfname}-starting.lock" ]; then
  53. if [ "$(find "${lockdir}/${selfname}-starting.lock" -mmin +5)" ]; then
  54. fn_print_dots "Checking start: "
  55. fn_print_checking_eol
  56. fn_print_warn "Checking start: Removing stale lockfile: "
  57. fn_print_warn_eol_nl
  58. fn_script_log_warn "Checking start: Removing stale lockfile"
  59. rm -f "${lockdir:?}/${selfname}-starting.lock"
  60. fi
  61. fi
  62. if [ -f "${lockdir}/${selfname}-starting.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} start")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} s")" != "0" ]]; then
  63. fn_print_dots "Checking start: "
  64. fn_print_checking_eol
  65. fn_print_info "Checking start: LinuxGSM is starting: "
  66. fn_print_info_eol_nl
  67. fn_script_log_info "Checking backup: LinuxGSM is starting"
  68. core_exit.sh
  69. fi
  70. }
  71. fn_monitor_check_stopping() {
  72. # Remove stale lockfile.
  73. if [ -f "${lockdir}/${selfname}-stopping.lock" ]; then
  74. if [ "$(find "${lockdir}/${selfname}-stopping.lock" -mmin +5)" ]; then
  75. fn_print_dots "Checking stop: "
  76. fn_print_checking_eol
  77. fn_print_warn "Checking stop: Removing stale lockfile: "
  78. fn_print_warn_eol_nl
  79. fn_script_log_warn "Checking stop: Removing stale lockfile"
  80. rm -f "${lockdir:?}/${selfname}-stopping.lock"
  81. fi
  82. fi
  83. if [ -f "${lockdir}/${selfname}-stopping.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} stop")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} s")" != "0" ]]; then
  84. fn_print_dots "Checking stop: "
  85. fn_print_checking_eol
  86. fn_print_info "Checking stop: LinuxGSM is stopping: "
  87. fn_print_info_eol_nl
  88. fn_script_log_info "Checking backup: LinuxGSM is stopping"
  89. core_exit.sh
  90. fi
  91. }
  92. fn_monitor_check_backup() {
  93. # Remove stale lockfile.
  94. if [ -f "${lockdir}/backup.lock" ]; then
  95. if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then
  96. fn_print_dots "Checking backup: "
  97. fn_print_checking_eol
  98. fn_print_warn "Checking backup: Removing stale lockfile: "
  99. fn_print_warn_eol
  100. fn_script_log_warn "Checking backup: Removing stale lockfile"
  101. rm -f "${lockdir:?}/backup.lock"
  102. fi
  103. fi
  104. if [ -f "${lockdir}/backup.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} backup")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} b")" != "0" ]]; then
  105. fn_print_dots "Checking backup: "
  106. fn_print_checking_eol
  107. fn_print_info "Checking backup: Backup is running: "
  108. fn_print_info_eol_nl
  109. fn_script_log_info "Checking backup: Backup is running"
  110. core_exit.sh
  111. fi
  112. }
  113. fn_monitor_check_update() {
  114. # Remove stale lockfile.
  115. if [ -f "${lockdir}/update.lock" ]; then
  116. if [ "$(find "${lockdir}/update.lock" -mmin +15)" ]; then
  117. fn_print_dots "Checking update: "
  118. fn_print_checking_eol
  119. fn_print_warn "Checking update: Removing stale lockfile: "
  120. fn_print_warn_eol_nl
  121. fn_script_log_warn "Checking update: Removing stale lockfile"
  122. rm -f "${lockdir:?}/update.lock"
  123. fi
  124. fi
  125. if [ -f "${lockdir}/update.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} update")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} validate")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} v")" != "0" || "$(pgrep -fc force-update "${USER}" "/bin/bash ./${selfname} fu")" != "0" ]]; then
  126. fn_print_dots "Checking update: "
  127. fn_print_checking_eol
  128. fn_print_info "Checking update: LinuxGSM is updating the game server: "
  129. fn_print_info_eol_nl
  130. fn_script_log_pass "Checking update: LinuxGSM is updating the game server"
  131. core_exit.sh
  132. fi
  133. }
  134. # Source engine games may display a messages to indicate the server needs restarting.
  135. fn_monitor_check_update_source() {
  136. if [ -f "${consolelogdir}/${selfname}-console.log" ] && [ "${engine}" == "source" ]; then
  137. if grep -q "Your server needs to be restarted in order to receive the latest update." "${consolelogdir}/${selfname}-console.log"; then
  138. fn_print_dots "Checking update: "
  139. fn_print_checking_eol
  140. fn_script_log_info "Checking update: CHECKING"
  141. fn_print_ok "Checking update: "
  142. fn_print_ok_eol_nl
  143. fn_script_log_info "Checking update: Monitor is restarting ${selfname} to apply update"
  144. alert="restart"
  145. alert.sh
  146. command_restart.sh
  147. core_exit.sh
  148. fi
  149. fi
  150. }
  151. fn_monitor_check_session() {
  152. fn_print_dots "Checking session: "
  153. fn_print_checking_eol
  154. fn_script_log_info "Checking session: CHECKING"
  155. # Tmux session width and height needs to be reviewed as may no longer be required.
  156. sessionwidth="80"
  157. sessionheight="23"
  158. # Check for PIDS with identical tmux sessions running.
  159. if [ "$(pgrep -fcx "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" -ge "2" ]; then
  160. fn_print_error "Checking session: There are PIDS with identical tmux sessions running: "
  161. fn_print_error_eol_nl
  162. fn_script_log_error "Checking session: ERROR"
  163. fn_script_log_error "Checking session: There are PIDS with identical tmux sessions running"
  164. fn_script_log_error "Checking session: Killing all tmux sessions with the socketname name ${socketname} and session name ${sessionname}"
  165. pkill -f "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}"
  166. command_restart.sh
  167. core_exit.sh
  168. # Check for tmux pids with the same tmux session and socket names. This will reduce issues with migration to release v23.5.0. #4296
  169. elif [ "$(pgrep -fc -u "${USER}" "tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" != "0" ]; then
  170. fn_print_error "Checking session: PIDS with the same tmux session and socket names are running: "
  171. fn_print_error_eol_nl
  172. fn_script_log_error "Checking session: ERROR"
  173. fn_script_log_error "Checking session: PIDS with the same tmux session and socket names are running"
  174. fn_script_log_error "Checking session: Killing session with the socketname name ${sessionname} and session name ${sessionname}"
  175. pkill -f "tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}"
  176. command_restart.sh
  177. core_exit.sh
  178. elif [ "${status}" != "0" ]; then
  179. fn_print_ok "Checking session: "
  180. fn_print_ok_eol_nl
  181. fn_script_log_pass "Checking session: OK"
  182. else
  183. fn_print_error "Checking session: "
  184. fn_print_fail_eol_nl
  185. fn_script_log_fatal "Checking session: FAIL"
  186. alert="restart"
  187. alert.sh
  188. fn_script_log_info "Checking session: Monitor is restarting ${selfname}"
  189. command_restart.sh
  190. core_exit.sh
  191. fi
  192. }
  193. # Monitor will check queryport is set before continuing.
  194. fn_monitor_check_queryport() {
  195. if [ -z "${queryport}" ] || [ "${queryport}" == "0" ]; then
  196. fn_print_dots "Checking port: "
  197. fn_print_checking_eol
  198. fn_script_log_info "Checking port: CHECKING"
  199. if [ -n "${rconenabled}" ] && [ "${rconenabled}" != "true" ] && [ "${shortname}" == "av" ]; then
  200. fn_print_warn "Checking port: Unable to query, rcon is not enabled"
  201. fn_script_log_warn "Checking port: Unable to query, rcon is not enabled"
  202. else
  203. fn_print_error "Checking port: Unable to query, queryport is not set"
  204. fn_script_log_error "Checking port: Unable to query, queryport is not set"
  205. fi
  206. core_exit.sh
  207. fi
  208. }
  209. fn_query_gsquery() {
  210. if [ ! -f "${modulesdir}/query_gsquery.py" ]; then
  211. fn_fetch_file_github "lgsm/modules" "query_gsquery.py" "${modulesdir}" "chmodx" "norun" "noforce" "nohash"
  212. fi
  213. "${modulesdir}"/query_gsquery.py -a "${queryip}" -p "${queryport}" -e "${querytype}" > /dev/null 2>&1
  214. querystatus="$?"
  215. }
  216. fn_query_tcp() {
  217. bash -c "exec 3<> /dev/tcp/'${queryip}'/'${queryport}'" > /dev/null 2>&1
  218. querystatus="$?"
  219. }
  220. fn_monitor_query() {
  221. # Will loop and query up to 5 times every 15 seconds.
  222. # Query will wait up to 60 seconds to confirm server is down as server can become non-responsive during map changes.
  223. totalseconds=0
  224. for queryattempt in {1..5}; do
  225. for queryip in "${queryips[@]}"; do
  226. fn_print_dots "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}: "
  227. fn_print_querying_eol
  228. fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : QUERYING"
  229. # querydelay
  230. if [ "$(head -n 1 "${lockdir}/${selfname}-started.lock")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then
  231. fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  232. fn_print_delay_eol_nl
  233. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : DELAY"
  234. fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago"
  235. fn_script_log_info "Server started: $(date -d "@$(head -n 1 "${lockdir}/${selfname}-started.lock")")"
  236. fn_script_log_info "Current time: $(date)"
  237. monitorpass=1
  238. core_exit.sh
  239. # will use query method selected in fn_monitor_loop
  240. # gamedig
  241. elif [ "${querymethod}" == "gamedig" ]; then
  242. query_gamedig.sh
  243. # gsquery
  244. elif [ "${querymethod}" == "gsquery" ]; then
  245. fn_query_gsquery
  246. #tcp query
  247. elif [ "${querymethod}" == "tcp" ]; then
  248. fn_query_tcp
  249. fi
  250. if [ "${querystatus}" == "0" ]; then
  251. # Server query OK.
  252. fn_print_ok "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}: "
  253. fn_print_ok_eol_nl
  254. fn_script_log_pass "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : OK"
  255. monitorpass=1
  256. if [ "${querystatus}" == "0" ]; then
  257. # Add query data to log.
  258. if [ "${gdname}" ]; then
  259. fn_script_log_info "Server name: ${gdname}"
  260. fi
  261. if [ "${gdplayers}" ]; then
  262. fn_script_log_info "Players: ${gdplayers}/${gdmaxplayers}"
  263. fi
  264. if [ "${gdbots}" ]; then
  265. fn_script_log_info "Bots: ${gdbots}"
  266. fi
  267. if [ "${gdmap}" ]; then
  268. fn_script_log_info "Map: ${gdmap}"
  269. fi
  270. if [ "${gdgamemode}" ]; then
  271. fn_script_log_info "Game Mode: ${gdgamemode}"
  272. fi
  273. # send LinuxGSM stats if monitor is OK.
  274. if [ "${stats}" == "on" ] || [ "${stats}" == "y" ]; then
  275. info_stats.sh
  276. fi
  277. fi
  278. core_exit.sh
  279. else
  280. # Server query FAIL.
  281. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}: "
  282. fn_print_fail_eol
  283. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  284. # Monitor will try gamedig (if supported) for first 30s then gsquery before restarting.
  285. # gsquery will fail if longer than 60s
  286. if [ "${totalseconds}" -ge "59" ]; then
  287. # Monitor will FAIL if over 60s and trigger gane server reboot.
  288. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}: "
  289. fn_print_fail_eol_nl
  290. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  291. # Send alert if enabled.
  292. alert="restartquery"
  293. alert.sh
  294. command_restart.sh
  295. fn_firstcommand_reset
  296. core_exit.sh
  297. fi
  298. fi
  299. done
  300. # Second counter will wait for 15s before breaking loop.
  301. for seconds in {1..15}; do
  302. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt} : ${cyan}WAIT${default}"
  303. sleep 0.5
  304. totalseconds=$((totalseconds + 1))
  305. if [ "${seconds}" == "15" ]; then
  306. break
  307. fi
  308. done
  309. done
  310. }
  311. fn_monitor_loop() {
  312. # loop though query methods selected by querymode.
  313. totalseconds=0
  314. if [ "${querymode}" == "2" ]; then
  315. local query_methods_array=(gamedig gsquery)
  316. elif [ "${querymode}" == "3" ]; then
  317. local query_methods_array=(gamedig)
  318. elif [ "${querymode}" == "4" ]; then
  319. local query_methods_array=(gsquery)
  320. elif [ "${querymode}" == "5" ]; then
  321. local query_methods_array=(tcp)
  322. fi
  323. for querymethod in "${query_methods_array[@]}"; do
  324. # Will check if gamedig is installed and bypass if not.
  325. if [ "${querymethod}" == "gamedig" ]; then
  326. if [ "$(command -v gamedig 2> /dev/null)" ] && [ "$(command -v jq 2> /dev/null)" ]; then
  327. if [ -z "${monitorpass}" ]; then
  328. fn_monitor_query
  329. fi
  330. else
  331. fn_script_log_info "gamedig is not installed"
  332. fn_script_log_info "https://docs.linuxgsm.com/requirements/gamedig"
  333. fi
  334. else
  335. # will not query if query already passed.
  336. if [ -z "${monitorpass}" ]; then
  337. fn_monitor_query
  338. fi
  339. fi
  340. done
  341. }
  342. monitorflag=1
  343. # Dont do any monitoring or checks if installer is running.
  344. fn_monitor_check_install
  345. check.sh
  346. core_logs.sh
  347. info_game.sh
  348. # query pre-checks
  349. fn_monitor_check_update_source
  350. fn_monitor_check_update
  351. fn_monitor_check_backup
  352. fn_monitor_check_debug
  353. fn_monitor_check_monitoring
  354. fn_monitor_check_starting
  355. fn_monitor_check_stopping
  356. fn_monitor_check_session
  357. # Monitor will not continue if session only check.
  358. if [ "${querymode}" != "1" ]; then
  359. fn_monitor_check_queryport
  360. # Add a querydelay of 1 min if var missing.
  361. if [ -z "${querydelay}" ]; then
  362. querydelay="1"
  363. fi
  364. fn_monitor_loop
  365. fi
  366. core_exit.sh