Case ninth, Tomcat log analysis

If you run on the company's server is java code, it will likely be using Tomcat, and Tomcat problems, we have to see Tomcat logs. Tomcat has a total log called catalina out, it records the Tomcat relevant information, including the right and wrong. Demand is the background of the case:

Example 4 ran Tomcat server, the directory structure as follows:

/opt/TOMCAT/
├── crontabs
├── t1
├── t2
├── t3
└── t4

The path where the catalina out as follows:

/opt/TOMCAT/t1/logs/catalina.out
/opt/TOMCAT/t2/logs/catalina.out
/opt/TOMCAT/t3/logs/catalina.out
/opt/TOMCAT/t4/logs/catalina.out

Specific requirements are as follows:

1) may take a script Tomcat instance log of t1-t4, which is specified by a parameter.

2) custom script can take the log of the starting position, such as after today take 10 am now log, the time required to provide the 24-hour clock.

3) customize the script can take the start and end positions of the log, such as taking the log morning 9:00 to 20:00, the time required to provide the 24-hour clock.

4) The first parameter for which a Tomcat (t1, t2, t3, t4), the second parameter as a starting point in time (considering only the time of the day), the third parameter is the end point of time, may be omitted, if omitted, it is the current point in time.

5) providing a time point of determining the legitimacy required, i.e. this format must be 12:00:00.


Log segment as follows:

Aug 22,2019 15:58:33 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 22,2019 16:38:23 PM org.apahce.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 22,2019 16:45:46 PM org.apahce.catalina.startup.Catalina start
INFO: Server startup in 2102 ms



A knowledge: Tomcat, and installation

Tomcat is a web container, we mainly use it to run servlet and JSP. Tomcat itself is a http server like Apache or that it can parse HTML pages, JS, CSS, and images and other elements Nginx, but its most important function is to run Servlet or JSP. About Tomcat involves a number of related concepts JAVA, following a simple to do list.

JAVAEE Java Plateform Enterprise Edition Enterprise Edition, used to make the site 
JAVASE Java Plateform Standard Edition version of the standard, used for software running on the computer 
JAVAME Java Plateform Micro Edition miniature version, do phone software 
JDK Java Development kit Java development and Operating environment, JDK = Java development tools, the JRE + 
the JRE Java Runtime environment Java Runtime environment program, including the required Java runtime libraries and the JVM 
the JVM Java virtual machine 
compression jar (Java application archive) contains the class and a number of resources and configuration files package 
war (web application archive) the jar is substantially the same, will contain all of the web applications, Tomcat automatically deploy

The above mentioned Servlet and JSP, their main difference between the two following points:

1) Java code embedded in html code is jsp, while the servlet is written in pure Java code.

2) jsp page is mainly used to show the effect, and servlet responsible for logic control.

3) The first time a user runs jsp, will be automatically converted to servlet code, so that is a kind of jsp servlet on nature.

4) When the first visit the servlet, which will be compiled into class file, the class file can be accessed directly follow.


On installing Tomcat, where a simple column about steps for reference:

1. Install JDK (a method alternative)

Method a: yum install java-1.8.0-openjdk

# yum install -y java-1.8.0-openjdk

Method two: Install Oracle official version jdk

1) to https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html download jdk8

2) extracting and renamed /usr/local/jdk1.8

3) edit the configuration file / etc / profile, add the following to the end:

JAVA_HOME=/usr/local/jdk1.8/
JAVA_BIN=/usr/local/jdk1.8/bin
JRE_HOME=/usr/local/jdk1.8/jre
PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin
CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar


2. Install Tomcat (version 9.0)

1) Download the binary package, download address https://tomcat.apache.org/download-90.cgi

2) decompression and renamed / usr / local / tomcat

3) Start /usr/local/tomcat/bin/startup.sh


Knowledge point two: Tomcat click multi-instance

Click multiple instances Tomcat is to run multiple services on a single server. In fact, I want to run Tomcat, Tomcat is not only the main program file (such as / usr / binary files in the local / tomcat / bin), also need to configure file and other auxiliary class files. To run multiple Tomcat service, you may need only one copy of the main program file to a different Tomcat services use different configuration files. Therefore, in order to achieve click multi-instance, so you can plan what directory, as shown:

111.png

其中CATALINA_HOME指的是Tomcat安装目录(如果你按照我的方法安装,那么就是在/usr/local/tomcat),CATALINA_BASE为实例所在目录。CATALINA_HOME路径下只需要包含bin和lib目录,而CATALINA_BASE只存放conf、webapps、logs等这些文件,这样部署的好处在于升级方便,配置及安装文件间互不影响,在不影响Tomcat实例的前提下,替换掉CATALINA_HOME中的安装文件。


具体的部署步骤如下:

1.创建实例目录

# mkdir -p /data/tomcat-instance
# mkdir /data/tomcat-instance/www.123.com
# cd !$
# cp -r /usr/local/tomcat/conf /data/tomcat-instance/www.123.com/

2.创建Tomcat服务相关目录

# mkdir -p /data/tomcat-instance/www.123.com/{common,logs,temp,server,shared,webapps,work}

3.创建启动和关闭脚本

# cd /data/tomcat-instance/www.123.com
# vim start.sh  //启动脚本
#!/bin/bash
export CATALINA_HOME=/usr/local/tomcat
export CATALINA_BASE=/data/tomcat-instance/www.123.com
TOMCAT_ID=`ps aux |grep "java"|grep "Dcatalina.base=$CATALINA_BASE "|grep -v "grep"|awk '{ print $2 }'`
if [ -n "$TOMCAT_ID" ]
then
    echo "tomcat(${TOMCAT_ID}) still running now , please shutdown it first";
    exit 2;
else
       $CATALINA_HOME/bin/startup.sh
    if [ "$?" = "0" ]; then
        echo "start succeed"
    else
        echo "start failed"
    fi
fi

# vim shutdown.sh  //关闭脚本
#!/bin/bash
export CATALINA_HOME=/usr/local/tomcat
export CATALINA_BASE=/data/tomcat-instance/www.123.com
TOMCAT_ID=`ps aux |grep "java"|grep "Dcatalina.base=$CATALINA_BASE "|grep -v "grep"|awk '{ print $2}'`
if [ -n "$TOMCAT_ID" ] ; then
    TOMCAT_STOP_LOG=`$CATALINA_HOME/bin/shutdown.sh`
       if [ "$?" = "0" ]; then
        echo "stop succeed"
    else
        echo "stop failed"
    fi
else
    echo "Tomcat instance not found"
    exit
fi

4.编辑配置文件

# cd /data/tomcat-instance/www.123.com/conf
# vim server.xml  //修改三个端口,目的是为了不和其他实例冲突

有了第一个实例后,第二个实例可以直接复制/data/tomcat-instance/www.123.com目录,然后修改对应的配置、启动脚本、停止脚本内容。


知识点三:shell脚本的参数个数

在前面的案例中多次用到$1,$2,即shell脚本的参数。和参数相关的还有一个常用的概念,那就是shell脚本参数的个数。先看示例脚本:

# vim pa_nu.sh
#!/bin/bash
echo "脚本有$#个参数"

执行脚本,过程如下:

# sh pa_nu.sh 1 a
脚本有2个参数
# sh pa_nu.sh 
脚本有0个参数
# sh pa_nu.sh a b c
脚本有3个参数

所以,结论就是在shell脚本中用$#表示脚本的参数个数。


知识点四:判断一个时间是否合法

如果使用传统的方法,去比对时、分、秒的范围是可以做到,但是这样太繁琐,有一个简单的方法,如下:

# date -d "19:60:" +%s
date: 无效的日期"19:60:"
# date -d "19:59" +%s
1566561540

就是用date命令来做。


知识点五:将24小时制的时间转换为12小时制

直接看命令,如下:

# date -d "17:13:14" +%r
05:13:14 PM

也可以直接判断一个时间是AM还是PM

# date -d "17:13:14" +%p   //小写字母p
PM

如果想用小写的,也可以做到:

# date -d "9:10:33" +%P  //大写字母P
am


知识点六:比较两个时间大小

比较时间大小,只能通过时间戳来实现,如下:

# t1=`date -d "13:22:32" +%s`
# t2=`date -d "15:00:00" +%s`
# if [ $t1 -lt $t2 ]; then echo "t1比t2要早"; else echo "t1比t2要晚"; fi
t1比t2要早
# if [ $t1 -lt $t2 ]; then echo "t1比t2要早"; else echo "t1比t2要晚"; fi
t1比t2要晚


本案例参考脚本

#!/bin/bash
#截取指定Tomcat的日志片段
#作者:
#日期:

LANG=en
logfile="/opt/TOM/$1/logs/catalina.out"

#将当天的英文月、数字日期、数字年作为变量赋值给d_mdy
d_mdy=`date "+%b %d, %Y"`

#判断参数个数
if [ $# -ne 2 ] && [ $# -ne 3 ]
then
    echo "你提供的参数个数不对,请提供2个或者3个参数。例:sh $0 t1 08:01:00 14:00:00" 
    exit 1
fi

#判断第一个参数是否符合要求
if ! echo $1|grep -qE '^t1$|^t2$|^t3$|^t4$'
then
    echo "第一个参数必须是t1、t2、t3或t4"
    exit 1
fi

#判断时间有效性
judge_time()
{
    date -d "$1" +%s &>/dev/null
    if [ $? -ne 0 ]
    then
        echo "你提供的时间$1格式不正确"
        exit 1
    fi
}

#判断提供的时间点是否在日志中出现
judge_time_in_log()
{
    if ! grep -q "$d_mdy $(tr_24_12 $1)" $logfile
        then
            echo "你提供的时间$1在日志$logfile中不曾出现,请换一个时间点"
            exit 1
        fi
}

#将24小时制时间转换为12小时
tr_24_12()
{
    date -d "$1" +%r
}

#判断第2个参数是否合法
judge_time $2

#判断起始时间点是否出现在日志里
judge_time_in_log $2

#如果提供第3个参数
if [ $# -eq 3 ]
then
    #判断第3个参数是否合法
    judge_time $3

    #判断起始时间是否早于结束时间
    t1=`date -d "$2" +%s`
        t2=`date -d "$3" +%s`
        if [ $t2 -lt $t1 ]
        then
            echo "你提供的时间$2比$3要晚,应该把早的时间放到前面"
            exit
        fi

        #判断提供的结束时间点是否出现在日志中
        judge_time_in_log $3
fi


#取起始时间所在行行号
begin_n=`grep -n "$d_mdy $(tr_24_12 $2)" $logfile|head -1|awk -F ':' '{print $1}'`

#取结束时间所在行行号,并用sed截取日志内容
if [ $# -eq 3 ]
then
    n=`grep -n "$d_mdy $(tr_24_12 $3)" $logfile|tail -1|awk -F ':' '{print $1}'`
    #结束日期所在行的下一行才是日志的内容
    end_n=$[$n+1]
    sed -n "$begin_n,$end_n"p $logfile
else
    sed -n "$begin_n,$"p $logfile
fi


Guess you like

Origin blog.51cto.com/13576245/2432335