ELK之使用Filebeat插件收集日志到Logstash

news/2024/7/27 8:11:46/文章来源:https://blog.csdn.net/zuihongyan518/article/details/132278805

对于Springboot项目接入ELK非常方便,对于非maven,非Spring项目来说就比较复杂,这个时候我们就可以使用Filebeat插件还完成日志的收集发送工作。

Filebeat介绍

Filebeat是用于转发和收集数据的轻量级工具,Filebeat可以监视指定的日志文件或位置,收集日志事件,并将数据转发到Elasticsearch或Logstash进行存储。

Filebeat的工作方式

启动Filebeat时,它将启动一个或多个输入;这些输入将在指定的位置中查找数据,对于Filebeat所找到的每个日志,Filebeat都会启动收集器。每个收集器都读取单个日志以获取新内容,并将新日志数据发送到libbeat,libbeat将聚集事件,并将聚集的数据发送到位Filebeat配置的输出。

Filebeat由两个主要组成部分:Prospector(探勘者)和Harvesters(矿车)。

prospector:  负责找到所有需要进行读取的数据源。
harvesters : 负责读取单个文件的内容,并将内容发送到output,负责文件的打开和关闭。

prospector和harvesters 这些组件一起工作来读取文件并将时间数据发送到指定的output。

Filebeat的工作原理

Filebeat可以记录每个日志文件的状态(可以理解成就是offset),并且把这个状态从注册表里更新到磁盘,offset是用来记录上一次Harvaster读取文件时,读取到的位置;这样来保证能把全部的日志都读取出来发给output。

如果,作为output的Elasticsearch或则Logstash变成了不可用,Filebeat将会把最后的文件读取位置保存下来,直到output重新可用的时候,快速地恢复文件数据的读取。

在Filebeat运行过程中,每个Prospector的状态信息都会保存在内存里,如果Filebeat出现了重启,完成重启后,会从注册表文件里恢复重启之间的状态信息,让Filebeat继续从之间已知的位置开始进行读取数据。

安装并配置Filebeat

1. 下载Filebeat

方式1:wget命令下载filebeat 
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.6.2-linux-x86_64.tar.gz

方式2:也可以直接下载到本地上传到服务器上

下载地址:Filebeat 7.6.2 | Elastic

 下载完成后使用命令将其解压:

tar -zxvf filebeat-6.6.0-linux-x86_64.tar.gz

2. 配置Filebeat

 进入解压目录

 [root@cenos7~] cd filebeat-7.6.2-linux-x86_64

#  备份filebest主配置文件

 [root@cenos7 filebeat-7.6.2-linux-x86_64] cp filebeat.yml  filebeat.yml.bak  

# vim命令打开配置文件 进行配置

 [root@cenos7 filebeat-7.6.2-linux-x86_64] vim filebeat.yml

配置日志采集

参看文档:https://www.elastic.co/guide/en/beats/filebeat/current/configuration-filebeat-options.html

#=========================== Filebeat inputs =============================filebeat.inputs:# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.- type: log# Change to true to enable this input configuration.enabled: true# Paths that should be crawled and fetched. Glob based paths.# 配置要收集日志文件路径paths:- /usr/local/test_app/log/test_app.logencoding: utf-8#- c:\programdata\elasticsearch\logs\*# Exclude lines. A list of regular expressions to match. It drops the lines that are# matching any regular expression from the list.exclude_lines: ['DEBUG','INFO'] #不收集 'DEBUG','INFO' 级别日志# Include lines. A list of regular expressions to match. It exports the lines that are# matching any regular expression from the list.include_lines: ['ERROR','Exception','^WARN'] #收集 'ERROR','Exception', 'WARN' 级别日志# Exclude files. A list of regular expressions to match. Filebeat drops the files that# are matching any regular expression from the list. By default, no files are dropped.#exclude_files: ['.gz$']......#================================ General =====================================# The name of the shipper that publishes the network data. It can be used to group
# all the transactions sent by a single shipper in the web interface.
#name:# The tags of the shipper are included in their own field with each
# transaction published.
#tags: ["service-X", "web-tier"]# Optional fields that you can specify to add additional information to the
# output.
fields:appname: oa_test  # key-value 添加额外字段 收集到日志中
fields_under_root: true......
#============================== Kibana =====================================# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:# Kibana Host# Scheme and port can be left out and will be set to the default (http and 5601)# In case you specify and additional path, the scheme is required: http://localhost:5601/path# IPv6 addresses should always be defined as: https://[2001:db8::1]:5601#host: "localhost:5601"# Kibana Space ID# ID of the Kibana Space into which the dashboards should be loaded. By default,# the Default Space will be used.#space.id:#============================= Elastic Cloud ==================================# These settings simplify using Filebeat with the Elastic Cloud (https://cloud.elastic.co/).# The cloud.id setting overwrites the `output.elasticsearch.hosts` and
# `setup.kibana.host` options.
# You can find the `cloud.id` in the Elastic Cloud web UI.
#cloud.id:# The cloud.auth setting overwrites the `output.elasticsearch.username` and
# `output.elasticsearch.password` settings. The format is `<user>:<pass>`.
#cloud.auth:#================================ Outputs =====================================# Configure what output to use when sending the data collected by the beat.#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:# Array of hosts to connect to.# hosts: ["localhost:9200"]# Protocol - either `http` (default) or `https`.#protocol: "https"# Authentication credentials - either API key or username/password.#api_key: "id:api_key"#username: "elastic"#password: "changeme"#----------------------------- Logstash output --------------------------------
output.logstash:# The Logstash hosts 配置你自己的Logstash  ip地址hosts: ["127.0.0.1:5044","127.0.0.1:5044"]# Optional SSL. By default is off.# List of root certificates for HTTPS server verifications#ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]# Certificate for SSL client authentication#ssl.certificate: "/etc/pki/client/cert.pem"# Client Certificate Key#ssl.key: "/etc/pki/client/cert.key"......#================================= Migration ==================================# This allows to enable 6.7 migration aliases
#migration.6_to_7.enabled: true

配置完成以后就可以启动filebeat。

 # 前台启动

 [root@cenos7 filebeat-7.6.2-linux-x86_64] ./filebeat -e -c filebeat.yml 

 # 后台启动
 [root@cenos7 filebeat-7.6.2-linux-x86_64] nohup ./filebeat -e -c filebeat.yml > logs/filebeat.log 2>&1 & 

3. Kibana检索

其他重要配置

一般java异常,堆栈信息比较多,换行展示的,但是Filebeat收集的时候按行收集的,这算是一个缺陷。不过我们可以配置进行全部收集。

例如,常见的Java堆栈日志:

上面文档表示很多行,在Kibana里也视为多个单独的文档,但实际这是一个异常日志,如果分开阅读会脱离上下文关系,不利于分析。

1. 配置合并日志

  ### Multiline options# Multiline can be used for log messages spanning multiple lines. This is common# for Java Stack Traces or C-Line Continuation# The regexp Pattern that has to be matched. The example pattern matches all lines starting with [multiline.pattern: '^\d{4}-\d{2}-\d{2}' # 匹配以 YYYY-MM-DD开头的行 # Defines if the pattern set under pattern should be negated or not. Default is false.multiline.negate: true # 是否匹配pattern的情况# Match can be set to "after" or "before". It is used to define if lines should be append to a pattern# that was (not) matched before or after or as long as a pattern is not matched based on negate.# Note: After is the equivalent to previous and before is the equivalent to to next in Logstashmultiline.match: after   # 将其追加到上一行之后 pattern + negate + match 组合成一条语意为: 如果匹配 YYYY-MM-DD HH:mm:ss 开头的行,则将其合并到当前行的上一行multiline.max_lines: 200  # 最多匹配多少行,如果超出最大行数,则丢弃多余的行(默认500)multiline.timeout: 6s # 一次合并事件的超时时间,默认为 5s

 添加上面的配置表明:非日期开头的日志 追加到 日期开头日志的后面  即:非匹配行追加到匹配行后面

核心参数是上述参数,以下是参数说明:

  1. multiline.pattern:希望匹配到的结果(正则表达式)
  2. multiline.negate:值为 true 或 false。使用 false 代表匹配到的行合并到上一行;使用 true 代表不匹配的行合并到上一行
  3. multiline.match:值为 after 或 before。after 代表合并到上一行的末尾;before 代表合并到下一行的开头
  4. multiline.max_lines:合并的最大行数,默认 500
  5. multiline.timeout:一次合并事件的超时时间,默认为 5s,防止合并消耗太多时间导致 filebeat 进程卡死

不好理解咱们整张表格,协助理解

可以看到成功收集到java堆日志。

2. 去除Filebeat自带的日志字段

Filebeat自带了很多字段,都不是必须的,而且没有必要保存

添加去除Filebeat自带日志属性的配置

#================================ Processors =====================================# Configure processors to enhance or manipulate events generated by the beat.
processors:- drop_fields:fields: ["log","host","input","agent","ecs"]ignore_missing: false#- add_host_metadata: ~#- add_cloud_metadata: ~#- add_docker_metadata: ~#- add_kubernetes_metadata: ~

这个配置是 把"log","host","input","agent","ecs"等这些字段属性去除,不在日志中收集展示

添加上配置后,需要重启Filebeat。

Filebeat官方配置文档: filebeat.reference.yml | Filebeat Reference [8.12] | Elastic

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_1007647.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端接口防止重复请求实现方案

虽然大部分的接口处理我们都是加了loading的&#xff0c;但又不能确保真的是每个接口都加了的&#xff0c;可是如果要一个接口一个接口的排查&#xff0c;那这维护了四五年的系统&#xff0c;成百上千的接口肯定要耗费非常多的精力&#xff0c;根本就是不现实的&#xff0c;所以…

王道机试C++第 5 章 数据结构二:队列queue和21年蓝桥杯省赛选择题Day32

目录 5.2 队列 1&#xff0e;STL-queue 课上演示&#xff1a; 基本代码展示&#xff1a; 2. 队列的应用 例:约瑟夫问题 No. 2 题目描述&#xff1a; 思路提示&#xff1a; 代码展示&#xff1a; 例&#xff1a;猫狗收容所 题目描述&#xff1a; 代码表示&#xff1…

阿里云的服务器迁移到腾讯云

第一次用在线迁移&#xff0c;说下我的感受&#xff1a; 总体说&#xff0c;整个迁移过程非常简单&#xff08;一个命令都不需要&#xff09;&#xff0c;操作确实很方便&#xff0c;迁移成功后的项目运行环境&#xff08;本人是通过宝塔安装的LNMP环境&#xff09;、网站配置、…

保持长期高效的七个法则(一)7 Rules for Staying Productive Long-Term(2)

If your system is going to be liberating rather than suffocating, however, you need to follow a few guidelines: 然而&#xff0c;如果你的系统想要解放而不是扼制&#xff0c;就需要遵循一些准则&#xff1a; Rule #1 - Your system needs to fit your work(not the ot…

抖音在线点赞任务发布接单运营平台PHP网站源码 多个支付通道+分级会员制度

介绍 1、三级代理裂变&#xff0c;静态返佣/动态返佣均可设置。&#xff08;烧伤制度&#xff09;。 2、邀请二维码接入防红跳转。 3、自动机器人做任务&#xff0c;任务时间可设置&#xff0c;机器人价格时间可设置。 4、后台可设置注册即送X天机器人。 5、不同级别会员使…

学习Android的第二十八天

目录 Android Service (服务) 线程 Service (服务) Service 相关方法 Android 非绑定 Service startService() 启动 Service 验证 startService() 启动 Service 的调用顺序 Android 绑定 Service bindService() 启动 Service 验证 BindService 启动 Service 的顺序 …

Day42-企业级网络存储NFS01

Day42-企业级网络存储NFS01 1. 什么是NFS&#xff1f;2. 为什么要用网络共享存储&#xff1f;3. 共享存储的种类4. NFS工作原理5. 环境准备6. NFS软件列表7. 安装8. 配置nfs9. 项目实践作业&#xff1a;10. ()权限 对应参数11. 在生产中配置NFS的重要技巧&#xff1a;12. 项目实…

R语言深度学习-1-深度学习入门(H2O包安装报错解决及接入/H2O包连接数据集)

本教程参考《RDeepLearningEssential》 1.1 深度学习概念 深度学习是机器学习的一个子集&#xff0c;它特别指的是那些试图模拟人脑工作原理的算法和技术。这种类型的机器学习通过使用多层的人工神经网络来学习和表示数据的内在规律和层次结构。深度学习已经在多个领域取得了…

华为OD技术C卷“测试用例执行计划”Java解答

描述 示例 算法思路1 整体思路是&#xff0c;先读取特性的优先级和测试用例覆盖的特性列表&#xff0c;然后计算每个测试用例的优先级&#xff0c;并将其与测试用例的索引存储到二维数组中。最后按照优先级和索引排序&#xff0c;输出测试用例的索引&#xff0c;即为执行顺序。…

Ajax学习笔记(一):原生AJAX、HTTP协议、AJAX案例准备工作、发送AJAX请求、AJAX 请求状态

目录 一、原生AJAX 1.1AJAX 简介 1.2 XML 简介 1.3 AJAX的特点 二、HTTP协议 三、AJAX案例准备工作 四、发送AJAX请求 1.发送GET请求 2.发送POST请求 3.JSON响应 IE缓存问题&#xff1a; 五、AJAX 请求状态 一、原生AJAX 1.1AJAX 简介 AJAX 全称为 Asynchronous …

强化学习工具箱(Matlab)

1、Get Started 1.1、MDP环境下训练强化学习智能体 MDP环境如下图 每个圆圈代表一个状态每个状态都有上或下的选择智能体从状态 1 开始智能体接收的奖励值为图中状态转移的值训练目标是最大化累计奖励 &#xff08;1&#xff09;创建 MDP 环境 创建一个具有 8 个状态和 2 …

[Kali] 安装Nessus及使用

在官方网站下载对应的 Nessus 版本:Download Tenable Nessus | TenableDownload Nessus and Nessus Managerhttp://www.tenable.com/products/nessus/select-your-operating-system这里选择 Kali 对应的版本 一、安装 Nessus 1、下载得到的是 deb 文件,与

html5cssjs代码 018颜色表

html5&css&js代码 018颜色表 一、代码二、效果三、解释 这段代码展示了一个基本的颜色表&#xff0c;方便参考使用&#xff0c;同时也应用了各种样式应用方式。 一、代码 <!DOCTYPE html> <html lang"zh-cn"> <head><title>编程笔记…

ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。

发生的错误信息&#xff1a; File "C:\Users\malongqiang\.conda\envs\ObjectDetection\lib\ssl.py", line 1309, in do_handshakeself._sslobj.do_handshake() ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。 分析原因&#xff1a; …

2024年了,关键词还重要吗?(川圣SEO)蜘蛛池

baidu搜索:如何联系八爪鱼SEO? baidu搜索:如何联系八爪鱼SEO? baidu搜索:如何联系八爪鱼SEO? 是的&#xff0c;关键词仍然非常重要。 无论在哪个年份&#xff0c;关键词都是搜索引擎优化&#xff08;SEO&#xff09;的重要组成部分&#xff0c;它们帮助搜索引擎理解网页…

电源常用电路—驱动电路详解

数字电源控制核心对输入输出参数进行采集后&#xff0c;利用控制算法进行分析从而产生PWM控制信号&#xff0c;PWM信号将经过驱动电路的进行功率放大和隔离&#xff0c;随后接入功率开关器件从而完成电源的输出控制。本篇将主要针对电源的驱动电路进行讲解。 一、驱动电路概述…

【论文阅读】

4. Analysis of Large-Scale Multi-Tenant GPU Clusters for DNN Training Workloads 出处&#xff1a;2019 USENIX-TAC 大规模多租户GPU集群对DNN训练工作负载的分析 主要工作&#xff1a;描述了Microsoft中一个多租户GPU集群两个月的工作负载特征&#xff0c;研究影响多租户…

WanAndroid(鸿蒙版)开发的第五篇

前言 DevEco Studio版本&#xff1a;4.0.0.600 WanAndroid的API链接&#xff1a;玩Android 开放API-玩Android - wanandroid.com 其他篇文章参考&#xff1a; 1、WanAndroid(鸿蒙版)开发的第一篇 2、WanAndroid(鸿蒙版)开发的第二篇 3、WanAndroid(鸿蒙版)开发的第三篇 …

[云原生] Prometheus自动服务发现部署

一、部署服务发现 1.1 基于文件的服务发现 基于文件的服务发现是仅仅略优于静态配置的服务发现方式&#xff0c;它不依赖于任何平台或第三方服务&#xff0c;因而也是最为简单和通用的实现方式。 Prometheus Server 会定期从文件中加载 Target 信息&#xff0c;文件可使用 YAM…

<JavaEE> 了解网络层协议 -- IP协议

目录 初识IP协议 什么是IP协议&#xff1f; IP协议中的基础概念 IP协议格式 图示 4bit版本号&#xff08;version&#xff09; 4bit头部长度&#xff08;headerlength&#xff09; 8bit服务类型&#xff08;TypeOfService&#xff09; 16bit总长度&#xff08;total l…