command_monitor.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. # Checks if this instance is running one of the given commands, e.g. "update|u".
  13. # Matches the script run as ./gameserver, /path/to/gameserver or bash gameserver.
  14. fn_monitor_command_running() {
  15. [ "$(pgrep -fc -u "${USER}" "^([^ ]*/)?bash ([^ ]*/)?${selfname} ($1)( |$)")" != "0" ]
  16. }
  17. fn_monitor_check_monitoring() {
  18. if [ -f "${lockdir}/${selfname}.lock" ]; then
  19. # Part of migration to v23.5.0. #4296
  20. rm -f "${lockdir:?}/${selfname}.lock"
  21. date '+%s' > "${lockdir:?}/${selfname}-monitoring.lock"
  22. elif [ ! -f "${lockdir}/${selfname}-monitoring.lock" ]; then
  23. # Monitor does not run if lockfile is not found.
  24. fn_print_dots "Checking lockfile"
  25. fn_print_checking_eol
  26. fn_script_log_info "Checking lockfile: CHECKING"
  27. fn_print_error "Checking lockfile: No lockfile found"
  28. fn_print_error_eol_nl
  29. fn_script_log_error "Checking lockfile: No lockfile found: ERROR"
  30. echo -e "* Start ${selfname} to run monitor."
  31. core_exit.sh
  32. fi
  33. }
  34. fn_monitor_check_install() {
  35. if fn_monitor_command_running "install|i|auto-install|ai"; then
  36. fn_print_dots "Checking installer"
  37. fn_print_checking_eol
  38. fn_script_log_info "Checking installer: CHECKING"
  39. fn_print_info "Checking installer: LinuxGSM is installing"
  40. fn_print_info_eol_nl
  41. fn_script_log_pass "Checking installer: LinuxGSM is installing"
  42. core_exit.sh
  43. fi
  44. }
  45. fn_monitor_check_debug() {
  46. if fn_monitor_command_running "debug|d"; then
  47. fn_print_dots "Checking debug"
  48. fn_print_checking_eol
  49. fn_print_info "Checking debug: Debug is running"
  50. fn_print_info_eol_nl
  51. fn_script_log_pass "Checking debug: Debug is running"
  52. core_exit.sh
  53. fi
  54. }
  55. fn_monitor_check_details() {
  56. if fn_monitor_command_running "details|dt"; then
  57. fn_print_dots "Checking details"
  58. fn_print_checking_eol
  59. fn_print_info "Checking details: Details is running"
  60. fn_print_info_eol_nl
  61. fn_script_log_pass "Checking details: Details is running"
  62. core_exit.sh
  63. fi
  64. }
  65. fn_monitor_check_starting() {
  66. # Remove stale lockfile.
  67. if [ -f "${lockdir}/${selfname}-starting.lock" ]; then
  68. if [ "$(find "${lockdir}/${selfname}-starting.lock" -mmin +5)" ]; then
  69. fn_print_dots "Checking start"
  70. fn_print_checking_eol
  71. fn_print_warn "Checking start: Removing stale lockfile"
  72. fn_print_warn_eol_nl
  73. fn_script_log_warn "Checking start: Removing stale lockfile"
  74. rm -f "${lockdir:?}/${selfname}-starting.lock"
  75. fi
  76. fi
  77. if [ -f "${lockdir}/${selfname}-starting.lock" ] && fn_monitor_command_running "start|st"; then
  78. fn_print_dots "Checking start"
  79. fn_print_checking_eol
  80. fn_print_info "Checking start: LinuxGSM is starting"
  81. fn_print_info_eol_nl
  82. fn_script_log_info "Checking start: LinuxGSM is starting"
  83. core_exit.sh
  84. fi
  85. }
  86. fn_monitor_check_stopping() {
  87. # Remove stale lockfile.
  88. if [ -f "${lockdir}/${selfname}-stopping.lock" ]; then
  89. if [ "$(find "${lockdir}/${selfname}-stopping.lock" -mmin +5)" ]; then
  90. fn_print_dots "Checking stop"
  91. fn_print_checking_eol
  92. fn_print_warn "Checking stop: Removing stale lockfile"
  93. fn_print_warn_eol_nl
  94. fn_script_log_warn "Checking stop: Removing stale lockfile"
  95. rm -f "${lockdir:?}/${selfname}-stopping.lock"
  96. fi
  97. fi
  98. if [ -f "${lockdir}/${selfname}-stopping.lock" ] && fn_monitor_command_running "stop|sp"; then
  99. fn_print_dots "Checking stop"
  100. fn_print_checking_eol
  101. fn_print_info "Checking stop: LinuxGSM is stopping"
  102. fn_print_info_eol_nl
  103. fn_script_log_info "Checking stop: LinuxGSM is stopping"
  104. core_exit.sh
  105. fi
  106. }
  107. fn_monitor_check_restart_request() {
  108. if [ -f "${lockdir}/${selfname}-restart-request.lock" ]; then
  109. fn_print_dots "Checking restart: "
  110. fn_print_checking_eol
  111. fn_print_info "Checking restart: Restart requested: "
  112. fn_print_info_eol_nl
  113. fn_script_log_info "Checking restart: Restart requested"
  114. check_players_online.sh
  115. if [ -n "${playersonline}" ]; then
  116. fn_print_info_nl "${playersonline} players are on the server: restart postponed"
  117. fn_script_log_info "${playersonline} players are on the server: restart postponed"
  118. echo "${playersonline}" > "${lockdir:?}/${selfname}-player-numbers.lock"
  119. date '+%s' > "${lockdir:?}/${selfname}-restart-request.lock"
  120. core_exit.sh
  121. fi
  122. command_restart.sh
  123. core_exit.sh
  124. fi
  125. }
  126. # An update postponed because players were online runs once the server is empty.
  127. # It runs in the background as its own update command, so monitor returns straight away
  128. # (e.g. a healthcheck timeout can't stop it) and other monitor runs see the update in progress.
  129. fn_monitor_check_update_request() {
  130. local updaterequest="${lockdir}/${selfname}-update-request.lock"
  131. if [ -f "${updaterequest}" ]; then
  132. fn_print_dots "Checking update: "
  133. fn_print_checking_eol
  134. fn_print_info "Checking update: Update requested: "
  135. fn_print_info_eol_nl
  136. fn_script_log_info "Checking update: Update requested"
  137. check_players_online.sh
  138. if [ -n "${playersonline}" ]; then
  139. fn_print_info_nl "${playersonline} players are on the server: update postponed"
  140. fn_script_log_info "${playersonline} players are on the server: update postponed"
  141. echo "${playersonline}" > "${lockdir:?}/${selfname}-player-numbers.lock"
  142. else
  143. local requestforce=""
  144. if grep -qx "force" "${updaterequest}"; then
  145. requestforce="1"
  146. fi
  147. fn_print_info_nl "Checking update: Running requested update"
  148. fn_script_log_info "Checking update: Running requested update"
  149. forceupdate="${requestforce}" setsid "${rootdir}/${selfname}" update > /dev/null 2>&1 < /dev/null &
  150. # shellcheck disable=SC2034 # Used by core_exit.sh.
  151. exitcode=0
  152. core_exit.sh
  153. fi
  154. fi
  155. }
  156. # A backup postponed because players were online runs once the server is empty, in the background
  157. # like a requested update. An alert is sent once if it has been postponed for over 24 hours.
  158. fn_monitor_check_backup_request() {
  159. local backuprequest="${lockdir}/${selfname}-backup-request.lock"
  160. if [ -f "${backuprequest}" ]; then
  161. fn_print_dots "Checking backup: "
  162. fn_print_checking_eol
  163. fn_print_info "Checking backup: Backup requested: "
  164. fn_print_info_eol_nl
  165. fn_script_log_info "Checking backup: Backup requested"
  166. check_players_online.sh
  167. if [ -n "${playersonline}" ]; then
  168. fn_print_info_nl "${playersonline} players are on the server: backup postponed"
  169. fn_script_log_info "${playersonline} players are on the server: backup postponed"
  170. echo "${playersonline}" > "${lockdir:?}/${selfname}-player-numbers.lock"
  171. local requested
  172. requested="$(head -n 1 "${backuprequest}")"
  173. if [[ "${requested}" =~ ^[0-9]+$ ]] && [ "$(($(date '+%s') - requested))" -gt 86400 ] && ! grep -q "alerted" "${backuprequest}"; then
  174. alert="backup-postponed"
  175. alert.sh
  176. echo "alerted" >> "${backuprequest}"
  177. fi
  178. else
  179. fn_print_info_nl "Checking backup: Running requested backup"
  180. fn_script_log_info "Checking backup: Running requested backup"
  181. setsid "${rootdir}/${selfname}" backup > /dev/null 2>&1 < /dev/null &
  182. # shellcheck disable=SC2034 # Used by core_exit.sh.
  183. exitcode=0
  184. core_exit.sh
  185. fi
  186. fi
  187. }
  188. fn_monitor_check_backup() {
  189. # Remove stale lockfile, unless a long backup is still running.
  190. if [ -f "${lockdir}/backup.lock" ] && ! fn_monitor_command_running "backup|b"; then
  191. if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then
  192. fn_print_dots "Checking backup"
  193. fn_print_checking_eol
  194. fn_print_warn "Checking backup: Removing stale lockfile"
  195. fn_print_warn_eol
  196. fn_script_log_warn "Checking backup: Removing stale lockfile"
  197. rm -f "${lockdir:?}/backup.lock"
  198. fi
  199. fi
  200. if [ -f "${lockdir}/backup.lock" ] && fn_monitor_command_running "backup|b"; then
  201. fn_print_dots "Checking backup"
  202. fn_print_checking_eol
  203. fn_print_info "Checking backup: Backup is running"
  204. fn_print_info_eol_nl
  205. fn_script_log_info "Checking backup: Backup is running"
  206. core_exit.sh
  207. fi
  208. }
  209. fn_monitor_check_update() {
  210. # Remove stale lockfile, unless a long update is still running.
  211. if [ -f "${lockdir}/update.lock" ] && ! fn_monitor_command_running "update|u|validate|v|force-update|fu|update-restart|ur|workshop-update|wu"; then
  212. if [ "$(find "${lockdir}/update.lock" -mmin +15)" ]; then
  213. fn_print_dots "Checking update"
  214. fn_print_checking_eol
  215. fn_print_warn "Checking update: Removing stale lockfile"
  216. fn_print_warn_eol_nl
  217. fn_script_log_warn "Checking update: Removing stale lockfile"
  218. rm -f "${lockdir:?}/update.lock"
  219. fi
  220. fi
  221. if [ -f "${lockdir}/update.lock" ] && fn_monitor_command_running "update|u|validate|v|force-update|fu|update-restart|ur|workshop-update|wu"; then
  222. fn_print_dots "Checking update"
  223. fn_print_checking_eol
  224. fn_print_info "Checking update: LinuxGSM is updating the game server"
  225. fn_print_info_eol_nl
  226. fn_script_log_pass "Checking update: LinuxGSM is updating the game server"
  227. core_exit.sh
  228. fi
  229. }
  230. fn_monitor_check_backup_update() {
  231. fn_monitor_check_backup
  232. fn_monitor_check_update
  233. }
  234. # Source engine games may display a messages to indicate the server needs restarting.
  235. fn_monitor_check_update_source() {
  236. if [ -f "${consolelogdir}/${selfname}-console.log" ] && [ "${engine}" == "source" ]; then
  237. if grep -q "Your server needs to be restarted in order to receive the latest update." "${consolelogdir}/${selfname}-console.log"; then
  238. fn_print_dots "Checking update"
  239. fn_print_checking_eol
  240. fn_script_log_info "Checking update: CHECKING"
  241. fn_print_ok "Checking update"
  242. fn_print_ok_eol_nl
  243. fn_script_log_info "Checking update: ${selfname} has requested a restart for an update to be applied"
  244. alert="update-restart-request"
  245. alert.sh
  246. command_restart.sh
  247. core_exit.sh
  248. fi
  249. fi
  250. }
  251. fn_monitor_check_session() {
  252. fn_print_dots "Checking session"
  253. fn_print_checking_eol
  254. fn_script_log_info "Checking session: CHECKING"
  255. # Tmux session width and height needs to be reviewed as may no longer be required.
  256. sessionwidth="80"
  257. sessionheight="23"
  258. # Check for PIDS with identical tmux sessions running.
  259. if [ "$(pgrep -fcx "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" -ge "2" ]; then
  260. fn_print_error "Checking session: There are PIDS with identical tmux sessions running"
  261. fn_print_error_eol_nl
  262. fn_script_log_error "Checking session: ERROR"
  263. fn_script_log_error "Checking session: There are PIDS with identical tmux sessions running"
  264. fn_script_log_error "Checking session: Killing all tmux sessions with the socketname name ${socketname} and session name ${sessionname}"
  265. pkill -f "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}"
  266. fn_monitor_check_backup_update
  267. command_restart.sh
  268. core_exit.sh
  269. # Check for tmux pids with the same tmux session and socket names. This will reduce issues with migration to release v23.5.0. #4296
  270. elif [ "$(pgrep -fc -u "${USER}" "^tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)")" != "0" ]; then
  271. fn_print_error "Checking session: PIDS with the same tmux session and socket names are running"
  272. fn_print_error_eol_nl
  273. fn_script_log_error "Checking session: ERROR"
  274. fn_script_log_error "Checking session: PIDS with the same tmux session and socket names are running"
  275. fn_script_log_error "Checking session: Killing session with the socketname name ${sessionname} and session name ${sessionname}"
  276. pkill -f "^tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)"
  277. fn_monitor_check_backup_update
  278. command_restart.sh
  279. core_exit.sh
  280. # 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
  281. elif [ "$(pgrep -fc -u "${USER}" "^tmux new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)")" != "0" ]; then
  282. fn_print_error "Checking session: PIDS with old type tmux session are running"
  283. fn_print_error_eol_nl
  284. fn_script_log_error "Checking session: ERROR"
  285. fn_script_log_error "Checking session: PIDS with old type tmux session are running"
  286. fn_script_log_error "Checking session: Killing session with the session name ${sessionname}"
  287. pkill -f "^tmux new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}( |$)"
  288. fn_monitor_check_backup_update
  289. command_restart.sh
  290. core_exit.sh
  291. elif [ "${status}" != "0" ]; then
  292. fn_print_ok "Checking session"
  293. fn_print_ok_eol_nl
  294. fn_script_log_pass "Checking session: OK"
  295. # send LinuxGSM stats if monitor is OK.
  296. if [ "${stats}" == "on" ] || [ "${stats}" == "y" ] && [ "${querymode}" == "1" ]; then
  297. info_stats.sh
  298. fi
  299. else
  300. fn_print_error "Checking session"
  301. fn_print_fail_eol_nl
  302. fn_script_log_fail "Checking session: FAIL"
  303. fn_monitor_check_backup_update
  304. alert="monitor-session"
  305. alert.sh
  306. fn_script_log_info "Checking session: Monitor is restarting ${selfname}"
  307. command_restart.sh
  308. core_exit.sh
  309. fi
  310. }
  311. # Monitor will check queryport is set before continuing.
  312. fn_monitor_check_queryport() {
  313. if [ -z "${queryport}" ] || [ "${queryport}" == "0" ]; then
  314. fn_print_dots "Checking port"
  315. fn_print_checking_eol
  316. fn_script_log_info "Checking port: CHECKING"
  317. if [ -n "${rconenabled}" ] && [ "${rconenabled}" != "true" ] && [ "${shortname}" == "av" ]; then
  318. fn_print_warn "Checking port: Unable to query, rcon is not enabled"
  319. fn_script_log_warn "Checking port: Unable to query, rcon is not enabled"
  320. else
  321. fn_print_error "Checking port: Unable to query, queryport is not set"
  322. fn_script_log_error "Checking port: Unable to query, queryport is not set"
  323. fi
  324. core_exit.sh
  325. fi
  326. }
  327. fn_query_gsquery() {
  328. if [ ! -f "${modulesdir}/query_gsquery.py" ]; then
  329. fn_fetch_file_github "lgsm/modules" "query_gsquery.py" "${modulesdir}" "chmodx" "norun" "noforce" "nohash"
  330. fi
  331. "${modulesdir}"/query_gsquery.py -a "${queryip}" -p "${queryport}" -e "${querytype}" > /dev/null 2>&1
  332. querystatus="$?"
  333. }
  334. fn_query_tcp() {
  335. bash -c "exec 3<> /dev/tcp/'${queryip}'/'${queryport}'" > /dev/null 2>&1
  336. querystatus="$?"
  337. }
  338. fn_monitor_query_port_hint() {
  339. local sockets protocol state recvq sendq address peer details
  340. local serverpid="" protocols="" process="" pid="" ancestor="" serverprocess="" otherprocess="" hint=""
  341. command -v ss > /dev/null 2>&1 || return 0
  342. sockets="$(ss -H -lntup 2> /dev/null)" || return 0
  343. serverpid="$(TERM=screen tmux -L "${socketname}" list-sessions -F "#{session_name} #{pane_pid}" 2> /dev/null | awk -v session="${sessionname}" '$1 == session {print $2}')"
  344. while read -r protocol state recvq sendq address peer details; do
  345. if [ "${address##*:}" != "${queryport}" ]; then
  346. continue
  347. fi
  348. if [[ " ${protocols//\// } " != *" ${protocol} "* ]]; then
  349. protocols+="${protocols:+/}${protocol}"
  350. fi
  351. process="$(sed -nE 's/.*users:\(\("([^"]+)".*/\1/p' <<< "${details}")"
  352. pid="$(sed -nE 's/.*users:\(\("[^"]+",pid=([0-9]+).*/\1/p' <<< "${details}")"
  353. ancestor="${pid}"
  354. while [ -n "${serverpid}" ] && [ -n "${ancestor}" ] && [ "${ancestor}" -gt 1 ]; do
  355. if [ "${ancestor}" == "${serverpid}" ]; then
  356. serverprocess="${process}"
  357. break
  358. fi
  359. ancestor="$(ps -o ppid= -p "${ancestor}" 2> /dev/null | tr -d ' ')"
  360. done
  361. if [ -z "${serverprocess}" ] && [ -z "${otherprocess}" ]; then
  362. otherprocess="${process}"
  363. fi
  364. done <<< "${sockets}"
  365. if [ -z "${protocols}" ]; then
  366. hint="Nothing is listening on port ${queryport}; check the server started and the port settings"
  367. elif [ -n "${serverprocess}" ]; then
  368. hint="Port ${queryport}/${protocols} is open by the game server (${serverprocess}) but it did not answer; the server may be hung, or the query port or type may not match"
  369. elif [ -n "${otherprocess}" ]; then
  370. hint="Port ${queryport}/${protocols} is in use by another process (${otherprocess}); check for a port conflict"
  371. else
  372. hint="Port ${queryport}/${protocols} is in use by a process owned by another user; check for a port conflict"
  373. fi
  374. fn_print_warn "Query port: ${hint}"
  375. fn_print_warn_eol_nl
  376. fn_script_log_warn "Query port: ${hint}"
  377. }
  378. fn_monitor_query() {
  379. # Will loop and query up to 5 times every 15 seconds.
  380. # Query will wait up to 60 seconds to confirm server is down as server can become non-responsive during map changes.
  381. totalseconds=0
  382. for queryattempt in {1..5}; do
  383. for queryip in "${queryips[@]}"; do
  384. fn_print_dots "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  385. fn_print_querying_eol
  386. fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : QUERYING"
  387. # querydelay
  388. if [ "$(head -n 1 "${lockdir}/${selfname}-started.lock")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then
  389. fn_print_ok "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  390. fn_print_delay_eol_nl
  391. fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : DELAY"
  392. fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago"
  393. fn_script_log_info "Server started: $(date -d "@$(head -n 1 "${lockdir}/${selfname}-started.lock")")"
  394. fn_script_log_info "Current time: $(date)"
  395. monitorpass=1
  396. core_exit.sh
  397. # will use query method selected in fn_monitor_loop
  398. # gamedig
  399. elif [ "${querymethod}" == "gamedig" ]; then
  400. query_gamedig.sh
  401. # gsquery
  402. elif [ "${querymethod}" == "gsquery" ]; then
  403. fn_query_gsquery
  404. #tcp query
  405. elif [ "${querymethod}" == "tcp" ]; then
  406. fn_query_tcp
  407. fi
  408. if [ "${querystatus}" == "0" ]; then
  409. # Server query OK.
  410. fn_print_ok "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  411. fn_print_ok_eol_nl
  412. fn_script_log_pass "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : OK"
  413. monitorpass=1
  414. if [ "${querystatus}" == "0" ]; then
  415. # Add query data to log.
  416. if [ "${gdname}" ]; then
  417. fn_script_log_info "Server name: ${gdname}"
  418. fi
  419. if [ "${gdplayers}" ]; then
  420. fn_script_log_info "Players: ${gdplayers}/${gdmaxplayers}"
  421. fi
  422. if [ "${gdbots}" ]; then
  423. fn_script_log_info "Bots: ${gdbots}"
  424. fi
  425. if [ "${gdmap}" ]; then
  426. fn_script_log_info "Map: ${gdmap}"
  427. fi
  428. if [ "${gdgamemode}" ]; then
  429. fn_script_log_info "Game Mode: ${gdgamemode}"
  430. fi
  431. # send LinuxGSM stats if monitor is OK.
  432. if [ "${stats}" == "on" ] || [ "${stats}" == "y" ]; then
  433. info_stats.sh
  434. fi
  435. fi
  436. core_exit.sh
  437. else
  438. # Server query FAIL.
  439. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  440. fn_print_fail_eol
  441. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  442. # Monitor will try gamedig (if supported) for first 30s then gsquery before restarting.
  443. # gsquery will fail if longer than 60s
  444. if [ "${totalseconds}" -ge "59" ]; then
  445. # Monitor will FAIL if over 60s and trigger gane server reboot.
  446. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  447. fn_print_fail_eol_nl
  448. fn_script_log_warn "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : FAIL"
  449. fn_monitor_query_port_hint
  450. fn_monitor_check_backup_update
  451. # Send alert if enabled.
  452. alert="monitor-query"
  453. alert.sh
  454. command_restart.sh
  455. fn_firstcommand_reset
  456. core_exit.sh
  457. fi
  458. fi
  459. done
  460. # Second counter will wait for 15s before breaking loop.
  461. for seconds in {1..15}; do
  462. fn_print_fail "Querying port: ${querymethod}: ${queryip}:${queryport} : ${totalseconds}/${queryattempt}"
  463. fn_print_wait_eol
  464. fn_sleep_time_1
  465. totalseconds=$((totalseconds + 1))
  466. if [ "${seconds}" == "15" ]; then
  467. break
  468. fi
  469. done
  470. done
  471. }
  472. fn_monitor_loop() {
  473. # loop though query methods selected by querymode.
  474. totalseconds=0
  475. if [ "${querymode}" == "2" ]; then
  476. local query_methods_array=(gamedig gsquery)
  477. elif [ "${querymode}" == "3" ]; then
  478. local query_methods_array=(gamedig)
  479. elif [ "${querymode}" == "4" ]; then
  480. local query_methods_array=(gsquery)
  481. elif [ "${querymode}" == "5" ]; then
  482. local query_methods_array=(tcp)
  483. fi
  484. for querymethod in "${query_methods_array[@]}"; do
  485. # Will check if gamedig is installed and bypass if not.
  486. if [ "${querymethod}" == "gamedig" ]; then
  487. if [ "$(command -v gamedig 2> /dev/null)" ] || [ -f "${lgsmdir}/node_modules/gamedig/bin/gamedig.js" ] && [ "$(command -v jq 2> /dev/null)" ]; then
  488. if [ -z "${monitorpass}" ]; then
  489. fn_monitor_query
  490. fi
  491. else
  492. fn_script_log_info "gamedig is not installed"
  493. fn_script_log_info "https://docs.linuxgsm.com/requirements/gamedig"
  494. fi
  495. else
  496. # will not query if query already passed.
  497. if [ -z "${monitorpass}" ]; then
  498. fn_monitor_query
  499. fi
  500. fi
  501. done
  502. }
  503. fn_print_dots ""
  504. monitorflag=1
  505. # Dont do any monitoring or checks if installer is running.
  506. fn_monitor_check_install
  507. check.sh
  508. core_logs.sh
  509. info_game.sh
  510. # query pre-checks
  511. fn_monitor_check_details
  512. fn_monitor_check_backup
  513. fn_monitor_check_debug
  514. fn_monitor_check_update_source
  515. fn_monitor_check_update
  516. fn_monitor_check_monitoring
  517. fn_monitor_check_starting
  518. fn_monitor_check_stopping
  519. fn_monitor_check_session
  520. fn_monitor_check_restart_request
  521. fn_monitor_check_update_request
  522. fn_monitor_check_backup_request
  523. # Monitor will not continue if session only check.
  524. if [ "${querymode}" != "1" ]; then
  525. fn_monitor_check_queryport
  526. # Add a querydelay of 1 min if var missing.
  527. if [ -z "${querydelay}" ]; then
  528. querydelay="1"
  529. fi
  530. fn_monitor_loop
  531. fi
  532. core_exit.sh