Просмотр исходного кода

Major update to monitor_gsquery.sh

* Now loops for 60 seconds checking 4 times for if fails
* gsquery .py moved to functions dir
* uses new core_dl feature so gsquery is auto installed
* removed old code
* added colors
Daniel Gibbs 10 лет назад
Родитель
Сommit
5d687b023d
1 измененных файлов с 77 добавлено и 80 удалено
  1. 77 80
      functions/monitor_gsquery.sh

+ 77 - 80
functions/monitor_gsquery.sh

@@ -4,94 +4,91 @@
 # Website: http://gameservermanagers.com
 lgsm_version="271215"
 
-# Description: uses gsquery.py to directly query the server.
-# Detects if the server has frozen.
+# Description: uses gsquery.py to query the server port.
+# Detects if the server has frozen with the proccess still running.
 
 local modulename="Monitor"
-if [ -f "${rootdir}/gsquery.py" ]; then
-	if [ "${engine}" == "unreal" ]||[ "${engine}" == "unreal2" ]; then
-		gameport=$(grep Port= "${servercfgfullpath}"|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|tr -d '\r'|tr -cd '[:digit:]')
-		port=$((${gameport} + 1))
-	elif [ "${engine}" == "spark" ]; then
-		port=$((${port} + 1))
-	elif [ "${engine}" == "realvirtuality" ]; then
-		queryport=$(grep -s steamqueryport= "${servercfgfullpath}"|grep -v //|tr -d '\r'|tr -cd '[:digit:]')
-		port=${queryport}
-	elif [ "${gamename}" == "7 Days To Die" ]; then
-		gameport=$(grep ServerPort "${servercfgfullpath}"|tr -cd '[:digit:]')
-		port=$((${gameport} + 1))
-	elif [ "${gamename}" == "Hurtworld" ]; then
-		gameport="${port}"
-		port="${queryport}"
-	fi
-	fn_printinfo "Detected gsquery.py"
-	fn_scriptlog "Detected gsquery.py"
-	sleep 1
-	fn_printdots "Querying port: ${ip}:${port} : QUERYING"
-	fn_scriptlog "Querying port: ${ip}:${port} : QUERYING"
-	sleep 1
-	serverquery=$("${rootdir}/gsquery.py" -a ${ip} -p ${port} -e ${engine} 2>&1)
+
+# Downloads gsquery.py if missing
+if [ ! -f "${functionsdir}/gsquery.py" ]; then
+	fn_fetch_file_github "functions" "gsquery.py" "${functionsdir}" "executecmd" "norun" "noforce"
+fi	
+
+info_config.sh
+
+if [ "${engine}" == "unreal" ]||[ "${engine}" == "unreal2" ]; then
+	port=$((${port} + 1))
+elif [ "${engine}" == "spark" ]; then
+	port=$((${port} + 1))
+fi
+
+if [ -z "${queryport}" ]; then
+	port="${queryport}"
+fi
+
+
+fn_printinfo "Querying port: gsquery.py enabled"
+fn_scriptlog "gsquery.py enabled"
+sleep 1
+fn_printdots "Querying port: ${ip}:${port}: 0/1 : "
+fn_printqueryingeol
+fn_scriptlog "Querying port: ${ip}:${port}: 1 : QUERYING"
+sleep 1
+
+
+# Will query up to 4 times every 15 seconds.
+# Servers changing map can return a failure.
+# Will Wait up to 60 seconds to confirm server is down giving server time to change map.
+queryattempt=0
+totalseconds=0
+for i in {1..4}; do
+	gsquerycmd=$("${functionsdir}"/gsquery.py -a ${ip} -p 1 -e ${engine} 2>&1)
 	exitcode=$?
-	if [ "${exitcode}" == "1" ]||[ "${exitcode}" == "2" ]||[ "${exitcode}" == "3" ]||[ "${exitcode}" == "4" ]; then
-		fn_printfail "Querying port: ${ip}:${port} : ${serverquery}"
-		fn_scriptlog "Querying port: ${ip}:${port} : ${serverquery}"
+
+	if [ "${exitcode}" == "0" ]; then
+		# Server OK
+		fn_printok "Querying port: ${ip}:${port}: "
+		fn_printokeol
+		fn_scriptlog "Querying port: ${ip}:${port}: OK"
 		sleep 1
-		echo -en "\n"
-		if [ -z "${secondquery}" ]; then
-			if [ "${engine}" == "unreal2" ]; then
-				# unreal 2: Map change can take around 60 seconds
-				fn_printinfo "Waiting 60 seconds to re-query"
-				fn_scriptlog "Waiting 60 seconds to re-query"
-				sleep 60
-			else
-				fn_printinfo "Waiting 30 seconds to re-query"
-				fn_scriptlog "Waiting 30 seconds to re-query"
-				sleep 30
-			fi
-			secondquery=1
-			monitor_gsquery.sh
-		fi
+		exit
+	else
+		# Server failed query
+		queryattempt=$((queryattempt + 1))
+		fn_scriptlog "Querying port: ${ip}:${port}: ${totalseconds}/${queryattempt} : ${gsquerycmd}"
+		seconds=0
+		# Seconds counter
+		while [ true ]; do
+		    fn_printfail "Querying port: ${ip}:${port}: ${totalseconds}/${queryattempt} : \e[0;31m${gsquerycmd}\e[0m"
+		    seconds=$((seconds + 1))
+		    totalseconds=$((totalseconds + 1))
+		    sleep 1
+		    if [ "${seconds}" == "15" ]; then
+		    	fn_printdots "Querying port: ${ip}:${port}: ${totalseconds}/${queryattempt} : "
+		    	fn_printqueryingeol
+				fn_scriptlog "Querying port: ${ip}:${port}: ${queryattempt} : QUERYING"
+				sleep 1
+		    	break
+		    fi
+		done
+	fi
+
+	if [ "${queryattempt}" == "4" ]; then
+		# Server failed query 4 times confirmed failure
+		fn_printfail "Querying port: ${ip}:${port}: "
+		fn_printfaileol
+		fn_scriptlog "Querying port: ${ip}:${port}: ${gsquerycmd}"
+		fn_scriptlog "Querying port: ${ip}:${port}: FAIL"
+		sleep 1
+
+		# Send email notification if enabled
 		if [ "${emailnotification}" = "on" ]; then
 			info_config.sh
 			subject="${servicename} Monitor - Starting ${servername}"
-			failurereason="Failed to query ${servicename}: ${serverquery}"
+			failurereason="Failed to query ${servicename}: ${gsquerycmd}"
 			actiontaken="restarted ${servicename}"
 			email.sh
 		fi
 		fn_restart
-		exit 1
-	elif [ "${exitcode}" == "0" ]; then
-		fn_printok "Querying port: ${ip}:${port} : OK"
-		fn_scriptlog "Querying port: ${ip}:${port} : OK"
-		sleep 1
-		echo -en "\n"
-		exit
-	elif [ "${exitcode}" == "126" ]; then
-		fn_printfail "Querying port: ${ip}:${port} : ERROR: ${rootdir}/gsquery.py: Permission denied"
-		fn_scriptlog "Querying port: ${ip}:${port} : ERROR: ${rootdir}/gsquery.py: Permission denied"
-		sleep 1
-		echo -en "\n"
-		echo "Attempting to resolve automatically"
-		chmod +x -v "${rootdir}/gsquery.py"
-		if [ $? -eq 0 ]; then
-			monitor_gsquery.sh
-		else
-			fn_printfailure "Unable to resolve automatically. Please manually fix permissions.\n"
-			owner=$(ls -al ${rootdir}/gsquery.py|awk '{ print $3 }')
-			echo "As user ${owner} or root run the following command."
-			whoami=$(whoami)
-			echo -en "\nchown ${whoami}:${whoami} ${rootdir}/gsquery.py\n\n"
-		exit 1
-		fi
-	else
-		fn_printfail "Querying port: ${ip}:${port} : UNKNOWN ERROR"
-		fn_scriptlog "Querying port: ${ip}:${port} : UNKNOWN ERROR"
-		sleep 1
-		echo -en "\n"
-		${rootdir}/gsquery.py -a ${ip} -p ${port} -e ${engine}
-		exit 1
-	fi
-else
-	fn_printfailnl "Could not find ${rootdir}/gsquery.py"
-	fn_scriptlog "Could not find ${rootdir}/gsquery.py"
-fi
+	fi	
+done