ElasticSearch之Logstash简单使用

简介: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"
		}
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值