command_monitor.sh 17 KB

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