monitor_gsquery.sh 2.6 KB

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