1 filebeat 日志采集
Filebeat 由两个核心组件构成:
- Prospector(勘探器):监控配置的日志文件 / 目录,检测文件新增、滚动、删除;记录文件读取偏移量(存于 registry 注册文件),实现断点续传,重启不重复读日志。
- Harvester(收割机):逐行读取单个日志文件内容,支持多行合并、简单过滤,将日志打包发送至输出端(Logstash/Elasticsearch)。
日志采集完整工作流程
- 勘探监控:Prospector 持续监听配置的日志文件 / 目录,识别新增、滚动、删除的日志文件。
- 读取日志:Harvester 打开日志文件逐行读取,可按规则合并堆栈类多行日志,同时记录文件读取偏移量存入 registry 文件。
- 本地预处理:对日志做过滤、字段增减等轻量化处理。
- 日志推送:将处理后的日志批量发送至目标端(Logstash 或 Elasticsearch)。
- 更新偏移:推送成功后更新读取位置,实现断点续传,重启不会重复采集。
1.1 filebeat 采集 web 服务日志
安装 httpd 服务
[root@server4 ~]# yum install -y httpd[root@server4 ~]# echo server4 > /var/www/html/index.html[root@server4 ~]# systemctl start httpd上传filebeat-7.6.1-x86_64.rpm,安装 filebeat 并激活 apache 模块
[root@server4 ~]# rpm -ivh filebeat-7.6.1-x86_64.rpm[root@server4 ~]# cd /etc/filebeat/modules.d[root@server4 modules.d]# filebeat modules enable apache[root@server4 modules.d]# vim apache.yml
输出到ES
[root@server4 modules.d]# cd ..[root@server4 filebeat]# vim filebeat.yml
检查配置文件语法,测试与 ES 的连接,没问题启动 filebeat
[root@server4 filebeat]# filebeat test config -c /etc/filebeat/filebeat.yml[root@server4 filebeat]# systemctl enable --now filebeat
启动失败,apache.yml配置文件第 13 行YAML 语法错误
YAML 格式强制要求:key: 值冒号后必须跟空格、缩进统一用空格、不能用 Tab、符号不能缺失。
[root@server4 ~]# cd /etc/filebeat/modules.d[root@server4 modules.d]# vim apache.yml[root@server4 modules.d]# systemctl enable --now filebeat.service
自动添加索引
1.2 输出到 logstash
编辑配置文件并启动 logstash
[root@server5 ~]# cd /etc/logstash/conf.d/[root@server5 conf.d]# cp grok.conf beats.conf[root@server5 conf.d]# vim beats.conf[root@server5 conf.d]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/beats.conf#不要中断此程序
修改 filebeat 输出
[root@server4 modules.d]# vim /etc/filebeat/filebeat.yml[root@server4 modules.d]# systemctl restart filebeat.service
server5终端有数据输出,但是有警告
在两个网页上把 apachelog 索引删掉,避免生成新的 apachelog 索引时与之冲突,警告消失
测试
[root@server3 ~]# curl server4server5正常输出
网页上会自动创建新的 apachelog 索引
2 kibana 数据可视化
2.1 部署
kibana-7.6.1-x86_64.rpm上传到server4的/root,安装,修改配置文件后启动
[root@server4 ~]# rpm -ivh kibana-7.6.1-x86_64.rpm[root@server4 ~]# cd /etc/kibana/[root@server4 kibana]# vim kibana.yml[root@server4 kibana]# systemctl enable --now kibana.service
访问server4 IP:5601
2.2 定制数据可视化
2.2.1 网站访问量
测试
[root@server4 kibana]# ab -c1 -n 300 http://192.168.40.144/index.html2.2.2 访问量排行榜
测试
[root@server1 ~]# ab -c1 -n 100 http://192.168.40.144/index.html[root@server2 ~]# ab -c1 -n 300 http://192.168.40.144/index.html[root@server3 ~]# ab -c1 -n 300 http://192.168.40.144/index.html[root@server4 ~]# ab -c1 -n 200 http://192.168.40.144/index.html