以下是在Ubuntu上使用Filebeat分析日志数据的步骤:
安装Filebeat
从Elastic官网下载对应版本的安装包,通过包管理器安装:
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 /etc/apt/sources.list.d/elastic-7.x.list
sudo apt-get update && sudo apt-get install filebeat
配置Filebeat
编辑配置文件 /etc/filebeat/filebeat.yml:
filebeat.inputs 中添加要监控的日志文件路径,如:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["localhost:9200"]
index: "filebeat-%{+yyyy.MM.dd}"
若需经Logstash处理,启用Logstash输出并配置地址:output.logstash:
hosts: ["localhost:5044"]
启用模块(可选)
Filebeat内置模块(如System、Nginx)可简化日志解析,启用方式:
sudo filebeat modules enable system # 启用系统日志模块
sudo filebeat modules enable nginx # 启用Nginx日志模块
启动服务
sudo systemctl start filebeat
sudo systemctl enable filebeat
可视化分析
sudo apt-get install kibana
sudo vim /etc/kibana/kibana.yml
# 添加 Elasticsearch 地址
elasticsearch.hosts: ["http://localhost:9200"]
http://localhost:5601,通过“Discover”查看日志,或使用“Visualize”创建图表。高级配置(可选)
filebeat.yml 中配置 multiline 选项,如:filebeat.inputs:
- type: log
multiline.pattern: '^\[' # 匹配以[开头的行
multiline.negate: true
multiline.match: after
json.keys_under_root 选项自动解析字段。参考来源: