ELK收集nginx正常和错误日志
2020-06-23 本文已影响0人
南南宫问天
1.修改filebeat配置文件
在53主机操作
[root@db01 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
##收集正常日志
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
tags: ["access"]
##收集错误日志
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
tags: ["error"]
setup.kibana:
host: "172.16.210.53:5601"
##根据标签来决定把日志放进正确日志索引和错误日志索引
output.elasticsearch:
hosts: ["172.16.210.53:9200"]
indices:
- index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
tags: "access"
- index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
tags: "error"
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
2.重启filebeat
[root@db01 ~]# systemctl restart filebeat
3.使用ab工具来产生一些日志
[root@db01 ~]# ab -n 100 -c 100 http://172.16.210.53/cc.html
4.使用es-head插件查看
能发现生成的两个索引,分别是正常日志和错误日志
![](https://img.haomeiwen.com/i21497969/b23e2b2ea2af9802.png)
5.回到kibana添加数据项并查看项目
创建正常日志索引
![](https://img.haomeiwen.com/i21497969/f0514a1a9963a5da.png)
![](https://img.haomeiwen.com/i21497969/42ab7faec540ff46.png)
创建错误日志索引
![](https://img.haomeiwen.com/i21497969/59792cb03affbd63.png)
![](https://img.haomeiwen.com/i21497969/482c111423d0e7c6.png)
选择要查看的索引项
![](https://img.haomeiwen.com/i21497969/5bc76ff9b97d245d.png)
查看access日志
![](https://img.haomeiwen.com/i21497969/27e4d30568943ef7.png)
查看error日志
![](https://img.haomeiwen.com/i21497969/da779eb73b51ab0a.png)
要是想查看其他主机的正常和错误日志的话,就把53主机的filebeat配置文件拷贝过去,再重启filebeat
在53主机操作:
[root@db01 ~]# scp /etc/filebeat/filebeat.yml db02:/etc/filebeat/
[root@db01 ~]# scp /etc/filebeat/filebeat.yml db03:/etc/filebeat/
在54和55主机操作:
systemctl restart filebeat
在53主机操作
##给54和55主机产生一些错误日志
[root@db01 ~]# ab -n 100 -c 100 http://172.16.210.54/4444.html
[root@db01 ~]# ab -n 100 -c 100 http://172.16.210.55/5555.html
回到kibana界面,添加主机名为db02的过滤项
![](https://img.haomeiwen.com/i21497969/6945b4f4c31d421c.png)
就能查看db02主机的错误日志了
![](https://img.haomeiwen.com/i21497969/6e96d4b355bed8b5.png)
过滤db03,也能查看到db03的错误日志
![](https://img.haomeiwen.com/i21497969/5fe39cba5b8bd1d3.png)
![](https://img.haomeiwen.com/i21497969/123cf65b91f0bf5d.png)