开始使用Filebeat

认识Beats

Beats是用于单用途数据托运人的平台。它们以轻量级代理的形式安装,并将来自成百上千台机器的数据发送到Logstash或Elasticsearch。

(画外音:通俗地理解,就是采集数据,并上报到Logstash或Elasticsearch)

Beats对于收集数据非常有用。它们位于你的服务器上,将数据集中在Elasticsearch中,Beats也可以发送到Logstash来进行转换和解析。

为了捕捉(捕获)数据,Elastic提供了各种Beats:

Beats可以直接(或者通过Logstash)将数据发送到Elasticsearch,在那里你可以进一步处理和增强数据,然后在Kibana中将其可视化。

Filebeat

第1步:安装Filebeat

第2步:配置Filebeat

配置文件:filebeat.yml

为了配置Filebeat:

1. 定义日志文件路径

对于最基本的Filebeat配置,你可以使用单个路径。例如:

python 复制代码
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/*.log

在这个例子中,获取在/var/log/*.log路径下的所有文件作为输入,这就意味着Filebeat将获取/var/log目录下所有以.log结尾的文件。

为了从预定义的子目录级别下抓取所有文件,可以使用以下模式:/var/log/*/*.log。这将抓取/var/log的子文件夹下所有的以.log结尾的文件。它不会从/var/log文件夹本身抓取。目前,不可能递归地抓取这个目录下的所有子目录下的所有.log文件。

(画外音:

假设配置的输入路径是/var/log/*/*.log,假设目录结构是这样的:

那么只会抓取到2.log和3.log,而不会抓到1.log和4.log。因为/var/log/aaa/ccc/1.log和/var/log/4.log不会被抓到。

2. 如果你发送输出目录到Elasticsearch(并且不用Logstash),那么设置IP地址和端口以便能够找到Elasticsearch:

python 复制代码
output.elasticsearch:
    hosts: ["192.168.1.42:9200"]

3. 如果你打算用Kibana仪表盘,可以这样配置Kibana端点:

python 复制代码
setup.kibana:
      host: "localhost:5601"

4. 如果你的Elasticsearch和Kibana配置了安全策略,那么在你启动Filebeat之前需要在配置文件中指定访问凭据。例如:

python 复制代码
output.elasticsearch:
      hosts: ["myEShost:9200"]
      username: "filebeat_internal"
      password: "{pwd}" 
setup.kibana:
      host: "mykibanahost:5601"
      username: "my_kibana_user"  
      password: "{pwd}"

第3步:配置Filebeat以使用Logstash

如果你想使用Logstash对Filebeat收集的数据执行额外的处理,那么你需要将Filebeat配置为使用Logstash。

python 复制代码
output.logstash:
      hosts: ["127.0.0.1:5044"]

第4步:在Elasticsearch中加载索引模板

在Elasticsearch中,索引模板用于定义设置和映射,以确定如何分析字段。(画外音:相当于定义索引文档的数据结构,因为要把采集的数据转成标准格式输出)

Filebeat包已经安装了推荐的索引模板。如果你接受filebeat.yml中的默认配置,那么Filebeat在成功连接到Elasticsearch以后会自动加载模板。如果模板已经存在,不会覆盖,除非你配置了必须这样做。

通过在Filebeat配置文件中配置模板加载选项,你可以禁用自动模板加载,或者自动加载你自己的目标。

配置模板加载

默认情况下,如果Elasticsearch输出是启用的,那么Filebeat会自动加载推荐的模板文件 --------- fields.yml。

  • 加载不同的模板
python 复制代码
setup.template.name: "your_template_name"
setup.template.fields: "path/to/fields.yml"

覆盖一个已存在的模板
*

python 复制代码
setup.template.overwrite: true

禁用自动加载模板
*

python 复制代码
setup.template.enabled: false

修改索引名称
*

python 复制代码
# 默认情况下,Filebeat写事件到名为filebeat-6.3.2-yyyy.MM.dd的索引,其中yyyy.MM.dd是事件被索引的日期。为了用一个不同的名字,你可以在Elasticsearch输出中设置index选项。例如:

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

手动加载模板

python 复制代码
./filebeat setup --template -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'

第5步:设置Kibana dashboards

Filebeat附带了Kibana仪表盘、可视化示例。在你用dashboards之前,你需要创建索引模式,filebeat-*,并且加载dashboards到Kibana中。为此,你可以运行setup命令或者在filebeat.yml配置文件中配置dashboard加载。

python 复制代码
./filebeat setup --dashboards

第6步:启动Filebeat

python 复制代码
./filebeat -e -c filebeat.yml -d "publish"

第7步:查看Kibana仪表板示例

python 复制代码
http://127.0.0.1:5601

完整的配置

python 复制代码
#=========================== Filebeat inputs ==============
filebeat.inputs:

- type: log

  enabled: true

  paths:
    - /var/log/*.log

#============================== Dashboards ===============
setup.dashboards.enabled: false

#============================== Kibana ==================
setup.kibana:
	host: "192.168.101.5:5601"

#-------------------------- Elasticsearch output ---------
output.elasticsearch:
  	hosts: ["localhost:9200"]

启动Elasticsearch

/usr/local/programs/elasticsearch/elasticsearch-6.3.2/bin/elasticsearch

启动Kibana

/usr/local/programs/kibana/kibana-6.3.2-linux-x86_64/bin/kibana

设置dashboard

./filebeat setup --dashboards

启动Filebeat

./filebeat -e -c filebeat.yml -d "publish"

浏览器访问 http://192.168.101.5:5601

查看索引

请求:

python 复制代码
curl -X GET "localhost:9200/_cat/indices?v"

响应:

python 复制代码
health status index                     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   bank                      59jD3B4FR8iifWWjrdMzUg   5   1       1000            0    475.1kb        475.1kb
green  open   .kibana                   DzGTSDo9SHSHcNH6rxYHHA   1   0        153           23    216.8kb        216.8kb
yellow open   filebeat-6.3.2-2018.08.08 otgYPvsgR3Ot-2GDcw_Upg   3   1        255            0     63.7kb         63.7kb
yellow open   customer                  DoM-O7QmRk-6f3Iuls7X6Q   5   1          1            0      4.5kb          4.5kb
相关推荐
七夜zippoe5 小时前
CANN Runtime任务描述序列化与持久化源码深度解码
大数据·运维·服务器·cann
Fcy6486 小时前
Linux下 进程(一)(冯诺依曼体系、操作系统、进程基本概念与基本操作)
linux·运维·服务器·进程
袁袁袁袁满7 小时前
Linux怎么查看最新下载的文件
linux·运维·服务器
代码游侠7 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法
Harvey9037 小时前
通过 Helm 部署 Nginx 应用的完整标准化步骤
linux·运维·nginx·k8s
珠海西格电力科技8 小时前
微电网能量平衡理论的实现条件在不同场景下有哪些差异?
运维·服务器·网络·人工智能·云计算·智慧城市
释怀不想释怀8 小时前
Linux环境变量
linux·运维·服务器
zzzsde9 小时前
【Linux】进程(4):进程优先级&&调度队列
linux·运维·服务器
聆风吟º10 小时前
CANN开源项目实战指南:使用oam-tools构建自动化故障诊断与运维可观测性体系
运维·开源·自动化·cann
NPE~10 小时前
自动化工具Drissonpage 保姆级教程(含xpath语法)
运维·后端·爬虫·自动化·网络爬虫·xpath·浏览器自动化