在 CentOS 上使用 Filebeat 监控目录,本质就是:
安装 Filebeat → 配置 filebeat.inputs → 指定要监控的目录 → 启动服务。
下面给你一套可直接用的完整流程。
# 下载(以 8.11.0 为例,可换成你需要的版本)
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.11.0-x86_64.rpm
# 安装
sudo rpm -ivh filebeat-8.11.0-x86_64.rpm
安装完成后,目录结构如下:
/etc/filebeat/filebeat.yml # 主配置文件
/usr/bin/filebeat # 可执行文件
假设你要监控:
/var/log/myapp/
目录下所有 .log 文件。
sudo vi /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: filestream # 8.x 推荐使用 filestream
enabled: true
paths:
- /var/log/myapp/*.log
output.elasticsearch:
hosts: ["localhost:9200"]
✅ 如果是老版本(7.x),可以使用:
- type: log
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/myapp/*.log
- /var/log/nginx/*.log
- /data/logs/**/*.log # 递归子目录
说明:
| 写法 | 作用 |
|---|---|
*.log |
匹配当前目录 |
**/*.log |
递归所有子目录 |
/var/log/*/*.log |
一级子目录 |
Filebeat 默认行为:
✅ 自动读取新增日志
✅ 记录读取位置(registry)
✅ 重启后不会重复读取
位置记录目录:
/var/lib/filebeat/registry
output.elasticsearch:
hosts: ["http://localhost:9200"]
output.logstash:
hosts: ["localhost:5044"]
output.console:
pretty: true
sudo systemctl enable filebeat
sudo systemctl start filebeat
查看状态:
systemctl status filebeat
# 查看日志
journalctl -u filebeat -f
# 或
tail -f /var/log/filebeat/filebeat
sudo chmod 644 /var/log/myapp/*.log
或给 filebeat 用户授权。
✅ 确认:
ls -l /var/log/myapp/
✅ 确认 paths 是否写对
✅ 确认 enabled: true
⚠ 不推荐,尽量使用英文路径。
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/myapp/*.log
scan_frequency: 10s
close_timeout: 5m
queue.mem:
events: 4096
flush.min_events: 512
flush.timeout: 1s
output.logstash:
hosts: ["logstash:5044"]
Filebeat 监控 CentOS 目录 = 配 paths + 启动服务 + 看输出
如果你愿意,可以告诉我:
我可以直接帮你写 一份完整可上线配置。