command_monitor.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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: Monitor is restarting ${selfname} to apply update"
  144. alert="update-restart"
  145. alert.sh
  146. command_restart.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. core_exit.sh
  167. # Check for tmux pids with the same tmux session and socket names. This will reduce issues with migration to release v23.5.0. #4296
  168. elif [ "$(pgrep -fc -u "${USER}" "tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" != "0" ]; then
  169. fn_print_error "Checking session: PIDS with the same tmux session and socket names are running: "
  170. fn_print_error_eol_nl
  171. fn_script_log_error "Checking session: ERROR"
  172. fn_script_log_error "Checking session: PIDS with the same tmux session and socket names are running"
  173. fn_script_log_error "Checking session: Killing session with the socketname name ${sessionname} and session name ${sessionname}"
  174. pkill -f "tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}"
  175. core_exit.sh
  176. # 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
  177. elif [ "$(pgrep -fc -u "${USER}" "tmux new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" != "0" ]; then
  178. fn_print_error "Checking session: PIDS with old type tmux session are running: "
  179. fn_print_error_eol_nl
  180. fn_script_log_error "Checking session: ERROR"
  181. fn_script_log_error "Checking session: PIDS with old type tmux session are running"
  182. fn_script_log_error "Checking session: Killing session with the session name ${sessionname}"
  183. pkill -f "tmux new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}"
  184. core_exit.sh
  185. elif [ "${status}" != "0" ]; then
  186. fn_print_ok "Checking session: "
  187. fn_print_ok_eol_nl
  188. fn_script_log_pass "Checking session: OK"
  189. else
  190. fn_print_error "Checking session: "
  191. fn_print_fail_eol_nl
  192. fn_script_log_fatal "Checking session: FAIL"
  193. alert="restart"
  194. alert.sh
  195. fn_script_log_info "Checking session: Monitor is restarting ${selfname}"
  196. command_restart.sh
  197. core_exit.sh
  198. fi
  199. }
  200. # Monitor will check queryport is set before continuing.
  201. fn_monitor_check_queryport() {
  202. if [ -z "${queryport}" ] || [ "${queryport}" == "0" ]; then
  203. fn_print_dots "Checking port: "
  204. fn_print_checking_eol
  205. fn_script_log_info "Checking port: CHECKING"
  206. if [ -n "${rconenabled}" ] && [ "${rconenabled}" != "true" ] && [ "${shortname}" == "av" ]; then
  207. fn_print_warn "Checking port: Unable to query, rcon is not enabled"
  208. fn_script_log_warn "Checking port: Unable to query, rcon is not enabled"
  209. else
  210. fn_print_error "Checking port: Unable to query, queryport is not set"
  211. fn_script_log_error "Checking port: Unable to query, queryport is not set"
  212. fi
  213. core_exit.sh
  214. fi
  215. }
  216. fn_query_gsquery() {
  217. if [ ! -f "${modulesdir}/query_gsquery.py" ]; then
  218. fn_fetch_file_github "lgsm/modules" "query_gsquery.py" "${modulesdir}" "chmodx" "norun" "noforce" "nohash"
  219. fi
  220. "${modulesdir}"/query_gsquery.py -a "${queryip}" -p "${queryport}" -e "${querytype}" > /dev/null 2>&1
  221. querystatus="$?"
  222. }
  223. fn_query_tcp() {
  224. bash -c "exec 3<> /dev/tcp/'${queryip}'/'${queryport}'" > /dev/null 2>&1
  225. querystatus="$?"
  226. }
  227. fn_monitor_query() {
  228. # Will loop and query up to 5 times every 15 seconds.
  229. # Query will wait up to 60 seconds to confirm server is down as server can become non-responsive during map changes.
  230. totalseconds=0
  231. for queryattempt in {1..5}; do
  232. for queryip in "${queryips[@]}"; do
  233. fn_print_dots "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}: "
  234. fn_print_querying_eol
  235. fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : QUERYING"
  236. # querydelay
  237. if [ "$(head -n 1 "${lockdir}/${selfname}-started.lock")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then
  238. fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
  239. fn_print_delay_eol_nl
  240. fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : DELAY"
  241. fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago"
  242. fn_script_log_info "Server started: $(date -d "@$(head -n 1 "${lockdir}/${selfname}-started.lock")")"
  243. fn_script_log_info "Current time: $(date)"
  244. monitorpass=1
  245. core_exit.sh
  246. # will use query method selected in fn_monitor_loop
  247. # gamedig
  248. elif [ "${querymethod}" == "gamedig" ]; then
  249. query_gamedig.sh
  250. # gsquery
  251. elif [ "${querymethod}" == "gsquery" ]; then
  252. fn_query_gsquery
  253. #tcp query
  254. elif [ "${querymethod}" == "tcp" ]; then
  255. fn_query_tcp
  256. fi
  257. if [ "${querystatus}" == "0" ]; then
  258. # Server query OK.
  259. fn_print_ok "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}: "
  260. fn_print_ok_eol_nl
  261. fn_script_log_pass "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : OK"
  262. monitorpass=1
  263. if [ "${querystatus}" == "0" ]; then
  264. # Add query data to log.
  265. if [ "${gdname}" ]; then
  266. fn_script_log_info "Server name: ${gdname}"
  267. fi
  268. if [ "${gdplayers}" ]; then
  269. fn_script_log_info "Players: ${gdplayers}/${gdmaxplayers}"
  270. fi
  271. if [ "${gdbots}" ]; then
  272. fn_script_log_info "Bots: ${gdbots}"
  273. fi
  274. if [ "${gdmap}" ]; then
  275. fn_script_log_info "Map: ${gdmap}"
  276. fi
  277. if [ "${gdgamemode}" ]; then
  278. fn_script_log_info "Game Mode: ${gdgamemode}"
  279. fi
  280. # send LinuxGSM stats if monitor is OK.
  281. if [ "${stats}" == "on" ] || [ "${stats}" == "y" ]; then
  282. info_stats.sh
  283. fi
  284. fi
  285. core_exit.sh
  286. else
  287. # Server query FAIL.
  288. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}: "
  289. fn_print_fail_eol
  290. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  291. # Monitor will try gamedig (if supported) for first 30s then gsquery before restarting.
  292. # gsquery will fail if longer than 60s
  293. if [ "${totalseconds}" -ge "59" ]; then
  294. # Monitor will FAIL if over 60s and trigger gane server reboot.
  295. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}: "
  296. fn_print_fail_eol_nl
  297. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  298. # Send alert if enabled.
  299. alert="restartquery"
  300. alert.sh
  301. command_restart.sh
  302. fn_firstcommand_reset
  303. core_exit.sh
  304. fi
  305. fi
  306. done
  307. # Second counter will wait for 15s before breaking loop.
  308. for seconds in {1..15}; do
  309. fn_print_fail "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt} : ${cyan}WAIT${default}"
  310. sleep 0.5
  311. totalseconds=$((totalseconds + 1))
  312. if [ "${seconds}" == "15" ]; then
  313. break
  314. fi
  315. done
  316. done
  317. }
  318. fn_monitor_loop() {
  319. # loop though query methods selected by querymode.
  320. totalseconds=0
  321. if [ "${querymode}" == "2" ]; then
  322. local query_methods_array=(gamedig gsquery)
  323. elif [ "${querymode}" == "3" ]; then
  324. local query_methods_array=(gamedig)
  325. elif [ "${querymode}" == "4" ]; then
  326. local query_methods_array=(gsquery)
  327. elif [ "${querymode}" == "5" ]; then
  328. local query_methods_array=(tcp)
  329. fi
  330. for querymethod in "${query_methods_array[@]}"; do
  331. # Will check if gamedig is installed and bypass if not.
  332. if [ "${querymethod}" == "gamedig" ]; then
  333. if [ "$(command -v gamedig 2> /dev/null)" ] && [ "$(command -v jq 2> /dev/null)" ]; then
  334. if [ -z "${monitorpass}" ]; then
  335. fn_monitor_query
  336. fi
  337. else
  338. fn_script_log_info "gamedig is not installed"
  339. fn_script_log_info "https://docs.linuxgsm.com/requirements/gamedig"
  340. fi
  341. else
  342. # will not query if query already passed.
  343. if [ -z "${monitorpass}" ]; then
  344. fn_monitor_query
  345. fi
  346. fi
  347. done
  348. }
  349. monitorflag=1
  350. # Dont do any monitoring or checks if installer is running.
  351. fn_monitor_check_install
  352. check.sh
  353. core_logs.sh
  354. info_game.sh
  355. # query pre-checks
  356. fn_monitor_check_update_source
  357. fn_monitor_check_update
  358. fn_monitor_check_backup
  359. fn_monitor_check_debug
  360. fn_monitor_check_monitoring
  361. fn_monitor_check_starting
  362. fn_monitor_check_stopping
  363. fn_monitor_check_session
  364. # Monitor will not continue if session only check.
  365. if [ "${querymode}" != "1" ]; then
  366. fn_monitor_check_queryport
  367. # Add a querydelay of 1 min if var missing.
  368. if [ -z "${querydelay}" ]; then
  369. querydelay="1"
  370. fi
  371. fn_monitor_loop
  372. fi
  373. core_exit.sh