awstats 日志分析是使用perl语言编写的日志分析工具,它的很多功能都是靠perl编写的脚本实现的。

它可以统计您站点的如下信息:  (摘自百度百科)

一:访问量,访问次数,页面浏览量,点击数,数据流量等

二:精确到每月、每日、每小时的数据

三:访问者国家

四:访问者IP

五:Robots/Spiders的统计

六:访客持续时间

七:对不同Files type 的统计信息

八:Pages-URL的统计

九:访客操作系统浏览器等信息

十:其它信息(搜索关键字等等)


awstat的工作流程:

    用户访问web界面apache会产生日志信息,日志信息中包含了用户的ip地址,浏览器型号,操作系统等信息,awstats会收集apache访问产生的日志信息进行分析,生成报表,存储在数据库中,这个数据库是awstats自己的数据存放方式,不需要第三方软件,随后将报表信息通过cgi以web的方式,呈现给管理员



一:安装软件

    1.yum安装apache服务

[root@www ~]# yum -y install httpd

    2.下载awstats软件包。(这个在个人搭建的ftp里面下载的,文章附件里会提供)

 
[root@www ~]# lftp 192.168.100.100
lftp 192.168.100.100:~> cd tools/
lftp 192.168.100.100:/tools> get awstats-7.3.tar.gz 
2897463 bytes transferred                                
lftp 192.168.100.100:/tools> bye

    3.解压awstats。软件是免安装的,下载下来解压移动到指定位置就可以

[root@www ~]# tar zxf awstats-7.3.tar.gz 
[root@www ~]# mv awstats-7.3 /usr/local/awstats
[root@www ~]# cd /usr/local/awstats/tools/
[root@www tools]# ll
总用量 176
-rw-r--r-- 1 1000 1000 19641 1月  29 2014 awstats_buildstaticpages.pl
-rw-r--r-- 1 1000 1000 25979 1月  29 2014 awstats_configure.pl
-rw-r--r-- 1 1000 1000 12604 1月  29 2014 awstats_exportlib.pl
-rw-r--r-- 1 1000 1000  5382 1月  29 2014 awstats_updateall.pl
-rw-r--r-- 1 1000 1000 16353 1月  29 2014 geoip_generator.pl
-rw-r--r-- 1 1000 1000   855 11月  4 2013 httpd_conf
-rw-r--r-- 1 1000 1000 32591 1月  29 2014 logresolvemerge.pl
-rw-r--r-- 1 1000 1000 27761 1月  29 2014 maillogconvert.pl
drwxr-xr-x 2 1000 1000  4096 11月  4 2013 nginx
-rw-r--r-- 1 1000 1000  9755 1月  29 2014 urlaliasbuilder.pl
drwxr-xr-x 2 1000 1000  4096 1月  29 2014 webmin
drwxr-xr-x 2 1000 1000  4096 11月  4 2013 xslt

    4.授权:文件是可执行的脚本文件,执行它需要给授权

[root@www tools]# chmod +x ./*
[root@www tools]# ll
总用量 176
-rwxr-xr-x 1 1000 1000 19641 1月  29 2014 awstats_buildstaticpages.pl
-rwxr-xr-x 1 1000 1000 25979 1月  29 2014 awstats_configure.pl
-rwxr-xr-x 1 1000 1000 12604 1月  29 2014 awstats_exportlib.pl
-rwxr-xr-x 1 1000 1000  5382 1月  29 2014 awstats_updateall.pl
-rwxr-xr-x 1 1000 1000 16353 1月  29 2014 geoip_generator.pl
-rwxr-xr-x 1 1000 1000   855 11月  4 2013 httpd_conf
-rwxr-xr-x 1 1000 1000 32591 1月  29 2014 logresolvemerge.pl
-rwxr-xr-x 1 1000 1000 27761 1月  29 2014 maillogconvert.pl
drwxr-xr-x 2 1000 1000  4096 11月  4 2013 nginx
-rwxr-xr-x 1 1000 1000  9755 1月  29 2014 urlaliasbuilder.pl
drwxr-xr-x 2 1000 1000  4096 1月  29 2014 webmin
drwxr-xr-x 2 1000 1000  4096 11月  4 2013 xslt

二:配置服务:

    1.执行脚本awstats_configure.pl 这个是交互的配置脚本,提示我们输入信息

            提示输入确认信息的(Y/N)Y 都确认

[root@www tools]# ./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
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    ##这里输入httpd的配置文件位置
-----> Check and complete web server config file '/etc/httpd/conf/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
-----> 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:
>       ##这里输入主机名或者域名 
-----> 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):
>    ##这里提示输入awstats的配置保存位置,回车默认保存在/etc/awstats
-----> Create config file '/etc/awstats/awstats.www.leslie.com.conf'
 Config file /etc/awstats/awstats.www.leslie.com.conf created.
-----> Restart Web server with '/sbin/service httpd restart'
停止 httpd:                                               [失败]
正在启动 httpd:                                           [确定]
-----> 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.leslie.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.leslie.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.leslie.com' with command:
> perl awstats.pl -update -config=www.leslie.com
You can also read your statistics for 'www.leslie.com' with URL:
> http://localhost/awstats/awstats.pl?config=www.leslie.com
Press ENTER to finish...    ##提示可以通过上面的url访问到日志分析页面  回车


    2.再修改刚才执行脚本生成的配置文件,在上一步输入的配置文件保存位置:/etc/awstats

            logfile=apache的日志文件

            dirdata=awstats生成的信息文件夹位置

[root@www tools]# vim /etc/awstats/awstats.www.leslie.com.conf 
 50  LogFile="/var/log/httpd/access_log"
203   DirData="/var/lib/awstats"

    3.新建数据目录,上一步指定的文件夹

[root@www tools]# mkdir /var/lib/awstats

    4.执行收集日志的脚本,它会读取apached的日志信息,然后分析生成报表,存放到/var/lib/awstats数据目录下。


[root@www tools]# /usr/local/awstats/tools/awstats_updateall.pl now
Running '"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -update -config=www.leslie.com -configdir="/etc/awstats"' to update config www.leslie.com
Create/Update database for config "/etc/awstats/awstats.www.leslie.com.conf" by AWStats version 7.3 (build 20140126)
From data in log file "/var/log/httpd/access_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: 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.
[root@www tools]# /etc/init.d/httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:                                           [确定]

    生成计划任务,让它每隔一段时间,执行一次,更新数据。

[root@www tools]# echo "*/5 * * * *  /usr/local/awstats/tools/awstats_updateall.pl now" \
 >>/var/spool/cron/root

    其实就是分析apache的日志信息,日志信息中包含用户的ip地址,浏览器,操作信息等信息,

    awstats通过分析,生成数据文件。最终通过cgi程序,以web页面的方式呈现给管理员。

[root@www tools]# cat /var/log/httpd/access_log 
192.168.100.11 - - [15/Aug/2017:20:42:30 +0800] "GET / HTTP/1.1" 403 5039 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
192.168.100.11 - - [15/Aug/2017:20:42:33 +0800] "GET / HTTP/1.1" 403 5039 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
192.168.100.11 - - [15/Aug/2017:20:42:49 +0800] "GET /awstats/awstats.pl?config=www.leslie.com HTTP/1.1" 200 1033 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
192.168.100.11 - - [15/Aug/2017:20:42:49 +0800] "GET /awstats/awstats.pl?config=www.leslie.com&framename=mainleft HTTP/1.1" 200 10745 "http://192.168.100.150/awstats/awstats.pl?config=www.leslie.com" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
192.168.100.11 - - [15/Aug/2017:20:42:49 +0800] "GET /awstats/awstats.pl?config=www.leslie.com&framename=mainright HTTP/1.1" 200 64026 "http://192.168.100.150/awstats/awstats.pl?config=www.leslie.com" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"

三:测试

    用浏览器访问awstats的日志分析界面:

    

wKioL1mSiAHifEC5AAJRJgcvfbI373.png

通过以上方式访问日志分析界面,发现url太长 不方便。

四:编写优化界面:

    当访问192.168.100.150/awstats.html时候就自动链接到:

    http://192.168.100.150/awstats/awstats.pl?config=www.leslie.com 这个url


[root@www ~]# vi /var/www/html/awstats.html
[root@www ~]# cat /var/www/html/awstats.html
<html>
  <head>
    <meta http-equiv="refresh" content="0; url=http://192.168.100.150/awstats/awstats.pl?config=www.leslie.com" />
  </head>
  <body></body>
</html>



wKioL1mSilHChJJ8AAG-fGD2L34508.png