elk(一)

ELK第一天
第一章: 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安装部署
ES
kibana
es-head

0.更新系统时间 ntpdate time1.aliyun.com1.安装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.keyyum makecache fast yum install nginx -y systemctl start nginx2.安装filebeat rpm -ivh filebeat-6.6.0-x86_64.rpm rpm -qc filebeat3.配置filebeat [root@db-01 /data/soft]# cat /etc/filebeat/filebeat.yml filebeat.inputs: - type: log enabled: true paths: - /var/log/nginx/access.logoutput.elasticsearch: hosts: ["10.0.0.51:9200"]4.启动并检查 systemctl start filebeat tail -f /var/log/filebeat/filebeat5.查看日志结果 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.logjson;

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: trueoutput.elasticsearch: hosts: ["10.0.0.51:9200"]重启filebeat systemctl restart filebeat6.清空nginx日志 >/var/log/nginx/access.log nginx -t systemctl restart nginx

【elk(一)】问题总结:
1.不知道es-head怎么查看数据
2.filebeat直接复制粘贴,没有修改IP地址
3.以前的索引没有删除,nginx日志没有清空
4.没有访问Nginx产生日志
第六章: 没日志生成问题总结
11:00 收集的普通日志 es-headfilebeat kibanafilebeat nginxlog11:30 nginx转换成json nginx.confjson nginx -t nginx -s reload nginx curl 127.0.0.1 cat /var/log/nginx/access.log普通的和json的都有 > /var/log/nginx/access.log systemctl restart filebeat es-head删除 kibana删除es里没有索引生成结论: 1.filebeat如果没有新日志产生,就不会发送给ES 2.做实验执行的顺序也会影响实验结果 3.如果修改了日志格式,做如下3步操作: - 清空以前的日志 - 删除以前存在的ES索引 - 删除以前添加的kiaban的索引 4.kibana自己不能创建索引,他只能添加ES里已经存在的索引

第七章: filebeat工作模式
1.如果没日志filebeat就不会发送给ES数据
2.重启filebeat不会从头开始读日志
3.类似于tial -f
4.当filebeat停止的时候,会记录停止那一刻记录的行数,下次启动的时候,从上次记录的下一行开始读数据
5.filebeat对于已经发送给ES的数据不关心
12:05 读取nginx日志
nginx 100
停止了filebeat 100
es 100
es 删除了
12:06 写入了新的日志
nginx 120 20 100-最后一行
启动filebeat
es 20
第八章: 自定义索引名称并按月生成
理想中的情况:
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: trueoutput.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: true2.重新filebeat后查看是否生成对应的索引 nginx_access-6.6.0-2019.09

第九章: 按日志分类存储
理想中的情况:
nginx_access-6.6.0-2019.09
nginx_error-6.6.0-2019.09
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: true2.删除以前的旧索引并重启filebeat systemctl restart filebeat

第十章: 收集tomcat的json日志
1.安装tomcat yum install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp tomcat-javadoc -y2.配置tomcat日志格式为json [root@db-01 /etc/tomcat]# sed -n '139p' server.xml pattern="{" clientip" :" %h" ," ClientUser" :" %l" ," authenticated" :" %u" ," AccessTime" :" %t" ," method" :" %r" ," status" :" %s" ," SendBytes" :" %b" ," Query?string" :" %q" ," partner" :" %{Referer}i" ," AgentVersion" :" %{User-Agent}i" }"/>3.启动tomcat systemctl start tomcat 4.配置filebeat [root@db-01 /etc/tomcat]# cat /etc/filebeat/filebeat.yml filebeat.inputs:- type: log enabled: true paths: - /var/log/tomcat/localhost_access_log.*.txt json.keys_under_root: true json.overwrite_keys: true tags: ["tomcat"]output.elasticsearch: hosts: ["10.0.0.51:9200"] index: "tomcat_access-%{[beat.version]}-%{+yyyy.MM}"setup.template.name: "tomcat" setup.template.pattern: "tomcat_*" setup.template.enabled: false setup.template.overwrite: true5.重启filebeat systemctl restart filebeat6.访问tomcat查看是否有数据生成

第十一章: 收集JAVA日志
java日志的特点:
1.报错信息巨多
2.报错信息巨多还是一个事件.不能分开看
一段java报错日志如下: [2019-09-10T16:15:41,630][ERROR][o.e.b.Bootstrap] [CcJTI28] Exception java.lang.IllegalArgumentException: unknown setting [nnode.name] did you mean [node.name]? at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:482) ~[elasticsearch-6.6.0.jar:6.6.0] at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettgs.java:427) ~[elasticsearch-6.6.0.jar:6.6.0] at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:398) ~[elasticsearch-6.6.0.jar:6.6.0] at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:369) ~[elasticsearch-6.6.0.jar:6.6.0] at org.elasticsearch.common.settings.SettingsModule.(SettingsModule.java:148) ~[elasticsearch-6.6.0.jar:6.6.0] [2019-09-10T16:18:16,742][INFO ][o.e.c.m.MetaDataIndexTemplateService] [node-1] adding template [kibana_index_template:.kibana] for index patterns [.kibana] [2019-09-10T16:18:17,981][INFO ][o.e.c.m.MetaDataIndexTemplateService] [node-1] adding template [kibana_index_template:.kibana] for index patterns [.kibana] [2019-09-10T16:18:33,417][INFO ][o.e.c.m.MetaDataIndexTemplateService] [node-1] adding template [kibana_index_template:.kibana] for index patterns [.kibana]匹配思路: 1.java报错日志特点 正常日志是以[日期]开头的 报错日志行数多,但是不是以[ 2.匹配以[开头的行,一直到下一个以[开头的行,中间所有的数据属于一个事件,放在一起发给ESfilebeat配置多行匹配模式: [root@db-01 ~]# cat /etc/filebeat/filebeat.yml filebeat.inputs: - type: log enabled: true paths: - /var/log/elasticsearch/elasticsearch.log multiline.pattern: '^\[' multiline.negate: true multiline.match: afteroutput.elasticsearch: hosts: ["10.0.0.51:9200"] index: "es-%{[beat.version]}-%{+yyyy.MM}"setup.template.name: "es" setup.template.pattern: "es_*" setup.template.enabled: false setup.template.overwrite: true

自定义图形
条形图

elk(一)
文章图片
image.png elk(一)
文章图片
image.png
elk(一)
文章图片
image.png
elk(一)
文章图片
image.png 扇形图

elk(一)
文章图片
image.png 根据生产需要吧需要的数据做成图形,然后全部排版到一起
elk(一)
文章图片
image.png
elk(一)
文章图片
image.png
elk(一)
文章图片
image.png

    推荐阅读