monitor_gsquery.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 commandnane="MONITOR"
  8. local commandaction="Monitor"
  9. local selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. # Forces legecy 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. fi
  27. if [ -n "${queryport}" ]; then
  28. port="${queryport}"
  29. fi
  30. fn_print_info "Querying port: gsquery.py enabled"
  31. fn_script_log_info "Querying port: gsquery.py enabled"
  32. sleep 1
  33. # Will query up to 4 times every 15 seconds.
  34. # Servers changing map can return a failure.
  35. # Will Wait up to 60 seconds to confirm server is down giving server time to change map.
  36. totalseconds=0
  37. for queryattempt in {1..5}; do
  38. fn_print_dots "Querying port: ${ip}:${port} : ${totalseconds}/${queryattempt} : "
  39. fn_print_querying_eol
  40. fn_script_log_info "Querying port: ${ip}:${port} : ${queryattempt} : QUERYING"
  41. gsquerycmd=$("${functionsdir}"/gsquery.py -a "${ip}" -p "${port}" -e "${engine}" 2>&1)
  42. exitcode=$?
  43. sleep 1
  44. if [ "${exitcode}" == "0" ]; then
  45. # Server OK
  46. fn_print_ok "Querying port: ${ip}:${port} : ${queryattempt} : "
  47. fn_print_ok_eol_nl
  48. fn_script_log_pass "Querying port: ${ip}:${port} : ${queryattempt} : OK"
  49. exitcode=0
  50. break
  51. else
  52. # Server failed query
  53. fn_script_log_info "Querying port: ${ip}:${port} : ${queryattempt} : ${gsquerycmd}"
  54. if [ "${queryattempt}" == "5" ]; then
  55. # Server failed query 4 times confirmed failure
  56. fn_print_fail "Querying port: ${ip}:${port} : ${totalseconds}/${queryattempt} : "
  57. fn_print_fail_eol_nl
  58. fn_script_log_error "Querying port: ${ip}:${port} : ${queryattempt} : FAIL"
  59. sleep 1
  60. # Send alert if enabled
  61. alert="restartquery"
  62. alert.sh
  63. command_restart.sh
  64. break
  65. fi
  66. # Seconds counter
  67. for seconds in {1..15}; do
  68. fn_print_fail "Querying port: ${ip}:${port} : ${totalseconds}/${queryattempt} : ${red}${gsquerycmd}${default}"
  69. totalseconds=$((totalseconds + 1))
  70. sleep 1
  71. if [ "${seconds}" == "15" ]; then
  72. break
  73. fi
  74. done
  75. fi
  76. done
  77. fi