部署ELK日志分析系统5-Filebeat

1 Filebeat简介

Filebeat是一个日志文件转发工具,在服务器上安装后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件并转发这些信息到elasticsearch或者logstarsh中存放。

1.1 工作流程

当你开启filebeat程序的时候,它会启动一个或多个探测器(prospectors)去检测你指定的日志目录或文件,对于探测器找出的每一个日志文件,filebeat启动收割进程(harvester),每一个收割进程读取一个日志文件的新内容,并发送这些新的日志数据到处理程序(spooler),处理程序会集合这些事件,最后filebeat会发送集合的数据到你指定的地点。
部署ELK日志分析系统5-Filebeat

2. Filebeat配置

2.1 下载使用

根据系统来下载安装filebeat。

https://www.elastic.co/downloads/past-releases/

2.2 配置修改

修改配置文件filebeat.yml
部署ELK日志分析系统5-Filebeat

filebeat.prospectors:
- input_type: log
  paths:
    - /usr/local/nginx/logs/access_ods.log   #日志文件,可以使用通配符*
    - /usr/local/nginx/logs/access_gateway.log
  encoding: utf-8
  tail_files: true  #tail读取文件模式
  tags: ["nginx","access"]  #额外添加的标签
output.logstash:
  hosts: ["10.253.128.4:5044"]  #logstash beats地址及端口
  compression_level: 3
  loadbalance: true

2.3 nginx配置修改

修改nginx配置文件conf/nginx.conf,增加或修改为如下配置

#日志输出格式
  log_format xxxx  '{"@timestamp":"$time_iso8601",'
                    '"host":"$server_addr",'
                    '"clientip":"$remote_addr",'
                    '"size":$body_bytes_sent,'
                    '"responsetime":$request_time,'
                    '"upstreamtime":"$upstream_response_time",'
                    '"upstreamhost":"$upstream_addr",'
                    '"http_host":"$host",'
                    '"url":"$uri",'
                    '"type":"xxxx",' #代理的项目名,根据项目定义
                    '"referer":"$http_referer",'
                    '"agent":"$http_user_agent",'
                    '"status":"$status"}';
  location /xxxx {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            access_log logs/access_xxxx.log xxxx;  #日志输出并指定上面自定义的输出格式
        }