command_monitor.sh 16 KB

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