在之前的 “Beats:Beats processors” 文章中,我详细地描述了如何使用 Beat 的 processors 对数据进行清洗。在很多情况下它是非常有用的一种方法。Beats 的 processors 有很多在 ingest pipeline 的 processors 中 以及 Logstash 的过滤器中都有相应的实现。针对 ingest pipeline,我们可以使用 Simulate pipeline API 来进行测试,而对于 Logstash 它也有一个很好的方法就是直接输出到 console 中进行显示。如果你想了解的话,请参阅文章 “Logstash:Logstash 入门教程 (二)”。 那么针对 Beats processors,我们有没有一种办法不写入到 Elasticsearch 而直接进行测试呢?
答案是肯定的,我们也可以把 Beats 的输出写到 console 中,这样很方便我们进行测试。
我们首先来创建一个如下的 filebeat 的配置文件:
filebeat.yml
-
logging.level: error
-
-
filebeat.inputs:
-
-
type:
stdin
-
-
processors:
-
-
add_fields:
-
target:
example
-
fields:
-
key1:
val1
-
key2:
val2
-
-
output.console:
-
pretty:
true
在上面,它的设计非常之简单。我们使用了 add_fields 这个 processor 来进行演示。在使用中,我们可以使用如下的命令来进行运行:
echo "message" | ./filebeat -c ~/data/filebeat_test/filebeat.yml -e 2> /dev/null
上面的命令显示:
-
$ echo
"message" | ./filebeat -c ~
/data/filebeat_test/filebeat.yml -e
2>
/dev/
null
-
{
-
"@timestamp":
"2021-03-17T09:02:08.483Z",
-
"@metadata": {
-
"beat":
"filebeat",
-
"type":
"_doc",
-
"version":
"7.11.0"
-
},
-
"example": {
-
"key2":
"val2",
-
"key1":
"val1"
-
},
-
"log": {
-
"offset":
0,
-
"file": {
-
"path":
""
-
}
-
},
-
"message":
"message",
-
"input": {
-
"type":
"stdin"
-
},
-
"ecs": {
-
"version":
"1.6.0"
-
},
-
"host": {
-
"name":
"liuxg"
-
},
-
"agent": {
-
"ephemeral_id":
"16146b1d-6135-4834-bd5d-3dccdf0ef864",
-
"id":
"e2b7365d-8953-453c-87b5-7e8a65a5bc07",
-
"name":
"liuxg",
-
"type":
"filebeat",
-
"version":
"7.11.0",
-
"hostname":
"liuxg"
-
}
-
}
在上面,我们看到有添加 key1 和 key 2 到 example 字段下。上面的执行速度非常快 :)这个方法的好处也是非常明显的,我们不用把数据导入到 Elasticsearch 中去并查看。
我们也可以拿我们之前在 “Beats:Beats processors” 文章中介绍的例子来进行展示。
我们创建数据文件 sample.log:
sample.log
-
"321 - App01 - WebServer is starting"
-
"321 - App01 - WebServer is up and running"
-
"321 - App01 - WebServer is scaling 2 pods"
-
"789 - App02 - Database is will be restarted in 5 minutes"
-
"789 - App02 - Database is up and running"
-
"789 - App02 - Database is refreshing tables"
我们创建如下的 filebeat.yml 配置文件:
filebeat.yml
-
logging.level: error
-
-
filebeat.inputs:
-
-
type:
stdin
-
-
processors:
-
-
dissect:
-
tokenizer:
'"%{pid|integer} - %{service.name} - %{service.status}"'
-
field:
"message"
-
target_prefix:
""
-
-
drop_fields:
-
fields: [
"ecs",
"agent",
"log",
"input",
"host"]
-
-
output.console:
-
pretty:
true
我们可以使用如下的方式来进行运行:
cat sample.log | ./filebeat -c ~/data/filebeat_test/filebeat.yml -e 2> /dev/null
上面的命令显示的结果为:
-
{
-
"@timestamp":
"2021-03-17T09:11:02.812Z",
-
"@metadata": {
-
"beat":
"filebeat",
-
"type":
"_doc",
-
"version":
"7.11.0"
-
},
-
"service": {
-
"name":
"App01",
-
"status":
"WebServer is starting"
-
},
-
"message":
"\"
321 - App01 - WebServer
is starting\
"",
-
"pid":
321
-
}
-
{
-
"@timestamp":
"2021-03-17T09:11:02.812Z",
-
"@metadata": {
-
"beat":
"filebeat",
-
"type":
"_doc",
-
"version":
"7.11.0"
-
},
-
"service": {
-
"status":
"WebServer is up and running",
-
"name":
"App01"
-
},
-
"pid":
321,
-
"message":
"\"
321 - App01 - WebServer
is up
and running\
""
-
}
-
...
显然在上面我们可以很清楚地看到 Beats processors 处理的结果。
转载:https://blog.csdn.net/UbuntuTouch/article/details/114937542