预热:基础知识
Logstash 是一个开源的数据收集引擎,它具有备实时数据传输能力。它可以统一过滤来自不同源的数据,并按照开发者的制定的规范输出到目的地。 顾名思义,Logstash 收集数据对象就是日志文件。由于日志文件来源多(如:系统日志、服务器 日志等),且内容杂乱,不便于人类进行观察。因此,我们可以使用 Logstash 对日志文件进行收集和统一过滤,变成可读性高的内容,方便开发者或运维人员观察,从而有效的分析系统/项目运行的性能,做好监控和预警的准备工作等。
1,安装
下载:
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.5.3.tar.gz
解压:
tar -zxvf logstash-6.5.3.tar.gz
2,配置文件结构
#输入(must)
input {
#beats {
#port => 5044
#}
#从文件读取日志信息
#file {
#path => "/var/log/messages"
#type => "system"
#start_position => "beginning"
#}
}
#过滤(可选)
filter {
}
#输出(must)
output {
elasticsearch {
hosts => ["http://localhost:9200"]
#index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
index => "test-log"
#user => "elastic"
#password => "changeme"
}
}
2.1,input基本讲解
输入支持的类型有很多,官方文档记载
常用的有
input {
#从filebeat里面取值
beats {
port => 5044
}
#从文件读取日志信息
#file {
#path => "/var/log/messages"
#type => "system"
#start_position => "beginning"
#}
}
2.2,output基本讲解
output同样支持很多组件:
常用的输出组件有:
output {
#输出到elasticseartch
elasticsearch {
hosts => ["http://localhost:9200"]
#index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
index => "test-log"
#user => "elastic"
#password => "changeme"
}
#file {
#path => /usr/local/test-2013-05-29.txt
#codec => line { format => "custom format: %{message}"}
#gzip => false #Gzip the output stream before writing to disk.
#}
#stdout { codec => json } #输出到控制台 json格式
}
2.3,filter 基本讲解
logstash支持多种过滤器,
常用的过滤器如下:
grok :用于匹配log,生成模块化数据,匹配成功的话,会在返回的字段中多几个自定义的模块化字段
filter {
grok {
match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %
{NUMBER:bytes} %{NUMBER:duration}" }# 55.3.244.1 GET /index.html 15824 0.043
}
}
example log: 55.3.244.1 GET /index.html 15824 0.043
返回的json数据:
{
"offset":10,
"@version":"1",
"beat":{
"name":"izuf6136vql89ybnyypmbaz",
"hostname":"izuf6136vql89ybnyypmbaz",
"version":"6.5.3"
},
"message":"55.3.244.1 GET /index.html 15824 0.043",
"request":"/index.html",
"input":{
"type":"log"
},
"host":{
"id":"963c2c41b08343f7b063dddac6b2e486",
"name":"izuf6136vql89ybnyypmbaz",
"os":{
"version":"7 (Core)",
"codename":"Core",
"platform":"centos",
"family":"redhat"
},
"containerized":true,
"architecture":"x86_64"
},
"prospector":{
"type":"log"
},
"bytes":"15824",
"duration":"0.043",
"source":"/usr/local/logs/catalina.out",
"client":"55.3.244.1",
"@timestamp":"2018-12-22T08:44:09.372Z",
"tags":[
"beats_input_codec_plain_applied"
],
"method":"GET",
"meta":{
"cloud":{
"availability_zone":"cn-shanghai-d",
"region":"cn-shanghai",
"instance_id":"i-uf6136vql89ybnyypmba",
"provider":"ecs"
}
}
}
3,启动
bin/logstash -f logstash.conf
更多配置:https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html