开源中文网

您的位置: 首页 > Linux应用 > Apache > 正文

Apache日志切割、合并、分析

来源:  作者:

一、安装cronolog ---日志切割
Cd /usr/ports/sysutiles/cronolog
Make install clean

Ee /usr/local/etc/apache22/httpd.conf
ErrorLog "|/usr/local/sbin/cronolog /usr/local/www/log/BDR-%w-error.log"
CustomLog "|/usr/local/sbin/cronolog /usr/local/www/log/BDR-%Y%m%d-access.log" combined
确保错误日志跟访问日志修改好。
二、日志远程拷贝
scp.sh
#!/usr/local/bin/expect

set timeout 0
set password kevin
set filename [exec date -v -1d +%Y%m%d]
spawn scp kevin@192.168.1.23:/usr/local/www/log/DR-$filename-access.log /usr/local/www/log/DR-$filename-access.log
set timeout 3
expect "Password:"
send "$password\r"
expect eof
三、日志合并
#!/usr/local/bin/bash
file=$(date '+%Y%m%d')
#file=$(date -v -1d +%Y%m%d)
sort -m -t " " -k 4 -o $file-all.log /usr/local/www/log/$file-access.log /usr/local/www/log/$file-access_log
把它放到crotab里每天晚上3点钟合并前一天的日志文件
四、安装awstats-----日志分析
Cd /usr/ports/www/awstats
Make install clean
1.配置 把下面内容放到httpd.conf最后面


#
# Directives to allow use of AWStats as a CGI
#
Alias /awstatsclasses "/usr/local/www/awstats/classes/"
Alias /awstatscss "/usr/local/www/awstats/css/"
Alias /awstatsicons "/usr/local/www/awstats/icons/"
ScriptAlias /awstats/ "/usr/local/www/awstats/cgi-bin/"

#
# This is to permit URL access to scripts/files in AWStats directory.
#
<Directory "/usr/local/www/awstats/">
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
2. 配置awstats
cd /usr/local/www/awstats/tools
ee awstats_configure.pl
把37行的¥AWSTATS_DIRDATA_PATH='/var/lib/awstats';改为
$AWSTATS_DIRDATA_PATH='/etc/awstats/awstats';
在66行后面加入:
'/usr/local/etc/apache22/httpd.conf',

perl awstats_configure.pl

----- AWStats awstats_configure 1.0 (build 1.7) (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
Warning: AWStats standard directory on Linux OS is '/usr/local/awstats'.
If you want to use standard directory, you should first move all content
of AWStats distribution from current directory:
/usr/local/www/awstats
to standard directory:
/usr/local/awstats
And then, run configure.pl from this location.
Do you want to continue setup from this NON standard directory [yN] ? y

-----> Check for web server install
Found Web server Apache config file '/usr/local/etc/apache22/httpd.conf'

-----> Check and complete web server config file '/usr/local/etc/apache22/httpd.conf' -->这里就自动检测出来了,不用手工输入了

-----> Update model config file '/usr/local/www/awstats/cgi-bin/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.kevin.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.kevin.com.conf'
Config file /etc/awstats/awstats.www.kevin.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/www/awstats/cgi-bin/awstats.pl -update -config=www.kevin.com
Or if you have several config files and prefer having only one command:
/usr/local/www/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...


A SIMPLE config file has been created: /etc/awstats/awstats.www.kevin.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.kevin.com' with command:
> perl awstats.pl -update -config=www.kevin.com
You can also read your statistics for 'www.kevin.com' with URL:
> http://localhost/awstats/awstats.pl?config=www.kevin.com

Press ENTER to finish...

3.make dir
/etc/awstats/awstats
chow -R 757 awstats

4. add the script to the crontab
#Update awstats
*/1 * * * * root /usr/local/www/awstats/cgi-bin/awstats.pl -update -config=www.mysite.com
#


5.通过这个网址来访问

http://192.168.1.15/awstats/awstats.pl?config=www.kevin.com
五、 认证
有时候某些目录下的文件需要增加认证,Apache缺省的认证模块都是很完备的,以下就是一个通过.htaccess文件增加用户认证的例子。
即使对于虚拟主机用户也可以通过上传一些文件来实现认证控制。

首先用htpasswd创建一个密码文件:比如文件名叫做my.passwd
/home/apache/bin/htpasswd -c -b my.passwd myusername mypassword
如果增加帐号:
/home/apache/bin/htpasswd -b my.passwd anotherusername anotherpassword

将my.passwd上传到一个非发布路径下(密码文件与.htaccess文件可在同一目录 权限设置为600)。
比如你的物理WEB根目录的上一级
/home/apache/conf/my.passwd
/usr/local/www/awstats/cgi-bin/my.passwd

创建 .htaccess
AuthName "My Authorization Directory"
AuthType Basic
AuthUserFile /home/apache/conf/my.passwd
Require valid-user --->这个地方不能改,改了就验证不了,老让你重新输入密码(其实密码是对的)

将这个文件上传到你需要进行用户认证的目录:
/home/apache/htdocs/admin/.htaccess
/usr/local/www/awstats/cgi-bin/.htaccess

配置httpd.conf文件:
<Directory "/usr/local/www/awstats/cgi-bin/">
Options Indexes MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>

这样这个目录的访问就需要认证了。

总结:my.passwd .htaccess文件可以放在同一目录。但要设置正确
httpd.conf配置文件里一定要有如下配置,不然不需要密码也可以访问
<Directory "/usr/local/www/awstats/cgi-bin/">
Options Indexes MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>

规范apache日志文件的命名:
access.log命名为:服务器名-%Y%m%d-access.log
error.log 命名为:服务器名-%Y%m%d-error.log

合并后的日志命名为:$(date '+%Y%m%d')-all.log

Tags:Apache日志
相关文章列表:
关于开源中文网 - 联系我们 - 广告服务 - 网站地图 - 版权声明