一、Filebeat 的基础概念
1.输入
输入可以指定:file, stdin, redis, udp, docker, tcp, syslog。
2.收集器
(1). 对于每个单独的文件,会启动一个收集器按行读取文件,并发送到输出端。
(2). 收集器负责管理文件描述符,即使一个文件被重命名了,它仍会继续读该文件。
(3). 当日志内容一段时间没有变化时,收集器会关闭。
3.输出
输出可以指定:elasticsearch, logstash, kafka, redis, file, console, cloud。
4.消息队列
Filebeat 使用一个内部队列来暂时存储消息,有内存队列和磁盘队列两种形式。消息队列会等待输出的应答,如果队列满了,就不再接收新消息。
二、Filebeat 配置说明
filebeat.yml 配置文件说明如下:
filebeat.inputs:
- type: log # 输入类型
enabled: true # 启用或禁用配置
paths:
- /home/source/api/logs/*.out
encoding: gbk # 编码
include_lines: ['WARN -', 'ERROR-', 'INFO -', 'DEBUG-'] # 采集包含指定内容的行
exclude_files: ['\.swp$'] # 排除日志文件正则表达
tags: ["shop_dev"] # 标识
output.logstash: # 指定 Logstash 作为输出
enabled: true # 启用或禁用
hosts: hosts: ["localhost:5044"]
三、多个客户端使用不同的索引
多个客户端在 filebeat.yml 配置中定义不同的 tags,如:
第一个客户端:
tags: ["shop_dev"] # 标识
第二个客户端:
tags: ["shop_prod"] # 标识
然后在 Logstash 的 config/logstash-sample.conf 中加入判断:
input {
beats {
port => 5044
codec => "json"
}
}
filter {
mutate {
remove_field => [ "host" ]
}
}
output {
if "shop_dev" in [tags] {
elasticsearch {
hosts => ["localhost:9200"]
index => "shop_dev-%{+YYYY.MM.dd}"
}
}
if "shop_prod" in [tags] {
elasticsearch {
hosts => ["localhost:9200"]
index => "shop_prod-%{+YYYY.MM.dd}"
}
}
elasticsearch {
hosts => ["http://localhost:9200"]
index => "user-%{+YYYY.MM.dd}"
}
}