nginx日志采集(filebeat->logstash->es)

目录

nginx日志输出json格式配置,修改nginx.conf

filebeat配置

logstash配置


  • nginx日志输出json格式配置,修改nginx.conf

...
http {
...
  log_format json '{"@timestamp":"$time_iso8601",'
                    '"@version":1,'
                    '"host":"$server_addr",'
                    '"client":"$remote_addr",'
                    '"size":$body_bytes_sent,'
                    '"responsetime":$request_time,'
                    '"domain":"$host",'
                    '"url":"$uri",'
                    '"status":"$status"}';
    #access.log路径                
   access_log /app/rpa/nginx/logs/access.log  json;}
...
  • filebeat配置,修改filebeat.yml

    filebeat.inputs:
    
    - type: log
      enabled: true
      paths:
        - /app/rpa/nginx/logs/access.log
      ignore_older: 5m
      fields:
        log_resource: nginx
        filetype: nginx-access
      #fields_under_root: true
      tags: ["nginx"]
    
    - type: log
      enabled: true
      paths:
        - /app/rpa/nginx/logs/error.log
      ignore_older: 5m
      fields:
        log_resource: nginx
        filetype: nginx-error
      tags: ["nginx"]
    
    filebeat.config.modules:
      # Glob pattern for configuration loading
      path: ${path.config}/modules.d/*.yml
    
      # Set to true to enable config reloading
      reload.enabled: false
    
    setup.template.settings:
      index.number_of_shards: 1
    
    setup.kibana:
    
    output.logstash:
      # logstash的ip,需要按照实际配置
      hosts: ["ip:port"]
    
    processors:
      - add_host_metadata: ~
      - add_cloud_metadata: ~
      - add_docker_metadata: ~
      #- add_kubernetes_metadata: 
    
    • logstash配置,修改logstash.conf

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.

input {
  beats {
    port => 5044
  }
}
filter {
  if [fields][log_resource] == "nginx"{
    ruby {
      code => '
        require "json"
        json_data = event.get("message")
        begin
          parsed_data = JSON.parse(json_data)
          event.set("requestTime", parsed_data["@timestamp"])
          event.set("host", parsed_data["host"])
          event.set("client", parsed_data["client"])
          event.set("size", parsed_data["size"])
          event.set("responsetime", parsed_data["responsetime"])
          event.set("domain", parsed_data["domain"])
          event.set("url", parsed_data["url"])
          event.set("status", parsed_data["status"])
        end
      '
    }
    # 1. 增加一个字段,计算timestamp+8小时,处理索引少8h问题
 ruby { 
    code => "event.set('index_date', event.get('@timestamp').time.localtime + 8*60*60)" 
 } 
 mutate { 
    convert => ["index_date", "string"] 
    gsub => ["index_date", "T([\S\s]*?)Z", ""]  
 }      
    fingerprint {
        source => ["host", "client", "domain", "url", "status","message"]
        target => "[@metadata][generated_id]"
        method => "MURMUR3"
        concatenate_sources => true
    }
  }
}
# elasticsearch的ip,按照实际情况配置
output {
if [fields][filetype] == "nginx-access"{
    elasticsearch {
      hosts => ["ip:port"]
      index => "nginx-access-logs-%{index_date}"
      document_id => "%{[@metadata][generated_id]}"
    }
  } else if [fields][filetype] == "nginx-error"{
    elasticsearch {
      hosts => ["ip:port"]
      index => "nginx-error-logs-%{index_date}"
      document_id => "%{[@metadata][generated_id]}"
    }
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值