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

[经验分享] awstats 分析nginx 日志

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-7-28 09:39:45 | 显示全部楼层 |阅读模式
1

nginx 日志格式设定,去掉如下行#号

1
2
3
4
5
#vim /usr/local/nginx/conf/nginx.conf
log_format access '$remote_addr -$remote_user [$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent"$http_x_forwarded_for';
access_log log/access.log main;



参数说明:
$remote_addr与$http_x_forwarded_for:用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local:用来记录访问时间与时区;
$request:用来记录请求的url与http协议;
$status:用来记录请求状态;成功是200;
$body_bytes_sent:记录发送给客户端文件主体内容大小;
$http_referer:用来记录从哪个页面链接访问过来的;
$http_user_agent:记录客户端浏览器的相关信息;
nginx日志切割脚本
1
2
3
4
5
6
7
8
#vim /usr/local/nginx/nginx_log.sh

#!/bin/bash
logs_path="/usr/local/nginx/logs/" #设置日志文件存放目录
pid_path="/usr/local/nginx/logs/nginx.pid" #设置pid文件
mv${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
#重命名日志文件,将access.log切割为昨天的日志文件,因为要在每天零点切割,所有日志文件格式是前一天的日期,如access_20130728.log
kill-USR1 `cat${pid_path}` #向nginx主进程发信号重新打开日志



安装和配置awstats

1
2
3
4
5
6
7
8
9
10
11
12
yum方式安装
sudo yum -y install awstats
如果不能yum安装,请运行
sudo rpm -ivh http://packages.sw.be/rpmforge-r ... 2.el6.rf.x86_64.rpm
另外安装一下GeoIP,一会可以分析IP的国家
yum install GeoIP GeoIP-data GeoIP-devel perl-Geo-IP -y
如果安装不成功,可以运行如下方法
rpm -ivh https://epel.mirror.angkasa.id/p ... ease-6-8.noarch.rpm
yum install mod_geoip GeoIP GeoIP-devel GeoIP-data zlib-devel   

sudo chown -R sysadmin:sysadmin /var/lib/awstats/
sudo chown -R sysadmin:sysadmin /var/www/awstats/



执行tools目录中的awstats_configure.pl配置向导,创建一个新的统计
1
/usr/share/awstats/tools/awstats_configure.pl



将会有如下一些提示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-----> 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:
/home/sysadmin
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

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



回车之后下一个选项:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)

-----> 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.a.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.nginx.conf'
Configfile /etc/awstats/awstats.nginx.conf created.
-----> Add updateprocess inside a scheduler Sorry, configure.pl does not support automatic addto cron yet.
You can do it manually by adding the following command to yourcron: /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
Or if you have several config files and prefer having only onecommand: /usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...#按回车继续
A SIMPLE config file has been created: /etc/awstats/awstats.nginx.conf You should have a lookinside to check and change manually main parameters.
Youcan then manually update your statistics for'yuyuanchun.com' withcommand: > perl awstats.pl -update -config=nginx
Youcan also build static report pages for'nginx' with command:> perl awstats.pl -output=pagetype -config=nginx
PressENTER to finish... #回车完成配置文件的创建



默认会生成一个名为awstats.www.a.com.conf配置文件在/etc/awstats/目录下,修改该配置文件的日志位置
1
2
vim /etc/awstats/awstats.www.a.com.conf
LogFile="/usr/share/awstats/tools/logresolvemerge.pl /var/log/nginx/access_log* |"



这里是对应上面Nginx日志切割所生成的目录存放位置,注意awstats的年月日格式,-24表示昨天的日志,-0表示当前的,我这里因为要分析多种不同的日志文件,所以采用了*| 的方式
分析的执行顺序是:
Nginx产生日志->日志切割->Nginx继续产生日志->另存切割日志->交由Awstats统计->生成结果

默认awstats用于记录数据的目录在:
1
/var/lib/awstats



Nginx对Perl支持并不好,所以这里利用awstats的工具将统计的结果生成静态文件,首先创建一个文件,例:

1
mkdir  /var/www/awstats



生成静态文件:
1
2
/usr/share/awstats/tools/awstats_buildstaticpages.pl -update -config=www.a.com -lang=cn
-dir=/var/www/awstats -awstatsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl



/usr/share/awstats/tools/awstats_buildstaticpages.pl Awstats静态页面生成工具
-update -config=www.a.com 更新配置项
-lang=cn    语言为中文
-dir=/var/www/awstats  统计输出结果
-awstatsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl  Awstats 日志更新程序路径
修改nginx配置文件,添加日志分析结果站点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
server {
        listen 8080;
        server_name awstats.a.com;
        access_log /var/log/nginx/awstats.access_log main;
        error_log /var/log/nginx/awstats.error_log info;
        root /var/www/awstats;
        index index.html;

        # Restrict access
        #auth_basic "Restricted";
        #auth_basic_user_file /etc/awstats/htpasswd;

        # Dynamic stats.
        location ~ ^/cgi-bin/(awredir|awstats)\.pl {
                gzip off;
                include         fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME /usr/share/awstats/wwwroot/cgi-bin/fcgi.php;
                fastcgi_param X_SCRIPT_FILENAME /usr/share/awstats/wwwroot$fastcgi_script_name;
                fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;
                include fastcgi_params;
        }


        # Static awstats files: HTML files stored in DOCUMENT_ROOT/awstats/
        location /classes/ {
                alias /usr/share/awstats/wwwroot/classes/;
        }

        location /css/ {
        alias /usr/share/awstats/wwwroot/css/;
        }

        location /icon/ {
                alias /usr/share/awstats/wwwroot/icon/;
        }

        location /awstats-icon/ {
        alias /usr/share/awstats/wwwroot/icon/;

        }

        location /js/ {
                alias /usr/share/awstats/wwwroot/js/;
        }

}



重新加载nginx

1
2
sudo nginx -t    #检查nginx配置文件是否有误
sudo nginx -s reload    #加载



添加定时任务,每晚切割,分析nginx日志
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

/bin/bash /opt/scripts/filetransfer.sh
#=====================================================================

/usr/share/awstats/tools/awstats_buildstaticpages.pl -update -config=www.a.com -lang=cn -dir=/var/www/awstats -a
wstatsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl

/usr/share/awstats/tools/awstats_buildstaticpages.pl -update -config=wx.a.com -lang=cn -dir=/var/www/awstats -aw
statsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl

/usr/share/awstats/tools/awstats_buildstaticpages.pl -update -config=app.a.com -lang=cn -dir=/var/www/awstats -a
wstatsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl



1
2
crontab -e
01 00 * * * /bin/bash /opt/scripts/awstats.sh >> /tmp/awstats.log






运维网声明 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-250497-1-1.html 上篇帖子: awstats CGI模式下动态生成页面缓慢的改进 下篇帖子: 搭建web服务与awstats日志分析系统部署
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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