Linux 日志分析工具之awstats

1、awstats 是什么

        官方网站:AWStats is a free powerful and featureful tool that generates advanced web, streaming, ftp or mail server statistics, graphically. This log analyzer works as a CGI or from command line and shows you all possible information your log contains, in few graphical webb pages. It uses a partial information file to be able to process large log files, often and quickly. It can analyze log files from all major server tools like Apache log files (NCSA combined/XLF/ELF log format or common/CLF log format), WebStar, IIS(W3C log format) and a lot of other web, proxy, wap, straming servers, mail servers and some ftp servers.php

        Awstats 是一个免费的日志分析工具,用perl开发。能够用于分析各类linux下的应用程序产生的日志,包括 apache/squid/samba/resin/tomcat 等等。在这里,咱们主要用它来分析apache日志。css

        统计包括访问,访问者,页面,点击,高峰时间,操做系统,浏览器,搜索引擎,关键字,机器人访问,端开的连接和更多的阻力截图排序。html

        Awstats是一个很是简洁并且强大的统计工具。它能够统计您站点的以下信息:mysql

一:访问量,访问次数,页面浏览量,点击数,数据流量等精确到每个月、每日、每小时的数据
二:访问者国家、访问者IP、操做系统、浏览器等
三:Robots/Spiders的统计
四:纺客持续时间
五:对不一样Files type 的统计信息
六:Pages-URL的统计
七:其它信息(搜索关键字等等) linux

  • 访问次数、独特访客人数,nginx

  • 访问时间和上次访问,web

  • 使用者认证、最近认证的访问,sql

  • 每周的高峰时间(页数,点击率,每小时和一周的千字节),数据库

  • 域名/国家的主机访客(页数,点击率,字节,269域名/国家检测, geoip 检测),apache

  • 主机名单,最近访问和未解析的 IP 地址名单

  • 大多数看过的进出页面,

  • 档案类型,

  • 网站压缩统计表(mod_gzip 或者 mod_deflate),

  • 使用的操做系统 (每一个操做系统的页数,点击率 ,字节, 35 OS detected),

  • 使用的浏览器,

  • 机器人访问(检测 319 个机器人),

  • 蠕虫攻击 (5 个蠕虫家族),

  • 搜索引擎,利用关键词检索找到你的地址,

  • HTTP 协议错误(最近查阅没有找到的页面),

  • 其余基于 URL 的个性报导,连接参数, 涉及综合行销领域目的.

  • 贵网站被加入"最喜好的书签".次数.

  • 屏幕大小(须要在索引页补充一些 HTML 标签).

  • 浏览器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.

  • 负载平衡服务器比率集群报告.

 

        Awstats 的运行须要 perl 环境的支持,从awstats 官方文档来看,它对apache http server 的支持是很是完美的。若是使用的 nginx,要运行awstats有点麻烦。首先nginx对perl 的支持是比较弱的,甚至官方也不建议使用;另外在日志格式上须要修改后才能正常运行。

awstats官方网站: http://awstats.sourceforge.net/   

perl官方网站: http://www.perl.com/    

awstats项目地址:http://sourceforge.net/projects/awstats/

 

 

2、awstats 工做原理以及其余同类软件

        AWStats的功能不少,我在此主要用它来分析apache服务器的日志。安装使用以前仍是说说大体的工做原理,AWStats提供一系列的perl脚本实现:服务配置,日志读取,报表生成等功能。

        而功能实现的具体执行过程是:首先,固然是apache将访问状况记录到日志中,AWStats每次执行更新时读取这些日志,分析日志数据,将结果存储到数据库中,(这个数据库是AWStats自带的【就是一文本文件】,并不须要第三方软件支持。),最后AWStats提供一个cgi程序经过web页面来显示数据库中所统计的数据。

 

AWStats的工做模式是这样的:

分析日志:运行后将日志统计结果归档到一个AWStats的数据库(纯文本)里;

输出日志:分两种形式

  • 一种是经过cgi程序读取统计结果数据库输出(Linux中);

  • 一种是运行后台脚本将输出导出成静态文件(Windows中);

其余同类日志统计分析工具备:

  • AWStats

  • Analog

  • Webalizer

 

 

3、awstats 安装和配置

环境信息约定:

  • 日志文件位置:/usr/local/apache/logs

  • 系统:CentOS6.5 X64

  • awstats安装位置:/usr/local/awstats

  • JAWStats安装位置:/htdoc/stats/

  • 这里咱们配置的站点域名:www.test.com

  • WEB服务器:Apache 2.2

  • 静态页面文件输出路径:/usr/local/apache/data(可选)

 

Requirements:

To use AWStats, you need the following requirements:

  • Your server must log web access in a log file you can read.

  • You must be able to run perl scripts(.pl files) from command line and / or as CGI(Perl 5.007003 or higher required to run AWStats 6.9 or higher)

If not, you can solve this by downloading last Perl version at  ActivePerl (Win32) or  Perl.com(Unix/Linux/Other)

一、安装 perl

awstats 是由 perl 语言编写的,无论是在Windows仍是Linux下,都须要安装perl。因此要首先准备好 perl 环境,一般linux系统都是自带了perl的。

# perl --version # 查看是否已经安装了perl, 以及perl 版本号 # yum -y install perl* # 若是没有安装,yum install 吧
 

二、下载 awstats 工具(Last stable version is 7.4),并解压至常规目录 /usr/local

# wget http://www.awstats.org/files/awstats-7.4.tar.gz # tar xf awstats-7.4.tar.gz # mv awstats-7.4 /usr/local/awstats ################ 华丽的分割线 ############### # 固然,也能够经过 yum 安装 # yum -y install awstats
 

三、安装 awstats

因为 wget 下来的包文件的权限是非 root用户的,因此须要修改权限,不然稍后 *.pl 没法运行。

# mkdir /etc/awstats # mkdir /var/lib/awstats # chown apache:apache /var/lib/awstats 上面建立的两个目录都将在后面的配置中用到,一个用于存放站点日志分析的配置文件,一个用于存放日志数据信息。 接下来执行 /usr/local/awstats/tools下的awstats_configure.pl 配置向导(交互式),用来生成awstats的配置文件,awstats配置文件的命名规则是awstats.WEBSITE.conf。固然这些都是能够在之后的配置文件中修改的。 # cd /usr/local/awstats/tools/ # perl awstats_configure.pl ---执行该命令 ----- AWStats awstats_configure 1.0 (build 20140126) (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 Found Web server Apache config file '/etc/httpd/httpd.conf' # 检测到apache 配置文件 -----> Check and complete web server config file '/etc/httpd/httpd.conf' 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. -----> 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 # 询问是否建立一个新的配置文件,这里填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.test.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): > # 这里要填写你配置文件存放路径,咱们使用它默认的路径/etc/awstats,因此直接回车便可。 -----> Create config file '/etc/awstats/awstats.www.test.com.conf' Config file /etc/awstats/awstats.www.test.com.conf created. -----> Restart Web server with '/sbin/service httpd restart' grep: /etc/httpd/conf/httpd.conf: No such file or directory Stopping httpd: [ OK ] Starting httpd: [ OK ] -----> 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.test.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... #提示不能自动加入crontab定时任务,须要稍后本身添加,咱们按回车继续便可。 #回头把该命令填入crontab 按指定时间执行 A SIMPLE config file has been created: /etc/awstats/awstats.www.test.com.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for 'www.test.com' with command: > perl awstats.pl -update -config=www.test.com You can also read your statistics for 'www.test.com' with URL: > http://localhost/awstats/awstats.pl?config=www.test.com Press ENTER to finish... #提示配置文件建立完成和如何更新配置及创建静态报告页,这里咱们回车便可结束这个配置向导。 注意:这里要在Awstats安装目录运行。不然会有一些关于标准目录的提示。Awstats缺省标准安装目录是/usr/local/awstats,若是要安装到其它目录须要对awstats_configure.pl进行修改。
 

 

Awstats 会自动检测httpd.conf 配置文件,并在其配置文件末尾添加以下语句:

# # Directives to allow use of AWStats as a CGI # Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/" Alias /awstatscss "/usr/local/awstats/wwwroot/css/" Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/" ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/" # # This is to permit URL access to scripts/files in AWStats directory. # <Directory "/usr/local/awstats/wwwroot"> Options None AllowOverride None Order allow,deny Allow from all </Directory> 若是是apache 2.4,则需调整: Require all granted 在这里,容许 /usr/local/awstats/wwwroot 目录定义本身的访问规则,所以我作一些简单的认证。 # # This is to permit URL access to scripts/files in AWStats directory. # <Directory "/usr/local/awstats/wwwroot"> Options None AllowOverride None AuthType Basic AuthName "Restricted Files" AuthUserFile /etc/httpd/passwd Require user admin Order allow,deny Allow from all </Directory> ## 添加apache 认证用户和密码 # htpasswd -c /etc/httpd/passwd admin New password: Re-type new password: Adding password for user admin ## 重启apache # service httpd restart
 

 

awstat 目录:

安装目录: /usr/local/awstats

配置文件: /etc/awstats

数据文件: /var/lib/awstats

三、查看awstats配置文件

        awstats_configure.pl 还将根据安装过程当中您的回答(要分析的网站域名)信息,生成配置文件。结束这个向导之后,咱们对www.test.com站点的awstats配置文件已经配置好了。下面咱们来查看并修改一下这个配置文件。

 

接下来,编辑配置文件,AWStats 支持针对多个子域名生成分析日志,主要修改如下几行:

# cd /etc/awstats/ # ls awstats.www.test.com.conf # vim awstats.www.test.com.conf #这个配置文件有不少配置,咱们这里只讲解重点配置。 LogFile="/usr/local/apache/logs/access_%YYYY-24%MM-24%DD-24.log" #日志文件存放路径 LogType=W #分析web日志 LogFormat=4 #表示日志格式 combined DirData="/var/lib/awstats" #建立生成的数据路径 SiteDomain="www.test.com" Lang="cn" #默认语言中文 SkipHosts="127.0.0.1 REGEX[^192\.168\.]" #本地及内部的访问不作分析统计 LevelForWormsDetection=2 #日志等级,不对警告日志进行统计
 

LogFile 指明apache的access log所在,这里也可使用变量,例如:LogFile="/data/log/imysql.cn-access_log.%YY-24%MM-24%DD",具体的请查看 相关文档
LogFormat 表示日志格式,我在apache的日志格式中,采用 combined,所以这里写成4。若是apache里设置的是 common,则在这里写 2,具体的对应关系能够看 Awstats文档
SiteDomain 要分析的域名
DirData 分析结果存放位置(awstas数据库)

若是apache 进行了日志切割, 这里须要随机应变

## awstats # vim awstats.www.test.com.conf LogFile="/log/www/access_%YYYY-24%MM-24%DD-24.log" 若是进行日志切割,可以使用通配符%YYYY-24%MM-24%DD-24 是指年月日模式(昨天) %YYYY-0%MM-0%DD-0 (当天) ## apache # vim /etc/httpd/conf/httpd.conf #CustomLog logs/access_log combined CustomLog "|/usr/local/sbin/cronolog /log/www/access_%Y%m%d.log" combined
 

 

 

4、awstats 执行日志分析 

 

    在上面的咱们讲解了awstats的yum安装与源码安装,同时配置了一个监控实例,下面咱们就执行一下这个监控实例www.test.com

一、更新awasts数据库

## 首先重启一下 httpd # service httpd restart # perl /usr/local/awstats/tools/awstats_updateall.pl now 上面这种方式是一会儿分析全部的域名,也能够像下面这样只分析某个域名: # perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=WEBSITE [root@web ~]# perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.test.com Create/Update database for config "/etc/awstats/awstats.www.test.com.conf" by AWStats version 7.4 (build 20150714) From data in log file "/var/log/httpd/access_log"... Phase 1 : First bypass old records, searching new record... Direct access after last parsed record (after line 319) Jumped lines in file: 319 Found 319 already parsed records. Parsed lines in file: 5 Found 0 dropped records, Found 0 comments, Found 0 blank records, Found 0 corrupted records, Found 0 old records, Found 5 new qualified records.
 

        如今咱们须要用awstats来生成对apache日志的统计分析信息。因为咱们用的awstats是支持perl的FCGI动态化访问页面,因此此处咱们只须要直接更新数据库便可。FCGI程序会自动将数据库以动态页面的形式展示出来,无须再手动生成静态页面了。

 

 

接下来咱们来查看一下分析结果

若是还想经过web server查看分析结果,就须要让web server支持cgi才行了。在这里,我使用apache做为web server,所以我在编译apache时,加上了选项:--enable-cgi

  • LoadModule cgid_module modules/mod_cgid.so

  • 注意cgi脚本的执行权限,必定要apache用户拥有执行权限,不然可能提示Server Internal 错误。

# chown -R apache:apache /usr/local/awstats
 

 

注,若是,你顺利的执行了上面的全部操做。那么OK,如今你已经能够经过访问:http://localhost/awstats/awstats.pl?config=www.test.com ,效果以下图:

wKioL1X1d_DSOLffAAPA5CWJgzc014.jpg

 

 

cron更新数据库

    因为要获得新的统计信息,就必须更新数据库,也就是说从新作读取日志。分析日志提取里面新增的部分进行分析,将更新的数据存入数据库的过程。最好就让它定时执行。

    最后须要说明的是,在文中已经屡次提到过,要能反应最新信息就必须更新数据库。也就是说你可能已经对它进行了屡次访问,可是查看统计页面发现没变化。这是由于这些日志尚未更新到数据库.简单的方法就是定时执行。

00 5 * * * /usr/bin/perl  /usr/local/awstats/tools/awstats_updateall.pl now &> /dev/null
 

 

 

5、awstats 多站点日志分析

假设我有两个站点:

配置多站点监控(也就是配置 多个awstats 配置文件便可。分析多个域名的日志,就是把 awstats_configure.pl 多操做几回)

注,配置 mail.test.com的 配置文件,其实步骤和上面www.test.com同样,只是在域名的地方作更改便可。下面咱们就再来演示一下,

# cd /usr/local/awstats/tools/ # ./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): > /etc/httpd/conf/httpd.conf 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: > mail.test.com #咱们这里配置域名就是mail.test.com,其它地方与www.test.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): > #这里要填写你配置文件存放路径,咱们使用它默认的路径/etc/awstats,因此直接回车便可。 -----> Create config file '/etc/awstats/awstats.www.test.com.conf' Config file /etc/awstats/awstats.www.test.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=mail.test.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... #提示不能自动加入crontab定时任务,须要稍后本身添加,咱们按回车继续便可。 A SIMPLE config file has been created: /etc/awstats/awstats.mail.test.com.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for 'mail.test.com' with command: > perl awstats.pl -update -config=mail.test.com You can also build static report pages for 'mail.test.com' with command: > perl awstats.pl -output=pagetype -config=mail.test.com Press ENTER to finish... #提示配置文件建立完成和如何更新配置及创建静态报告页,这里咱们回车便可结束这个配置向导。
 

好了,下面步骤和上面www.test.com配置步骤同样,我这里就不重复说明。

 

6、awstats 问题汇总

一、IP 地址国家、区域显示问题

    Awstats默认安装以后是不具备识别访问者的国家和地区信息的,因此须要安装插件支持Awstats列出访问者的国家和地区,便于分析GeoIP免费的是国家/IP的数据表,GeoIPCityLite是地区的数据表。

(1).MaxMind目前免费提供了GeoIP 和GeoIPCityLite 数据包:能够按期每月从如下地址下载

## 下载 # wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz # wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz ## 解压 # gzip -d GeoIP.dat.gz # gzip -d GeoLiteCity.dat.gz ## 移至新建目录 # mkdir -pv /var/geoip # mv GeoIP.dat GeoLiteCity.dat GeoIPASNum.dat /var/geoip/ ## 安装源 # wget http://ftp.sjtu.edu.cn/fedora/epel/6/i386/epel-release-6-8.noarch.rpm # rpm -ivh epel-release-6-8.noarch.rpm warning: epel-release-6-8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY Preparing... ########################################### [100%] 1:epel-release ########################################### [100%] # yum clean all # yum makecache ## 安装GeoIP C库与 GeoIP perl库 # yum -y install GeoIP perl-Geo-IP ## 修改 awstats 配置文件 # vim /etc/awstats/awstats.www.test.com.conf #1444 行左右添加: LoadPlugin="geoip GEOIP_STANDARD /var/geoip/GeoIP.dat" #1464 行左右添加: LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /var/geoip/GeoLiteCity.dat" ## 删除旧的统计数据 # rm -rf /var/lib/awstats/* ## 从新生成数据库 # perl /usr/local/awstats/tools/awstats_updateall.pl now # perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.test.com # 生成 html 页面 # /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.test.com -lang=cn
 

二、中文乱码问题

 

    Awstats是一套很是好用的免费的日志分析软件,他是用perl实现的,支持web log、ftp log和mail log;并且它还能自动根据你浏览器的字符设置来选取语言(支持中文)。可是缺省安装的话有个问题,就是用来搜索的关键字若是是中文的话显示出来是乱码的。 之因此搜索的关键字句会变成乱码的缘由,主要是由于如今的搜索引擎都是使用UTF8,而Awstats是使用decodeUTFkeys这个plugin来处理搜索引擎的UTF8关键字,默认是没有打开的,因此在显示上会出现乱码。要解决中文乱码问题,方法也很简单,

在配置文件中把decodeutfkeys这个plugin打开就能够了。在配置文件中找到:

#LoadPlugin="decodeutfkeys"

去掉前面的#就能够了。

# vim /etc/awstats/awstats.www.test.com.conf #LoadPlugin="decodeutfkeys" #1300 行 LoadPlugin="decodeutfkeys"
 

这样咱们的中文显示就完成了,可是打开LoadPlugin="decodeutfkeys"这个插件在一些机器上会报错,下面咱们来演示一下报错的状况。

报错

从上面的报错能够看出这是缺乏 Perl 的 URI:Escape模块,下面咱们解决一下这个问题:

# wget http://search.cpan.org/CPAN/authors/id/G/GA/GAAS/URI-1.60.tar.gz # tar xf URI-1.60.tar.gz # cd URI-1.60 # perl Makefile.PL # make && make install
 

 

 

7、awstats 总结

    AWStats是在Sourceforge上发展很快的一个基于Perl的WEB日志分析工具。相对于另一个很是优秀的开放源代码的日志分析工具Webalizer,AWStats的优点在于:

1.界面友好:能够根据浏览器直接调用相应语言界面(有简体中文版)

2.基于Perl:而且很好的解决了跨平台问题,系统自己能够运行在GNU/Linux上或Windows上(安装了ActivePerl后);分析的日志直接支持Apache格式 (combined)和IIS格式(须要修改)。Webalizer虽然也有Windows平台版,但目前已经缺少 维护;AWStats彻底能够实现用一套系统完成对自身站点不一样WEB服务器:GNU/Linux/Apache和Windows/IIS服务器的统一统计。

3.效率比较高:AWStats输出统计项目比Webalizer丰富了不少,速度仍能够达到Webalizer的1/3左右,对于一个日访问量 百万级的站点,这个速度都是足够的;

4.配置/定制方便:系统提供了足够灵活但缺省也很合理的配置规则,须要修改的缺省配置不超过3,4项就能够开始运行,并且修改和扩展的插件仍是 比较多的;

5.AWStats的设计者是面向精确的"Human visits"设计的,所以不少搜索引擎的机器人访问都被过滤掉了,所以有可能比其余日志统计工具统计的数字要低,来自公司内部的访问也能够经过IP过滤 设置过滤掉。

6.提供了不少扩展的参数统计功能:使用ExtraXXXX系列配置生成针对具体应用的参数分析会对产品分析很是有用。

AWStats 是一个免费的强大而有个性的工具,带来先进的网络,流量,FTP或邮件服务器统计图. 本日志分析器做为CGI或从命令行在数个图形网页中显示你日志中包含的全部可能信息. 它利用一部分档案资料就能常常很快地处理大量日志档案, 它能分析日志文件来自从各大服务器工具 ,如 Apache日志档案 s (NCSA combined/XLF/ELF log format or common/CLF log format), WebStar, IIS (W3C的日志格式)及许多其余网站,Proxy(代理服务器)、Wap、流量服务器、邮件服务器和一些 FTP服务器。

看一看这个比较表在最著名统计工具 (AWStats, Analog, Webalizer,...)之间有何特色和不一样的想法。AWStats 是一个在GNU通用公共许可证下发行的免费软件. 你能够看看这个许可证图表而知道你能够/不能够作。因为AWStats工程来自网上信息,但也做为CGI、 它能够与容许进入Perl、CGI与日志的大型网站主办提供商一块儿工做。

 

8、安装配置 JAWStats 

 

JAWStats 是基于 Awstats 统计数据的,但其界面更为强悍,有了JAWStats 你甚至能够抛弃第三方统计了。 

2.1. 下载与安装 
# wget http://static.jawstats.com/src/jawstats-0.7beta.tar.gz # mkdir /var/www/stats/ # tar -zxf jawstats-0.7beta.tar.gz -C /var/www/stats/ # cd /var/www/stats/ # cp config.dist.php config.php 2.2. 修改配置文件 官方配置说明见:http://www.jawstats.com/documentation jawstats 是支持多站点的,每一个 $aConfig 对应一个站点,并能够自由切换 <?php // core config parameters $sDefaultLanguage = "zh-cn"; // 设置默认语言 $sConfigDefaultView = "thismonth.all"; $bConfigChangeSites = false; //是否能够更换站点 $bConfigUpdateSites = false; //是否能够更新统计数据 $sUpdateSiteFilename = "xml_update.php"; // individual site configuration $aConfig["www.zzxtbl.com"] = array( // site1 网站域名(注意不能乱填) "statspath" => "/var/lib/awstats/", //awstats 统计数据目录 "updatepath" => "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl/", //用于更新统计数据 "siteurl" => "http://www.zzxtbl.com", //点击首页标题后会前往这个地址 "sitename" => "test.org", //首页标题 "theme" => "default", "fadespeed" => 250, "password" => "my-1st-password", //更新统计数据须要输入这个密码 "includes" => "", "language" => "zh-cn" ); ?> <?php // core config parameters $sDefaultLanguage = "zh-cn"; $sConfigDefaultView = "thismonth.all"; $bConfigChangeSites = false; $bConfigUpdateSites = false; $sUpdateSiteFilename = "xml_update.php"; // individual site configuration $aConfig["site1"] = array( "statspath" => "/var/lib/awstats/", "updatepath" => "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl/", "siteurl" => "http://www.zzxtbl.com", "theme" => "default", "fadespeed" => 250, "password" => "my-1st-password", "includes" => "" ); ?> 2.3. 安装中文语言包 首先下载 jawstats 多国语言包,解压缩后上传替换 lang 目录。 wget http://www.jawstats.com/src/languagepack.zip 须要注意的是,你要修改 translations.php 文件的编码为UTF-8,否则全乱码了。 另外修改汉化文件,也是translations.php,不能只修改zh-cn.js文件。 2.4.在http服务器的httpd.conf添加虚拟目录 Alias /zzxtbl "/usr/local/jawstats" <Directory "/usr/local/jawstats"> Options Indexes MultiViews Order allow,deny Allow from all </Directory>
 

 

 

http://www.cnblogs.com/fnng/archive/2012/08/31/2666175.html

http://freeloda.blog.51cto.com/2033581/1346412

http://blog.fity.cn/post/389/

http://zzxtbl.blog.51cto.com/7620672/1650825

 

 

 

9、Windows 平台安装 awstats 

一、安装环境

  • windows Server 2008 R2

  • apache 2.2

  • awstats版本为7.3

 

 

Windows系统下配置 apache 日志切割方法:

A、使用 cronolog 日志轮转工具

从 http://cronolog.org/download/  能够下载 cronolog程序, 把下载的 cronolog程序放到 apache 的bin 目录下(详细参数参加: http://cronolog.org/usage.html)

wKioL1X24nDSIdMOAAFUTs0ku_o306.jpg

B、使用 rotatelogs 程序

rotatelogs 是 apache 自带的管道日志程序,参数以下(参考:http://lamp.linux.gov.cn/Apache/ApacheMenu/programs/rotatelogs.html )

wKiom1X24D2CzlWqAAHhStGyHRY612.jpg

 

2、安装备忘

一、注意:因为awstats依赖于perl,因此提早安装perl环境

下载perl:   http://www.activestate.com/activeperl/downloads

或者直接在百度搜索下载: activeperl 

根据本身的环境选择不一样的安装包,一般下载 ActivePerl_5.x.msi 可执行程序,而后安装

一、测试是否加入环境变量
C:\Users\Administrator> perl -v

二、根据安装提示
记录perl的路径(如X:\perl\bin\perl.exe),下面会用到

三、可能存在的问题
perl自动安装的状况会将路径配置到%system%以前,致使系统没法识别,解决方法就是把路径放置到PATH最后
 

二、配置 apache 支持 perl (CGI)

修改httpd.conf文件:
#ScriptAlias /cgi-bin/ "c:/Apache/cgi-bin/" 将前面的 # 号去掉,改成 ScriptAlias /cgi-bin/ "c:/Apache/cgi-bin/" AddHandler cgi-script .cgi 在后面加上一个空格和 .pl , 改成 AddHandler cgi-script .cgi .pl #LoadModule alias_module modules/mod_alias.so 启用alias_module模块,把前面的#号去掉,改成 LoadModule alias_module modules/mod_alias.so 修改后记得重启apache
 

 

三、解压AWStats

 

下载AWStats(国内环境的话,须要自带工具)

http://sourceforge.net/projects/awstats

自行选择须要解压的路径

 

四、设置配置文件

定位到awstats-7.3\tools下,执行 awstats_configure 文件,这个文件的主要功能就是设置WEB容器的路径,log路径等。

配置完后,APACHE的 httpd.conf 下会生成相似一下的内容

# # Directives to allow use of AWStats as a CGI # Alias /awstatsclasses "c:/awstats-7.3/wwwroot/classes/" Alias /awstatscss "c:/awstats-7.3/wwwroot/css/" Alias /awstatsicons "c:/awstats-7.3/wwwroot/icon/" ScriptAlias /awstats/ "c:/awstats-7.3/wwwroot/cgi-bin/" # # This is to permit URL access to scripts/files in AWStats directory. # <Directory "c:/awstats-7.3/wwwroot"> Options none AllowOverride none Order allow,deny Allow from all </Directory>
 

 

同时在awstats-7.3\wwwroot\cgi-bin文件夹下会生成一个 awstats.domain.com.conf 文件

 

四、修改配置文件

定位到awstats-x.x\wwwroot\cgi-bin下,打开awstats.domain.com.conf文件
修改以下内容

LogFile="C:/Apache2.2/logs/fs_access-%YYYY-24%MM-24%DD-24.log" #日志文件存放路径,要与你的apache的log路径一致 LogFormat=4 SiteDomain="fs.zhubajie.la" Lang="cn" #默认语言中文 SkipHosts="127.0.0.1" #本地访问不作分析统计
 

五、生成数据

cmd 定位到awstats文件下(awstats-x.x\wwwroot\cgi-bin),在cmd中执行如下命令

注意:请将domain.com替换为你的域名或地址
# perl.exe awstats.pl -update -config=domain.com 正常状况下会显示 不出意外的话,会显示出以下信息: C:\awstats-7.3\wwwroot\cgi-bin>perl.exe awstats.pl -update -config=fs.zhubajie.l a Create/Update database for config "./awstats.fs.zhubajie.la.conf" by AWStats ver sion 7.3 (build 20140126) From data in log file "C:/Apache2.2/logs/fs_access-20150914.log"... Phase 1 : First bypass old records, searching new record... Searching new records from beginning of log file... Phase 2 : Now process new records (Flush history on disk after 20000 hosts)... Jumped lines in file: 0 Parsed lines in file: 134025 Found 0 dropped records, Found 0 comments, Found 0 blank records, Found 0 corrupted records, Found 0 old records, Found 134025 new qualified records. 同时在awstats-x.x\wwwroot\cgi-bin下会生成一个相似awstats0X2013.domain.txt 的文件
 

六、修改awstats文件

编辑: awstats-x.x\wwwroot\cgi-bin\awstats.pl

因为win下路径与linux中不一样,因此这个文件第一行应该改为 #!X:/perl/bin/perl.exe (该地址为前期你安装的perl的地址) 不然后期运行会出错 couldn’t create child process: 720002: awstats.pl
 

七、浏览器打开

在浏览器中输入相似的地址 http://localhsot/awstats/awstats.pl?config=domain.com,正常状况下便可看到结果

 

高级配置

一、增长一个显示来访IP归属地的功能

执行以下命令

ppm install Geo::IP::PurePerl
ppm install Geography::Countries
ppm install IP::Country
ppm install Geo-IPfree
ppm install Net-Xwhois

安装GeoIP

http://dev.maxmind.com/geoip/legacy/geolite/

修改awstats.domain.com.conf文件,找到以下内容

# 开启 GeoIP 反查模块
LoadPlugin="geoip GEOIP_STANDARD D:/awstats/awstats-x.x/wwwroot/cgi-bin/ipdata/GeoIP.dat"
# 开启 IP 反查的外挂
LoadPlugin="geoipfree"
# 开启 decodeutfkeys 外挂,解决”用以搜索的关键词”乱码
LoadPlugin="decodeutfkeys"

 

更新配置文件

perl.exe awstats.pl -config=domain.com -update

二、按期更新

经过win的计划任务搞定

编辑一个 bat 批处理文件, 内容以下:

C:\Perl\bin\perl.exe  C:\awstats-7.3\wwwroot\cgi-bin\awstats.pl -update -config=fs.zhubajie.la
 

 

三、加一个须要用户名才能够访问的功能

因为要求在内网访问,因此没有涉及

 

参考阅读:

 

http://luestudy.blog.51cto.com/8480481/1395362

http://blog.chinaunix.net/uid-7354272-id-2643593.html

 

怎样在windows下安装Perl

http://jingyan.baidu.com/article/380abd0aa6c7371d90192c1b.html

 

 

awstats 分析 nginx 日志

一、安装nginx 以及 awstats 的过程就不讲解了, 能够参考上面的内容。

 

二、本文主要介绍经过让 awstats 对日志统计的结果生成静态页面,而后经过 Nginx 输出以达到统计 Nginx 访问日志的效果,其中还包括如何让 Nginx 自动切割日志文件。对于nginx的日志,个人作法是按天切割。

 

配置 Nginx 自动切割日志

跟 Apache HTTP Server(如下称 Apache)不一样的是,Apache 能够将日志输出经过管道的方式进行从新定向,依此来进行自动的日志切割。Nginx 在现今版本上还没能跟 Apache 同样,经过%YY等参数按日期分批建立日志,可是经过给 nginx 进程发送一个特定的信号,可使 nginx 从新生成日志文件。咱们能够按期执行一个 Shell 脚原本切换日志,从新命名或转移,具体的脚本以下:

# mv  /usr/local/nginx/logs/access.log  /usr/local/nginx/logs/access_`date -d "yesterday" +%Y%m%d`.log
# killall –s USR1 nginx	#使用USR1参数通知Nginx进程切换日志文件
 

将以上脚本内容保存为文件名为 logcron.sh 存到自定的目录中,例如 /opt/nginx/sbin/logcron.sh

使用 Crontab 让该脚本程序在天天晚上 11 点 59 分自动执行,便可作到按天建立日志。

 

须要注意的是,nginx的日志应该遵循如下格式,才能够被awstats识别,如定义日志格式

# 定义日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # 使用日志格式 access_log /home/www/logs/access.log main; # nginx 日志进行了切割 mv ${logs_path}/access.log ${logs_path}/access_$(date -d "yesterday" +"%Y%m%d").log 生成的日志格式: access_20150912.log
 

 

执行 tools 目录中的 awstats_configure.pl 配置向导,建立一个新的统计。

[root@iredmail tools]# ./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 #这里填none并回车,由于咱们没有使用apache,咱们这里用的是 Nginx,因此跳过。 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 #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: > slog.fity.cn #统计网站的域名,例如slog.fity.cn -----> 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): > #直接回车,使用默认路径/etc/awstats -----> Create config file '/etc/awstats/awstats.slog.fity.cn.conf' Config file /etc/awstats/awstats.slog.fity.cn.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=slog.fity.cn #回头把该命令填入crontab 按指定时间执行 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.slog.fity.cn.conf #新配置文件所在的路径 You should have a look inside to check and change manually main parameters. You can then manually update your statistics for 'slog.fity.cn' with command: > perl awstats.pl -update -config=slog.fity.cn You can also build static report pages for 'slog.fity.cn' with command: > perl awstats.pl -output=pagetype -config=slog.fity.cn Press ENTER to finish... #回车完成配置文件的建立
 

 

以上步骤默认会产生配置文件 ==>> /etc/awstats/awstats.slog.fity.cn.conf

awstats 配置文件的命名规则:awstats.website.conf

修改配置文件

# vi /etc/awstats/awstats.slog.fity.cn.conf (1)指定日志文件路径(很是重要,必须的一个步骤) LogFile="/usr/local/nginx/logs/access.log"(51行左右) //指定log文件的存储位置 若是你但愿天天能定时生成昨天的报表,能够: LogFile="/usr/local/nginx/logs/access_%YYYY-24%MM-24%DD-24.log" // 日志切割后 注:根据你的日志路径和对应的日志文件名进行修改。对应Nginx日志切割程序所生成的目录存放结构保存的nginx日志文件。要注意Awstats的年月日格式的跟Nginx的写法有所不一样,其中%YYYY-24 、 %MM-2四、%DD-24表示年月日都减去24小时,也就是昨天的日志目录。 ############# 可选 ################## (2)修改Nginx日志格式 ## 这一步感受非必须 ## 这里Awstats执行统计的顺序是: Nginx产生日志–>日志切割–>Nginx继续产生日志–>另存切割日志–>交由Awstats统计–>生成结果 修改日志格式,因为这里nginx生成日志格式要与awstats所须要的匹配,因此要修改日志格式,以便awstats读取。注释掉LogFormat=1,添加以下,每个变量的参数,配置文件上方有解释,这里很少说 ## 编辑 awstats 配置文件 #LogFormat=1 LogFormat="%host %other %other %time1 %other %methodurl %code %bytesd %refererquot %uaquot" ## 修改Nginx日志格式: nginx的日志应该遵循如下格式,才能够被awstats识别。这里的main是指格式代称,若是有多个虚拟主机,代称不能同样 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; 使用日志格式 access_log /usr/local/nginx/logs/access.log main; 补充说明:把log_format这段代码放在你nginx的http的定义段中,能够在下面的每个server中引用此格式。没必要在每一个server里面都去定义格式。 (3)修改SiteDomain=slog.fity.cn 网站域名 (4)修改LoadPlugin="decodeutfkeys",去掉注视,不然会出现中文乱码 (5)修改Lang="cn"语言为中文 (6)添加LIMITFLUSH=5000000尽可能大,可根据状况而定。增大AWStats的$LIMITFLUSH,减小磁盘临时文件读写 Flush history file on disk (unique url reach flush limit of 5000) ############# 可选 ################## 运行awstats的wwwroot目录中的awatsts.pl来测试一下 # /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=slog.fity.cn 若是看到相似下面的提示,就说明配置文件都正确了 From data in log file "/usr/local/nginx/logs/access_20150914.log"... Phase 1 : First bypass old records, searching new record... Searching new records from beginning of log file... Phase 2 : Now process new records (Flush history on disk after 20000 hosts)... Jumped lines in file: 0 Parsed lines in file: 95 Found 0 dropped records, Found 0 comments, Found 0 blank records, Found 0 corrupted records, Found 0 old records, Found 95 new qualified records. 看到以上显示,证实日志切割和 Awstats 都已经运行无误了。统计分析完成后,结果还在 Awstats 的数据库中。在 Apache 上,能够直接打开 Perl 程序的网页查看统计。 但本文开始时已经提到,Nginx 对 Perl 支持并很差,因此咱们要换个方法,利用 awstats 的工具将统计的结果生成静态文件,具体的步骤以下: 一、首先在 webroot 目录下建立一个文件夹。例:/usr/local/nginx/html/awstats # mkdir /usr/local/nginx/html/awstats 二、写一个脚本,按期执行让 Awstats 把静态页面生成到该目录中 #!/bin/bash /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \ -config=slog.fity.cn -lang=cn -dir=/usr/local/nginx/html/awstats \ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl ## 加上可执行权限,并测试 # chmod +x /root/awstats.sh # /root/awstats.sh ## 加入 crontab 00 1 * * * /root/awstats.sh 上述命令的具体意思以下: § /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具 § -update -config=slog.fity.cn 更新配置项 § -lang=cn 语言为中文 § -dir=/usr/local/nginx/html/awstats 统计结果输出目录 § -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径。 三、而后在你的nginx的配置文件中,在你想要安置awstats或默认的ip或域名的server段中,加入关于awstats和icon的两个目录配置。 server { listen 80; server_name localhost; location ~ ^/awstats/ { # awstats 静态页面目录 root /usr/local/nginx/html; # 靠, 不是 /usr/local/nginx/html/awstats 坑了很久 autoindex on; #能够目录浏览你的多个域名的目录用于分析 index index.html; access_log off; error_log off; } location ~ ^/icon/ { # 图标目录 root /usr/local/awstats/wwwroot; index index.html; access_log off; error_log off; } }
 

 

用浏览器查看到统计的详细结果  http://youhostname/awstats/awstats.www.slog.fity.cn.html

至此,使用 awstats 已能彻底支持 Nginx 的日志统计。

 

认证访问

 

通常站长都不肯随便让人知道本身站的真实流量,因此要把 Awstats 统计结果页面进行密码保护。Nginx 使用的是跟 Apache 同样的密码加密格式,这里须要用到 apache 自带的工具 htpasswd。

若是你在本机上默认装有 Apache,这你就只需在它的程序目录下运行

 

# /usr/bin/htpasswd -c /usr/local/nginx/passwd  admin          #用户名为admin

New password:            输入密码
Re-type new password:        重复输入
Adding password for user admin    建立成功


修改 nginx.conf 在 location 中加入(加粗部分):

server {
    listen       80;
    server_name  localhost;

    location ~ ^/awstats/ {         # awstats  静态页面目录
        root   /usr/local/nginx/html;    
        # 靠, 不是 /usr/local/nginx/html/awstats 坑了很久
        autoindex on;         #能够目录浏览你的多个域名的目录用于分析
        index  index.html;
        access_log off;
        error_log off;
        auth_basic "secret";                             #虚拟主机认证命名 
                auth_basic_user_file  /usr/local/nginx/passwd.db; #虚拟主机用户名密码认证数据库 
          
    }

    location ~ ^/icon/ {             # 图标目录
        root   /usr/local/awstats/wwwroot;
        index  index.html;
        access_log off;
        error_log off;
    }
}

而后,从新启动一下 nginx

 

 

若是你想给你的awstats加上访问密码,能够见这里:nginx为目录或网站加上密码认证

问题汇总

 

当再次输入网址时,在页面上只出现了一行字:
Content-type: text/html; charset=gb2312 Cache-Control: public Last-Modified: Sat Feb 11 13:14:37 2016 Expires: Sat Feb 11 13:14:37 2016 

若是出现,是由于浏览器不支持frame(彷佛是服务器端问题),请在页面上右键选择查看源文件,有这样一行字:Your browser does not support frames.You must set AWStats UseFramesWhenCGI parameter to 0 to see your reports.
好,如今去修改awstats.conf 配置文件,把UseFramesWhenCGI参数设成0。

 

 

http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/

http://blog.sina.com.cn/s/blog_8db76d6d0101342m.html

http://lodar.net/methods-of-apache-web-log-daily-rotation/

http://www.linuxde.net/2013/06/14027.html

http://www.cnblogs.com/cocowool/archive/2010/09/07/1820626.html

http://www.cnblogs.com/cocowool/archive/2010/09/10/1823504.html

相关文章
相关标签/搜索