计算机系统应用教程网站

网站首页 > 技术文章 正文

ELK之filebeat基础篇

btikc 2024-09-22 01:07:42 技术文章 18 ℃ 0 评论


总览

功能

Filebeat是用于转发和集中日志数据的轻量级传送程序。作为服务器上的代理安装,Filebeat监视您指定的日志文件或位置,收集日志事件,并将它们转发到Elasticsearch或 Logstash进行索引。

工作方式

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

快速开始

安装

https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation.html

liunx tar

curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.7.0-linux-x86_64.tar.gz
tar xzvf filebeat-7.7.0-linux-x86_64.tar.gz

rpm

curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.7.0-x86_64.rpm
# wget https://mirrors.huaweicloud.com/filebeat/7.7.0/filebeat-7.7.0-x86_64.rpm
sudo rpm -vi filebeat-7.7.0-x86_64.rpm

配置

/etc/filebeat/filebeat.reference.yml /etc/filebeat/filebeat.yml

# 配置文件输入
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/*.log
    #- c:\programdata\elasticsearch\logs\*

# 存储数据到es
output.elasticsearch:
  hosts: ["myEShost:9200"]
  username: "filebeat_internal"
  password: "YOUR_PASSWORD" 
setup.kibana:
  host: "mykibanahost:5601"
  username: "my_kibana_user"  
  password: "YOUR_PASSWORD"

模板加载

加载模板

setup.template.name: "your_template_name"
setup.template.fields: "path/to/fields.yml"

覆盖模板

setup.template.overwrite: true

关闭自动加载模板

setup.template.enabled: false

自动修改索引名

output.elasticsearch.index: "customname-%{[agent.version]}-%{+yyyy.MM.dd}"
setup.template.name: "customname"
setup.template.pattern: "customname-*"

自动设置kibana仪表盘

./filebeat setup --dashboards

启动

sudo chown root filebeat.yml 
sudo ./filebeat -e

工作原理

haverster

收割者负责读取单个文件的内容。收割机逐行读取每个文件,然后将内容发送到输出。每个文件启动一个收割机。收割机负责打开和关闭文件,这意味着在收割机运行时文件描述符保持打开状态。如果在收集文件时将其删除或重命名,Filebeat将继续读取该文件。这样做的副作用是磁盘上的空间将保留,直到收割机关闭为止。默认情况下,Filebeat保持文件打开直到close_inactive到达。

关闭收割机有以下后果:

  • 关闭文件处理程序,如果在收割机仍在读取文件时删除了文件,则释放了基础资源。
  • 只有在scan_frequency经过之后,才会再次开始文件的收集。
  • 如果在收割机关闭时移动或删除文件,则文件的收割将不会继续。

要控制收割机何时关闭,请使用close_*配置选项。

input

如果输入类型为log,则输入将在驱动器上找到与定义的全局路径匹配的所有文件,并为每个文件启动收集器。每个输入都在其自己的Go例程中运行。

filebeat 如何保持文件状态

Filebeat保留每个文件的状态,并经常将状态刷新到注册表文件中的磁盘。该状态用于记住收割机正在读取的最后一个偏移量,并确保发送所有日志行。如果无法到达输出(例如Elasticsearch或Logstash),则Filebeat会跟踪发送的最后几行,并在输出再次变为可用时继续读取文件。在Filebeat运行时,状态信息也保存在内存中,用于每个输入。重新启动Filebeat时,将使用注册表文件中的数据来重建状态,并且Filebeat会在最后一个已知位置继续每个收割机。

对于每个输入,Filebeat会保留找到的每个文件的状态。由于可以重命名或移动文件,因此文件名和路径不足以标识文件。对于每个文件,Filebeat都存储唯一的标识符以检测文件是否以前被收获过。

如果您的用例涉及每天创建大量新文件,则可能会发现注册表文件变得太大。有关可以设置为解决此问题的配置选项的详细信息,请参阅注册表文件太大。

如何确保 at-least once

Filebeat保证事件将至少一次传递到配置的输出,并且不会丢失数据。Filebeat之所以能够实现此行为,是因为它在注册表文件中存储了每个事件的传递状态。

在定义的输出被阻止并且尚未确认所有事件的情况下,Filebeat将继续尝试发送事件,直到输出确认已接收到事件为止。

如果Filebeat在发送事件的过程中关闭,则它不会在关闭之前等待输出确认所有事件。重新启动Filebeat时,将再次发送发送到输出但在Filebeat关闭之前未确认的所有事件。这样可以确保每个事件至少发送一次,但是最终可能会将重复的事件发送到输出。您可以通过设置shutdown_timeout选项将Filebeat配置为在关闭之前等待特定的时间。

配置详解

多个input

filebeat.inputs:
- type: log
  paths:
    - /var/log/system.log
    - /var/log/wifi.log
- type: log
  paths:
    - "/var/log/apache2/*"
  fields:
    apache: true
  fields_under_root: true

input类型

  • Azure eventhub
  • Cloud Foundry
  • Container
  • Docker
  • Google Pub/Sub
  • HTTP JSON
  • Kafka
  • Log
  • MQTT
  • NetFlow
  • Office 365 Management Activity API
  • Redis
  • s3
  • Stdin
  • Syslog
  • TCP
  • UDP

常规配置

# 注册表根路径 默认为${path.data}/registry
filebeat.registry.path: registry
# 注册表的文件权限 默认0600 最大0640
filebeat.registry.file_permissions: 0600
# 控制注册表项何时写入磁盘的超时值(刷新)。当未写入的更新超过此值时,它将触发对磁盘的写入。当registry.flush设置为0s时,成功发布每一批事件后,将注册表写入磁盘。默认值为0s。
filebeat.registry.flush: 0
# 为指向旧的注册表文件
filebeat.registry.migrate_file: /path/to/old/registry_file
# 包含其他输入配置文件的目录的完整路径。每个配置文件都必须以结尾.yml。
filebeat.config_dir: path/to/configs
# Filebeat在关闭之前等待发布者完成发送事件的时间,然后Filebeat关闭。
filebeat.shutdown_timeout: 5s
# 节点名
name: "my-shipper"
# 节点标签
tags: ["service-X", "web-tier"]
# 您可以指定可选字段以将其他信息添加到输出中。字段可以是标量值,数组,字典或它们的任何嵌套组合。默认情况下,您在此处指定的字段将被分组fields到输出文档中的子词典下。要将自定义字段存储为顶级字段,请将fields_under_root选项设置为true。
fields: {project: "myproject", instance-id: "574734885120952459"}
# 如果将此选项设置为true,则自定义字段将作为顶级字段存储在输出文档中,而不是分组在fields子词典下。如果自定义字段名称与其他字段名称冲突,则自定义字段将覆盖其他字段。
fields_under_root: true
fields:
  instance_id: i-10a64379
  region: us-east-1

配置文件加载

Filebeat可以为输入和模块加载外部配置文件,从而使您可以将配置分为多个较小的配置文件。有关详细信息,请参见“ 输入配置”和“ 模块配置”部分。

对于输入配置,您path可以在文件的filebeat.config.inputs部分中指定选项 filebeat.yml

filebeat.config.inputs:
  enabled: true
  path: inputs.d/*.yml

找到的每个文件都必须包含一个或多个输入定义的列表。

- type: log
  paths:
    - /var/log/mysql.log
  scan_frequency: 10s

- type: log
  paths:
    - /var/log/apache.log
  scan_frequency: 5s

对于模块配置,请在文件path的filebeat.config.modules部分中指定选项 filebeat.yml。默认情况下,Filebeat会在modules.d目录中加载启用的模块配置 。

filebeat.config.modules:
  enabled: true
  path: ${path.config}/modules.d/*.yml

您可以将Filebeat配置为在发生更改时动态重新加载外部配置文件。此功能可用于作为外部配置文件加载的输入和模块配置 。您不能使用此功能来重新加载主filebeat.yml配置文件。

要配置此功能,请指定路径(Glob)来监视配置更改。当通过Glob找到的文件更改时,新输入和/或模块将根据配置文件中的更改启动和停止。

此功能在容器环境中特别有用,在容器环境中,一个容器用于跟踪在同一主机上其他容器中运行的服务的日志。

要启用动态配置重载,请在或部分下指定path和reload选项。例如:filebeat.config.inputs``filebeat.config.modules

filebeat.config.inputs:
  enabled: true
  path: configs/*.yml
  reload.enabled: true
  reload.period: 10s

path

定义要检查更改的文件的Glob。

reload.enabled

设置true为时,启用动态配置重新加载。

reload.period

指定检查文件更改的频率。请勿将设置period为小于1s,因为文件的修改时间通常以秒为单位存储。将设置period为小于1s将导致不必要的开销。

OUTPUT

  • Elasticsearch
  • Logstash
  • Kafka
  • Redis
  • File
  • Console
  • Elastic Cloud

elasticsearch

example:

output.elasticsearch:
  hosts: ["https://localhost:9200"]
  index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
  ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]
  ssl.certificate: "/etc/pki/client/cert.pem"
  ssl.key: "/etc/pki/client/cert.key"

https:

output.elasticsearch:
  hosts: ["https://localhost:9200"]
  username: "filebeat_internal"
  password: "YOUR_PASSWORD"

api key

output.elasticsearch:
  hosts: ["https://localhost:9200"]
  api_key: "KnR6yE41RrSowb0kQ0HWoA"

协议指定

output.elasticsearch:
  hosts: ["localhost"]
  protocol: "https"
  username: "filebeat_internal"
  password: "YOUR_PASSWORD"

集群

output.elasticsearch:
  hosts: ["10.45.3.2:9220", "10.45.3.1:9230"]
  protocol: https
  path: /elasticsearch

index

output.elasticsearch:
  hosts: ["http://localhost:9200"]
  index: "%{[fields.log_type]}-%{[agent.version]}-%{+yyyy.MM.dd}"

pipeline

output.elasticsearch:
  hosts: ["http://localhost:9200"]
  pipeline: my_pipeline_id
output.elasticsearch:
  hosts: ["http://localhost:9200"]
  pipelines:
    - pipeline: "warning_pipeline"
      when.contains:
        message: "WARN"
    - pipeline: "error_pipeline"
      when.contains:
        message: "ERR"
output.elasticsearch:
  hosts: ["http://localhost:9200"]
  pipelines:
    - pipeline: "%{[fields.log_type]}"
      mappings:
        critical: "sev1_pipeline"
        normal: "sev2_pipeline"
      default: "sev3_pipeline"

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表