ELK基础

ELK基础

一、ELK简介

E: elasticsearch	java	 复制存储收集过来的日志
L: logstash			java	
K: kibana			java  	 负责过滤,分析,绘图展示日志数据
F: filebeat			go		 负责收集日志

一开始都是ELK,现在用的比较多的是EFK,只不过大家还是习惯说ELK。

filebeat比较轻量,是用go语言开发的,大小是logstash的十分之一。

二、传统日志分析需求

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.更新系统时间
[root@db01 ~]# ntpdate time1.aliyun.com


# 1.安装nginx 
[root@db01 ~]# 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

[root@db01 ~]# yum makecache fast
[root@db01 ~]# yum install nginx -y
[root@db01 ~]# systemctl start nginx
[root@db01 ~]# curl 127.0.0.1
[root@db01 ~]# tail -f /var/log/nginx/access.log



# 2.安装filebeat 
[root@db01 /data/soft]# ll
-rw-r--r-- 1 root root  11790119 Jan 20 18:23 filebeat-6.6.0-x86_64.rpm
[root@db01 ~]# rpm -ivh filebeat-6.6.0-x86_64.rpm


# 3.查看filebeat配置文件
[root@db01 ~]# rpm -qc filebeat


# 4.配置filebeat 
[[root@db01 /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"]


# 5.启动并检查
[root@db01 ~]# systemctl start filebeat
[root@db01 ~]# tail -f /var/log/filebeat/filebeat


# 6.查看日志结果
es-head查看

# 7.测试插入数据
[root@db01 ~]# ab -c 10 -n 500 http://10.0.0.51/

UTOOLS1582689854213.png

UTOOLS1582689898970.png

五、filebeat收集nginx的json格式日志

1.查看日志信息

2.上面查询不完善的地方

如果只想看ip或者只想看某个值,看不到
日志都在一个字段的valuse里,不能拆分单独显示

3.理想中的情况

将日志里每一个选项的内容都拆分出来
拆分成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:-
}

4.如何使nginx日志格式转换成我们想要的json格式

# 修改nginx配置文件使日志转换成json
[root@db01 ~]# vim /etc/nginx/nginx.conf
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;

[root@db01 ~]# nginx -t

5.nginx转换成json之后仍然不完善的地方

通过查看发现,虽然nginx日志变成了json,但是es里还是存储在message里仍然不能拆分

6.修改filebeat配置文件

# 修改filebeat配置文件
[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

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]


# 重启filebeat
[root@db01 ~]# systemctl restart filebeat   


# 清空nginx日志 (如果不清空日志 就会两种格式混合在一起 会打不开)
[root@db01 ~]# >/var/log/nginx/access.log 
[root@db01 ~]# nginx -t 
[root@db01 ~]# systemctl restart nginx 

kibana需要重新加这个索引,把之前的删掉

7.结论

# 问题总结
1.不知道es-head怎么查看数据
2.filebeat直接复制粘贴,没有修改IP地址
3.以前的索引没有删除,nginx日志没有清空
4.没有访问Nginx产生日志 
# 结论
1.filebeat如果没有新日志产生,就不会发送给ES
2.做实验执行的顺序也会影响实验结果 
3.如果修改了日志格式,做如下3步操作:
- 清空以前的日志
- 删除以前存在的ES索引
- 删除以前添加的kiaban的索引
4.kibana自己不能创建索引,他只能添加ES里已经存在的索引

六、filebeat工作模式

1.如果没日志filebeat就不会发送给ES数据
2.重启filebeat不会从头开始读日志
3.filebeat类似于tial -f 
4.当filebeat停止的时候,会记录停止那一刻记录的行数,下次启动的时候,从上次记录的下一行开始读数据
5.filebeat对于已经发送给ES的数据不关心
12:05 读取nginx日志
nginx 	100行 
停止了filebeat 		我在100行 (我收集了100条日志 前面99条我还关心吗?)
es		100行  
es 		删除了

12:06 写入了新的日志
nginx	120行  新20行  从100行读~最后一行
启动filebeat
es 		20行

七、自定义索引名称并按月生成

1.理想中的情况

nginx_access-xxxxx-年-月

2.配置filebeat实现自定义索引名称

[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

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
setup.ilm.enabled: false


[root@db01 ~]# systemctl restart filebeat.service

八、filebeat根据tag生成access和error索引

1.理想中的情况

nginx_access-6.6.0-2019.09
nginx_error-6.6.0-2019.09

2.filebeat根据source生成access和error日志

[root@db01 ~]# 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

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/error.log

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  indices:
    - index: "nginx_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        source: "/var/log/nginx/access.log"
    - index: "nginx_error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        source: "/var/log/nginx/error.log"

setup.template.name: "nginx"
setup.template.pattern: "nginx_*"
setup.template.enabled: false
setup.template.overwrite: true


# 2.删除以前的旧索引并重启filebeat
[root@db01 ~]# systemctl restart filebeat

3.filebeat根据tag生成access和error日志

# 1.配置filebeat实现根据不同条件存储到不同的索引
[root@db01 ~]# 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
[root@db01 ~]# systemctl restart filebeat

九、filebeat收集nginx不同域名的访问日志

UTOOLS1582690059673.png

UTOOLS1582690100270.png

十、filebeat收集tomcat的json日志

# 查看tomcat日志 为了之后把这个日志格式改成json的
[root@db01 /var/log/tomcat]# ll
total 24
-rw-r--r-- 1 tomcat tomcat 9771 Jan 22 20:37 catalina.2020-01-22.log
-rw-rw---- 1 tomcat tomcat   28 Mar 12  2019 catalina.out
-rw-r--r-- 1 tomcat tomcat    0 Jan 22 20:37 host-manager.2020-01-22.log
-rw-r--r-- 1 tomcat tomcat  446 Jan 22 20:37 localhost.2020-01-22.log
-rw-r--r-- 1 tomcat tomcat 1318 Jan 22 20:37 localhost_access_log.2020-01-22.txt
-rw-r--r-- 1 tomcat tomcat    0 Jan 22 20:37 manager.2020-01-22.log
[root@db01 /var/log/tomcat]# cat localhost_access_log.2020-01-22.txt
10.0.0.1 - - [22/Jan/2020:20:37:18 +0800] "GET / HTTP/1.1" 200 11217
10.0.0.1 - - [22/Jan/2020:20:37:18 +0800] "GET /tomcat.css HTTP/1.1" 200 5581
10.0.0.1 - - [22/Jan/2020:20:37:18 +0800] "GET /tomcat.png HTTP/1.1" 200 5103
10.0.0.1 - - [22/Jan/2020:20:37:18 +0800] "GET /bg-middle.png HTTP/1.1" 200 1918
10.0.0.1 - - [22/Jan/2020:20:37:18 +0800] "GET /asf-logo-wide.svg HTTP/1.1" 200 26447
10.0.0.1 - - [22/Jan/2020:20:37:18 +0800] "GET /bg-nav.png HTTP/1.1" 200 1401
10.0.0.1 - - [22/Jan/2020:20:37:18 +0800] "GET /bg-button.png HTTP/1.1" 200 713
10.0.0.1 - - [22/Jan/2020:20:37:18 +0800] "GET /bg-upper.png HTTP/1.1" 200 3103
10.0.0.1 - - [22/Jan/2020:20:37:18 +0800] "GET /favicon.ico HTTP/1.1" 200 21630
# 1.安装tomcat 
[root@db01 ~]# yum install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp tomcat-javadoc -y


# 2.配置tomcat日志格式为json
[root@db01 /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 查看有没有变成json格式
[root@db01 /etc/tomcat]# > /var/log/tomcat/localhost_access_log.2020-01-22.txt
[root@db01 /etc/tomcat]# systemctl restart tomcat 
[root@db01 /var/log/tomcat]# cat localhost_access_log.2020-01-22.txt
{"clientip":"10.0.0.1","ClientUser":"-","authenticated":"-","AccessTime":"[22/Jan/2020:21:13:39 +0800]","method":"GET /docs/setup.html HTTP/1.1","status":"304","SendBytes":"-","Query?string":"","partner":"http://10.0.0.51:8080/","AgentVersion":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"}


# 4.配置filebeat
[root@db01  /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: true
		   

# 5.重启filebeat
[root@db01 ~]# systemctl restart filebeat


# 6.访问tomcat查看是否有数据生成

十一、filebeat收集多行JAVA日志

1.java日志的特点

1.报错信息巨多
2.报错信息巨多还是一个事件.不能分开看

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(AbstractScopedSettings.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.<init>(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]

3.匹配思路

1.java报错日志特点
正常日志是以[日期]开头的
报错日志行数多,但是不是以[
2.匹配以[开头的行,一直到下一个以[开头的行,中间所有的数据属于一个事件,放在一起发给ES

4.配置

[root@db01 ~]# cat /etc/filebeat/filebeat.yml 
filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/elasticsearch/elasticsearch.log
  multiline.pattern: '^\['
  multiline.negate: true 
  multiline.match: after

output.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

十二、kibana画图展示

直接显示图

直接显示数据

饼图查看状态码

UTOOLS1582690273796.png

好多图可以自己研究

posted @ 2020-01-22 22:44  干瘪的柠檬  阅读(371)  评论(0)    收藏  举报