command_monitor.sh 15 KB

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