image-20230712140456022

安装kibana

# 下载kibana
kibana-5.6.16-x86_64.rpm
# 安装kibana
yum localinstall -y kibana-5.6.16-x86_64.rpm
# 编辑配置文件
vim /etc/kibana/kibana.yml
2 server.port: 5601
7 server.host: "0.0.0.0"
21 elasticsearch.url: "http://10.0.0.81:9200"
# 启动kibana
systemctl start kibana
# 检查端口
[root@elk04 ~]# netstat -lntup
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 5970/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN  6753/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 6903/master
tcp 0 0 0.0.0.0:5601 0.0.0.0:* LISTEN 7094/node
# 浏览器访问
10.0.0.84:5601

image-20230712140955873

#添加生片检测
tomcat_log-2023.07.12
tomcat_log- == 生片名
2023.07.12 == YYYY.MM.DD

!image-20230712151058300

#匹配规则如下

image-20230712141814891

dev-tools

Dev Tools介绍:
Dev Tools 页面包含开发工具,您可以使用这些Dev Tools与Kibana中的数据进行交互。
原先的交互式控制台Sense,使用户方便的通过浏览器直接与Elasticsearch进行交互。从Kibana 5开始改名并直接内建在Kibana,就是Dev Tools选项。
Kibana提供了Console UI来通过REST API与Elasticsearch交互,Console位于Kibana的Dev Tools栏下。Console有两个主要区域,左边是编辑区用来书写REST请求,右边用来显示请求返回结果。

自动提示

Console提供了自动提示功能,可以为你提供API、方法等提示。编写完请求后点击绿色执行按钮,会在右侧面板给出请求结果。执行按钮旁边的“小扳手”按钮,可以将请求copy转化为curl(copy),还有一个功能就是自动缩紧格式(Auto Indent)。如果对已经锁进好的代码进行Auto Indent,Console会将请求体(body)缩进在一行中。

image-20230717153505297

查看es的所有索引

GET _cat/indices

image-20230717153934205

查询集群节点是否禁用swap

GET _nodes?filter_path=**.mlockall

image-20230717154624064

#配置解释
"nodes": 这是一个节点对象的键,表示接下来的内容是有关节点的信息。
"dy4Oo_GtQl6gLxE5oVw5bA": 这是第一个节点的标识符或名称。每个节点都有一个唯一的标识符来区分它们。
"process": 表示接下来的内容是有关节点进程的信息。
"mlockall": 这是一个布尔值键,用于指示节点进程是否启用了内存锁定功能。
false: 这是 "mlockall" 键的值,表示节点进程没有开启内存锁定功能。
"tXuIJrwkQbGAbb3zbGCEOQ": 这是第二个节点的标识符或名称,与第一个节点类似。
"process": 同样表示接下来的内容是有关节点进程的信息。
"mlockall": 这是第二个节点的内存锁定键。
false: 这是第二个节点的内存锁定值,同样表示节点进程没有开启内存锁定功能。

查询集群节点最大文件描述符

GET _nodes/stats/process?filter_path=**.max_file_descriptors

image-20230717155312039

集群分片情况查询

GET _cat/shards

image-20230717155402566

LUCENE语法

LUCENE 是一个用于全文搜索和信息检索的开源库。它提供了一套功能强大的搜索和索引技术,广泛应用于许多搜索引擎和信息检索系统中。

全文搜索

在搜索栏输入200,会返回所有字段值中包含404的日志

image-20230717155819072

字段搜索

也可以按页面左侧显示的字段搜索限定字段全文搜索:field:value
精确搜索:关键字加上双引号filed:"value"
status:404 搜索http状态码为404的日志
#字段本身是否存在
_exists_:这是一个查询条件,用于检查文档中是否存在指定字段。它可以用来查找包含某个字段的文档。例如,_exists_:http 将返回包含 http 字段的文档。

_missing_:这是一个查询条件,用于检查文档中是否缺少指定字段。它可以用来查找缺少某个字段的文档。例如,_missing_:http 将返回缺少 http 字段的文档。

image-20230717160138434

模糊搜索

# ~:在一个单词后面加上~启用模糊搜索,可以搜到一些拼写错误的单词
nginx_llh~
#还可以设置编辑距离(整数),指定需要多少相似度
first~ 这种也能匹配到 frist
#默认2,越大越接近搜索的原始值,设置为1基本能搜到80%拼写错误的单词
cromm~1 会匹配到 from 和 chrome

image-20230717161658882

近似搜索

#在短语后面加上~,可以搜到被隔开或顺序不同的单词
"where select"~5 表示 select 和 where 中间可以隔着5个单词,可以搜到 select password from users where id=1

范围搜索

数值/时间/IP/字符串
类型的字段可以对某一范围进行查询
length:[100 TO 200]
sip:["172.24.20.110" TO "172.24.20.140"]
date:{"now-6h" TO "now"}
tag:{b TO e}   #搜索b到e中间的字符
count:[10 TO *]   #  * 表示一端不限制范围
count:[1 TO 5} [ ]   #表示端点数值包含在范围内,{ } 表示端点数值不包含在范围内,可以混合使用,此语句为1到5,包括1,不包括5
可以简化成以下写法:
age:>10
age:<=10
age:(>=10 AND <20)

优先级

quick^2 fox quick比fox优先级高
使用使一个词语比另一个搜索优先级更高,默认为1,可以为0~1之间的浮点数,来降低优先级

逻辑操作

AND
OR
+:搜索结果中必须包含此项
-:不能含有此项
+apache -jakarta test aaa bbb:结果中必须存在apache,不能有jakarta,剩余部分尽量都匹配到

分组

(elasticsearch OR logstash) AND kibana
elasticsearch kibana
logstash kibana

字段分组

title:(+return +"pink panther")
host:(baidu OR qq OR google) AND host:(com OR cn)
host baidu com / cn
    qq com /cn
    google com /cn

转译特殊字符

+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
以上字符当作值搜索的时候需要用\转义
\(1\+1\)\=2用来查询(1+1)=2

kibana图形化展示

Kibana支持多重图从展示功能,需要日志是json格式的支持

#打开浏览器访问10.0.0.84:5601

image-20230717162821451

已区域图举例:

image-20230717162954414

image-20230717163049475

image-20230717163833340

练习

替换日志信息使其访问ip变成国外后生成kibana地图高亮形式

#查看nginx日志,复制一条
[root@logstash conf.d]# tail -f /var/log/nginx/access.log
{"@timestamp":"2023-07-17T15:24:15+08:00","host":"10.0.0.83","clientip":"10.0.0.1","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"10.0.0.83","url":"/index.html","domain":"10.0.0.83","xff":"-","referer":"-","status":"304"}
#手动更改写入日志
[root@logstash conf.d]# echo "{\"@timestamp\":\"2023-07-17T15:24:15+08:00\",\"host\":\"10.0.0.83\",\"clientip\":\"218.176.242.6\",\"size\":0,\"responsetime\":0.000,\"upstreamtime\":\"-\",\"upstreamhost\":\"-\",\"http_host\":\"10.0.0.83\",\"url\":\"/index.html\",\"domain\":\"10.0.0.83\",\"xff\":\"-\",\"referer\":\"-\",\"status\":\"304\"}" >> /var/log/nginx/access.log
[root@logstash conf.d]# echo "{\"@timestamp\":\"2023-07-17T15:24:15+08:00\",\"host\":\"10.0.0.83\",\"clientip\":\"121.78.74.69\",\"size\":0,\"responsetime\":0.000,\"upstreamtime\":\"-\",\"upstreamhost\":\"-\",\"http_host\":\"10.0.0.83\",\"url\":\"/index.html\",\"domain\":\"10.0.0.83\",\"xff\":\"-\",\"referer\":\"-\",\"status\":\"304\"}" >> /var/log/nginx/access.log
#进入Logstash目录
[root@logstash ~]# cd /etc/logstash/
#下载地址库
[root@logstash logstash]# wget http://test.driverzeng.com/other/GeoLite2-City_20211102.tar.gz
#解压地址库文件
[root@logstash logstash]# tar xf GeoLite2-City_20211102.tar.g
#查看地址库文件
[root@logstash logstash]# ll
total 33640
drwxrwxr-x 2 root root      131 Jul 17 17:51 conf.d
drwxrwxr-x 2 root root       90 Nov  2  2021 GeoLite2-City_20211102
-rw-r--r-- 1 root root 34423245 Nov  6  2021 GeoLite2-City_20211102.tar.gz
-rw-rw-r-- 1 root root     1738 Mar 23  2017 jvm.options
-rw-rw-r-- 1 root root     1334 Mar 23  2017 log4j2.properties
-rw-rw-r-- 1 root root     4484 Jul 11 17:02 logstash.yml
-rw-rw-r-- 1 root root     1659 Mar 23  2017 startup.options
# 编写logstash配置文件
[root@logstash ~]# vim /etc/logstash/conf.d/nginx_log.conf 
input{
    file{
        type => "nginx_log"
        path => "/var/log/nginx/access.log"
        start_position => "end"
        }
}
filter {
        json {
            source => "message"
            remove_field => ["message"]
        }
        geoip {
                    source => "clientip"
                    target => "geoip"
                    database => "/etc/logstash/GeoLite2-City_20211102/GeoLite2-City.mmdb"
                    add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
                    add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]
        }
    mutate {
      convert => [ "[geoip][coordinates]", "float"]
        }
}
output{
    elasticsearch{
        hosts => ["10.0.0.81:9200"]
        index => 'logstash-%{type}-%{+yyyy.MM.dd}'
        }
}
#启动logstash
[root@logstash ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx_log.conf
#然后手动刷新nginx日志记录,重复上诉echo即可
#访问浏览器

image-20230717174215603

#添加至kibana

image-20230717174321968

选择画图类型

image-20230717182534232

选择分片索引

image-20230717182553135

image-20230717182623077