一、Logstash:用于收集、分析、处理日志。
1、准备主机
准备主机192.168.1.57,修改/etc/hosts文件(ELK所有主机上都相同)
192.168.1.51 es1
192.168.1.52 es2
192.168.1.53 es3
192.168.1.54 es4
192.168.1.55 es5
192.168.1.56 kibana
192.168.1.57 logstash
2、安装相关软件包
yum -y install java-1.8.0-openjdk
yum -y install logstash # 默认安装在/opt/logstash下
3、创建配置文件
logstash默认没有配置文件,需要自定义配置文件
vim /etc/logstash/logstash.conf # 创建配置文件
配置文件中需要分为三个部分(模块)
①、input{} 读取数据
②、filter{} 处理数据
③、output{} 输出数据
4、配置文件语法:
类型:(变量名=>值)
布尔:ssl_enable=>true # 给ssl_enable赋值为true
字节:bytes=>"1MiB"
字符串:name=>"jack"
数值:port=>22
数组:match=>["abc", "filter"]
哈希:options=>[k=>'v', k2=>'v2']
编码解码:codec=>“json” # 支持yaml,json等格式
路径:file_path=>"/tmp/filename"
注释:#
条件判断:
==, !=, <, >, <=, >=,
=~ # 匹配正则
!= # 不匹配正则
5、第一个配置文件
vim /etc/logstash/logstash.conf
input{
stdin{} # 插件,表示手动输入
}
filter{
}
output{
stdout{} #插件,表示输出到屏幕
}
/opt/logstash/bin/logstash -f /etc/logstash/logstash.conf #根据配置文件执行logstash
6、插件:
像stdin{},stdout{}都属于插件。
/opt/logstash/bin/logstash-plugin list # 查看可用的插件
/opt/logstash/bin/logstash-plugin install xxx # 安装插件
/opt/logstash/bin/logstash-plugin uninstall xxx #卸载插件
# 到https://github.com/logstash-plugins可以查看所有插件以及手册
插件的分类主要有四种:
input:仅用于input模块
output:仅用于output模块
filter:仅用于output模块
codec:可以用于所有模块
vim /etc/logstash/logstash.conf
input{
stdin{
codec => "json" //输入设置为编码json
}
}
filter{
}
output{
stdout{
codec => "rubydebug" //输出设置为rubydebug
}
}
7、常用插件:
input-file插件:从文件中读取数据
input{
file { # 一个input中可以包含多个file插件
path => [ "/tmp/a.log", "/tmp/b.log" ]
sincedb_path => "/var/lib/logstash/sincedb" # 记录读取文件的位置,logstash关闭时向这个文件中写数据
start_position => "beginning" # 配置第一次读取文件从什么地方开始
type => "testlog" # 类型名称,标签,日志的种类(自定义)
}
}
filter-grok插件:解析各种非结构化日志
①:使用正则表达式
filter{
grok{
match => [ "message", "(?<client_ip>[0-9.]+)" ] # 正则表达式,给匹配到的ip地址取名为client_ip
}
}
②:使用宏(别人定义好的正则)
在/opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.5/patterns/这个目录下,grok-patterns这个文件
filter{
grok{
match => ["message", "%{COMBINEDAPACHELOG}"] # 使用了宏,标准apache正则
}
}
output-elasticsearch插件:向elasticsearch中写入数据
output{
stdout{ codec => "rubydebug" }
if [type] == "apachelog"{ # 接受日志的类型,在web服务器上的filebeat中定义为 document_type: apachelog
elasticsearch {
hosts => ["192.168.1.51:9200", "192.168.1.52:9200"] # elasticsearch集群中主机
index => "apachelog" # elasticsearch中的index
flush_size => 2000 # 数据累计超过2000字节,向数据库写入一次
idle_flush_time => 10 # 10秒向数据库写入一次
}
}
}
input-beats:接受web服务器上通过filebeat发送过来的数据
input{
beats{
port=>5044 # 通过5044接受filebeat发送的日志数据
}
}
8、web服务器向Logstash发送数据:(web服务器192.168.1.55)
yum -y install filebeat # 从本地读取日志,然后发送给elasticsearch或者logstash
vim/etc/filebeat/filebeat.yml
paths:
- /var/log/httpd/access_log # 日志的路径,短横线加空格代表yml格式
document_type: apachelog # 日志类型,在logstash中使用
elasticsearch: # 发送给elasticsearch主机
hosts: ["es5:9200"] # elasticsearch主机
logstash: # 发送给logstash主机
hosts: ["192.168.1.57:5044"] # logstash主机
systemctl enable filebeat
systemctl start filebeat
给logstash的filter加上判断(192.168.1.57)
filter{
if [type] == "apachelog"{ # 日志类型,在web服务器上的filebeat中定义为 document_type: apachelog
grok{
match => ["message", "%{COMBINEDAPACHELOG}"]
}
}
}
二、Kibana:elk可视化工具
1、准备主机(192.168.1.56)
同上,
2、安装软件包
yum -y install kibana
rpm -qc kibana # -qc查看对应软件包配置文件的位置
3、修改配置文件
vim /opt/kibana/config/kibana.yml
server.port: 5601 # 第2行,服务端口,若把端口改为80,可以成功启动kibana,但ss时没有端口,没有监听80端口,服务里面写死了,不能用80端口,只能是5601这个端口
server.host: "0.0.0.0" # 第5行,服务器监听地址
elasticsearch.url: http://192.168.1.51:9200 # 第15 行,es集群中任意一台主机
kibana.index: ".kibana" # 第23行,kibana自己创建的索引,名字为.kibana
kibana.defaultAppId: "discover" # 第26行,打开kibana页面时,默认打开的页面discover,即默认首页
elasticsearch.pingTimeout: 1500 # 第53行,ping检测超时时间
elasticsearch.requestTimeout: 30000 # 第57行,请求超时
elasticsearch.startupTimeout: 5000 # 第64行,启动超时
4、启动服务
systemctl restart kibana
systemctl enable kibana
访问:http://192.168.1.56://5601
5、测试
提前将数据导入到elasticsearch中,然后访问http://192.168.1.56://5601进行分析