monitor_gsquery.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/bash
  2. # LGSM monitor_gsquery.sh function
  3. # Author: Daniel Gibbs
  4. # Website: http://gameservermanagers.com
  5. lgsm_version="271215"
  6. # Description: uses gsquery.py to directly query the server.
  7. # Detects if the server has frozen.
  8. local modulename="Monitor"
  9. if [ -f "${rootdir}/gsquery.py" ]; then
  10. if [ "${engine}" == "unreal" ]||[ "${engine}" == "unreal2" ]; then
  11. gameport=$(grep Port= "${servercfgfullpath}"|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|tr -d '\r'|tr -cd '[:digit:]')
  12. port=$((${gameport} + 1))
  13. elif [ "${engine}" == "spark" ]; then
  14. port=$((${port} + 1))
  15. elif [ "${engine}" == "realvirtuality" ]; then
  16. queryport=$(grep -s steamqueryport= "${servercfgfullpath}"|grep -v //|tr -d '\r'|tr -cd '[:digit:]')
  17. port=${queryport}
  18. elif [ "${gamename}" == "7 Days To Die" ]; then
  19. gameport=$(grep ServerPort "${servercfgfullpath}"|tr -cd '[:digit:]')
  20. port=$((${gameport} + 1))
  21. elif [ "${gamename}" == "Hurtworld" ]; then
  22. gameport="${port}"
  23. port="${queryport}"
  24. elif [ "${gamename}" == "Rust" ]; then
  25. gameport="${port}"
  26. port="${rconport}"
  27. fi
  28. fn_printinfo "Detected gsquery.py"
  29. fn_scriptlog "Detected gsquery.py"
  30. sleep 1
  31. fn_printdots "Querying port: ${ip}:${port} : QUERYING"
  32. fn_scriptlog "Querying port: ${ip}:${port} : QUERYING"
  33. sleep 1
  34. serverquery=$("${rootdir}/gsquery.py" -a ${ip} -p ${port} -e ${engine} 2>&1)
  35. exitcode=$?
  36. if [ "${exitcode}" == "1" ]||[ "${exitcode}" == "2" ]||[ "${exitcode}" == "3" ]||[ "${exitcode}" == "4" ]; then
  37. fn_printfail "Querying port: ${ip}:${port} : ${serverquery}"
  38. fn_scriptlog "Querying port: ${ip}:${port} : ${serverquery}"
  39. sleep 1
  40. echo -en "\n"
  41. if [ -z "${secondquery}" ]; then
  42. if [ "${engine}" == "unreal2" ]; then
  43. # unreal 2: Map change can take around 60 seconds
  44. fn_printinfo "Waiting 60 seconds to re-query"
  45. fn_scriptlog "Waiting 60 seconds to re-query"
  46. sleep 60
  47. else
  48. fn_printinfo "Waiting 30 seconds to re-query"
  49. fn_scriptlog "Waiting 30 seconds to re-query"
  50. sleep 30
  51. fi
  52. secondquery=1
  53. monitor_gsquery.sh
  54. fi
  55. if [ "${emailnotification}" = "on" ]; then
  56. info_config.sh
  57. subject="${servicename} Monitor - Starting ${servername}"
  58. failurereason="Failed to query ${servicename}: ${serverquery}"
  59. actiontaken="restarted ${servicename}"
  60. email.sh
  61. fi
  62. fn_restart
  63. exit 1
  64. elif [ "${exitcode}" == "0" ]; then
  65. fn_printok "Querying port: ${ip}:${port} : OK"
  66. fn_scriptlog "Querying port: ${ip}:${port} : OK"
  67. sleep 1
  68. echo -en "\n"
  69. exit
  70. elif [ "${exitcode}" == "126" ]; then
  71. fn_printfail "Querying port: ${ip}:${port} : ERROR: ${rootdir}/gsquery.py: Permission denied"
  72. fn_scriptlog "Querying port: ${ip}:${port} : ERROR: ${rootdir}/gsquery.py: Permission denied"
  73. sleep 1
  74. echo -en "\n"
  75. echo "Attempting to resolve automatically"
  76. chmod +x -v "${rootdir}/gsquery.py"
  77. if [ $? -eq 0 ]; then
  78. monitor_gsquery.sh
  79. else
  80. fn_printfailure "Unable to resolve automatically. Please manually fix permissions.\n"
  81. owner=$(ls -al ${rootdir}/gsquery.py|awk '{ print $3 }')
  82. echo "As user ${owner} or root run the following command."
  83. whoami=$(whoami)
  84. echo -en "\nchown ${whoami}:${whoami} ${rootdir}/gsquery.py\n\n"
  85. exit 1
  86. fi
  87. else
  88. fn_printfail "Querying port: ${ip}:${port} : UNKNOWN ERROR"
  89. fn_scriptlog "Querying port: ${ip}:${port} : UNKNOWN ERROR"
  90. sleep 1
  91. echo -en "\n"
  92. ${rootdir}/gsquery.py -a ${ip} -p ${port} -e ${engine}
  93. exit 1
  94. fi
  95. else
  96. fn_printfailnl "Could not find ${rootdir}/gsquery.py"
  97. fn_scriptlog "Could not find ${rootdir}/gsquery.py"
  98. fi