Elk

005.ELK收集Nginx日志

2020-04-16  本文已影响0人  CoderJed

1. ELK收集Nginx普通格式的日志

1.1 测试服务器架构

1.2 ab工具使用

yum install httpd-tools -y

# -n 总共发送多少条请求,注意,最后"/"一定要写,否则命令无法执行
# -c 多少条请求发送一次
ab -c 10 -n 100 http://10.0.0.100:80/

[root@node01 log]# tail -f /var/log/nginx/access.log 
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.0.0.100 - - [16/Apr/2020:19:03:40 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"

ab工具用于批量发送HTTP请求到指定的URL,是一个压力测试工具,这里使用它来生成Nginx的日志

1.3 filebeat配置

1.4 Kibana WEB-UI 配置

2. ELK收集Nginx Json格式的日志

3. ELK收集多台Nginx服务器的日志

3.1 测试服务器架构

3.2 部署过程

# 使用3个服务器发送请求
[root@node01 ~]# ab -c 5 -n 5 http://10.0.0.101:80/test
[root@node01 ~]# ab -c 5 -n 5 http://10.0.0.102:80/test
[root@node02 ~]# ab -c 5 -n 5 http://10.0.0.100:80/test
[root@node02 ~]# ab -c 5 -n 5 http://10.0.0.102:80/test
[root@node03 ~]# ab -c 5 -n 5 http://10.0.0.100:80/test
[root@node03 ~]# ab -c 5 -n 5 http://10.0.0.101:80/test
GET _cat/indices
# 数据增加了30条
green open nginx-2020.04             2l7iUDU9SpWDxN96ui2DhQ 5 1 630 0   1.8mb 921.4kb

4. Nginx正常日志与错误日志拆分

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["access"]
- type: log
  enabled: true
  # 错误日志不需要使用json格式,因为我们很少对错误日志进行聚合分析
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]

output.elasticsearch:
  hosts: ["10.0.0.100:9200","10.0.0.101:9200","10.0.0.102:9200"]
  indices:
  - index: "nginx-access-%{+yyyy.MM}"
    when.contains:
      tags: "access"
  - index: "nginx-error-%{+yyyy.MM}"
    when.contains:
      tags: "error"

setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true

setup.template.settings:
  # 设置目标index的shard个数
  index.number_of_shards: 3
# 设置kibana的IP和端口
setup.kibana:
  host: "10.0.0.100:5601"
GET _cat/indices
green open nginx-error-2020.04       723oaOL3SamTcJId6E--9Q 5 1 1011 0   1.5mb 738.8kb
green open nginx-access-2020.04      v-9G7VLeREKvfh9kg-Wi3g 5 1   30 0 394.6kb 197.3kb

5. 使用filebeat自带的nginx module收集nginx日志

filebeat配置

filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: true
  reload.period: 10s

output.elasticsearch:
  hosts: ["10.0.0.100:9200","10.0.0.101:9200","10.0.0.102:9200"]
  indices:
    - index: "nginx_access-%{+yyyy.MM}"
      when.contains:
        fileset.name: "access"
    - index: "nginx_error-%{+yyyy.MM}"
      when.contains:
        fileset.name: "error"
setup.template.name: "nginx"
setup.template.pattern: "nginx_*"
setup.template.enabled: false
setup.template.overwrite: true
setup.template.settings:
  index.number_of_shards: 3
setup.kibana:
  host: "10.0.0.100:5601"

查看filebeat自带的模块

[root@node01 ~]# filebeat modules list
Enabled:

Disabled:
apache2
auditd
elasticsearch
haproxy
icinga
iis
kafka
kibana
logstash
mongodb
mysql
nginx
osquery
postgresql
redis
suricata
system
traefik

修改nginx模块的配置

[root@node01 ~]# cat /etc/filebeat/modules.d/nginx.yml.disabled 
- module: nginx
  access:
    enabled: true
    var.paths: ["/var/log/nginx/access.log"]
  error:
    enabled: true
    var.paths: ["/var/log/nginx/error.log"]

激活nginx模块

激活后原来的配置文件nginx.yml.disabled变为了nginx.yml

[root@node01 ~]# filebeat modules enable nginx
Enabled nginx
[root@node01 ~]# filebeat modules list
Enabled:
nginx

Disabled:
apache2
auditd
elasticsearch
haproxy
icinga
iis
kafka
kibana
logstash
mongodb
mysql
osquery
postgresql
redis
suricata
system
traefik

nginx还是使用默认的日志格式

access_log  /var/log/nginx/access.log main;

安装ingest-user-agent插件和ingest-geoip插件

/usr/share/elasticsearch/bin/elasticsearch-plugin install ingest-user-agent
/usr/share/elasticsearch/bin/elasticsearch-plugin install ingest-geoip
wget https://artifacts.elastic.co/downloads/elasticsearch-plugins/ingest-user-agent/ingest-user-agent-6.6.0.zip
wget https://artifacts.elastic.co/downloads/elasticsearch-plugins/ingest-geoip/ingest-geoip-6.6.0.zip
/usr/share/elasticsearch/bin/elasticsearch-plugin install file:///root/ingest-geoip-6.6.0.zip 
/usr/share/elasticsearch/bin/elasticsearch-plugin install file:///root/ingest-user-agent-6.6.0.zip

[root@node03 ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin install file:///root/ingest-user-agent-6.6.0.zip
-> Downloading file:///root/ingest-user-agent-6.6.0.zip
[=================================================] 100%   
-> Installed ingest-user-agent
[root@node03 ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin install file:///root/ingest-geoip-6.6.0.zip
-> Downloading file:///root/ingest-geoip-6.6.0.zip
[=================================================] 100%   
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.lang.RuntimePermission accessDeclaredMembers
* java.lang.reflect.ReflectPermission suppressAccessChecks
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed ingest-geoip

说明:

测试

GET _cat/indices

green open nginx_access-2020.04 7ibKAbFGQx66-a86s_53SQ 5 1 25 0 568.9kb 284.4kb
green open nginx_error-2020.04  bt-yYMQBTbqyZdBvmAzkRQ 5 1 15 0 275.9kb   145kb

注意,给nginx_error创建index pattern时,Time Filter field name 选择read_timestamp,而nginx_access选择@timestamp

可以看到,filebeat内置的nginx模块配合解析User-agent的插件ingest-user-agent-6.6.0.zip以及解析IP的插件ingest-geoip-6.6.0.zip帮我们把nginx的普通日志做了很细力度的解析,并且自动保存成JSON格式,但是error日志还是使用message来表示一整行日志

上一篇 下一篇

猜你喜欢

热点阅读