1、ELK 日志收集介绍
E:Elasticsearch
F:filebeat
L:logstash
K:kibana
2、常规分析日志的操作
1.三剑客 awk sed grep
分析需求:
1.找出访问网站频次最高的IP排名前十
2.找出访问网站排名前十的URL
3.找出中午10点到2点之间www网站访问频次最高的IP
4. 对比昨天这个时间段和今天这个时间段访问频次有什么变化
5.对比上周这个时间和今天这个时间的区别
6.找出特定的页面被访问了多少次
7.找出有问题的IP地址,并告诉我这个IP地址都访问了什么页面,在对比前几天他来过吗?他从什么时间段开始访问的,什么时间段走了
8.找出来访问最慢的前十个页面并统计平均响应时间,对比昨天这也页面访问也这么慢吗?
9.找出搜索引擎今天各抓取了多少次?抓取了哪些页面?响应时间如何?
10.找出伪造成搜索引擎的IP地址
11.5分钟之内告诉我结果
需要收集哪些日志?
系统层面:message secure
代理层:nginx haproxy lvs
web层: nginx tomcat php apache
数据库层:mysql redis mongo elasticsearch
3、ELK环境部署
3.1 所有节点还原环境
systemctl stop elasticsearch
systemctl stop kibana
rm -rf /var/lib/kibana/*
rm -rf /data/elasticsearch/*
systemctl start elasticsearch
systemctl start kibana
3.2 更新时间
yum install ntpdate -y
ntpdate time1.aliyun.com
3.3 安装nginx
yum install nginx -y
systemctl start nginx
netstat -luntp |grep 80
tail -f /var/log/nginx/access.log
3.4 编辑配置filebeat文件
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
setup.kibana:
output.elasticsearch:
hosts: ["10.0.0.51:9200"]
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
3.5 生成一点访问数据
[root@db02 ~]# for i in {1..2000};do curl 10.0.0.51/db01; echo "${i} is ok";done
for i in `seq 1000`
do
curl 192.168.12.201/$i &>/dev/null
done
for i in `seq 1000`
do
curl 192.168.12.201/$i &>/dev/null
done
4、配置日志成json格式
nginx日志格式
我们期望的格式
$remote_addr:10.0.0.1
$remote_user:-
[$time_local]:[10/Jul/2019:17:59:52 +0800]
$request:GET/db01.html HTTP/1.1"
$status :404
$body_bytes_sent:3650
$http_referer:-
$http_user_agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
$http_x_forwarded_for:-
操作步骤:注意!所有nginx服务器都需要操作!
1. 修改nginx配置文件
log_format main '{ "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"'
' }';
2.重启nginx
nginx -t # 检查语法
systemctl reload nginx
3.重新访问nginx产生新数据
curl 10.0.0.51
4.修改后的日志格式
{
"time_local": "10/Jul/2019:18:55:59 +0800",
"remote_addr": "10.0.0.1",
"referer": "-",
"request": "GET /db01.html HTTP/1.1",
"status": 404,
"bytes": 3650,
"agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
"x_forwarded": "-",
"up_addr": "-",
"up_host": "-",
"upstream_time": "-",
"request_time": "0.000"
}
5.清空nginx日志
> /var/log/nginx/access.log
6.修改filebeat配置文件
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"]
7.es删除旧的索引
8.重启filebeat
systemctl restart filebeat