日期:2014-05-16  浏览次数:21331 次

Shell文件处理,Tomcat默认项目,AccessLog的配置
最近处理了几个简单的技术问题,在这儿写一下,加深一下映象。

1. shell读取解析固定格式文件
在当前项目中,需要处理一个文件的内容,解析每行的数据,以这些数据位参数然后调用一些job完成任务,文件是固定的格式,以“|”分隔每个参数,例子:
假设有如下的一个文件,格式为First_Name|Surname|age, Job的任务是提取出每个人的age,判断是否为成年人
Xianning|Liu|27
Yujiao|Zhang|25

代码:
#!\bin\bash
 #################################
 #Usage: sh parse.sh <input_file>
 #
 #example: sh parse.sh records.txt
 #
 #################################
 
 if [ $# -lt 1 ] ; then
       echo "Usage: sh parse.sh <input_file>"
       exit 0
 fi
 
 while read line ; do
   age=`echo $line | cut -f3 -d"|"`
   first_name=`echo $line | cut -f1 -d"|"`
 
   if [ $age -gt 18 ]; then
     echo "$first_name is growup"
   else
     echo "$first_name is a child"
   fi
 done < $1


2. Tomcat7的默认项目配置
Tomcat的所有项目都是放在Webapps目录下的,当我们访问Tomcat根目录, 默认情况下会访问webapps目录下的Root项目,可以通过修改Tomcat目录下的配置文件conf/server.xml,来设置自己想要的默认项目,只需要在server.xml的Host节点下添加Context节点,并把其docBase属性设置为自己的项目名即可。
 <Service name="Catalina">
	...
        <Engine name="Catalina" defaultHost="localhost">
			...
            <Host name="localhost">
                <Context path="" docBase="<your project name>"/>
            </Host>
        </Engine>
    </Service>


3. Tomcat7 通过Access log记录所有request的performance
Tomcat服务器默认会记录下所有的请求,这其实是一个很好的performance记录,在我们系统中可以把这个文件导入到Splunk中,然后就可以获得整个系统的Performance状态。Tomcat7中关于access log的配置在conf/server.xml中, 如下:
<Service name="Catalina">
	...
        <Engine name="Catalina" defaultHost="localhost">
			...
            <Host name="localhost">

                <Valve className="org.apache.catalina.valves.AccessLogValve"
                    directory="logs" prefix="localhost_access_log." suffix=".txt"
                    pattern="%h %l %u %t "%r" %s %b" resolveHosts="false" />
                ...
            </Host>
        </Engine>
    </Service>


通过修改日志记录的格式,可以获取基于每个Request的各种信息,包括每个request消耗的时间,从哪儿来的等等,其日志格式支持的参数有:
%a - Remote IP address
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if zero
%B - Bytes sent, excluding HTTP headers
%h - Remote host name (or IP address if enableLookups for the connector is false)
%H - Request protocol
%l - Remote logical username from identd (always returns '-')
%m - Request method (GET, POST, etc.)
%p - Local port on which this request was received
%q - Query string (prepended with a '?' if it exists)
%r - First line of the request (method and request URI)
%s - HTTP status code of the response
%S - User session ID
%t - Date and time, in Common Log Format
%u - Remote user that was authenticated (if any), else '-'
%U - Requested URL path
%v - Local server name
%D - Time taken to process the request, in millis
%T - Time taken to process the request, in seconds
%F - Time taken to commit the response, in millis
%I - Current request thread name (can compare later with stacktraces)
%{xxx}i for incoming headers
%{xxx}o for outgoing response headers
%{xxx}c for a specific cookie
%{xxx}r xxx is an attribute in the ServletRequest
%{xxx}s xxx is an attribute in the HttpSession
%{xxx}t xxx is an enhanced SimpleDateFormat pattern