Filebeat使用模塊收集日誌

1.先決條件

在運行Filebeat模塊以前:html

  • 安裝並配置Elastic stack
  • 完成Filebeat的安裝
  • 檢查Elasticsearch和Kibana是否正在運行,以及Elasticsearch是否準備好從Filebeat那裏接收數據

 

2.配置Nginx模塊

nginx模塊解析Nginx建立的access和error日誌nginx

當你運行模塊的時候,它在底層執行一些任務:ruby

  • 設置默認的日誌文件路徑
  • 確保將每一個多行日誌事件做爲單個事件發送
  • 使用ingest節點解析和處理日誌行,將數據塑形成適合在Kibana中可視化的結構
  • 部署顯示日誌數據的dashboards

這個模塊須要 ingest-user-agent 和 ingest-geoip 兩個Elasticsearch插件elasticsearch

你能夠在Elasticsearch主目錄下運行下列命令來安裝這些插件:spa

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

而後,重啓Elasticsearch插件

 

 配置文件更名debug

cd /etc/filebeat/modules.d/
mv nginx.yml.disabled nginx.yml

修改nginx.yml文件rest

- 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日誌

查看啓用或者禁用的模塊列表code

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

 

參考:

http://www.javashuo.com/article/p-knvmcdqe-kz.html

相關文章
相關標籤/搜索