其他分享
首页 > 其他分享> > ELK——ElasticStack日志分析平台(未完待续)

ELK——ElasticStack日志分析平台(未完待续)

作者:互联网

ElasticStack日志分析平台

ELK日志采集与分析系统概述

ELK架构

在这里插入图片描述

Filebeat

Filebeat安装

#官网下载filebeat
[root@filebeat ~]# curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.13.2-linux-x86_64.tar.gz
#解压至/usr/local
[root@filebeat ~]# tar xf filebeat-7.13.2-linux-x86_64.tar.gz -C /usr/local/
[root@filebeat ~]# mv /usr/local/filebeat-7.13.2-linux-x86_64/ /usr/local/filebeat

Filebeat启动管理

[root@filebeat ~]# vim /usr/lib/systemd/system/filebeat.service
[Unit]
Description=Filebeat sends log files to Logstash or directly to Elasticsearch.
Wants=network-online.target
After=network-online.target

[Service]

ExecStart=/usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml
Restart=always

[Install]
WantedBy=multi-user.target

#建立系统进程
[root@filebeat ~]# systemctl daemon-reload
[root@filebeat ~]# systemctl start filebeat

Filebeat简单使用

[root@filebeat ~]# vim /tmp/access.log
112.195.209.90 - - [20/Feb/2018:12:12:14 +0800] "GET / HTTP/1.1" 200 190 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36" "-"
#备份配置文件
[root@filebeat ~]# cp /usr/local/filebeat/filebeat.yml /usr/local/filebeat/filebeat.yml.bak
[root@filebeat ~]# vim /usr/local/filebeat/filebeat.yml
filebeat.inputs:                      #输入模块,希望收集什么
- type: log                           #类型:日志
  enabled: true                       #开启手机日志
  paths:                              #日志路径
    - /tmp/*.log

#- type: filestream
#  enabled: false
#  paths:
#    - /var/log/*.log

filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml#安装路径 modules.d下有更多的规则
  reload.enabled: false               #当模块配置文件发生变化时,filebeat自身重启,影响收集日志过程,一般配置完成才启动
setup.template.settings:
  index.number_of_shards: 1           # 索引副本数量, 1 不产生副本

output.console:                       #添加 输出到终端屏幕上
  pretty: true                        #开启

#setup.kibana:
#output.elasticsearch:
#  hosts: ["localhost:9200"]

processors:                             #处理
  - add_host_metadata:                  #添加此主机的源数据信息到输出数据中,如 IP MAC OS 等信息
      when.not.contains.tags: forwarded 
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~

Filebeat模块测试

#启动时发生了报错
[root@filebeat ~]# /usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml
Exiting: data path already locked by another beat. Please make sure that multiple beats are not sharing the same data path (path.data).
#关闭filebeat即可,本机器已经存在filebeat启动,datapath被lock
[root@filebeat ~]# systemctl stop filebeat


[root@filebeat ~]# /usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml
{
  "@timestamp": "2021-07-17T05:33:45.381Z",
  "@metadata": {
    "beat": "filebeat",
    "type": "_doc",
    "version": "7.13.2"
  },
  "input": {
    "type": "log"
  },
  "host": {
    "id": "ad8a55213faa46babc18170804417b90",
    "containerized": false,
    "name": "filebeat",
    "ip": [
      "192.168.100.200",
      "fe80::ec53:d68d:60ea:b5e0"
    ],
    "mac": [
      "00:0c:29:ae:a5:a7"
    ],
    "hostname": "filebeat",
    "architecture": "x86_64",
    "os": {
      "type": "linux",
      "platform": "centos",
      "version": "7 (Core)",
      "family": "redhat",
      "name": "CentOS Linux",
      "kernel": "3.10.0-862.el7.x86_64",
      "codename": "Core"
    }
  },
  "agent": {
    "id": "33541cdc-c78e-4cf1-9181-e03db1ebdc36",
    "name": "filebeat",
    "type": "filebeat",
    "version": "7.13.2",
    "hostname": "filebeat",
    "ephemeral_id": "4f5cb4e0-47b3-4398-8574-8e36905aea10"
  },
  "ecs": {
    "version": "1.8.0"
  },
  "message": "112.195.209.90 - - [20/Feb/2018:12:12:14 +0800] \"GET / HTTP/1.1\" 200 190 \"-\" \"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36\" \"-\"",
  "log": {
    "offset": 0, #从日志文件什么地方开始取的,从第一行
    "file": {
      "path": "/tmp/access.log"
    }
  }
}
#==================================Logging================================

# Sets log level. The default log level is info.
#Available log levels are: error, warning, info,debug
#logging.level: debug
path.logs: /var/log/   #添加此行即可

专用日志搜集模块

[root@filebeat ~]# ls /usr/local/filebeat/modules.d
/usr/local/filebeat/filebeat modules disable 模块名
/usr/local/filebeat/filebeat modules enable 模块名

Nginx模块

[root@filebeat ~]# vim /var/log/access.log
123.127.39.50 - - [04/Nar/2021:10:50:28 +0800] "GET/logo.jpg HTTP/1.1" 200 14137 "http://81.68.233.173/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) ApplewebKit/537.36(KHTAL, like Gecko) Chrome/88.0.4324.192 Safari/537.36" "_"
[root@filebeat ~]# vim /var/log/error.log
2021/03/04 10:50:28 [error] 11396#0: *5 open() "/farm/bg.jpg" failed (2: No such file or directory), client: 123.127.39.50, server: localhost, request: "GET /bg.jpg HTTP/1.1",  host:"81.68.233.173", referrer: "http://81.68.233.173/"
[root@filebeat ~]# /usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml modules enable nginx
Enabled nginx
[root@filebeat ~]# ls /usr/local/filebeat/modules.d
nginx.yml...
- module: nginx
  access:
    enabled: true
  error:
    enabled: true
[root@filebeat ~]# vim /usr/local/filebeat/modules.d/nginx.yml
- module: nginx
  access:
    enabled: true
  error:
    enabled: true
    var.paths: ["/var/log/access.log","/var/log/error.log"]
- module: nginx
  access:
    enabled: true
  error:
    enabled: true
    var.paths: 
      - "/var/log/access.log*"
      - "/var/log/error.log*"
[root@filebeat filebeat]# pwd
/usr/local/filebeat
# -c 指定配置文件 -e 开启模块
[root@filebeat filebeat]# ./filebeat -c /usr/local/filebeat/filebeat.yml -e

配置output

output.console:
  pretty: true
./filebeat
output.console:
  codec.format:
    string: '%{[@timestamp]} %{[message]}'

其他输出目标

output.elasticsearch:
  hosts: ['http://es01:9200','http://es02:9200']
output.logstach:
  hosts: ["127.0.0.1:5044"]

重读日志文件

Exiting: data path already locked by another beat. Please make sure that multiple beats are not sharing the same data path (path.data).
[root@filebeat filebeat]# ps -ef | grep 'filebea[t]'
root       2322   2019  0 17:10 pts/2    00:00:00 ./filebeat -c /usr/local/filebeat/filebeat.yml -e

使用Processors(处理器)过滤和增强数据

去除日志中的某些行

processors:
  - drop_event:            #丢弃事件
      when:                #当
        regexp:            #正则表达式,告诉系统下面这段话带正则表达式
          message: "^DBG:" #message为自定义字段

向输出的数据中添加某些自定义字段

processors:
  - add_fields:
      target: project      #要添加的自定义字段key的名称
      fields:
        name: myproject
        id: '574734885120952459'
processors:
  - drop_fields:
      fields: ["field1","field2",...]
      ignore_missing: false
  - drop_fields:
      fields: ['input',"ecs.version"]

Logstach

Logstach安装

#下载
[root@filebeat ~]# curl -L -O https://artifacts.elastic.co/downloads/logstash/logstash-7.13.2-linux-x86_64.tar.gz
#解压至/usr/local
[root@filebeat ~]# tar xf logstash-7.13.2-linux-x86_64.tar.gz -C /usr/local/
[root@filebeat ~]# mv /usr/local/logstash-7.13.2/ /usr/local/logstash

测试运行

bin/logstash -e ''

未完待续

标签:ELK,filebeat,log,root,ElasticStack,未完待续,usr,日志,local
来源: https://blog.csdn.net/sixeleven611/article/details/117910939