Elastic/日志分析

ElasticSearch+Logstash+Kibana+re

2019-04-03  本文已影响0人  linjiajiam

一、背景

二、环境软件准备

  1. 安装jdk1.8
  2. 关闭防火墙
  3. 禁用selinux
    vi /etc/selinux/config, 修改成SELINUX=disabled即可。重启后生效。
  4. 服务器IP


    image.png
  5. 此处我演示的是收集nginx日志。

三、安装配置软件

一. 安装配置Redis

参考这个教程

二. 安装配置Filebeat.
  1. 下载Filebeat,此处我统一下载到/opt目录下,下载网址可以到官网上查找。我下载的是rpm包,这个安装包方便设置开机启动。注意Filebeat是安装在需要采集日志的服务器上,比如你的Nginx服务器,或者Tomcat服务器等。
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.7.0-x86_64.rpm
  1. 安装
rpm -ivh filebeat-6.7.0-x86_64.rpm 

3.配置Filebeat

vi /etc/nginx/ngxin.conf
log_format json '{"@timestamp": "$time_iso8601", '
                              '"time": "$time_iso8601", '
                               '"remote_addr": "$remote_addr", '
                               '"remote_user": "$remote_user", '
                               '"body_bytes_sent": "$body_bytes_sent", '
                               '"request_time": "$request_time", '
                               '"status": "$status", '
                               '"host": "$host", '
                               '"request": "$request", '
                               '"request_method": "$request_method", '
                               '"uri": "$uri", '
                               '"http_referrer": "$http_referer", '
                               '"body_bytes_sent":"$body_bytes_sent", '
                               '"http_x_forwarded_for": "$http_x_forwarded_for", '
                               '"http_user_agent": "$http_user_agent" '
               '}';
access_log  /var/log/nginx/access-json.log  json;

user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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 '{"@timestamp": "$time_iso8601", '
                              '"time": "$time_iso8601", '
                               '"remote_addr": "$remote_addr", '
                               '"remote_user": "$remote_user", '
                               '"body_bytes_sent": "$body_bytes_sent", '
                               '"request_time": "$request_time", '
                               '"status": "$status", '
                               '"host": "$host", '
                               '"request": "$request", '
                               '"request_method": "$request_method", '
                               '"uri": "$uri", '
                               '"http_referrer": "$http_referer", '
                               '"body_bytes_sent":"$body_bytes_sent", '
                               '"http_x_forwarded_for": "$http_x_forwarded_for", '
                               '"http_user_agent": "$http_user_agent" '
               '}';

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
vi /etc/filebeat/filebeat.yml
###################### Filebeat Configuration Example #########################

# This file is an example configuration file highlighting only the most common
# options. The filebeat.reference.yml file from the same directory contains all the
# supported options with more comments. You can use it as a reference.
#
# You can find the full configuration reference here:
# https://www.elastic.co/guide/en/beats/filebeat/index.html

# For more available modules and options, please see the filebeat.reference.yml sample
# configuration file.

#=========================== Filebeat inputs =============================

filebeat.inputs:

- type: log

  enabled: true

  paths:
    - /var/log/nginx/access-json.log   #指明读取文件的位置
#============================= Filebeat modules ===============================

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: false

  # Period on which files under path should be checked for changes
  #reload.period: 10s

#==================== Elasticsearch template setting ==========================

setup.template.settings:
  index.number_of_shards: 3
  #index.codec: best_compression
  #_source.enabled: false

#================================ Outputs =====================================

# Configure what output to use when sending the data collected by the beat.

#-------------------------- Redis output ------------------------------
output.redis:
   hosts: ["192.168.1.110:6379"]   #输出到redis的机器
   password: "123456"
   key: "filebeat:test16"   #redis中日志数据的key值ֵ
   db: 0
   timeout: 5


#================================ Processors =====================================

# Configure processors to enhance or manipulate events generated by the beat.

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~



  1. 启动Filebeat
systemctl start filebeat   #启动
systemctl enable filebeat     # 设置开机启动
systemctl status filebeat  
  1. 正常情况下这时应该能在redis 中看到日志。如果看不到,可以到/var/log/filebeat文件夹中,查看日志,排除故障。
三. 安装配置Elasticsearch
  1. 下载Elasticsearch-6.7.0。此处我统一下载到/opt目录下,下载网址可以到官网上查找。我下载的是rpm包,这个安装包方便设置开机启动
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.0.rpm
  1. 安装
rpm -ivh elasticsearch-6.7.0.rpm
  1. 配置ES
vi /etc/elasticsearch/elasticsearch.yml
path.data: /mnt/ELK/data  #数据存储目录,可以不修改,看个人情况,此处修改是因为我这个目录挂在的磁盘空间大
path.logs: /mnt/ELK/logs  #日志存储目录,可以不修改,看个人情况
network.host: 192.168.1.110 #监听本地ip,防止外网恶意访问9200端口
http.port: 9200    #http监听端口
groupadd elk          # 添加用户组
useradd -g elk elk    # 添加用户到指定用户组
passwd elk            # 为elk用户设置密码
chown -R elk /etc/elasticsearch/  #将安装的ES文件夹赋权限给elk用户
chown -R elk /usr/share/elasticsearch #将安装的ES文件夹赋权限给elk用户
chown -R elk /usr/lib/systemd/system/elasticsearch.service  # 后台开机命令赋权限
chown -R elk /var/log/elasticsearch/
chown -R elk /var/run/elasticsearch/
chown -R elk /etc/sysconfig/elasticsearch
chown -R elk /mnt/ELK/data  #将数据存储目录赋权限给elk用户
chown -R elk /mnt/ELK/logs  #将日志存储目录赋权限给elk用户
vi /usr/lib/systemd/system/elasticsearch.service
# 修改如下
User=elk
Group=elk
systemctl daemon-reload
systemctl enable elasticsearch.service   # 设置开机启动
systemctl start elasticsearch.service #启动
systemctl status elasticsearch.service  #查看运行状态
su root # 切换到root 用户
vi /etc/security/limits.conf   # 修改
# limits.conf文件中,增加如下内容
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
vi /etc/sysctl.conf 
# sysctl.conf 文件中,增加如下内容
vm.max_map_count = 655360
sysctl -p   #执行命令立即生效
curl -l http://localhost:9200
image.png
四. 安装配置Logstash
  1. 下载Logstash。此处我统一下载到/opt目录下,下载网址可以到官网上查找。我下载的是rpm包,这个安装包方便设置开机启动
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.7.0.rpm
  1. 安装
rpm -ivh logstash-6.7.0.rpm
  1. 安装GeoLite2,用来分析访问客户端IP归属地
cd /opt/
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
tar -zxvf GeoLite2-City.tar.gz
mv GeoLite2-City_20190402/ GeoLite2-City/
  1. 配置Logstash
vi /etc/logstash/conf.d/nginx16-access.conf
input {
    redis {
        data_type =>"list"
        key =>"filebeat:test16"
        host =>"192.168.1.110"
        port => 6379
        password => "123456"
        threads => "8"
        db => 0
        #codec => json
        }
}

filter {
    #在json化之前,使用mutte对\\x字符串进行替换,防止以下错误:ParserError: Unrecognized character escape 'x' (code 120)
    mutate {
        gsub => ["message", "\\x", "\\\x"]
    }
    json {
        source => "message"
        remove_field => ["beat","message"]
    }
    #使用geoip库定位ip
    geoip {
        source => "remote_addr" #nginx日志中外部访问ip对应字段
        database => "/opt/GeoLite2-City/GeoLite2-City.mmdb"
        #去掉显示geoip显示的多余信息
        remove_field => ["[geoip][latitude]", "[geoip][longitude]", "[geoip][country_code]", "[geoip][country_code2]", "[geoip][country_code3]", "[geoip][timezone]", "[geoip][continent_code]", "[geoip][region_code]", "[geoip][ip]"]
        target => "geoip"
     }
    mutate { 
        convert => [ "[geoip][coordinates]", "float" ] 
    }
}


output {
    elasticsearch {
        hosts => ["192.168.1.110:9200"]      
        index => "logstash-test16-nginx-access-%{+yyyy.MM.dd}"         #注意此处索引名称,一定要以logstash开头命名,后者地图功能不可用(mapping)
    }
}

  1. 启动
systemctl enable logstash   # 设置开机启动
systemctl start logstash    #启动
systemctl status logstash     #查看运行状态
五. 安装配置Kibana
  1. 下载Kibana
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.7.0-x86_64.rpm
  1. 安装
rpm -ivh kibana-6.7.0-x86_64.rpm
  1. 配置
vi /etc/kibana/kibana.yml 
# Kibana is served by a back end server. This setting specifies the port to use.
server.port: 5601

# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
# The default is 'localhost', which usually means remote machines will not be able to connect.
# To allow connections from remote users, set this parameter to a non-loopback address.
server.host: "0.0.0.0"

# Enables you to specify a path to mount Kibana at if you are running behind a proxy.
# Use the `server.rewriteBasePath` setting to tell Kibana if it should remove the basePath
# from requests it receives, and to prevent a deprecation warning at startup.
# This setting cannot end in a slash.
#server.basePath: ""

# Specifies whether Kibana should rewrite requests that are prefixed with
# `server.basePath` or require that they are rewritten by your reverse proxy.
# This setting was effectively always `false` before Kibana 6.3 and will
# default to `true` starting in Kibana 7.0.
#server.rewriteBasePath: false

# The maximum payload size in bytes for incoming server requests.
#server.maxPayloadBytes: 1048576

# The Kibana server's name.  This is used for display purposes.
#server.name: "your-hostname"

# The URLs of the Elasticsearch instances to use for all your queries.
elasticsearch.hosts: ["http://192.168.1.110:9200"]
  1. 启动
systemctl enable kibana   # 设置开机启动
systemctl start kibana #启动
systemctl status kibana  #查看运行状态
  1. 配置kibana界面
上一篇 下一篇

猜你喜欢

热点阅读