Linux USES Crontab to periodically monitor the way Tomcat applications are maintained
- 2020-05-10 23:17:39
- OfStack
Monitoring application interface: news interface, weather interface
Solution: automatically restart Tomcat when the application interface is not available, and send an alert email to the relevant personnel
#!/bin/bash
#---------------------------------------------------------
# Function description:
# Monitor the specified HTTP Whether the service is available, and restart immediately if not Tomcat
#
# Instructions:
# 1. Place this script in /home/opentsp/crontab/ Directory.
# 2. Modify script execution right to be executable.
# 3. Add to timed task, timed execution time (recommended as 20 Minutes)
# 4. Modify the email sender information list (email to the relevant person when the service restarts)
# - Zhou Lingfei (2014-08-13)
#---------------------------------------------------------
export LC_ALL=zh_CN.UTF-8
# Website address and parameters
SERVER_NAME=" Interesting driving cloud interface service "
URL_2="http://127.0.0.1/get_rss_news?p=%7b%27chId%27:%27TIYU%27%7d"
KeyWorld_2='<title>'
URL_3="http://127.0.0.1/get_json_weather?p=%7blon:116.407617,lat:39.993956,date:1%7d"
KeyWorld_3='temperature'
# Mailing list
mail_ary=(
xxxxxxxxx@navinfo.com
xxxxxxxxx@navinfo.com
xxxxxxxxx@navinfo.com
)
# The interface calls the failed handler
function doFail(){
local ipinfo=$(ifconfig |sed -n '2p'|awk '{print substr($2,6)}');
# Send E-mail
for _v in ${mail_ary[*]} ; do
echo "[$SERVER_NAME abnormal ] - [$(date -d "0 min" +"%Y-%m-%d %H:%M:%S")] - [ Request the address : $1] - [ Request return code : $2]" | mail -s ${ipinfo} Service exceptions ${_v}
done
# Written to the log
echo "[ERROR] - [$(date -d "0 min" +"%Y-%m-%d %H:%M:%S")] - Return code [$2] - restart Tomcat service " >> detect-http.log
# Shut down Tomcat
sh /home/opentsp/crontab/ibr-shutdown.sh
exit;
}
# Request timeout Settings
TIME_OUT=40
function doCheck(){
local URL_X=$1;
local KeyWorld_X=$2;
HTTP_STATUS_CODE=`curl -m $TIME_OUT -o /dev/null -s -w "%{http_code}" "${URL_X}"`
if [ $HTTP_STATUS_CODE != 200 ];then
# The request failed
echo "-> Fail - Return code ${HTTP_STATUS_CODE}";
doFail ${URL_X} ${HTTP_STATUS_CODE};
else
# The server responds normally and checks the returned content
if curl -m ${TIME_OUT} -s ${URL_X} | grep -q ${KeyWorld_X};then
echo "-> SUCCESS";
else
echo "->> Fail";
# Returns content error handling
doFail ${URL_X} ${HTTP_STATUS_CODE};
fi
fi
}
#
# check - news
doCheck ${URL_2} ${KeyWorld_2}
# check - The weather
doCheck ${URL_3} ${KeyWorld_3}
You can put the above code into the timing task of Linux. The recommended timing task time is once every 20 minutes.