第一章: ELK简介
E: elasticsearch java 复制存储收集过来的日志
L: logstash java
K: kibana java 负责过滤,分析,绘图展示日志数据
F: filebeat go 负责收集日志
第二章: 传统日志分析需求
1.统计排名前10的IP地址
2.统计排名前10的URL
3.查询上午11点-14点之间的排名情况
5.对比今天11点-12点和昨天相同时间段的访问差别
6.找出各个广告渠道今天分别访问了多少次
7.找出各个爬虫来的次数,爬的最多的页面
8.找出伪造的爬虫IP并查封
9.找出具体的某个URL的访问次数
10.找出访问最慢的前十个页面,对比昨天也这么慢吗
11.5分钟之内告诉我结果
12.一天不定时的会有这些需求
第三章: 日志收集分类
代理层: nginx haproxy
web层: nginx tomcat java php
db层: mysql mongo redis es
系统层: message secure
第四章: ELK安装部署
elasticsearch下载地址
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-x86_64.rpm
kibana下载地址
https://artifacts.elastic.co/downloads/kibana/kibana-7.9.3-x86_64.rpm
filebeat下载地址
https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.9.3-x86_64.rpm
0.更新系统时间
ntpdate time1.aliyun.com
1.安装nginx
[root@db-01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
yum makecache fast
yum install nginx -y
systemctl start nginx
2.安装filebeat
rpm -ivh filebeat-6.6.0-x86_64.rpm
rpm -qc filebeat
3.配置filebeat
[root@db-01 /data/soft]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
output.elasticsearch:
hosts: ["10.0.0.51:9200"]
4.启动并检查
systemctl start filebeat
tail -f /var/log/filebeat/filebeat
5.查看日志结果
es-head查看
第五章:filebeat收集nginx的json格式日志
1.上面方案不完善的地方
日志都在一个字段的valuse里,不能拆分单独显示
2.理想中的情况
将日志里每一个选项的内容都拆分出来
拆分成key-valuse形式,json格式
理想中存在ES里的数据格式
{
$remote_addr : 192.168.12.254
- : -
$remote_user : -
[$time_local]: [10/Sep/2019:10:52:08 +0800]
$request: GET /jhdgsjfgjhshj HTTP/1.0
$status : 404
$body_bytes_sent : 153
$http_referer : -
$http_user_agent :ApacheBench/2.3
$http_x_forwarded_for:-
}
3.目标如何使nginx日志格式转换成我们想要的json格式
修改nginx配置文件使日志转换成json
log_format json '{ "time_local": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"upstream_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';
access_log /var/log/nginx/access.log json;
4.nginx转换成json之后仍然不完善的地方
通过查看发现,虽然nginx日志变成了json,但是es里还是存储在message里仍然不能拆分
目标: 如何在ES里展示的是json格式
5.修改filebeat配置文件
[root@db-01 ~]# cat /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
output.elasticsearch:
hosts: ["10.0.0.51:9200"]
重启filebeat
systemctl restart filebeat
6.清空nginx日志
>/var/log/nginx/access.log
nginx -t
systemctl restart nginx
注意
filebeat 收集nginx日志才能在es-head看见
curl 127.0.0.1
第六章:自定义索引名称
理想中的情况:
nginx_access-xxxxx-年-月
1.配置filebeat实现自定义索引名称
[root@db-01 ~]# cat /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
output.elasticsearch:
hosts: ["10.0.0.51:9200"]
index: "nginx_access-%{[beat.version]}-%{+yyyy.MM}"
setup.template.name: "nginx"
setup.template.pattern: "nginx_*"
setup.template.enabled: false
setup.template.overwrite: true
2.重新filebeat后查看是否生成对应的索引
systemctl restart filebeat
nginx_access-6.6.0-2020.02
第七章:按日志分类存储
理想中的情况:
nginx_access-6.6.0-2020.02
nginx_error-6.6.0-2020.02
1.配置filebeat实现根据不同条件存储到不同的索引
[root@db-01 ~]# cat /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"]
output.elasticsearch:
hosts: ["10.0.0.51: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
systemctl restart filebeat
网友评论