command_monitor.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #!/bin/bash
  2. # LinuxGSM command_monitor.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://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. # Checks if this instance is running one of the given commands, e.g. "update|u".
  13. # Matches the script run as ./gameserver, /path/to/gameserver or bash gameserver.
  14. fn_monitor_command_running() {
  15. [ "$(pgrep -fc -u "${USER}" "^([^ ]*/)?bash ([^ ]*/)?${selfname} ($1)( |$)")" != "0" ]
  16. }
  17. fn_monitor_check_monitoring() {
  18. if [ -f "${lockdir}/${selfname}.lock" ]; then
  19. # Part of migration to v23.5.0. #4296
  20. rm -f "${lockdir:?}/${selfname}.lock"
  21. date '+%s' > "${lockdir:?}/${selfname}-monitoring.lock"
  22. elif [ ! -f "${lockdir}/${selfname}-monitoring.lock" ]; then
  23. # Monitor does not run if lockfile is not found.
  24. fn_print_dots "Checking lockfile"
  25. fn_print_checking_eol
  26. fn_script_log_info "Checking lockfile: CHECKING"
  27. fn_print_error "Checking lockfile: No lockfile found"
  28. fn_print_error_eol_nl
  29. fn_script_log_error "Checking lockfile: No lockfile found: ERROR"
  30. echo -e "* Start ${selfname} to run monitor."
  31. core_exit.sh
  32. fi
  33. }
  34. fn_monitor_check_install() {
  35. if fn_monitor_command_running "install|i|auto-install|ai"; then
  36. fn_print_dots "Checking installer"
  37. fn_print_checking_eol
  38. fn_script_log_info "Checking installer: CHECKING"
  39. fn_print_info "Checking installer: LinuxGSM is installing"
  40. fn_print_info_eol_nl
  41. fn_script_log_pass "Checking installer: LinuxGSM is installing"
  42. core_exit.sh
  43. fi
  44. }
  45. fn_monitor_check_debug() {
  46. if fn_monitor_command_running "debug|d"; then
  47. fn_print_dots "Checking debug"
  48. fn_print_checking_eol
  49. fn_print_info "Checking debug: Debug is running"
  50. fn_print_info_eol_nl
  51. fn_script_log_pass "Checking debug: Debug is running"
  52. core_exit.sh
  53. fi
  54. }
  55. fn_monitor_check_details() {
  56. if fn_monitor_command_running "details|dt"; then
  57. fn_print_dots "Checking details"
  58. fn_print_checking_eol
  59. fn_print_info "Checking details: Details is running"
  60. fn_print_info_eol_nl
  61. fn_script_log_pass "Checking details: Details is running"
  62. core_exit.sh
  63. fi
  64. }
  65. fn_monitor_check_starting() {
  66. # Remove stale lockfile.
  67. if [ -f "${lockdir}/${selfname}-starting.lock" ]; then
  68. if [ "$(find "${lockdir}/${selfname}-starting.lock" -mmin +5)" ]; then
  69. fn_print_dots "Checking start"
  70. fn_print_checking_eol
  71. fn_print_warn "Checking start: Removing stale lockfile"
  72. fn_print_warn_eol_nl
  73. fn_script_log_warn "Checking start: Removing stale lockfile"
  74. rm -f "${lockdir:?}/${selfname}-starting.lock"
  75. fi
  76. fi
  77. if [ -f "${lockdir}/${selfname}-starting.lock" ] && fn_monitor_command_running "start|st"; then
  78. fn_print_dots "Checking start"
  79. fn_print_checking_eol
  80. fn_print_info "Checking start: LinuxGSM is starting"
  81. fn_print_info_eol_nl
  82. fn_script_log_info "Checking start: LinuxGSM is starting"
  83. core_exit.sh
  84. fi
  85. }
  86. fn_monitor_check_stopping() {
  87. # Remove stale lockfile.
  88. if [ -f "${lockdir}/${selfname}-stopping.lock" ]; then
  89. if [ "$(find "${lockdir}/${selfname}-stopping.lock" -mmin +5)" ]; then
  90. fn_print_dots "Checking stop"
  91. fn_print_checking_eol
  92. fn_print_warn "Checking stop: Removing stale lockfile"
  93. fn_print_warn_eol_nl
  94. fn_script_log_warn "Checking stop: Removing stale lockfile"
  95. rm -f "${lockdir:?}/${selfname}-stopping.lock"
  96. fi
  97. fi
  98. if [ -f "${lockdir}/${selfname}-stopping.lock" ] && fn_monitor_command_running "stop|sp"; then
  99. fn_print_dots "Checking stop"
  100. fn_print_checking_eol
  101. fn_print_info "Checking stop: LinuxGSM is stopping"
  102. fn_print_info_eol_nl
  103. fn_script_log_info "Checking stop: LinuxGSM is stopping"
  104. core_exit.sh
  105. fi
  106. }
  107. fn_monitor_check_restart_request() {
  108. if [ -f "${lockdir}/${selfname}-restart-request.lock" ]; then
  109. fn_print_dots "Checking restart: "
  110. fn_print_checking_eol
  111. fn_print_info "Checking restart: Restart requested: "
  112. fn_print_info_eol_nl
  113. fn_script_log_info "Checking restart: Restart requested"
  114. check_players_online.sh
  115. if [ -n "${playersonline}" ]; then
  116. fn_print_info_nl "${playersonline} players are on the server: restart postponed"
  117. fn_script_log_info "${playersonline} players are on the server: restart postponed"
  118. echo "${playersonline}" > "${lockdir:?}/${selfname}-player-numbers.lock"
  119. date '+%s' > "${lockdir:?}/${selfname}-restart-request.lock"
  120. core_exit.sh
  121. fi
  122. command_restart.sh
  123. core_exit.sh
  124. fi
  125. }
  126. # An update postponed because players were online runs once the server is empty.
  127. # It runs in the background as its own update command, so monitor returns straight away
  128. # (e.g. a healthcheck timeout can't stop it) and other monitor runs see the update in progress.
  129. fn_monitor_check_update_request() {
  130. local updaterequest="${lockdir}/${selfname}-update-request.lock"
  131. if [ -f "${updaterequest}" ]; then
  132. fn_print_dots "Checking update: "
  133. fn_print_checking_eol
  134. fn_print_info "Checking update: Update requested: "
  135. fn_print_info_eol_nl
  136. fn_script_log_info "Checking update: Update requested"
  137. check_players_online.sh
  138. if [ -n "${playersonline}" ]; then
  139. fn_print_info_nl "${playersonline} players are on the server: update postponed"
  140. fn_script_log_info "${playersonline} players are on the server: update postponed"
  141. echo "${playersonline}" > "${lockdir:?}/${selfname}-player-numbers.lock"
  142. else
  143. local requestforce=""
  144. if grep -qx "force" "${updaterequest}"; then
  145. requestforce="1"
  146. fi
  147. fn_print_info_nl "Checking update: Running requested update"
  148. fn_script_log_info "Checking update: Running requested update"
  149. forceupdate="${requestforce}" setsid "${rootdir}/${selfname}" update > /dev/null 2>&1 < /dev/null &
  150. # shellcheck disable=SC2034 # Used by core_exit.sh.
  151. exitcode=0
  152. core_exit.sh
  153. fi
  154. fi
  155. }
  156. fn_monitor_check_backup() {
  157. # Remove stale lockfile.
  158. if [ -f "${lockdir}/backup.lock" ]; then
  159. if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then
  160. fn_print_dots "Checking backup"
  161. fn_print_checking_eol
  162. fn_print_warn "Checking backup: Removing stale lockfile"
  163. fn_print_warn_eol
  164. fn_script_log_warn "Checking backup: Removing stale lockfile"
  165. rm -f "${lockdir:?}/backup.lock"
  166. fi
  167. fi
  168. if [ -f "${lockdir}/backup.lock" ] && fn_monitor_command_running "backup|b"; then
  169. fn_print_dots "Checking backup"
  170. fn_print_checking_eol
  171. fn_print_info "Checking backup: Backup is running"
  172. fn_print_info_eol_nl
  173. fn_script_log_info "Checking backup: Backup is running"
  174. core_exit.sh
  175. fi
  176. }
  177. fn_monitor_check_update() {
  178. # Remove stale lockfile, unless a long update is still running.
  179. if [ -f "${lockdir}/update.lock" ] && ! fn_monitor_command_running "update|u|validate|v|force-update|fu|update-restart|ur|workshop-update|wu"; then
  180. if [ "$(find "${lockdir}/update.lock" -mmin +15)" ]; then
  181. fn_print_dots "Checking update"
  182. fn_print_checking_eol
  183. fn_print_warn "Checking update: Removing stale lockfile"
  184. fn_print_warn_eol_nl
  185. fn_script_log_warn "Checking update: Removing stale lockfile"
  186. rm -f "${lockdir:?}/update.lock"
  187. fi
  188. fi
  189. if [ -f "${lockdir}/update.lock" ] && fn_monitor_command_running "update|u|validate|v|force-update|fu|update-restart|ur|workshop-update|wu"; then
  190. fn_print_dots "Checking update"
  191. fn_print_checking_eol
  192. fn_print_info "Checking update: LinuxGSM is updating the game server"
  193. fn_print_info_eol_nl
  194. fn_script_log_pass "Checking update: LinuxGSM is updating the game server"
  195. core_exit.sh
  196. fi
  197. }
  198. # Source engine games may display a messages to indicate the server needs restarting.
  199. fn_monitor_check_update_source() {
  200. if [ -f "${consolelogdir}/${selfname}-console.log" ] && [ "${engine}" == "source" ]; then
  201. if grep -q "Your server needs to be restarted in order to receive the latest update." "${consolelogdir}/${selfname}-console.log"; then
  202. fn_print_dots "Checking update"
  203. fn_print_checking_eol
  204. fn_script_log_info "Checking update: CHECKING"
  205. fn_print_ok "Checking update"
  206. fn_print_ok_eol_nl
  207. fn_script_log_info "Checking update: ${selfname} has requested a restart for an update to be applied"
  208. alert="update-restart-request"
  209. alert.sh
  210. command_restart.sh
  211. core_exit.sh
  212. fi
  213. fi
  214. }
  215. fn_monitor_check_session() {
  216. fn_print_dots "Checking session"
  217. fn_print_checking_eol
  218. fn_script_log_info "Checking session: CHECKING"
  219. # Tmux session width and height needs to be reviewed as may no longer be required.
  220. sessionwidth="80"
  221. sessionheight="23"
  222. # Check for PIDS with identical tmux sessions running.
  223. if [ "$(pgrep -fcx "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" -ge "2" ]; then
  224. fn_print_error "Checking session: There are PIDS with identical tmux sessions running"
  225. fn_print_error_eol_nl
  226. fn_script_log_error "Checking session: ERROR"
  227. fn_script_log_error "Checking session: There are PIDS with identical tmux sessions running"
  228. fn_script_log_error "Checking session: Killing all tmux sessions with the socketname name ${socketname} and session name ${sessionname}"
  229. pkill -f "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}"
  230. command_restart.sh
  231. core_exit.sh
  232. # Check for tmux pids with the same tmux session and socket names. This will reduce issues with migration to release v23.5.0. #4296
  233. elif [ "$(pgrep -fc -u "${USER}" "^tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)")" != "0" ]; then
  234. fn_print_error "Checking session: PIDS with the same tmux session and socket names are running"
  235. fn_print_error_eol_nl
  236. fn_script_log_error "Checking session: ERROR"
  237. fn_script_log_error "Checking session: PIDS with the same tmux session and socket names are running"
  238. fn_script_log_error "Checking session: Killing session with the socketname name ${sessionname} and session name ${sessionname}"
  239. pkill -f "^tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)"
  240. command_restart.sh
  241. core_exit.sh
  242. # Check for tmux pids that are using the old type of tmux session. This will reduce issues with migration to release v23.5.0. #4296
  243. elif [ "$(pgrep -fc -u "${USER}" "^tmux new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)")" != "0" ]; then
  244. fn_print_error "Checking session: PIDS with old type tmux session are running"
  245. fn_print_error_eol_nl
  246. fn_script_log_error "Checking session: ERROR"
  247. fn_script_log_error "Checking session: PIDS with old type tmux session are running"
  248. fn_script_log_error "Checking session: Killing session with the session name ${sessionname}"
  249. pkill -f "^tmux new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)"
  250. command_restart.sh
  251. core_exit.sh
  252. elif [ "${status}" != "0" ]; then
  253. fn_print_ok "Checking session"
  254. fn_print_ok_eol_nl
  255. fn_script_log_pass "Checking session: OK"
  256. # send LinuxGSM stats if monitor is OK.
  257. if [ "${stats}" == "on" ] || [ "${stats}" == "y" ] && [ "${querymode}" == "1" ]; then
  258. info_stats.sh
  259. fi
  260. else
  261. fn_print_error "Checking session"
  262. fn_print_fail_eol_nl
  263. fn_script_log_fail "Checking session: FAIL"
  264. alert="monitor-session"
  265. alert.sh
  266. fn_script_log_info "Checking session: Monitor is restarting ${selfname}"
  267. command_restart.sh
  268. core_exit.sh
  269. fi
  270. }
  271. # Monitor will check queryport is set before continuing.
  272. fn_monitor_check_queryport() {
  273. if [ -z "${queryport}" ] || [ "${queryport}" == "0" ]; then
  274. fn_print_dots "Checking port"
  275. fn_print_checking_eol
  276. fn_script_log_info "Checking port: CHECKING"
  277. if [ -n "${rconenabled}" ] && [ "${rconenabled}" != "true" ] && [ "${shortname}" == "av" ]; then
  278. fn_print_warn "Checking port: Unable to query, rcon is not enabled"
  279. fn_script_log_warn "Checking port: Unable to query, rcon is not enabled"
  280. else
  281. fn_print_error "Checking port: Unable to query, queryport is not set"
  282. fn_script_log_error "Checking port: Unable to query, queryport is not set"
  283. fi
  284. core_exit.sh
  285. fi
  286. }
  287. fn_query_gsquery() {
  288. if [ ! -f "${modulesdir}/query_gsquery.py" ]; then
  289. fn_fetch_file_github "lgsm/modules" "query_gsquery.py" "${modulesdir}" "chmodx" "norun" "noforce" "nohash"
  290. fi
  291. "${modulesdir}"/query_gsquery.py -a "${queryip}" -p "${queryport}" -e "${querytype}" > /dev/null 2>&1
  292. querystatus="$?"
  293. }
  294. fn_query_tcp() {
  295. bash -c "exec 3<> /dev/tcp/'${queryip}'/'${queryport}'" > /dev/null 2>&1
  296. querystatus="$?"
  297. }
  298. fn_monitor_query() {
  299. # Will loop and query up to 5 times every 15 seconds.
  300. # Query will wait up to 60 seconds to confirm server is down as server can become non-responsive during map changes.
  301. totalseconds=0
  302. for queryattempt in {1..5}; do
  303. for queryip in "${queryips[@]}"; do
  304. fn_print_dots "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  305. fn_print_querying_eol
  306. fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : QUERYING"
  307. # querydelay
  308. if [ "$(head -n 1 "${lockdir}/${selfname}-started.lock")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then
  309. fn_print_ok "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  310. fn_print_delay_eol_nl
  311. fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : DELAY"
  312. fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago"
  313. fn_script_log_info "Server started: $(date -d "@$(head -n 1 "${lockdir}/${selfname}-started.lock")")"
  314. fn_script_log_info "Current time: $(date)"
  315. monitorpass=1
  316. core_exit.sh
  317. # will use query method selected in fn_monitor_loop
  318. # gamedig
  319. elif [ "${querymethod}" == "gamedig" ]; then
  320. query_gamedig.sh
  321. # gsquery
  322. elif [ "${querymethod}" == "gsquery" ]; then
  323. fn_query_gsquery
  324. #tcp query
  325. elif [ "${querymethod}" == "tcp" ]; then
  326. fn_query_tcp
  327. fi
  328. if [ "${querystatus}" == "0" ]; then
  329. # Server query OK.
  330. fn_print_ok "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  331. fn_print_ok_eol_nl
  332. fn_script_log_pass "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : OK"
  333. monitorpass=1
  334. if [ "${querystatus}" == "0" ]; then
  335. # Add query data to log.
  336. if [ "${gdname}" ]; then
  337. fn_script_log_info "Server name: ${gdname}"
  338. fi
  339. if [ "${gdplayers}" ]; then
  340. fn_script_log_info "Players: ${gdplayers}/${gdmaxplayers}"
  341. fi
  342. if [ "${gdbots}" ]; then
  343. fn_script_log_info "Bots: ${gdbots}"
  344. fi
  345. if [ "${gdmap}" ]; then
  346. fn_script_log_info "Map: ${gdmap}"
  347. fi
  348. if [ "${gdgamemode}" ]; then
  349. fn_script_log_info "Game Mode: ${gdgamemode}"
  350. fi
  351. # send LinuxGSM stats if monitor is OK.
  352. if [ "${stats}" == "on" ] || [ "${stats}" == "y" ]; then
  353. info_stats.sh
  354. fi
  355. fi
  356. core_exit.sh
  357. else
  358. # Server query FAIL.
  359. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  360. fn_print_fail_eol
  361. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  362. # Monitor will try gamedig (if supported) for first 30s then gsquery before restarting.
  363. # gsquery will fail if longer than 60s
  364. if [ "${totalseconds}" -ge "59" ]; then
  365. # Monitor will FAIL if over 60s and trigger gane server reboot.
  366. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  367. fn_print_fail_eol_nl
  368. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  369. # Send alert if enabled.
  370. alert="monitor-query"
  371. alert.sh
  372. command_restart.sh
  373. fn_firstcommand_reset
  374. core_exit.sh
  375. fi
  376. fi
  377. done
  378. # Second counter will wait for 15s before breaking loop.
  379. for seconds in {1..15}; do
  380. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  381. fn_print_wait_eol
  382. fn_sleep_time_1
  383. totalseconds=$((totalseconds + 1))
  384. if [ "${seconds}" == "15" ]; then
  385. break
  386. fi
  387. done
  388. done
  389. }
  390. fn_monitor_loop() {
  391. # loop though query methods selected by querymode.
  392. totalseconds=0
  393. if [ "${querymode}" == "2" ]; then
  394. local query_methods_array=(gamedig gsquery)
  395. elif [ "${querymode}" == "3" ]; then
  396. local query_methods_array=(gamedig)
  397. elif [ "${querymode}" == "4" ]; then
  398. local query_methods_array=(gsquery)
  399. elif [ "${querymode}" == "5" ]; then
  400. local query_methods_array=(tcp)
  401. fi
  402. for querymethod in "${query_methods_array[@]}"; do
  403. # Will check if gamedig is installed and bypass if not.
  404. if [ "${querymethod}" == "gamedig" ]; then
  405. if [ "$(command -v gamedig 2> /dev/null)" ] || [ -f "${lgsmdir}/node_modules/gamedig/bin/gamedig.js" ] && [ "$(command -v jq 2> /dev/null)" ]; then
  406. if [ -z "${monitorpass}" ]; then
  407. fn_monitor_query
  408. fi
  409. else
  410. fn_script_log_info "gamedig is not installed"
  411. fn_script_log_info "https://docs.linuxgsm.com/requirements/gamedig"
  412. fi
  413. else
  414. # will not query if query already passed.
  415. if [ -z "${monitorpass}" ]; then
  416. fn_monitor_query
  417. fi
  418. fi
  419. done
  420. }
  421. fn_print_dots ""
  422. monitorflag=1
  423. # Dont do any monitoring or checks if installer is running.
  424. fn_monitor_check_install
  425. check.sh
  426. core_logs.sh
  427. info_game.sh
  428. # query pre-checks
  429. fn_monitor_check_details
  430. fn_monitor_check_backup
  431. fn_monitor_check_debug
  432. fn_monitor_check_update_source
  433. fn_monitor_check_update
  434. fn_monitor_check_monitoring
  435. fn_monitor_check_starting
  436. fn_monitor_check_stopping
  437. fn_monitor_check_session
  438. fn_monitor_check_restart_request
  439. fn_monitor_check_update_request
  440. # Monitor will not continue if session only check.
  441. if [ "${querymode}" != "1" ]; then
  442. fn_monitor_check_queryport
  443. # Add a querydelay of 1 min if var missing.
  444. if [ -z "${querydelay}" ]; then
  445. querydelay="1"
  446. fi
  447. fn_monitor_loop
  448. fi
  449. core_exit.sh