Fluentd (td-agent) 日志收集系统

来源:互联网 发布:驱动安装软件 编辑:程序博客网 时间:2024/05/27 10:43

Fluentd是一个日志收集系统,它的特点在于其各部分均是可定制化的,你可以通过简单的配置,将日志收集到不同的地方。

目前开源社区已经贡献了下面一些存储插件:MongoDB, Redis, CouchDB,AmazonS3, Amazon SQS, Scribe, 0MQ, AMQP, Delayed, Growl等等。


安装

可参考http://docs.fluentd.org/categories/installation安装

此版本采用: Installingtd-agent for Redhat and CentOS  

http://docs.fluentd.org/articles/install-by-rpm

Fluentd 是由RubyC编写的,需要ruby进行,然而安装td-agent 是fluentd 的易安装版本,不用考虑太多的依赖关系。

1.首先please create /etc/yum.repos.d/td.repo with the followingcontents.

[treasuredata]
name=TreasureData
baseurl=http://packages.treasure-data.com/redhat/$basearch
gpgcheck=0
Then,you can install via yum command.

2. $ yumupdate
$ yuminstall td-agent

3. 安装完成后,可使用以下方式启动关闭服务。

$ /etc/init.d/td-agent start 

  $ /etc/init.d/td-agent stop 
$/etc/init.d/td-agent restart

4. 默认的 /etc/td-agent/td-agent.conf td-agent的配置文件

/var/log/td-agent/td-agent.log 为td-agent的日志文件

5. 查看td-agent的安装

6.查看ruby fluent插件的列表:ruby的安装路径在/usr/lib64/fluent/ruby/


fluent-plugin-tail-ex与fluent-plugin-tail-multiline为后期安装的插件,其他的为安装td-agent后默认安装的插件。

fluent-plugin-tail-ex:为输入扩展插件,支持对文件路径、日期的扩展

fluent-plugin-tail-multiline:为输入扩展插件,支持多行数据的收集,能够更好的收集异常信息。

5. 插件安装

   Fluent插件地址http://fluentd.org/plugin/

   两种安装方法:

1)  可以本地安装,下载gem安装包  https://rubygems.org/gems,推荐此方法

2)  ruby库远程安装

两种方法的安装命令为:$ /usr/lib64/fluent/ruby/bin/gem install 插件名称

6. 配置
首先我们编辑配置文件/etc/td-agent/td-agent.conf 中的source来设置日志来源

[plain] view plain copy
  1. <source>  
  2.  type tail  
  3.  format apache  
  4.  path /var/log/apache2/access_log  
  5. pos_file /var/log/apache2/access_log.pos  
  6.  tag mongo.apache  
  7. </source>  

其中:
type tail: tail方式是 Fluentd 内置的输入方式,其原理是不停地从源文件中获取增量日志,与linx命令tail相似,也可以使用其他输入方式如http、forward等输入,也可以使用输入插件,将 tail 改为相应的插件名称 如: type tail_ex  ,注意tail_ex为下划线。
format apache: 指定使用 Fluentd 内置的 Apache 日志解析器。可以自己配置表达式。
path /var/log/apache2/access_log: 指定收集日志文件位置。

Pos_file /var/log/apache2/access_log.pos:强烈建议使用此参数,access_log.pos文件可以自动生成,要注意access_log.pos文件的写入权限,因为要将access_log上次的读取长度写入到该文件,主要保证在fluentd服务宕机重启后能够继续收集,避免日志数据收集丢失,保证数据收集的完整性。

tag mongo.apache: 指定tag,tag被用来对不同的日志进行分类,与后面的标签match相匹配。
下面再来编辑输出配置,配置日志收集后存储到MongoDB中,也可以输出到其他组件如文件,转发等。

[plain] view plain copy
  1. <match mongo.**>  
  2.  # plugin type  
  3.  type mongo  
  4.  # mongodb db + collection  
  5.  database apache  
  6.  collection access  
  7.  # mongodb host + port  
  8.  host localhost  
  9.  port 27017  
  10.  # interval  
  11.  flush_interval 10s  
  12. </match>  

match标签后面可以跟正则表达式以匹配我们指定的tag,只有匹配成功的tag对应的日志才会运用里面的配置。配置中的其它项都比较好理解,看注释就可以了,其中flush_interval是用来控制多长时间将日志写入MongoDB一次。

7.高可用的配置:http://docs.fluentd.org/articles/high-availability


[plain] view plain copy
  1. # TCP input  
  2. <source>  
  3.   type forward  
  4.   port 24224  
  5. </source>  
  6.   
  7. # HTTP input  
  8. <source>  
  9.   type http  
  10.   port 8888  
  11. </source>  
  12.   
  13. # Log Forwarding  
  14. <match mytag.**>  
  15.   type forward  
  16.   
  17.   # primary host  
  18.   <server>  
  19.     host 192.168.0.1  
  20.     port 24224  
  21.   </server>  
  22.   # use secondary host  
  23.   <server>  
  24.     host 192.168.0.2  
  25.     port 24224  
  26.     standby  
  27.   </server>  
  28.   
  29.   # use longer flush_interval to reduce CPU usage.  
  30.   # note that this is a trade-off against latency.  
  31.   flush_interval 60s  
  32. </match>  

8.fluent对Java的支持:fluent-logger-java is a Java library, to record events via Fluentd, from Java application.

http://fluentd.org/releases/java/  可下载最新的jar

[java] view plain copy
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3. import org.fluentd.logger.FluentLogger;  
  4.   
  5. public class Main {  
  6.     private static FluentLogger LOG = FluentLogger.getLogger("app""192.168.0.1"24224);  
  7.     public static void main(String[] args) {  
  8.         // ...  
  9.         Map<String, String> data = new HashMap<String, String>();  
  10.         data.put("from""aaa");  
  11.         data.put("to""bbb");  
  12.         LOG.log("follow", data); //...  
  13.         FluentLogger.close();  
  14.     }  
  15. }  

配置fluentd服务器端/etc/td-agent/td-agent.conf

添加:

[plain] view plain copy
  1. <source>  
  2.  ##必须启动tcp端口,端口号为24224,不加port属性默认为24224  
  3.  type tcp  
  4.  port 24224  
  5. </source>  
  6. ##app.** 与java中的app匹配  
  7. <match app.**>  
  8.   ##匹配输出到/var/log/td-agent/td-agent.log  
  9.   type stdout  
  10. </match>  
查看td-agent.log输出结果:

2013-06-06 12:56:01 +0800 app.follow: {"to":"bbb","from":"aaa"}

0 0
原创粉丝点击