设为首页 收藏本站
查看: 1322|回复: 0

[经验分享] 基于日志的站点统计系统的构建 Awstats

[复制链接]

尚未签到

发表于 2019-1-30 08:14:22 | 显示全部楼层 |阅读模式
先来点废话:通过构建的HTTP服务器将Web发布后,随着不断被访问,Apache会记下每个访客的基本信息。CentOS的情况下,这些信息通常保存在位于/var/log/httpd/下的相应日志文件中。但Apache单调的日志,除了提供给我们一些最基本的服务器被访信息、以及出错信息之外,我们很难单纯的通过日志文件得到更多、更系统的关于时间、规律性、地点方面的信息。这也使得很难通过单纯的日志去系统的分析流量。

  但通过Awstats,我们可以把Apache的日志作为一种有效资源,获得更加系统化的信息。

  实质上,Awstats就是一套分析系统,它将日志文件统计学式的进行分析与统计,然后提供一个能够反映规律性的界面给我们。Awstats并不是仅能应用于分析Apache的日志,在邮件服务器等等方面,它也被广泛的应用。但在这一节中,只介绍在Web服务器中运用Awstats建立统计系统的方法。
1,下载并安装 Awstats
wget http://nchc.dl.sourceforge.net/s ... ts-6.5-1.noarch.rpm  ← 下载Awstats的rpm包
rpm -ivh awstats-6.5-1.noarch.rpm 用RPM包安装。
2,配制awstats
/usr/local/awstats/tools/awstats_configure.pl  ← 运行初始配置脚本
----- AWStats awstats_configure 1.0 (build 1.6) (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:
/root
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 ← 选择y,同意安装到默认目录下
-----> 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  ← 指定Apache配置文件所在位置
-----> Check and complete web server config file '/etc/httpd/conf/httpd.conf'
Add 'Alias /awstatsclasses "/root/wwwroot/classes/"'
Add 'Alias /awstatscss "/root/wwwroot/css/"'
Add 'Alias /awstatsicons "/root/wwwroot/icon/"'
Add 'ScriptAlias /awstats/ "/root/wwwroot/cgi-bin/"'
Add '' directive
AWStats directives added to Apache config file.
-----> 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 ← 选择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.centospub.com ← 为统计对象创建代号(推荐以网站URL作为代号)
-----> 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.centospub.com.conf'
Config file /etc/awstats/awstats.www.centospub.com.conf created.
-----> Restart Web server with '/sbin/service httpd restart'  ←HTTP服务重新启动
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:
/root/wwwroot/cgi-bin/awstats.pl -update -config=www.centospub.com
Or if you have several config files and prefer having only one command:
/root/tools/awstats_updateall.pl now
Press ENTER to continue... ← 按回车继续进行配置
A SIMPLE config file has been created: /etc/awstats/awstats.www.centospub.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.centospub.com' with command:
> perl awstats.pl -update -config=www.centospub.com
You can also read your statistics for 'www.centospub.com' with URL:
> http://localhost/awstats/awstats.pl?config=www.centospub.com
Press ENTER to finish... ← 按回车结束初始配置

3,然后对刚刚创建的对象配置文件进行配
vi /etc/awstats/awstats.www.test.com.conf ← 修改对象配置文件(文件名与上一步初始配置中设置的域名对应)


4,再对httpd.conf中面向Awstats的部分进行必要设置。
vim /etc/httpd/conf/httpd.conf
#
# Directives to allow use of AWStats as a CGI
#
----------------------------------------------------
Alias /awstatsclasses "/root/wwwroot/classes/"
Alias /awstatscss "/root/wwwroot/css/"
Alias /awstatsicons "/root/wwwroot/icon/"
ScriptAlias /awstats/ "/root/wwwroot/cgi-bin/"
----------------------------------------------------

找到以上水平线间的语句群,对相应路径进行正确修改。变为下面水平线间状态:

----------------------------------------------------
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.
#
  ← 找到这一行,对相应路径进行正确修改
 ↓
  ← 变为此状态


5,重启httpd 服务器 使新的设置生效。
6对日志进行分析统计

vi /root/awstats.sh  ← 建立统计用脚本如下:

#!/bin/bash
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.centospub.com
(以上紫色字体部分的域名请根据实际情况设置)

[root@sample ~]# chmod 700 /root/awstats.sh   ← 赋予脚本可被执行的属性

[root@sample ~]# /root/awstats.sh   ← 运行脚本,开始进行统计(如日志量比较大的话,要花一段时间)

Update for config "/etc/awstats/awstats.www.centospub.com.conf"
With 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: 55
Found 52 dropped records,
Found 0 corrupted records,
Found 0 old records,


7,然后在客户端的浏览器上访问 http://服务器IP地址(或你的域名)/awstats/awstats.pl?config=www.centospub.com 即可看到详细的分析统计资料,如下图:
8,计划任务
crontab -e  ← 对定期任务执行时间表进行编辑,添加如下一行:

00 00 * * * /root/awstats.sh  ← 添加此行到其中,让统计分析在每天0点更新
9,因为流量统计数据相对是不对外开放的。所以我们需要做一个访问控制
vim /etc/httpd/conf/httpd.conf

    AuthType Basic
    AuthName "Restricted Files"
    AuthUserFile "/etc/httpd/conf/passwords" ← 密码和用户名存放的
    Require user admin    访问用户名



本文参考:http://www.centospub.com

  10,为awstats添加插件
  LoadPlugin="tooltips"
#鼠标移动帮助
LoadPlugin="decodeutfkeys"
#解决搜索关键字的乱码
LoadPlugin="qqhostinfo"
#增加QQ纯真ip库

  利用QQ的纯真库IP信息来显示中国化的IP来源
  cd /usr/local/awstats/wwwroot/cgi-bin/plugins/
  一般的类似插件都需要pm文件的支持,下载qqhostinof.pm 、qqwry.pl 将文件都放在awstats的cgi-bin/plugins下
  下载纯真版IP数据库,解压缩后,只使用QQWry.dat(http://www.cz88.net/fox/这里有最新的下载我下载的08年的)
  修改qqwry.pl,把./QQWry.dat改为${DIR}/plugins/QQWry.Dat
[root@www plugins]# vi qqwry.pl
  my $ipfile="/usr/local/awstats/wwwroot/cgi-bin/plugins/QQWry.Dat";
LogFile="/var/log/httpd/mylog.log"  ← 找到这一行,修改日至文件所在位置(指定Apache的日志文件)
 ↓
LogFile="/var/log/httpd/access_log"   ← 变为此状态

DirData="/var/lib/awstats"  ← 找到这一行,更改Awstats数据的保存位置
 ↓
DirData="."  ← 变为此状态,让数据与awstats.pl脚本在同目录下

Lang="auto"  ← 找到这一行,将auto改为cn
 ↓
Lang="cn"   ← 变为此状态,让语言默认为中文

SkipHosts=""  ← 找到这一行,在""之间添加无效访问规则
 ↓
SkipHosts="127.0.0.1 REGEX[^192\.168\.]"   ← 变为此状态,本地及内部的访问不做分析统计

LevelForWormsDetection=0  ← 找到这一行,将0改为2
 ↓
LevelForWormsDetection=2  ← 变为此状态,将来自Worm的访问也不做分析统计





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-669379-1-1.html 上篇帖子: 使用awstats自动分析Nginx日志(二) 下篇帖子: httpd服务之部署AWStats日志分析系统
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表