首页 > 技术文章 > 使用flume-ng聚合双活Nginx日志

linwinfan 2013-08-21 22:25 原文

前不久使用Keepalived搭建了Nginx双活代理服务器,以达到一个公网IP后支持多个云主机的多个域名网站的目的。完成后又想在这双活的Nginx上有所有访问网站的日志,之前有了解过Google Analytics, 及一些日志分析系统。后来终于找到并部署了几个开源的分析系统,包括AWStats,JAWStats及Piwik。使用它发现有一个问题比较烦,就是如何将2个Nginx的日志发送到分析服务器后合并分析。

 

一、需求

     合并多台服务器同一域名网站的访问日志后,定时导入网站分析系统,生成网站分析数据。

二、技术方案

     前后想了很多方法,包括自已写脚本加入Cron定时发送到分析服务器,Fluentd日志收集系统等许多方法,最终选定使用flume-ng完成该了该任务,相对其它来说我觉得他应是安装配置最简单的一种方法。

     1.在分析服务器上安装flume-ng,用以日志收集。首先安装JDK(完成后记住配置JAVA_HOME等环境变量).下载flume-ng最新版后解压后即可使用。使用前添加收集服务器配置,示例如下:

collector1.sources = AvroIn
collector1.sources.AvroIn.type = avro
collector1.sources.AvroIn.bind = 0.0.0.0
collector1.sources.AvroIn.port = 4545
collector1.sources.AvroIn.channels = mc1

collector1.channels = mc1
collector1.channels.mc1.type = memory
collector1.channels.mc1.capacity = 100

collector1.sinks = LocalOut

collector1.sinks.LocalOut.type = file_roll
collector1.sinks.LocalOut.sink.directory = /var/log/flume/collector1
collector1.sinks.LocalOut.sink.rollInterval = 0
collector1.sinks.LocalOut.channel = mc1
View Code

    完成后运行:

bin/flume-ng agent -c conf  -f /etc/flume/conf/collector1.conf -n collector1
View Code

   完成后即系统即会在端口4545收集日志数据,写入指定的目录文件中。

    2.在Nginx服务器同1中安装好JDK并解压flume-ng后,同样新建一Nginx日志发送配置,示例如下:

agent1.sources = ngrinder
agent1.sources.ngrinder.type = exec 
agent1.sources.ngrinder.command = tail -F /var/log/nginx/otrs/access.log
agent1.sources.ngrinder.channels = mc1

agent1.channels = mc1
agent1.channels.mc1.type = memory
agent1.channels.mc1.capacity = 100

agent1.sinks = avro-sink

agent1.sinks.avro-sink.type = avro
agent1.sinks.avro-sink.channel = mc1
agent1.sinks.avro-sink.hostname = 172.22.2.203
agent1.sinks.avro-sink.port = 4545
View Code

    3.启动收集日志。

bin/flume-ng agent -c conf -f /etc/flume/conf/agent1.conf -n agent1
View Code

    完成后,访问你要收集网站访问日志的网站,然后到收集服务器上,到配置好的日志收集目录,您将可以看到相关日志。

     4.将收集到的日志定时给AWStats分析,以完成网站访问分析。运行:crontab -e添加:

0 1 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.xxxxx.com
View Code

     使每天凌晨1点分析日志。

 

 

推荐阅读