Tomcat monitoring script (monitoring process, test interface, alarm action for sending mail)

Source: Internet
Author: User
Tags shebang

Server environment: centos7.2 64-bit tomcat8.5.6

The Tomcat monitoring feature requires a total of two scripts, as follows:
initmonitor.sh

#!/bin/sh#初始化监控脚本相关变量Export tomcat_name=apache-tomcat-8.5.6export tomcat_home=/usr/local/apache-tomcat-8.5. 6 #测试接口访问地址 export WebUrl=127.0. 0.1:8080/yanglao/sysusercontroller/systest  #监控脚本日志路径 export tomcatmonitorlog=/tmp/yanglao/ Tomcatmonitor.logexport [Email protected]# Execute Tomcat monitoring script  #获取当前脚本所在目录baseDirForScriptSelf =$ (CD  "$ (dirname" $ ")"; pwd)  $baseDirForScriptSelf/tomcatmonitor.sh        

tomcatmonitor.sh

#!/bin/sh# Get Tomcat process id/usr/local/tomcat_ds_apitomcatid=$ (Ps-ef |grep Tomcat |grep-w$tomcat _name|grep-v' grep ' |awk' {print $} ')# Tomcat Launcher starttomcat=$tomcat _home/bin/startup.sh# Log Outputif [-F$TOMCATMONITORLOG];ThenEcho"Log file has been created"ElseEcho"Log file is created, create it now!" Touch$tomcatMonitorLogFi#tomcatMonitorLog =/tmp/yanglao/tomcatmonitor.logTryconnection () {Echo$tomcat _nameEcho' $tomcat _name ' trytomcatid=$ (ps-ef |grep Tomcat |grep-w$tomcat _name|grep-v' grep ' |awk' {print $} ')Echo[INFO] starts to re-view the Tomcat process:$TryTomcatID ... [$ (date + '%F%h:%m:%s ')] "if [[$TryTomcatID]];ThenEcho"[INFO] The current Tomcat process ID is:$TryTomcatID, continue to re-detect the interface ... "# Detect if the boot is successful (the interface will return a status of "tomcatservicecodetry=$") (Curl-s-mTen--connect-timeout10$WEBURL-W%{http_code})if [[$TomcatServiceCodeTry-eq200]];ThenEchoThe [INFO] System interface return code is$TomcatServiceCodeTry, Tomcat restarts successfully, Access system interface is OK ... "Echo"[ERROR] send alert to close mail"Echo"Tomcat reboot successfully, access system interface OK [$ (date + '%F%h:%m:%s ')] "|mail-S"Smart Retirement project Alarm off"$email _addresselse sleepTryconnectionFielse sleepTryconnectionFiMonitor () {Echo"[INFO] starts monitoring tomcat ... [$ (date + '%F%h:%m:%s ')] "if [[$TomcatID]];Then# here to determine if the Tomcat process existsEcho"[INFO] The current Tomcat process ID is:$TomcatID, continue to detect the system interface ... "# Detect if the boot is successful (the interface will return a status of "tomcatservicecode=$") (Curl-s-mTen--connect-timeout10$WEBURL-W%{http_code})if [[$TomcatServiceCode-eq200]];ThenEchoThe [INFO] interface return code is$TomcatServiceCode, Tomcat is running normally, access to the system interface is normal ... "ElseEcho"[ERROR] Error accessing system interface, please note ... Status code is$TomcatServiceCode "Echo"[ERROR] Accessing system interface error, starting reboot Tomcat"Echo"[ERROR] send alert to open mail"Echo"Tomcat started to restart automatically due to an error accessing the system interface [$ (date + '%F%h:%m:%s ')] "|mail-S"Intelligent Retirement Project Alarm Open"$email _address Kill-9$TomcatID# Kill the original Tomcat process sleep3 RM-RF$TomcatCache# Clean up the tomcat cache$StartTomcat Sleep3m Tryconnection#检查是否启动成功FiElseEcho"[Error]tomcat process does not exist!tomcat start auto restart ..."echo "[info]$StartTomcat, please wait ..." echo "[ERROR] send alarm to open mail" echo "because Tomcat process does not exist Tomcat starts automatic restart [$ (date + '%F%h:%m:%s ')]" |mail- s " Intelligent Retirement Project Alarm Open " $email _address rm-rf $TomcatCache $StartTomcat sleep 3m tryconnection #检查是否启动成功 C15>fi echo "------------------------------"} monitor>>$tomcatMonitorLog   

Pre-Preparation:

    1. Installing crontab
    2. Configuring the Mail delivery environment

Script Description:

    1. Variables used in the configurable script in initmonitor.sh, in addition to the parameter configuration, will call tomcatmonitor.sh in this script, which is called again because the variable set by export is valid only in that Shell and its child shells.
    2. tomcatmonitor.sh business logic scripts for Tomcat monitoring

Instructions for use:
1. Place initmonitor.sh,tomcatmonitor.sh two scripts into the server sibling directory (e.g./usr/bin/yanglao)
2. If you want to configure the script corresponding variable, please edit initmonitor.sh, do not need to change the can skip this step
3. Use crontab to set timed execution initmonitor.sh:
1) # CRONTAB-E
2) Insert /5 * * */usr/bin/yanglao/initmonitor.sh
3) #systemctl Reload Crond
#systemctl Restart Crond
After that, the script runs and starts detecting tomcat.

Detection range: Tomcat process shutdown, System interface inaccessible
Alarm action: Send mail to initmonitor.sh settings and restart Tomcat.
After rebooting, the Tomcat is re-detected, the detection is running normally, sending an alarm to close the message

Http://www.itwendao.com/article/detail/153863.html

c:\>curl -sL -w "%{http_code}""www.baidu.com"-o /dev/null200 我是状态码,还好不是返回250c:\>curl -sL -w "我是状态码:%{http_code}"www.baidu.com -o /dev/null我是状态码:200

Tomcat monitoring script (monitoring process, test interface, alarm action for sending mail)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.