command_monitor.sh 16 KB

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