Filebeat使用模块收集日志

1.先决条件

在运行Filebeat模块之前:

  • 安装并配置Elastic stack
  • 完成Filebeat的安装
  • 检查Elasticsearch和Kibana是否正在运行,以及Elasticsearch是否准备好从Filebeat那里接收数据

 

2.配置Nginx模块

nginx模块解析Nginx创建的access和error日志

当你运行模块的时候,它在底层执行一些任务:

  • 设置默认的日志文件路径
  • 确保将每个多行日志事件作为单个事件发送
  • 使用ingest节点解析和处理日志行,将数据塑造成适合在Kibana中可视化的结构
  • 部署显示日志数据的dashboards

这个模块需要 ingest-user-agent 和 ingest-geoip 两个Elasticsearch插件

你可以在Elasticsearch主目录下运行下列命令来安装这些插件:

elasticsearch-plugin install ingest-geoip
elasticsearch-plugin install ingest-user-agent

然后,重启Elasticsearch

 

 启用nginx模块

filebeat modules enable nginx

 

修改/etc/filebeat/modules.d/nginx.yml文件

- module: nginx
  # Access logs
  access:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["/var/log/openresty/*.access.log"]

  # Error logs
  error:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["/var/log/openresty/*.error.log"]

 然后重启filebeat

查看启用或者禁用的模块列表

filebeat modules list

 3.如果要搭配Logstash使用

1.安装插件

/usr/share/logstash/bin/logstash-plugin install logstash-filter-geoip

2.编辑配置文件/etc/logstash/conf.d/beats.conf

input {
    beats {
        port => 5044
    }
}

filter {
    grok {
        match => { "message" => "%{HTTPDATE:timestamp}\|%{IP:remote_addr}\|%{IPORHOST:http_host}\|(?:%{DATA:http_x_forwarded_for}|-)\|%{DATA:request_method}\|%{DATA:request_uri}\|%{DATA:server_protocol}\|%{NUMBER:status}\|(?:%{NUMBER:body_bytes_sent}|-)\|(?:%{DATA:http_referer}|-)\|%{DATA:http_user_agent}\|(?:%{DATA:request_time}|-)\|"}
    }
    mutate {
        convert => ["status","integer"]
        convert => ["body_bytes_sent","integer"]
        convert => ["request_time","float"]
    }
    geoip {
        source=>"remote_addr"
    }
    date {
        match => [ "timestamp","dd/MMM/YYYY:HH:mm:ss Z"]
    }
    useragent {
        source=>"http_user_agent"
    }
}

output {
    elasticsearch {
      hosts => ["127.0.0.1:9200"]
            index => "nginx-%{+YYYY.MM.dd}"
    }
    stdout { codec => rubydebug }

然后启动logstash

 

参考:

https://www.cnblogs.com/cjsblog/p/9495024.html

 

 
posted @ 2018-12-13 13:43  侠客书生  阅读(2170)  评论(0编辑  收藏  举报