在 Ubuntu 上使用 Filebeat 进行日志分析
一 架构与准备
- 组件选型:在 Ubuntu 上部署 Filebeat 作为日志采集器,常见后端为 Logstash → Elasticsearch → Kibana(集中解析与可视化),也可直连 Elasticsearch。Filebeat 具备低资源占用、多模块支持(Nginx、MySQL、System 等)、断点续传等优势,适合边缘节点采集。请确保各组件版本一致(如 8.x 或 7.x)。Kibana 默认访问端口为 5601。
二 安装与启用模块
- APT 安装(推荐)
- 导入 GPG 并添加仓库(以 7.x 为例,若用 8.x 将版本号替换为 8.x):
- wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
- echo “deb https://artifacts.elastic.co/packages/7.x/apt stable main” | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
- sudo apt update && sudo apt install -y filebeat
- 启用系统日志模块并启动服务:
- sudo filebeat modules enable system
- sudo systemctl start filebeat && sudo systemctl enable filebeat
- DEB 包安装(示例)
- wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.9.0-amd64.deb
- sudo dpkg -i filebeat-8.9.0-amd64.deb && sudo systemctl start filebeat
- 验证运行状态:
- sudo systemctl status filebeat
- tail -f /var/log/filebeat/filebeat.log
三 配置采集与输出
- 场景一 直连 Elasticsearch(最简)
- 编辑 /etc/filebeat/filebeat.yml:
- filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/syslog
- /var/log/auth.log
- output.elasticsearch:
- hosts: [“localhost:9200”]
- 重启:sudo systemctl restart filebeat
- 场景二 输出到 Logstash(便于解析与增强)
- Filebeat 配置:
- filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/syslog
- /var/log/auth.log
- output.logstash:
- hosts: [“logstash_host:5044”]
- output.elasticsearch.enabled: false
- Logstash 示例(/etc/logstash/conf.d/filebeat.conf):
- input { beats { port => 5044 } }
- filter {
if [fileset][module] == “system” {
if [fileset][name] == “auth” {
grok { match => { “message” => “%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} sshd(?:[%{POSINT:[system][auth][pid]}])?: %{DATA:[system][auth][ssh][event]} %{DATA:[system][auth][ssh][method]} for (invalid user )?%{DATA:[system][auth][user]} from %{IPORHOST:[system][auth][ip]} port %{NUMBER:[system][auth][ssh][port]} ssh2” } }
}
}
}
- output { elasticsearch { hosts => [“http://localhost:9200”] index => “system-logs-%{+YYYY.MM.dd}” } stdout { codec => rubydebug } }
- 启动 Logstash:/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/filebeat.conf
- 场景三 多域名 Nginx 日志写入不同索引
- filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/example.com.access.log
fields: { domain: “example.com” }
fields_under_root: true
- type: log
enabled: true
paths:
- /var/log/nginx/api.example.com.access.log
fields: { domain: “api.example.com” }
fields_under_root: true
- output.elasticsearch:
- hosts: [“localhost:9200”]
- indices:
- index: “example-com-%{+yyyy.MM.dd}”
when.equals: { domain: “example.com” }
- index: “api-example-com-%{+yyyy.MM.dd}”
when.equals: { domain: “api.example.com” }
- 提示:Filebeat 模块(如 Nginx/System)可一键启用并自带解析与仪表盘,适合快速落地。
四 在 Kibana 中查看与分析
- 创建索引模式:进入 Kibana → Management → Stack Management → Index Patterns,新建匹配索引(如 system-logs-* 或 nginx-logs-*),时间字段选择 @timestamp。
- 数据探索:在 Discover 选择索引模式,按 status、client_ip、method 等字段筛选与检索。
- 可视化与仪表盘:
- 状态码分布饼图(按 status 聚合)
- 请求量时序折线图(X 轴 @timestamp,按小时聚合)
- 地理分布地图(基于 client_ip 的 GeoIP,需提前解析 IP 地理信息)
- 若使用 Filebeat 模块(如 Nginx/System),可直接导入预置仪表盘,快速获得访问量、错误率、地域分布等图表。
五 常见问题与排查
- 无法发送数据到 Logstash
- 查看 Filebeat 日志:tail -f /var/log/filebeat/filebeat.log
- 检查端口连通:ss -lntp | grep 5044 或 telnet logstash_host 5044
- Grok 解析失败
- 使用 Grok Debugger 验证正则;临时在 Logstash 开启 stdout { codec => rubydebug } 观察原始事件
- Kibana 无数据
- 索引模式未匹配(如应为 nginx-logs-* 而非 filebeat-*)
- 右上角时间范围选择不当(调整为 Last 15 minutes/1 hour)
- 版本与模板冲突
- 多模块索引模板优先级冲突会导致 400 错误,需为自定义模板设置不同 priority,避免与内置模板冲突。