ELK nginx 오류 로그 수집

11322 단어 ELK
ELK nginx 오류 로그 수집
1. filebeat 채집 설정
1. nginx 서버 에 filebeat 설치
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.3.1-x86_64.rpm
yum localinstall filebeat-6.3.1-x86_64.rpm

2. filebeat 채집 파일 설정
vim /etc/filebeat/filebeat.yml
logging.level: info
logging.to_files: true
logging.files:
  path: /data/logs/filebeat
  name: filebeat.log
  keepfiles: 7
  permissions: 0644

filebeat.inputs:
- type: log
  enabled: true
  exclude_lines: ['\\x']
  fields:
    log-type: nginx-access-logs
  paths:
    - /data/logs_nginx/*.json.log

- type: log
  enabled: true
  fields:
    log-type: nginx-error-logs
  paths:
    - /data/logs_nginx/error.log

output.kafka:
  # initial brokers for reading cluster metadata
  hosts: ["kafka1:9092", "kafka2:9092", "kafka3:9092"]

  # message topic selection + partitioning
  topic: '%{[fields][log-type]}'
  partition.hash:
    reachable_only: false

  required_acks: 1
  compression: snappy
  max_message_bytes: 1000000

4. filebeat 시작
 systemctl start filebeat

2. logstash 필터 규칙 을 설정 하고 elasticsearch 에 저장 합 니 다.
1. nginx 오류 로그 grok 표현 식 추가
cd /usr/share/logstash/patterns/
vim nginx
NGINX_ERROR_LOG (?<timestamp>%{
     YEAR}[./-]%{
     MONTHNUM}[./-]%{
     MONTHDAY}[- ]%{
     TIME}) \[%{
     LOGLEVEL:severity}\] %{
     POSINT:pid}#%{NUMBER}: %{GREEDYDATA:errormessage}(?:, client: (?%{IP}|%{HOSTNAME}))(?:, server: %{IPORHOST:server}?)(?:, request: %{QS:request})?(?:, upstream: (?\"%{URI}\"|%{QS}))?(?:, host: %{QS:request_host})?(?:, referrer: \"%{URI:referrer}\")?

2. logstash 필터 nginx 로그 규칙 설정
cd /etc/logstash/conf.d
vim nginx-error.conf
input{
     
    kafka{
     
        bootstrap_servers => ["kafka1:9092,kafka2:9092,kafka3:9092"]
        client_id => "nginx-error-logs"
        group_id => "logstash"
        auto_offset_reset => "latest"
        consumer_threads => 10
        decorate_events => true 
        topics => ["nginx-error-logs"] 
        type => "nginx-error-logs"
        codec => json {
     charset => "UTF-8"} 
    }
}


filter {
     
  if [fields][log-type] == "nginx-error-logs" {
     
   grok {
     
       match => [ "message" , "%{NGINX_ERROR_LOG}"]
    }
    geoip {
     
        database =>"/usr/share/logstash/GeoLite2-City/GeoLite2-City.mmdb"
        source => "clientip"
    }
    date {
     
      timezone => "Asia/Shanghai"
      match => ["timestamp","yyyy/MM/dd HH:mm:ss"]
    }

  }
}



output {
     

  if [fields][log-type] == "nginx-error-logs" {
     
    elasticsearch {
     
      hosts => ["http://es1:9200","http://es2:9200","http://es3:9200"]
      index => "nginx-error-%{+YYYY.MM.dd}"
    }
  }

}

3. logstash 다시 시작
systemctl restart logstash

좋은 웹페이지 즐겨찾기