monitor_gsquery.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. # LGSM monitor_gsquery.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://gameservermanagers.com
  5. # Description: Uses gsquery.py to query the server port.
  6. # Detects if the server has frozen with the process still running.
  7. local commandname="MONITOR"
  8. local commandaction="Monitor"
  9. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. # Forces legacy servers to use gsquery
  11. if [ -z "${gsquery}" ]; then
  12. gsquery="yes"
  13. fi
  14. if [ "${gsquery}" == "yes" ]; then
  15. # Downloads gsquery.py if missing
  16. if [ ! -f "${functionsdir}/gsquery.py" ]; then
  17. fn_fetch_file_github "lgsm/functions" "gsquery.py" "${functionsdir}" "executecmd" "norun" "noforce" "nomd5"
  18. fi
  19. info_config.sh
  20. if [ "${engine}" == "unreal" ]||[ "${engine}" == "unreal2" ]; then
  21. port=$((port + 1))
  22. elif [ "${engine}" == "realvirtuality" ]; then
  23. port=$((port + 1))
  24. elif [ "${engine}" == "spark" ]; then
  25. port=$((port + 1))
  26. elif [ "${engine}" == "idtech3_ql" ]; then
  27. engine="quakelive"
  28. fi
  29. if [ -n "${queryport}" ]; then
  30. port="${queryport}"
  31. fi
  32. fn_print_info "Querying port: gsquery.py enabled"
  33. fn_script_log_info "Querying port: gsquery.py enabled"
  34. sleep 1
  35. # Will query up to 4 times every 15 seconds.
  36. # Servers changing map can return a failure.
  37. # Will Wait up to 60 seconds to confirm server is down giving server time to change map.
  38. totalseconds=0
  39. for queryattempt in {1..5}; do
  40. fn_print_dots "Querying port: ${ip}:${port} : ${totalseconds}/${queryattempt} : "
  41. fn_print_querying_eol
  42. fn_script_log_info "Querying port: ${ip}:${port} : ${queryattempt} : QUERYING"
  43. gsquerycmd=$("${functionsdir}"/gsquery.py -a "${ip}" -p "${port}" -e "${engine}" 2>&1)
  44. exitcode=$?
  45. sleep 1
  46. if [ "${exitcode}" == "0" ]; then
  47. # Server OK
  48. fn_print_ok "Querying port: ${ip}:${port} : ${queryattempt} : "
  49. fn_print_ok_eol_nl
  50. fn_script_log_pass "Querying port: ${ip}:${port} : ${queryattempt} : OK"
  51. exitcode=0
  52. break
  53. else
  54. # Server failed query
  55. fn_script_log_info "Querying port: ${ip}:${port} : ${queryattempt} : ${gsquerycmd}"
  56. if [ "${queryattempt}" == "5" ]; then
  57. # Server failed query 4 times confirmed failure
  58. fn_print_fail "Querying port: ${ip}:${port} : ${totalseconds}/${queryattempt} : "
  59. fn_print_fail_eol_nl
  60. fn_script_log_error "Querying port: ${ip}:${port} : ${queryattempt} : FAIL"
  61. sleep 1
  62. # Send alert if enabled
  63. alert="restartquery"
  64. alert.sh
  65. command_restart.sh
  66. break
  67. fi
  68. # Seconds counter
  69. for seconds in {1..15}; do
  70. fn_print_fail "Querying port: ${ip}:${port} : ${totalseconds}/${queryattempt} : ${red}${gsquerycmd}${default}"
  71. totalseconds=$((totalseconds + 1))
  72. sleep 1
  73. if [ "${seconds}" == "15" ]; then
  74. break
  75. fi
  76. done
  77. fi
  78. done
  79. fi
  80. core_exit.sh