简介:Logstash 是免费且开放的服务器端数据处理管道,能够从多个来源采集数据,转换数据,然后将数据发送到您最喜欢的“存储库”中。
1.下载并解压logstash的压缩包,我这边用的是6.2.2
logstash-6.2.2.tar.gz
2.运行解压后的bin/logstash脚本,我这是控制台上最简单的输入和输出
[root@zhangqi bin]# ./logstash -e 'input { stdin {} } output { stdout {} }'
3.注意:logstash的配置文件,由3个部分构成,input {} 输入|filter {} 过滤 | output {} 输出 ,输入和输出是必须的,过滤器是可选的
4.种类:可以是运行时,编辑运行时的配置,也可以时将运行的信息写在配置文件中,读取配置文件信息,举例如下
4-1 运行时,编辑配置
[root@zhangqi bin]# ./logstash -e 'input { stdin {} } output { stdout {} }'
4-2 运行时,读取配置文件
[root@zhangqi bin]# ./logstash -f /opt/config/mylog.conf
5.基本用法
5-1:示例一:
匹配的字符串:输入为字符串嵌套双重json,过滤后输出到控制台
123123|input_txt|{“time”:“1999-02-15”,“browser”:“firefox”,“zq”:{“name”:“zhangsan”,“gender”:“female”}}|UTC
logstash的配置文件编写:
input {
file {
path => "/opt/zhangqi/zhangqi.txt"
start_position => "beginning"
sincedb_path => "/dev/null"
}
}
filter {
grok {
match => {
"message" => "(?<userid>[0-9]+)\|(?<action>[a-z_]+)\|(?<info>.*)\|(?<area>[A-Z].*)"
}
}
json {
source => "info"
}
mutate {
add_field => { "new"=>"%{zq}" }
}
json {
source => "new"
remove_field => [ "new","zq","info","message" ]
}
}
output {
stdout {
codec => "rubydebug"
}
}
5-2.示例二:
匹配的两个字符串:单独的两个输入文件,过滤后,输出到elasticsearch中
文件一:
18311|event_login|948344400780|192.168.56.141
文件二:
{“browser”:“mozilla:true version:12”,“custid”:“43464”,“cm”:{“eventCategory”:“btn_click”,“position”:“126 65”,“time”:“2006-01–04 06:03:00”,“pageName”:“http://localhost:8080/testlist.html”,“msg”:“python”}}
logstash的配置文件编写:
input{
file {
path => "/opt/system/sys.log"
start_position => "beginning"
sincedb_path => "/dev/null"
type => "system"
}
file {
path => "/opt/action/user.log"
start_position => "beginning"
sincedb_path => "/dev/null"
codec => json
type => "action"
}
}
filter{
if [type] == "system" {
grok {
match => { "message" => "(?<userid>[0-9]+)\|(?<event_name>[a-zA-Z_]+)\|(?<times>[0-9]+)\|(?<ip>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" }
remove_field => [ "message" ]
}
}else {
mutate {
add_field => { "@new" => "%{cm}" }
}
json {
source => "@new"
remove_field => [ "@new","cm" ]
}
}
}
output{
if [type] == "system"{
elasticsearch {
hosts => "192.168.56.106:9200"
index => "systems"
document_type => "sys"
}
}else {
elasticsearch {
hosts => "192.168.56.106:9200"
index => "customs"
document_type => "actions"
}
}
}