command_monitor.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. fn_monitor_check_backup() {
  127. # Remove stale lockfile.
  128. if [ -f "${lockdir}/backup.lock" ]; then
  129. if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then
  130. fn_print_dots "Checking backup"
  131. fn_print_checking_eol
  132. fn_print_warn "Checking backup: Removing stale lockfile"
  133. fn_print_warn_eol
  134. fn_script_log_warn "Checking backup: Removing stale lockfile"
  135. rm -f "${lockdir:?}/backup.lock"
  136. fi
  137. fi
  138. if [ -f "${lockdir}/backup.lock" ] && fn_monitor_command_running "backup|b"; then
  139. fn_print_dots "Checking backup"
  140. fn_print_checking_eol
  141. fn_print_info "Checking backup: Backup is running"
  142. fn_print_info_eol_nl
  143. fn_script_log_info "Checking backup: Backup is running"
  144. core_exit.sh
  145. fi
  146. }
  147. fn_monitor_check_update() {
  148. # Remove stale lockfile.
  149. if [ -f "${lockdir}/update.lock" ]; then
  150. if [ "$(find "${lockdir}/update.lock" -mmin +15)" ]; then
  151. fn_print_dots "Checking update"
  152. fn_print_checking_eol
  153. fn_print_warn "Checking update: Removing stale lockfile"
  154. fn_print_warn_eol_nl
  155. fn_script_log_warn "Checking update: Removing stale lockfile"
  156. rm -f "${lockdir:?}/update.lock"
  157. fi
  158. fi
  159. if [ -f "${lockdir}/update.lock" ] && fn_monitor_command_running "update|u|validate|v|force-update|fu|update-restart|ur"; then
  160. fn_print_dots "Checking update"
  161. fn_print_checking_eol
  162. fn_print_info "Checking update: LinuxGSM is updating the game server"
  163. fn_print_info_eol_nl
  164. fn_script_log_pass "Checking update: LinuxGSM is updating the game server"
  165. core_exit.sh
  166. fi
  167. }
  168. # Source engine games may display a messages to indicate the server needs restarting.
  169. fn_monitor_check_update_source() {
  170. if [ -f "${consolelogdir}/${selfname}-console.log" ] && [ "${engine}" == "source" ]; then
  171. if grep -q "Your server needs to be restarted in order to receive the latest update." "${consolelogdir}/${selfname}-console.log"; then
  172. fn_print_dots "Checking update"
  173. fn_print_checking_eol
  174. fn_script_log_info "Checking update: CHECKING"
  175. fn_print_ok "Checking update"
  176. fn_print_ok_eol_nl
  177. fn_script_log_info "Checking update: ${selfname} has requested a restart for an update to be applied"
  178. alert="update-restart-request"
  179. alert.sh
  180. command_restart.sh
  181. core_exit.sh
  182. fi
  183. fi
  184. }
  185. fn_monitor_check_session() {
  186. fn_print_dots "Checking session"
  187. fn_print_checking_eol
  188. fn_script_log_info "Checking session: CHECKING"
  189. # Tmux session width and height needs to be reviewed as may no longer be required.
  190. sessionwidth="80"
  191. sessionheight="23"
  192. # Check for PIDS with identical tmux sessions running.
  193. if [ "$(pgrep -fcx "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" -ge "2" ]; then
  194. fn_print_error "Checking session: There are PIDS with identical tmux sessions running"
  195. fn_print_error_eol_nl
  196. fn_script_log_error "Checking session: ERROR"
  197. fn_script_log_error "Checking session: There are PIDS with identical tmux sessions running"
  198. fn_script_log_error "Checking session: Killing all tmux sessions with the socketname name ${socketname} and session name ${sessionname}"
  199. pkill -f "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}"
  200. command_restart.sh
  201. core_exit.sh
  202. # Check for tmux pids with the same tmux session and socket names. This will reduce issues with migration to release v23.5.0. #4296
  203. elif [ "$(pgrep -fc -u "${USER}" "^tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)")" != "0" ]; then
  204. fn_print_error "Checking session: PIDS with the same tmux session and socket names are running"
  205. fn_print_error_eol_nl
  206. fn_script_log_error "Checking session: ERROR"
  207. fn_script_log_error "Checking session: PIDS with the same tmux session and socket names are running"
  208. fn_script_log_error "Checking session: Killing session with the socketname name ${sessionname} and session name ${sessionname}"
  209. pkill -f "^tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)"
  210. command_restart.sh
  211. core_exit.sh
  212. # 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
  213. elif [ "$(pgrep -fc -u "${USER}" "^tmux new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)")" != "0" ]; then
  214. fn_print_error "Checking session: PIDS with old type tmux session are running"
  215. fn_print_error_eol_nl
  216. fn_script_log_error "Checking session: ERROR"
  217. fn_script_log_error "Checking session: PIDS with old type tmux session are running"
  218. fn_script_log_error "Checking session: Killing session with the session name ${sessionname}"
  219. pkill -f "^tmux new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)"
  220. command_restart.sh
  221. core_exit.sh
  222. elif [ "${status}" != "0" ]; then
  223. fn_print_ok "Checking session"
  224. fn_print_ok_eol_nl
  225. fn_script_log_pass "Checking session: OK"
  226. # send LinuxGSM stats if monitor is OK.
  227. if [ "${stats}" == "on" ] || [ "${stats}" == "y" ] && [ "${querymode}" == "1" ]; then
  228. info_stats.sh
  229. fi
  230. else
  231. fn_print_error "Checking session"
  232. fn_print_fail_eol_nl
  233. fn_script_log_fail "Checking session: FAIL"
  234. alert="monitor-session"
  235. alert.sh
  236. fn_script_log_info "Checking session: Monitor is restarting ${selfname}"
  237. command_restart.sh
  238. core_exit.sh
  239. fi
  240. }
  241. # Monitor will check queryport is set before continuing.
  242. fn_monitor_check_queryport() {
  243. if [ -z "${queryport}" ] || [ "${queryport}" == "0" ]; then
  244. fn_print_dots "Checking port"
  245. fn_print_checking_eol
  246. fn_script_log_info "Checking port: CHECKING"
  247. if [ -n "${rconenabled}" ] && [ "${rconenabled}" != "true" ] && [ "${shortname}" == "av" ]; then
  248. fn_print_warn "Checking port: Unable to query, rcon is not enabled"
  249. fn_script_log_warn "Checking port: Unable to query, rcon is not enabled"
  250. else
  251. fn_print_error "Checking port: Unable to query, queryport is not set"
  252. fn_script_log_error "Checking port: Unable to query, queryport is not set"
  253. fi
  254. core_exit.sh
  255. fi
  256. }
  257. fn_query_gsquery() {
  258. if [ ! -f "${modulesdir}/query_gsquery.py" ]; then
  259. fn_fetch_file_github "lgsm/modules" "query_gsquery.py" "${modulesdir}" "chmodx" "norun" "noforce" "nohash"
  260. fi
  261. "${modulesdir}"/query_gsquery.py -a "${queryip}" -p "${queryport}" -e "${querytype}" > /dev/null 2>&1
  262. querystatus="$?"
  263. }
  264. fn_query_tcp() {
  265. bash -c "exec 3<> /dev/tcp/'${queryip}'/'${queryport}'" > /dev/null 2>&1
  266. querystatus="$?"
  267. }
  268. fn_monitor_query() {
  269. # Will loop and query up to 5 times every 15 seconds.
  270. # Query will wait up to 60 seconds to confirm server is down as server can become non-responsive during map changes.
  271. totalseconds=0
  272. for queryattempt in {1..5}; do
  273. for queryip in "${queryips[@]}"; do
  274. fn_print_dots "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  275. fn_print_querying_eol
  276. fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : QUERYING"
  277. # querydelay
  278. if [ "$(head -n 1 "${lockdir}/${selfname}-started.lock")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then
  279. fn_print_ok "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  280. fn_print_delay_eol_nl
  281. fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : DELAY"
  282. fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago"
  283. fn_script_log_info "Server started: $(date -d "@$(head -n 1 "${lockdir}/${selfname}-started.lock")")"
  284. fn_script_log_info "Current time: $(date)"
  285. monitorpass=1
  286. core_exit.sh
  287. # will use query method selected in fn_monitor_loop
  288. # gamedig
  289. elif [ "${querymethod}" == "gamedig" ]; then
  290. query_gamedig.sh
  291. # gsquery
  292. elif [ "${querymethod}" == "gsquery" ]; then
  293. fn_query_gsquery
  294. #tcp query
  295. elif [ "${querymethod}" == "tcp" ]; then
  296. fn_query_tcp
  297. fi
  298. if [ "${querystatus}" == "0" ]; then
  299. # Server query OK.
  300. fn_print_ok "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  301. fn_print_ok_eol_nl
  302. fn_script_log_pass "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : OK"
  303. monitorpass=1
  304. if [ "${querystatus}" == "0" ]; then
  305. # Add query data to log.
  306. if [ "${gdname}" ]; then
  307. fn_script_log_info "Server name: ${gdname}"
  308. fi
  309. if [ "${gdplayers}" ]; then
  310. fn_script_log_info "Players: ${gdplayers}/${gdmaxplayers}"
  311. fi
  312. if [ "${gdbots}" ]; then
  313. fn_script_log_info "Bots: ${gdbots}"
  314. fi
  315. if [ "${gdmap}" ]; then
  316. fn_script_log_info "Map: ${gdmap}"
  317. fi
  318. if [ "${gdgamemode}" ]; then
  319. fn_script_log_info "Game Mode: ${gdgamemode}"
  320. fi
  321. # send LinuxGSM stats if monitor is OK.
  322. if [ "${stats}" == "on" ] || [ "${stats}" == "y" ]; then
  323. info_stats.sh
  324. fi
  325. fi
  326. core_exit.sh
  327. else
  328. # Server query FAIL.
  329. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  330. fn_print_fail_eol
  331. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  332. # Monitor will try gamedig (if supported) for first 30s then gsquery before restarting.
  333. # gsquery will fail if longer than 60s
  334. if [ "${totalseconds}" -ge "59" ]; then
  335. # Monitor will FAIL if over 60s and trigger gane server reboot.
  336. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  337. fn_print_fail_eol_nl
  338. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  339. # Send alert if enabled.
  340. alert="monitor-query"
  341. alert.sh
  342. command_restart.sh
  343. fn_firstcommand_reset
  344. core_exit.sh
  345. fi
  346. fi
  347. done
  348. # Second counter will wait for 15s before breaking loop.
  349. for seconds in {1..15}; do
  350. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  351. fn_print_wait_eol
  352. fn_sleep_time_1
  353. totalseconds=$((totalseconds + 1))
  354. if [ "${seconds}" == "15" ]; then
  355. break
  356. fi
  357. done
  358. done
  359. }
  360. fn_monitor_loop() {
  361. # loop though query methods selected by querymode.
  362. totalseconds=0
  363. if [ "${querymode}" == "2" ]; then
  364. local query_methods_array=(gamedig gsquery)
  365. elif [ "${querymode}" == "3" ]; then
  366. local query_methods_array=(gamedig)
  367. elif [ "${querymode}" == "4" ]; then
  368. local query_methods_array=(gsquery)
  369. elif [ "${querymode}" == "5" ]; then
  370. local query_methods_array=(tcp)
  371. fi
  372. for querymethod in "${query_methods_array[@]}"; do
  373. # Will check if gamedig is installed and bypass if not.
  374. if [ "${querymethod}" == "gamedig" ]; then
  375. if [ "$(command -v gamedig 2> /dev/null)" ] || [ -f "${lgsmdir}/node_modules/gamedig/bin/gamedig.js" ] && [ "$(command -v jq 2> /dev/null)" ]; then
  376. if [ -z "${monitorpass}" ]; then
  377. fn_monitor_query
  378. fi
  379. else
  380. fn_script_log_info "gamedig is not installed"
  381. fn_script_log_info "https://docs.linuxgsm.com/requirements/gamedig"
  382. fi
  383. else
  384. # will not query if query already passed.
  385. if [ -z "${monitorpass}" ]; then
  386. fn_monitor_query
  387. fi
  388. fi
  389. done
  390. }
  391. fn_print_dots ""
  392. monitorflag=1
  393. # Dont do any monitoring or checks if installer is running.
  394. fn_monitor_check_install
  395. check.sh
  396. core_logs.sh
  397. info_game.sh
  398. # query pre-checks
  399. fn_monitor_check_details
  400. fn_monitor_check_backup
  401. fn_monitor_check_debug
  402. fn_monitor_check_update_source
  403. fn_monitor_check_update
  404. fn_monitor_check_monitoring
  405. fn_monitor_check_starting
  406. fn_monitor_check_stopping
  407. fn_monitor_check_session
  408. fn_monitor_check_restart_request
  409. # Monitor will not continue if session only check.
  410. if [ "${querymode}" != "1" ]; then
  411. fn_monitor_check_queryport
  412. # Add a querydelay of 1 min if var missing.
  413. if [ -z "${querydelay}" ]; then
  414. querydelay="1"
  415. fi
  416. fn_monitor_loop
  417. fi
  418. core_exit.sh