command_monitor.sh 17 KB

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