一、概述
轻量型日志采集器,提供一种轻量型方法,用于转发和汇总日志文件。主页
用于监控、收集服务器日志文件.
1.1 工作原理
Filebeat由两个主要组件组成:prospector 和 harvester。
- harvester:
- 负责读取单个文件的内容。
- 如果文件在读取时被删除或重命名,Filebeat将继续读取文件。
- prospector
- prospector 负责管理harvester并找到所有要读取的文件来源。
- 如果输入类型为日志,则查找器将查找路径匹配的所有文件,并为每个文件启动一个harvester。
- Filebeat目前支持两种prospector类型:log和stdin。
- Filebeat如何保持文件的状态
- Filebeat 保存每个文件的状态并经常将状态刷新到磁盘上的注册文件中。
- 该状态用于记住harvester正在读取的最后偏移量,并确保发送所有日志行。
- 如果输出(例如Elasticsearch或Logstash)无法访问,Filebeat会跟踪最后发送的行,并在输出再次可用时继续读取文件。
- 在Filebeat运行时,每个prospector内存中也会保存的文件状态信息,当重新启动Filebeat时,将使用注册文件的数据来重建文件状态,Filebeat将每个harvester在从保存的最后偏移量继续读取。
- 文件状态记录在data/registry文件中。
1.2 命令相关
启动命令
sh
#参数说明
#-e: 输出到标准输出,默认输出到syslog和logs下
#-c: 指定配置文件
#-d: 输出debug信息
./filebeat -e -c peizhi.yml -d "publish"二、应用示例
2.1 读取文件
yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /haoke/beats/logs/*.log
tags: ["web"] #添加自定义tag,便于后续的处理
fields: #添加自定义字段
from: haoke-im
fields_under_root: true #true为添加到根节点,false为添加到子节点中
setup.template.settings:
index.number_of_shards: 3
output.console:
pretty: true
enable: true检测到日志文件有更新,立刻就会读取到更新的内容,并且输出到控制台。
2.2 输出到ElasticSearch
yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /haoke/beats/logs/*.log
tags: ["haoke-im"]
fields:
from: haoke-im
fields_under_root: false
setup.template.settings:
index.number_of_shards: 3 #指定索引的分区数
output.elasticsearch: #指定ES的配置
hosts: ["192.168.1.7:9200","192.168.1.7:9201","192.168.1.7:9202"]三、Module
3.1 相关命令
查看module
在Filebeat中,有大量的Module,可以简化配置,直接就可以使用
sh
./filebeat modules list
Enabled:
Disabled:
apache2
auditd
elasticsearch
haproxy
icinga
iis
kafka
kibana
logstash
mongodb
mysql
nginx
osquery
postgresql
redis
suricata
system
traefik启用/禁用Module
sh
./filebeat modules enable redis #启动
./filebeat modules disable redis #禁用
Enabled:
redis
Disabled:
apache2
auditd
elasticsearch
haproxy
icinga
iis
kafka
kibana
logstash
mongodb
mysql
nginx
osquery
postgresql
suricata
system
traefik3.2 redis module
├── log #日志
│ ├── config
│ │ └── log.yml
│ ├── ingest
│ │ └── pipeline.json
│ └── manifest.yml
├── module.yml
└── slowlog #慢查询日志
├── config
│ └── slowlog.yml
├── ingest
│ └── pipeline.json
└── manifest.yml配置
cd modules.d/
vim redis.yml
- module: redis
# Main logs
<NolebasePageProperties />
log:
enabled: true
# Set custom paths for the log files. If left empty,
# Filebeat will choose the paths depending on your OS.
var.paths: ["/data/redis-data/node01/*.log"]
# Slow logs, retrieved via the Redis API (SLOWLOG)
slowlog:
enabled: false
# The Redis hosts to connect to.
#var.hosts: ["localhost:6379"]
# Optional, the password修改redis的docker容器
redis默认情况下,是不会输出日志的,需要进行配置,前面我们使用的容器都没有配置日志输出,下面需要配置一下
docker create --name redis-node01 -v /data/redis-data/node01:/data -p 6379:6379 redis:5.0.2 --cluster-enabled yes --cluster-config-file nodes-node-01.conf --loglevel debug --logfile nodes-node-01.log
docker create --name redis-node02 -v /data/redis-data/node02:/data -p 6380:6379 redis:5.0.2 --cluster-enabled yes --cluster-config-file nodes-node-02.conf --loglevel debug --logfile nodes-node-02.log
docker create --name redis-node03 -v /data/redis-data/node03:/data -p 6381:6379 redis:5.0.2 --cluster-enabled yes --cluster-config-file nodes-node-03.conf --loglevel debug --logfile nodes-node-03.logloglevel 日志等级分为:debug、verbose、notice、warning
其中,debug 会有大量信息,对开发、测试有用;
verbose 等于log4j 中的info,有很多信息,但是不会像debug那样乱;
notice 一般信息;
warning 只有非常重要/关键的消息被记录。
配置filebeat
#vim haoke-redis.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /haoke/log/*.log
setup.template.settings:
index.number_of_shards: 3
output.console:
pretty: true
enable: true
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false测试
./filebeat -e -c haoke-redis.1 yml --modules redis测试发现,数据已经写入到了Elasticsearch中。
当然了,其他的Module的用法参加官方文档: https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-modules.html
3.3 nginx module
配置
yaml
- module: nginx
# Access logs
access:
enabled: true
var.paths: ["/usr/local/nginx/logs/access.log*"]
# Set custom paths for the log files. If left empty,
# Filebeat will choose the paths depending on your OS.
#var.paths:
# Error logs
error:
enabled: true
var.paths: ["/usr/local/nginx/logs/error.log*"]
# Set custom paths for the log files. If left empty,
# Filebeat will choose the paths depending on your OS.
#var.paths:配置filebeat
yml
#vim itcast-nginx.yml
filebeat.inputs:
#- type: log
# enabled: true
# paths:
# - /usr/local/nginx/logs/*.log
# tags: ["nginx"]
setup.template.settings:
index.number_of_shards: 3
output.elasticsearch:
hosts: ["192.168.40.133:9200","192.168.40.134:9200","192.168.40.135:9200"]
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false测试
sh
./filebeat -e -c itcast-nginx.yml
#启动会出错,如下
ERROR fileset/factory.go:142 Error loading pipeline: Error loading pipeline for fileset nginx/access: This module requires the following Elasticsearch plugins: ingest-user-agent, ingest-geoip. You can install them by running the following commands on all the Elasticsearch nodes: sudo bin/elasticsearch-plugin install ingest-user-agent sudo bin/elasticsearch-plugin install ingest-geoip
#解决:需要在Elasticsearch中安装ingest-user-agent、ingest-geoip插件
#在ingest-user-agent.tar、ingest-geoip.tar、ingest-geoip-conf.tar 3个文件
#其中,ingest-user-agent.tar、ingest-geoip.tar解压到plugins下 #ingest-geoip-conf.tar解压到config下
#问题解决。四、安装
4.1 普通安装
下载(或使用资料中提供的安装包,版本为:filebeat-6.5.4):https://www.elastic.co/downloads/beats
解压
shtar -xvf filebeat-6.5.4-linux-x86_64.tar.gz创建配置文件。
运行程序
sh./filebeat -e -c 配置文件.yml