command_monitor.sh 14 KB

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