小言_互联网的博客

Beats:如何测试 Beats processors

456人阅读  评论(0)

在之前的 “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


  
  1. logging.level: error
  2. filebeat.inputs:
  3. - type: stdin
  4. processors:
  5. - add_fields:
  6. target: example
  7. fields:
  8. key1: val1
  9. key2: val2
  10. output.console:
  11. pretty: true

在上面,它的设计非常之简单。我们使用了 add_fields 这个 processor 来进行演示。在使用中,我们可以使用如下的命令来进行运行:

echo "message" | ./filebeat -c ~/data/filebeat_test/filebeat.yml -e 2> /dev/null

上面的命令显示:


  
  1. $ echo "message" | ./filebeat -c ~ /data/filebeat_test/filebeat.yml -e 2> /dev/ null
  2. {
  3. "@timestamp": "2021-03-17T09:02:08.483Z",
  4. "@metadata": {
  5. "beat": "filebeat",
  6. "type": "_doc",
  7. "version": "7.11.0"
  8. },
  9. "example": {
  10. "key2": "val2",
  11. "key1": "val1"
  12. },
  13. "log": {
  14. "offset": 0,
  15. "file": {
  16. "path": ""
  17. }
  18. },
  19. "message": "message",
  20. "input": {
  21. "type": "stdin"
  22. },
  23. "ecs": {
  24. "version": "1.6.0"
  25. },
  26. "host": {
  27. "name": "liuxg"
  28. },
  29. "agent": {
  30. "ephemeral_id": "16146b1d-6135-4834-bd5d-3dccdf0ef864",
  31. "id": "e2b7365d-8953-453c-87b5-7e8a65a5bc07",
  32. "name": "liuxg",
  33. "type": "filebeat",
  34. "version": "7.11.0",
  35. "hostname": "liuxg"
  36. }
  37. }

在上面,我们看到有添加 key1 和 key 2 到 example 字段下。上面的执行速度非常快 :)这个方法的好处也是非常明显的,我们不用把数据导入到 Elasticsearch 中去并查看。

我们也可以拿我们之前在  “Beats:Beats processors” 文章中介绍的例子来进行展示。

我们创建数据文件 sample.log:

sample.log


  
  1. "321 - App01 - WebServer is starting"
  2. "321 - App01 - WebServer is up and running"
  3. "321 - App01 - WebServer is scaling 2 pods"
  4. "789 - App02 - Database is will be restarted in 5 minutes"
  5. "789 - App02 - Database is up and running"
  6. "789 - App02 - Database is refreshing tables"

我们创建如下的 filebeat.yml 配置文件:

filebeat.yml


  
  1. logging.level: error
  2. filebeat.inputs:
  3. - type: stdin
  4. processors:
  5. - dissect:
  6. tokenizer: '"%{pid|integer} - %{service.name} - %{service.status}"'
  7. field: "message"
  8. target_prefix: ""
  9. - drop_fields:
  10. fields: [ "ecs", "agent", "log", "input", "host"]
  11. output.console:
  12. pretty: true

 我们可以使用如下的方式来进行运行:

 cat sample.log | ./filebeat -c ~/data/filebeat_test/filebeat.yml -e 2> /dev/null

 上面的命令显示的结果为:


  
  1. {
  2. "@timestamp": "2021-03-17T09:11:02.812Z",
  3. "@metadata": {
  4. "beat": "filebeat",
  5. "type": "_doc",
  6. "version": "7.11.0"
  7. },
  8. "service": {
  9. "name": "App01",
  10. "status": "WebServer is starting"
  11. },
  12. "message": "\" 321 - App01 - WebServer is starting\ "",
  13. "pid": 321
  14. }
  15. {
  16. "@timestamp": "2021-03-17T09:11:02.812Z",
  17. "@metadata": {
  18. "beat": "filebeat",
  19. "type": "_doc",
  20. "version": "7.11.0"
  21. },
  22. "service": {
  23. "status": "WebServer is up and running",
  24. "name": "App01"
  25. },
  26. "pid": 321,
  27. "message": "\" 321 - App01 - WebServer is up and running\ ""
  28. }
  29. ...

显然在上面我们可以很清楚地看到 Beats processors 处理的结果。


转载:https://blog.csdn.net/UbuntuTouch/article/details/114937542
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场