- 论坛徽章:
- 0
|
独辟蹊径J2EE网站之Tomcat篇(下) ----日志分析及系统测试 ChinaUnix网友:gamester88、kns1024wh、loveradmin 本文前半部分详见《开源时代》第十二期刊。
六、Apache Tomcat整合虚拟主机配置
配置apache整合tomcat的虚拟主机需要解析tomcat中的server.xml中的host元素,并同时配置apache中的VirtualHost。配置Apache中的虚拟主机,在httpd.conf中添加如下的VirtualHost的配置信息。
#vi httpd.conf
NameVirtualHost *:80
LoadModule jk_module lib/mod_jk.so
# mod_jk settings
Include conf/mod_jk.conf
<VirtualHost *:80>
ServerName www.demotestdemo.com
DocumentRoot /projects/nihao
JkMount /servlet/* jsp-ap207
JkMount /*.jsp jsp-ap207
JkMount /*.do jsp-ap207
JkMount /*.action jsp-ap207
JkMount /*.java jsp-ap207
JkMount /authImg jsp-ap207
JkMount /fckeditor/* jsp-ap207
JkMount /*.act jsp-ap207
</VirtualHost>
<VirtualHost *:80>
ServerName chanpin1.demotestdemo.com
DocumentRoot /projects/chanpin1
JkMount /servlet/* jsp-ap207
JkMount /*.jsp jsp-ap207
JkMount /*.do jsp-ap207
JkMount /*.action jsp-ap207
JkMount /*.java jsp-ap207
JkMount /authImg jsp-ap207
JkMount /fckeditor/* jsp-ap207
JkMount /*.act jsp-ap207
</VirtualHost>
#JkMount行是说明这些文件交给tomcat解析
<Directory "/projects/chanpin1">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
#这个目录里的文件网页可以访问,要不提示没有打开权限
|
配置tomcat添加虚拟主机参数,整合apache指定的配置信息,这部分主要是修改server.xml中的Host部分的信息
#vi server.xml
<Host name="www.demotestdemo.com" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
-->
<Context path="" docBase="/projects/nihao" debug="0" reloadable="true"
crossC/>
</Host>
<Host name="chanpin1.demotestdemo.com" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
-->
<Context path="" docBase="/projects/chanpin1" debug="0" reloadable="true"
crossC/>
</Host>
|
七、整合cronolog分割apache日志使用awstats进行可视化分析
a)为Linux添加cronolog分割日志文件
Linux下运行的Web服务器Apache,默认日志文件是不分割的,一个整文件既不易于管理,也不易于分析统计。安装cronolog后,可以将日志文件按时间分割,易于管理和分析。
项目网站 http://cronolog.org/
[root@tomcat src]#wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
#在线获取软件包,对获取的源码进行编译安装
[root@tomcat src]# tar xvf cronolog-1.6.2.tar.tar
[root@tomcat src]# cd cronolog-1.6.2
[root@tomcat cronolog-1.6.2]# ./configure
[root@tomcat cronolog-1.6.2]# make
[root@tomcat cronolog-1.6.2]# make install
[root@tomcat cronolog-1.6.2]# which cronolog
/usr/local/sbin/cronolog
#使用which能查询到cronolog程序说明已经安装成功了
| 源码编译安装结束需要把日志的格式配置
1、虚拟主机配置文件 httpd-vhosts.conf,将Web日志设置 CustomLog 修改为以下格式
CustomLog "|/usr/local/sbin/cronolog /usr/local/http/logs/access_%Y%m%d.log" combined
| 当然,错误日志设置 ErrorLog 也可以利用 cronolog 分割,设置为
ErrorLog "|/usr/local/sbin/cronolog /www/logs/error_%Y%m%d.log"
| 2、如果服务器上只有一个站点(当然这种情况比较少),直接按上面所说格式修改 httpd.conf 文件日志设置部分。
说明:
- 绿色部分 为 cronolog 安装后所在位置,系统版本不同可能位置不完全一样,以 which 命令查看到的位置为准;
- 蓝色部分 为设置的日志文件所在位置,根据需要修改;
- 红色部分 为设置的日志文件标识性字符,根据需要修改;
- %Y%m%d 为日志文件分割方式,例子中为“年月日”,也可以修改成自己需要的。
|
然后重启一下apache,就会发现apache日志开始按照日期生成,而原来的apache日志文件就不会再记录新的访问数据了。
这时,如果有使用awstats的人也许会遇到一个问题,因为apache动态生成日志,所以得相应修改awstats中的配置文件。
将awstats配置文件中的LogFile做相应的修改:[root@tomcat conf]# vi /etc/awstats/awstats.www.demotestdemo.com.conf
LogFile="/usr/local/httpd/logs/access_log"
改为
LogFile="/usr/local/httpd/logs/access_%YYYY-24%MM-24%DD-24.log"
# %YYYY-24%MM-24%DD表示24小时之前的年份,%MM-24表示24小时之前的月份,%DD-24表示24小时之前的日份,合起来%YYYY-24%MM-24%DD-24就表示24小时前的年月日。我们在使用awstats分析apache日志的时候,必须分析已经生成的日志。而我们又常常使用cron来自动update,举个例子,当我们于2009年8月1日凌晨1点分析我们网站的apahce日志的时候,我们是要分析前一天也就是2009年7月31日的日志,而不是cron运行的当天8月1日的日志:)这样,减去一个数字,才能够正确得到我们想要分析的apche的日志的文件名。当然,你也可以不用24这个数字,可以根据自己的实际情况来调整:)
| b)为linux配置awstats
AWStats是一套免费基于Perl的网站日志分析工具,awstats有详细的统计流量工具。这个工具界面较为友好,执行速度快,扩展功能众多, AWStats是当之无愧的首选。
Awstats需要perl模块支持,在安装awstats前需要安装必要perl软件包
[root@tomcat src]# yum -y install perl-libwww-perl
[root@tomcat src]# rpm -ivh awstats-6.9-1.noarch.rpm
Preparing... ########################################### [100%]
1:awstats ########################################### [100%]
----- AWStats 6.9 - Laurent Destailleur -----
AWStats files have been installed in /usr/local/awstats
If first install, follow instructions in documentation
(/usr/local/awstats/docs/index.html) to setup AWStats in 3 steps:
Step 1 : Install and Setup with awstats_configure.pl (or manually)
Step 2 : Build/Update Statistics with awstats.pl
Step 3 : Read Statistics
| 执行awstats的配置脚本,如果设置上注意将会出现下面的错误信息,因/etc/awstats下没有awstats.model.conf文件造成的错误
[root@tomcat src]# cd /usr/local/awstats/tools/
[root@tomcat tools]# ./awstats_configure.pl
----- AWStats awstats_configure 1.0 (build 1.8) (c) Laurent Destailleur -----
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
- You are not an administrator user,
- You want to analyze downloaded log files without web server,
- You want to analyze mail or ftp log files instead of web log files,
- You need to analyze load balanced servers log files,
- You want to 'understand' all possible ways to use AWStats...
Read the AWStats documentation (docs/index.html).
-----> Running OS detected: Linux, BSD or Unix
-----> Check for web server install
Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path ('none' to skip web server setup):
> /usr/local/httpd/conf/httpd.conf
-----> Check and complete web server config file '/usr/local/httpd/conf/httpd.conf'
Warning: You Apache config file contains directives to write 'common' log files
This means that some features can't work (os, browsers and keywords detection).
Do you want me to setup Apache to write 'combined' log files [y/N] ? y
Add 'Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/"'
Add 'Alias /awstatscss "/usr/local/awstats/wwwroot/css/"'
Add 'Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/"'
Add 'ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/"'
Add '<Directory>' directive
AWStats directives added to Apache config file.
-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y
-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> www.testdemo.com
-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
>
-----> Create config file '/etc/awstats/awstats.www.testdemo.com.conf'
Error: Failed to open '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf' for read.
#/etc/awstats下没有awstats.model.conf文件造成的错误
| 执行awstats的配置脚本,如果设置上注意将会出现下面的错误信息,因/var/lib/awstats目录没有写入权限
[root@tomcat tools]# cp /usr/etc/awstats/awstats.model.conf /etc/awstats/
[root@tomcat tools]# ./awstats_configure.pl
----- AWStats awstats_configure 1.0 (build 1.8) (c) Laurent Destailleur -----
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
- You are not an administrator user,
- You want to analyze downloaded log files without web server,
- You want to analyze mail or ftp log files instead of web log files,
- You need to analyze load balanced servers log files,
- You want to 'understand' all possible ways to use AWStats...
Read the AWStats documentation (docs/index.html).
-----> Running OS detected: Linux, BSD or Unix
-----> Check for web server install
Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path ('none' to skip web server setup):
> /usr/local/httpd/conf/httpd.conf
-----> Check and complete web server config file '/usr/local/httpd/conf/httpd.conf'
All AWStats directives are already present.
-----> Update model config file '/etc/awstats/awstats.model.conf'
File awstats.model.conf updated.
-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y
-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> www.testdemo.com
-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
>
-----> Create config file '/etc/awstats/awstats.www.testdemo.com.conf'
Config file /etc/awstats/awstats.www.testdemo.com.conf created.
-----> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.testdemo.com
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...
A SIMPLE config file has been created: /etc/awstats/awstats.www.testdemo.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.testdemo.com' with command:
> perl awstats.pl -update -config=www.testdemo.com
You can also read your statistics for 'www.testdemo.com' with URL:
> http://localhost/awstats/awstats.pl?config=www.testdemo.com
Press ENTER to finish...
[root@tomcat tools]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl --update -config=www.testdemo.com
Error: AWStats database directory defined in config file by 'DirData' parameter (/var/lib/awstats) does not exist or is not writable.
Setup ('/etc/awstats/awstats.www.testdemo.com.conf' file, web server or permissions) may be wrong.
Check config file, permissions and AWStats documentation (in 'docs' directory).
#对于/var/lib/awstats目录没有写入权限造成的
| 如果出现上面的输出信息请修复对应的错误
[root@tomcat tools]# chmod 777 /var/lib/awstats
chmod: cannot access `/var/lib/awstats': No such file or directory
[root@tomcat tools]# mkdir /var/lib/awstats
[root@tomcat tools]# chmod 777 /var/lib/awstats
|
执行awstats分析脚本,如果配置不当将出现下面的配置信息,因日志路径错误造成的
[root@tomcat tools]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl --update -config=www.testdemo.com
Create/Update database for config "/etc/awstats/awstats.www.testdemo.com.conf" by AWStats version 6.9 (build 1.925)
From data in log file "/var/log/httpd/mylog.log"...
Error: Couldn't open server log file "/var/log/httpd/mylog.log" : No such file or directory
Setup ('/etc/awstats/awstats.www.testdemo.com.conf' file, web server or permissions) may be wrong.
Check config file, permissions and AWStats documentation (in 'docs' directory).
#日志路径错位造成的
[root@tomcat tools]# vi /etc/awstats/awstats.www.testdemo.com.conf
LogFile="/usr/local/httpd/logs/access_log"
#修复错误的日志路径信息
| 修复错误信息,重新执行awstats脚本
[root@tomcat tools]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl --update -config=www.testdemo.com
Create/Update database for config "/etc/awstats/awstats.www.testdemo.com.conf" by AWStats version 6.9 (build 1.925)
From data in log file "/usr/local/httpd/logs/access_log"...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log file...
Jumped lines in file: 0
Parsed lines in file: 30
Found 0 dropped records,
Found 30 corrupted records,
Found 0 old records,
Found 0 new qualified records.
| 从新启动apache
[root@tomcat tools]# /usr/local/httpd/bin/apachectl stop
[root@tomcat tools]# /usr/local/httpd/bin/apachectl start
| 添加定时分析指令
[root@tomcat logs]# crontab -e
20 1 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl --update -config=www.testdemo.com
| 通过WEB页面查看对网站的日志的分析
![]()
八、Apache和Tomcat整合好后验证码不加粗
在一些使用apache整合tomcat应用中,整合好后验证码不加粗,此问题是linux下没有ariblk字体,安装ariblk字体后重启apacht+tomcat后就可以显示了
使用构建的本地yum源安装ttmkfdir和 chkfontpath软件包
# yum install ttmkfdir
# yum install chkfontpath
|
a) 网上下载ariblk字体文件:
http://blogimg.chinaunix.net/blog/upfile2/090727180315.rar
b) 把这字体文件复制到 /usr/share/fonts/local/ 中,如果没有这个文件夹就创建之
c) 使用以下命令来更新字体信息:
ttmkfdir -d /usr/share/fonts/local/ -o /usr/share/fonts/local/fonts.scale
| d) 从fonts.scale拷贝出来一份文件 fonts.dir
cp fonts.scale fonts.dir
e) 把ariblk字体文件夹的路径添加到 X 字体服务器的路 径中:
chkfontpath --add /usr/share/fonts/local/
f) 重新启动 xfs 字体服务器:
service xfs reload
/etc/rc.d/init.d xfs stop
/etc/rc.d/init.d xfs start
/usr/local/httpd/bin/apachectl stop
/usr/local/httpd/bin/apachectl start
/opt/tomcat/bin/shutdown.sh
/opt/tomcat/bin/statup.sh
|
九、Apache和Tomcat性能测试
Apache默认的ab工具在这里不适合做Apache整合Tomcat的性能测试,在这里使用Jmeter是一个100%的纯java桌面应用,用于压力测试和性能测量。它可以用于对服务器,网络或对象模拟繁重的负载来测试它们的强度或分析不同压力类型下的整体性能。
Jmeter的并发线程数受机器硬件和web服务器处理速度的限制。Jmeter的工作量越大,每个线程等待cpu处理时间越长,时间信息就越不准确。
jmeter的安装
安装JDK
在http://download.java.net/jdk6/binaries/ 上下载jdk6的安装包 jre-6u10-beta-bin-b25-windows-i586-p-29_may_2008.exe,双击安装
设置环境变量:右击我的电脑->属性->高级->环境变量,在系统变量框里做如下工作:
新建变量JAVA_HOME,值为:安装JDK的目录
新建变量CLASSPATH,值为:%JAVA_HOME\lib
在path变量后加上:%JAVA_HOME\lib
在命令提示符窗口中输入:JAVA或JAVAC,如果出现帮助信息,则JDK安装成功安装JMeter
下载JMeter安装包,http://apache.mirror.phpchina.com/jakarta/jmeter/binaries/jakarta-jmeter-2.3.2.tgz。
直接解压就可以使用了。
分布式jmeter配置
因为Jmeter的并发线程数受机器硬件和web服务器处理速度的限制。单台机器运行jmeter,在处理器为1.4~3G时,根据应用所需要的资源可以运行100~300个并发线程。所以需要多级运行jmeter,实现分布式jmeter配置。
分布式测试的机器分为两种,一台作为控制台,几台作为工作站,作为控制台的机器可同时作为工作站,控制台控制测试的启动和停止,并收取工作站运行的测试结果。
在其他安装同一版本的jmeter的机器上,以文本方式打开jmeter.properties,找到第99行的server_port,将前面的注释去掉,为其设置一个端口,比如在192.168.1.20的机器上,为其设置端口2020。
在控制台机器上找到jmeter/bin目录,以文本方式打开jmeter.properties,找到99行的server_port,设置端口为2021,编辑95行的remote_hosts=127.0.0.1,添加机器的IP地址和端口号,逗号隔开,比如remote_hosts=192.168.1.21:2021,192.168.1.20:2020
录制测试计划
Jmeter可以录制自己的测试计划,需使用代理服务器
jmeter也可以使用badboy录制的测试计划,且简单,灵活方便
启动Badboy,你可以看到下面的界面。
![]()
在地址栏(图中用红色框住部分)中输入你需要录制的Web应用的URL http://www.testdemo.com,并点击GO按钮开始录制。
开始录制后,你可以直接在Badboy内嵌的浏览器(主界面的右侧)中对被测Web应用进行操作,此例中我们只录制加载首页。录制完成后,点击工具栏中的”停止录制”按钮,完成脚本的录制。最后选择”File”->”Export to JMeter”菜单,来导出为JMeter脚本格式,保存为script首页.jmx
运行测试,启动JMeter来打开测试脚本。
![]()
添加监听器,设置线程组属性,运行
聚合报告分析
![]()
Jmeter结果分析
聚合报告
![]()
Label:说明是请求类型,如Http请求。
Samples:也就是样本数目,总共发送到服务器的样本数目。
Average:是总运行时间除以发送到服务器的请求数。
Median:是代表时间的数字,有一半的服务器响应时间低于该值而另一半高于该值。
90%line:是指90%请求的响应时间比所得数值还要小。
Min:是代表时间的数字,是服务器响应的最短时间。
Max: 是代表时间的数字,是服务器响应的最长时间。
Error%:请求的错误百分比。
Throughput:也就是图形报表中的吞吐量,这里是服务器每单位时间处理的请求数,注意查看是秒或是分钟。
KB/sec:是每秒钟请求的字节数。
使用用户登录后再点击几次页面的脚本进行的测试
测试前系统状态
[root@tomcat ~]# free
total used free shared buffers cached
Mem: 1917952 1909716 8236 0 192228 519816
-/+ buffers/cache: 1197672 720280
Swap: 2031608 4 2031604
#系统的内存使用信息
[root@tomcat ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 4 8244 192232 519816 0 0 2 10 75 107 2 1 97 0 0
#通过vmstat查看到的系统资源信息
| 使用3台机器同时运行jmeter
每台机器并发10个线程
运行时间
Starting the test on host 192.168.1.21:2021 @ Wed Jul 30 10:07:40 CST 2008 (1217383660953)
Finished the test on host 192.168.1.21:2021 @ Wed Jul 30 10:09:01 CST 2008 (1217383741140)
|
Tomcat上看到的并发连接数为30,基本不变,表明目前的压力是30
[root@tomcat ~]# netstat -ant | grep 80|grep ESTA|wc -l
30
|
压力测试过程中系统状态
[root@tomcat ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
3 0 4 16360 168284 509984 0 0 2 10 76 112 2 1 97 0 0
#通过vmstat查看到的系统资源信息
[root@tomcat ~]# free
total used free shared buffers cached
Mem: 1917952 1902460 15492 0 168292 510696
-/+ buffers/cache: 1223472 694480
Swap: 2031608 4 2031604
#系统的内存使用信息
|
Jmeter压力测试聚合报告
![]()
掌握了上面的方式就可以完成一个基于J2EE应用的运行环境的构建,并能对应用进行一定的压力负载测试。当然每个J2EE的应用都是有所不同的,将本文作为参考请根据具体的应用进行实际的操作调整。本文实践过程参照部分公开于网络中的就技术文档,并于具体的应用逻辑进行结合,如有描述出入的部分欢迎大家与作者进行交流指证。
作者简介:CU网友kns1024wh,目前从事Linux群集方面的具体工作,之前做过多年的IT技术支持、MCT讲师、及REDFLAG的技术合作,技术专长群集、unix主机、AD部署等,您可以通过电子邮件lvsheat@qq.com或者Chinaunix社区与他取得联系。 |
|