Logstash埋点日志整理:
上一篇文章,我们将埋点日志自动生成,后来我把代码稍作修改,将服务器和用户的日志进行分别输出到各自的文件中。
然后我们需要使用logstash将从各种地方得到的不同的文件格式进行整理建表,最后输送到ElasticSearch中进行存储。
PS:logstash一般与部署tomcat的服务器装在一起,ElasticSearch由于索引较大,需要专门装载一个服务器.
logstash处理数据的流程:一般是在本地获取抓取格式文件,然后过滤器进行整理数据格式,远程连接输出到ElasticSearch服务器上。
一:Logstash安装:
Logstash需要与ElasticSearch保持版本配合,之前的ElasticSearch用的是6.2.2版本,所以我们的Logstash也下载此版本6.2.2,以下是tar包链接,分享出来请自行下载。
第一步:
将安装包放在opt目录下,并将它解压
tar -zxvf logstash-6.2.2.tar.gz
第二步:
将解压后的文件的转到/opt/soft/logstash622目录下
mv logstash-6.2.2 /opt/soft/logstash622
第三步:
走到logstash622/bin目录下,输入基本命令开启logstash
# 标准控制台输入和输出 , -e使用给定的字符串
./logstash -e 'input { stdin {} } output { stdout {codec => rubydebug}}'
二:处理埋点日志信息数据格式
第一步:
将idea的java程序打成胖包,传给linux,在Linux执行命令运行jar包,生成100300条数据
java -jar +(输入对应的jar包)
我们需要将命令封装,写到配置文件里,然后我们运行对应的配置文件mylog.conf即可,将命令写进去。
./logstash -f /opt/config/mylog.conf
./logstash -e ‘input { stdin {} } output { stdout {codec => rubydebug}}’// 使用给定的字符串
# 将其写在配置文件config里
input {
file {
path => "/opt/aa.txt"
start_position => "beginning" # 从文件开始读
sincedb_path => "/dev/null" # 不管对文件修不修改,每次读取此文件时,都会读取一遍,不加的话只有文件修改过后才会被全量读取。
}
}
output {
stdout {
codec => rubydebug # 输入时用code代码的新格式
}
}
1.JSON字符串过滤:
对收到的服务器日志的本身是json,但是存在嵌套json,我们将其做成二维表需要将json扁平化
- mutate过滤器:
- 能够帮助你修改指定字段的内容。
# 将嵌套JSON扁平化
input {
file {
path => "/opt/aa.txt"
start_position => "beginning"
sincedb_path => "/dev/null"
codec => json
}
}
filter {
mutate {
add_field => { '@adv' => '%{cm}' } # 先新建一个新的字段,并将friends赋值给它
}
json {
source => "@adv" # 对字段再进行json格式解析
remove_field => [ "@adv" , "cm" ] # 删除不必要的字段,也可以不用这语句
}
}
output {
stdout {
codec => rubydebug
}
}
输入命令运行:
./logstash -f /opt/config/mylog.conf
PS-1: add_field 新建表名
PS-2: source 再次对其解析成json
PS-3: remove_field 对多余的cm和@adv字段进行删除
2.正则匹配过滤:
收到的服务器的日志信息是用 | 分割的字符串。
- grok语法:
- 正则匹配:
(?<字段名>正则表达式)
# IP和NUMBER是预先定好的正则表达式
# | 是指分割以|为符号的字符串
%{IP:字段名}\|%{NUMBER:字段名}
输入命令运行:
./logstash -f /opt/config/mylog.conf
3.多文件和多过滤:
将服务器和用户的日志一次性全部处理:
需要加入多个file并对其进行type打标签和if逻辑判断完成多过滤
input {
file {
path => "/opt/bb.txt"
start_position => "beginning"
sincedb_path => "/dev/null"
type => "action"
codec => json
}
file {
path => "/opt/aa.txt"
start_position => "beginning"
sincedb_path => "/dev/null"
type => "system"
}
}
filter {
if [type] == "system" {
grok {
match => { "message" => "(?<userid>[0-9]+)\|(?<event_name>[a-zA-Z_]+)\|(?<times>[0-9]+)\|(?<clientip>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" }
remove_field => [ "message" ]
}
} else {
mutate {
add_field => { "nice" => "%{cm}" }
}
json {
source => "nice"
remove_field => [ "nice" , "cm" ]
}
}
}
output {
stdout {
codec => rubydebug
}
}
输入命令运行:
./logstash -f /opt/config/mylog.conf
分割后的效果
4.输出到Elasticsearch
input {
file {
path => "/opt/bb.txt"
start_position => "beginning"
sincedb_path => "/dev/null"
type => "action"
codec => json
}
file {
path => "/opt/aa.txt"
start_position => "beginning"
sincedb_path => "/dev/null"
type => "system"
}
}
filter {
if [type] == "system" {
grok {
match => { "message" => "(?<userid>[0-9]+)\|(?<event_name>[a-zA-Z_]+)\|(?<times>[0-9]+)\|(?<clientip>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" }
remove_field => [ "message" ]
}
} else {
mutate {
add_field => { "nice" => "%{cm}" }
}
json {
source => "nice"
remove_field => [ "nice" , "cm" ]
}
}
}
output {
if [type] == "system" {
elasticsearch {
hosts => "http://192.168.56.101:9200" #ip位置
index => "systems" # es的索引--库
document_type => "sys" # es的type--表
}
} else {
elasticsearch {
hosts => "http://192.168.56.101:9200"
index => "customs"
document_type => "actions"
}
}
}
启动ElasticSearch和ElasticSearch-head查看效果:
ElasticSearch拿到了100300条数据