day21(ELK第二天)

1、ELK第一天知识回顾


1.ELK各代表什么软件?
   - Elasticsearch
   - Filebeat
   - Logstash
   - Kibana

2.传统方式收集日志的弊端
   - 时间长,效率低,字段不能拆分,不能单独显示

3.ELK收集日志的架构

4.日志流转流程

5.安装部署filebeat
   - input    ----->nginx
   - output    ----->es
   - ES       ---->kibana

6.收集普通nginx日志有哪些不足
- 日志字段内容不能拆分,不能单独显示

7. 把nginx日志修改为JSON格式

8. filebeat配置文件添加2行JSON参数

9. 删除旧索引,重启filebeat

10. 在kibana里添加索引,测试数据
-时间范围    鼠标选择区间    quick time




2、现在的架构这不合理的地方以及合理的规划


1.按天生成太费事,不能聚合几天的数据
2.filebeat默认创建的索引不好区分是什么业务,不能区分域名



合理的规划:

1.索引按月生成
2.按照域名

3、怎么修改filebeat默认索引格式


索引格式为:服务名称-域名-日志格式-时间(按月)

1)vim /etc/filebeat/filebeat.yml

filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true
output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  index: "nginx-www-access-%{[beat.version]}-%{+yyyy.MM}"
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true

2)systemctl restart filebeat.service 



image.png

4、nginx修改多域名多日志


1)vim  /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
   log_format   json '{ "time_local": "$time_local", '
                           '"remote_addr": "$remote_addr", '
                           '"referer": "$http_referer", '
                           '"request": "$request", '
                           '"status": $status, '
                           '"bytes": $body_bytes_sent, '
                           '"agent": "$http_user_agent", '
                           '"x_forwarded": "$http_x_forwarded_for", '
                           '"up_addr": "$upstream_addr",'
                           '"up_host": "$upstream_http_host",'
                           '"upstream_time": "$upstream_response_time",'
                           '"request_time": "$request_time"'
    ' }';
    access_log  /var/log/nginx/access.log  json;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
}


2) [root@db01 /etc/nginx/conf.d]# cat bbs.conf 
server {
    listen 80;
    server_name  www.oldboy.com;
    location / {
        root /html/www;
        index index.html;
    }
    access_log  /var/log/nginx/www_access.log  json;
}


3)[root@db01 /etc/nginx/conf.d]# cat bbs.conf 
server {
    listen 80;
    server_name  bbs.oldboy.com;
    location / {
        root /html/bbs;
        index index.html;
    }
    access_log  /var/log/nginx/bbs_access.log  json;
}


4)[root@db01 /etc/nginx/conf.d]# cat blog.conf 
server {
    listen 80;
    server_name  blog.oldboy.com;
    location / {
        root /html/blog;
        index index.html;
    }
    access_log  /var/log/nginx/blog_access.log  json;
}

5)mkdir /html/{www,blog,bbs} -p
   echo "db01 www" > /html/www/index.html
   echo "db01 bbs" > /html/bbs/index.html
   echo "db01 blog" > /html/blog/index.html
   chown -R nginx:nginx /html/
   nginx -t
   systemctl restart nginx 


6)filebeat配置文件
vim /etc/filebeak/filebeak.yml

filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/bbs_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["bbs"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/blog_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["blog"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/www_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["www"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  indices:
    - index: "nginx_www_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "www"
    - index: "nginx_bbs_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "bbs"
    - index: "nginx_blog_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "blog"
    - index: "nginx_error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "error"

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

systemctl restart filebeat.service 


image.png

image.png

5、收集tomcat日志


1.安装tomcat
yum install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp tomcat-javadoc -y

2.修改tomcat配置文件为json格式
vim /etc/tomcat/server.xml

第139行替换为:
pattern="{"clientip":"%h","ClientUser":"%l","authenticated":"%u","AccessTime":"%t","method":"%r","status":"%s","SendBytes":"%b","Query?string":"%q","partner":"%{Referer}i","AgentVersion":"%{User-Agent}i"}"/>

3.重启tomcat
systemctl restart tomcat

4.修改filebeat配置文件
filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/bbs_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["bbs"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/blog_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["blog"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/www_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["www"]

- type: log
  enabled: true 
  paths:
    - /var/log/tomcat/localhost_access_log.*.txt
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["tomcat"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  indices:
    - index: "nginx_www_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "www"
    - index: "nginx_bbs_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "bbs"
    - index: "nginx_blog_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "blog"
    - index: "nginx_error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "error"
    - index: "tomcat_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "tomcat"

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


systemctl restart filebeat.service 


image.png
image.png

6、收集elasticsearch日志

cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/bbs_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["bbs"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/blog_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["blog"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/www_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["www"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]
- type: log
  enabled: true
  paths:
    - /var/log/tomcat/localhost_access_log.*.txt
  tags: ["tomcat"]
- type: log
  enabled: true
  paths:
    - /var/log/elasticsearch/linux58.log
  tags: ["java"]
  multiline.pattern: '^\['
  multiline.negate: true
  multiline.match: after

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  indices:
    - index: "nginx_www_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "www"
    - index: "nginx_bbs_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "bbs"
    - index: "nginx_blog_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "blog"
    - index: "nginx_error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "error"
    - index: "tomcat_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "tomcat"
    - index: "es_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "java"

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


systemctl restart filebeat.service 


image.png

image.png

你可能感兴趣的:(day21(ELK第二天))