WebalizerAwstats实现Apache日志分析监控

Webalizer

webalizer是一个高效的、免费的web服务器日志分析程序。其分析结果以HTML文件格式保存,从而可以很方便的通过web服务器进行浏览。Internet上的很多站点都使用webalizer进行web服务器日志分析。下面我们进行安装:

下载webalizer源码并解压缩:

进入解压缩文件查看里面的内容,在文件INSTALL里我们可以看到,直接可以直接执行./configure(当然,若有其他需要我们也可以在它后面添加,这里我们只添加一个配置文件路径)

接下来进行编译和安装:

make && make  install

安装完毕后我们就可以直接用webalizer指令了,对于它的用处可以用man来查看了,这里我们只对几个进行介绍。

-F type = 日志格式类型. type= (clf | ftp | squid)clf指通用的日志文件,我们的apache就是用clf类型的日志

-p = 保留状态 (递增模式)是指今天的日志信息到明天不再显示

-n 主机名

-o dir = 日志输出目录

-t name = 指定报告题目上的主机名

首先我们写一个bash脚本把日志信息输出出来,因为我们要对日志信息进行及时监控,所以我们要把它做成计划任务

/var/www/html/目录下创建一个log目录,我们假设把日志信息都输出到log目录下

创建计划任务,编辑crontab(在/etc/cron.web/下创建一个可执行文件,计划为每五分钟执行一次,这里我们只是实验需要,具体情况可以再进行改变)

先创建/etc/cron.web/目录,在目录下创建一个可执行文件web,并编辑

更改文件属性为可执行

Chmod  a+x web

当执行web文件时就会在log目录下产生一些文件

现在我们就可以进入log查看日志信息了

以后随着访问量的增多,访问报表的数据就会越来越多

Awstats

Awstats是在Sourceforge上发展很快的一个基于PerlWEB日志分析工具。相对于另外一个非常优秀的开放源代码的日志分析工具WebalizerAWStats的优势在于:

界面友好、由于基于perl很好的解决了跨平台问题、效率比较高、配置/定制方便等

下面对它进行安装:

首先要安装perl,可以通过本地yum安装

下载最新版本的awstats源代码进行安装(我这里是awstats-7.1.1

解压缩 tar –zxvf awstats-7.1.1.tar.gz

创建的两个目录都将在后面的配置中用到,一个用于存放站点日志分析的配置文件,一个用于存放日志数据信息

mkdir /etc/awstats

mkdir /var/lib/awstats

把解压缩后的awstats-7.1.1移动到/usr/local/目录下并改名字为awstats

Cp  -R  ./awstats-7.1.1  /usr/local/

Mv   awstats-7.1.1  awstats

接下来配置awstats

进入/usr/local/awstats/tools目录下

[root@station252 tools]# perl awstats_configure.pl

 

----- AWStats awstats_configure 1.0 (build 1.9) (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):

> none

 

Your web server config file(s) could not be found.

You will need to setup your web server manually to declare AWStats

script as a CGI, if you want to build reports dynamically.

See AWStats setup documentation (file docs/index.html)

 

-----> Update model config file '/usr/local/awstats/wwwroot/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.aaa.com

 

-----> Define config file path

In which directory do you plan to store your config file(s) ?

Default: /etc/awstats

然后连敲两次回车结束配置

awstats_configure.pl工具会自动修改apache配置,接下来去修改apache配置文件参数,添加以下内容:

编辑完毕后重启apache服务

awstats_configure.pl在安装过程中会根你的回答生成一个/etc/awstats/awstats.index.conf文件

编辑这个文件,修改日志文件路径为apacheaccess_log日志文件

vim /etc/awstats/awstats.www.aaa.com.conf

这样就完成了awstats的安装与配置

接下来对日志进行分析:

确保/usr/local/awstats/wwwroot/cgi-bin下的awstats.pl文件有执行权限

(若如下图对日志进行分析总会有错误出现)

我遇见的错误包括找不到文件路径的和权限不够的,倒是找到了一个解决的办法,在对日志进行分析更新时这样做(如下图)

把它输出到一个html网页文件内,我们去访问html网页便不会有问题(在浏览器框内输入http//localhost/awstats.html

下面是在外部机器上对日志监控的访问图:(在浏览器框内输入http://www.aaa.com/awstats.html