Logstash
一、Logstash简介
Logstash 是一个实时数据收集引擎,可收集各类型数据并对其进行分析,过滤和归纳。按照自己条件分析过滤出符合数据导入到可视化界面。它可以实现多样化的数据源数据全量或增量传输,数据标准格式处理,数据格式化输出等的功能,常用于日志处理。工作流程分为三个阶段:
(1)input数据输入阶段,可接收oracle、mysql、postgresql、file等多种数据源;
(2)filter数据标准格式化阶段,可过滤、格式化数据,如格式化时间、字符串等;
(3)output数据输出阶段,可输出到elasticsearch、mongodb、kfka等接收终端。
二、下载
https://artifacts.elastic.co/downloads/logstash/logstash-6.3.1.tar.gz
三.案例实战
一:控制台 To 控制台
shell
[root@itstar111 logstash-6.4.3]# bin/logstash -e ‘input { stdin { } } output { stdout {} }’
二:控制台 To ES
[root@master bin]# cat test_es.conf
input{
stdin{}
}
output{
elasticsearch{
hosts=>[“主机名:9200”]
index=>“testeslogstash”
}
stdout{codec=>rubydebug}
}
三:jason文件 To ES
[root@master mnt]# cat data.json
{“age”:16,“name”:“tom”}
{“age”:11,“name”:“tsd”}
[root@master bin]# cat json.conf
input{
file{
path=>"/mnt/data.json"
start_position=>“beginning”
sincedb_path=>"/dev/null"
codec=>json{
charset=>“ISO-8859-1”
}
}
}
output{
elasticsearch{
hosts=>“http://主机名:9200”
index=>“jsontestlogstash”
document_type=>“doc”
}
stdout{}
}
增加部分字段
[root@master bin]# cat json.conf
input{
file{
path=>"/mnt/data.json"
start_position=>“beginning”
sincedb_path=>"/dev/null"
codec=>json{
charset=>“ISO-8859-1”
}
}
}
filter{
mutate {
remove_field => “@timestamp”
remove_field => “@version”
remove_field => “host”
remove_field => “path”
}
}
output{
elasticsearch{
hosts=>“http://主机名:9200”
index=>“jsontestlogstash”
document_type=>“doc”
}
stdout{}
}
四:时区\类型转换\字段删除
#输入
input {
file {
path => [“文件路径”]
#自定义类型
type => “自定义”
start_position => “beginning”
}
}
#过滤器
filter{
#去除换行符
mutate{
gsub => [ “message”, “\r”, “” ]
}
#逗号分割
mutate {
split => [“message”,","]
}
#分割后,字段命名与赋值
mutate{
add_field => {
“id” => “%{[message][0]}”
“cc” => “%{[message][5]}”
“bcc” => “%{[message][6]}”
“from_user” => “%{[message][7]}”
“size” => “%{[message][8]}”
“attachments” => “%{[message][9]}”
“content” => “%{[message][10]}”
}
}
#字段里的日期识别,以及时区转换,生成date
date {
match => [ “mydate”, “MM/dd/yyyy HH:mm:ss” ]
target => “date”
locale => “en”
timezone => “+00:00”
input {
file {
path => [“文件路径”]
#自定义类型
type => “自定义”
start_position => “beginning”
}
#过滤器
filter{
#去除换行符
mut