飞道的博客

Logstash:Pipeline-to-Pipeline 通信 - 一个实例处理多种日志

576人阅读  评论(0)

使用 Logstash 的多管道功能时,你可能需要在同一 Logstash 实例中连接多个管道(Pipeline)。 此配置对于隔离这些管道的执行以及有助于打破复杂管道的逻辑很有用。 管道 input/output 启用了本文档后面讨论的许多高级体系结构模式。

如果需要在 Logstash 实例之间设置通信,请使用 Logstash 到 Logstash 的通信,或使用中间队列,例如 Kafka 或 Redis。

 

配置总揽

使用 pipeline input 和 pipeline output 来连接在同一 Logstash 实例中运行的两个管道。 这些输入使用 client/server 方法,其中管道输入注册一个虚拟地址,从而管道输出可以连接到这个地址。

  1. 创建一个 downstream 管道,以侦听虚拟地址上的事件。
  2. 创建一个产生事件的 upstream 管道,然后通过管道输出将它们发送到一个或多个虚拟地址。

这是此配置的一个简单示例。我们在 Logstash 安装的根目录下,寻找 config/pipelines.yml 文件,并做如下的配置:

config/pipelines.yml


  
  1. # config/pipelines.yml
  2. - pipeline.id: upstream
  3. config. string: input { stdin {} } output { pipeline { send_to => [myVirtualAddress] } }
  4. - pipeline.id: downstream
  5. config. string: input { pipeline { address => myVirtualAddress } }

我们使用如下的方法来运行 Logstash:

sudo ./bin/logstash

当我们从 stdin 输入任何一些字符,我们可以看到如下的输出:

从上面可以看出来:我们把从 stdin 中输入的任意字符从从一个 pipeline 发送到另外一个 pipeline。在上面,输出的地址为 myVirtualAddress。当然我们可以同时输出到多个地址中。在 downstream 中,我们可以看到在 input 中定义了一个 地址为 myVirtualAddress 的 pipeline。

在接下来的例子中,我将来展示一个比较复杂一点的带有应用场景的例子。在下面的例子中,我们将使用同样一个 Logstash 的实例同时来处理两种日志:syslog 及 apache。

 

准备数据

我们今天的数据非常简单。我们在 Filebeat 的安装根目录中创建如下的两个文件:

system.log


  
  1. May 11 10: 40: 48 scrooge disk-health-nurse[ 26783]: [ID 702911 user.error] m:SY-mon-full -500 c: H : partition health measures for /var did not suffice - still using 96% of partition space
  2. May 11 10: 00: 39 scrooge SG_child[ 808]: [ID 748625 user.info] m:WR-SG-SUMMARY c:X vhost:iscrooge61.seclutions. com: 80 (http) GET / => http:/ /bali/ , status: 200 , redirection URL:<n /a> , referer:<n/a> , mapping:bali , request size: 421 , backend response size: 12960 , audit token:- , time statistics (microseconds): [request total 16617 , allow /deny filters 1290 , backend responsiveness 11845 , response processing 1643 , ICAP reqmod <n/a> , ICAP respmod <n/a> ] timestamp: [ 2012 -05 -11 10: 00: 39] [ rid:T6zHJ38AAAEAAAo2BCwAAAMk sid: 910e5dd02df49434d0db9b445ebba975 ip: 172.18 .61 .2 ]

这是含有两个文档的 system 日志。关于日志的详细介绍,可以参阅文档

apache.log


  
  1. 83.149 .9 .216 - - [ 17 /May/2015:10:05:03 +0000] "GET /presentations/logstash-monitorama-2013/images/kibana-search.png HTTP/1.1" 200 203023 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"
  2. 83.149 .9 .216 - - [ 17 /May/2015:10:05:43 +0000] "GET /presentations/logstash-monitorama-2013/images/kibana-dashboard3.png HTTP/1.1" 200 171717 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"

同样,这也是含有两个文档的 apache.log 日志文件。

 

创建 Logstash 配置文件

在 Logstash 的安装目录下,我们会发现一个叫做 config/pipelines.yml 的文件:

我们修改改文件为如下的内容:

config/pipelines.yml


  
  1. # config/pipelines.yml
  2. - pipeline.id: beats-server
  3. config.string: |
  4. input { beats { port => 5044 } }
  5. filter {
  6. mutate {
  7. remove_field => [ "log", "host", "tags", "@version", "agent" ]
  8. }
  9. }
  10. output {
  11. if [ type] == "apache" {
  12. pipeline { send_to => weblogs }
  13. } else if [type] == "system" {
  14. pipeline { send_to => syslog }
  15. }
  16. }
  17. - pipeline.id: upstream
  18. config.string: input { stdin {} } output { pipeline { send_to => [myVirtualAddress] } }
  19. - pipeline.id: weblogs_downstream
  20. config.string: |
  21. input { pipeline { address => weblogs } }
  22. filter {
  23. grok {
  24. match => { "message" => "%{COMBINEDAPACHELOG}" }
  25. }
  26. date {
  27. match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
  28. }
  29. }
  30. output {
  31. stdout {
  32. codec => rubydebug
  33. }
  34. elasticsearch { hosts => [ "localhost:9200" ] }
  35. }
  36. - pipeline.id: syslog_downstream
  37. config.string: |
  38. input { pipeline { address => syslog } }
  39. filter {
  40. grok {
  41. match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?:%{GREEDYDATA:syslog_message}" }
  42. add_field => [ "read_es", "%{@timestamp}" ]
  43. }
  44. date {
  45. match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
  46. }
  47. mutate {
  48. remove_field => [ "message" ]
  49. remove_field => [ "syslog_timestamp" ]
  50. convert => [ "bool", "boolean"]
  51. rename => { "ip" => "[geoPoint][ip]" }
  52. rename => { "latitude" => "[geoPoint][latitude]" }
  53. rename => { "longitude" => "[geoPoint][longitude]" }
  54. rename => { "location" => "[geoPoint][location]" }
  55. }
  56. }
  57. output {
  58. stdout {
  59. codec => rubydebug
  60. }
  61. elasticsearch { hosts => [ "localhost:9200" ] }
  62. }

在上面,其实非常简单。我首先看如下的部分:


  
  1. - pipeline.id: beats-server
  2. config.string: |
  3. input { beats { port => 5044 } }
  4. filter {
  5. mutate {
  6. remove_field => [ "log", "host", "tags", "@version", "agent" ]
  7. }
  8. }
  9. output {
  10. if [ type] == "apache" {
  11. pipeline { send_to => weblogs }
  12. } else if [type] == "system" {
  13. pipeline { send_to => syslog }
  14. }
  15. }

在上面它接受从 port 为 5044 端口的数据。我们首先使用 filter 来把一些不需要的字段删除掉,这样我们的显示更加好看一些。在输出的部分,我们来依据文档的类型从而发送到不同的 pipeline 去做处理。如果是 apache 日志,我们发送到地址为 weblogs 的 pipeline 中去。如果类型是 system,那么我们发送至地址为 syslog 的 pipeline 中。在接下来的两个 pipeline:

  • pipeline.id: upstream
  • pipeline.id: syslog_downstream

它们分别针对不同的日志进行不同的数据清洗。因为不同的日志有不同的清洗方法,所以在我们使用不同的 pipeline 来分别进行处理。

我们可以使用如下的方法来启动 Logstash:

sudo ./bin/logstash -r 

 

启动 Filebeat 导入数据

我们接下来使用 Filebeat 来把数据导入到 Logstash 中。安装好我们的  Filebeat,并修改 Fillebeat 的配值文件。请在文件的前面添加如下的部分:

filebeat.yml


  
  1. filebeat.inputs:
  2. # Each - is an input. Most options can be set at the input level, so
  3. # you can use different inputs for various configurations.
  4. # Below are the input specific configurations.
  5. - type: log
  6. # Change to true to enable this input configuration.
  7. enabled: true
  8. fields_under_root: true
  9. fields:
  10. type: "apache"
  11. # Paths that should be crawled and fetched. Glob based paths.
  12. paths:
  13. - ./apache.log
  14. - type: log
  15. # Change to true to enable this input configuration.
  16. enabled: true
  17. fields_under_root: true
  18. fields:
  19. type: "system"
  20. # Paths that should be crawled and fetched. Glob based paths.
  21. paths:
  22. - ./system.log

在上面,我们添加了对 Filebeat 安装目录下的 system.log 及 apache.log 的数据采集。同时为了能够把数据导入到 Logstash 中的 5044 端口地址,我们需要针对 filebeat.yml 做更进一步的修改:

filebeat.yml


  
  1. # output.elasticsearch:
  2. # Array of hosts to connect to.
  3. # hosts: ["localhost:9200"]
  4. # Protocol - either `http` (default) or `https`.
  5. # protocol: "https"
  6. # Authentication credentials - either API key or username/password.
  7. # api_key: "id:api_key"
  8. # username: "elastic"
  9. # password: "changeme"
  10. # ------------------------------ Logstash Output -------------------------------
  11. output.logstash:
  12. # The Logstash hosts
  13. hosts: ["localhost:5044"]

在上面,我们禁止直接导入到 Elasticsearch。我们启动  Logstash  的输出。我们使用如下的方法来启动 Filebeat:

./filebeat -e

从输出中,我们可以看到有两个 havester 已经被创建。

 

在 Logstash 以及  Kibana 中检查数据

我们查看 Logstash 的输出 terminal:

从上面,我们可以看到有数据输出。

我们同时查看 Kibana:

GET logstash/_search

我们可以看到有四个文档被导入:

 

针对 Linux 的 Logstash 配置

在上面的 pipelines.yml 中,把各个 pipeline 放入一个文件中显得非常凌乱。对于使用 RPM 或者 DEB 安装包的 Logstash 用户来说,我们可以使用如下的方式来进行简化明了。

/etc/logstash/pipelines.yml


  
  1. # config/pipelines.yml
  2. - pipeline.id: beats-server
  3. config.string: |
  4. input { beats { port => 5044 } }
  5. filter {
  6. mutate {
  7. remove_field => [ "log", "host", "tags", "@version", "agent" ]
  8. }
  9. }
  10. output {
  11. if [ type] == "apache" {
  12. pipeline { send_to => weblogs }
  13. } else if [type] == "system" {
  14. pipeline { send_to => syslog }
  15. }
  16. }
  17. - pipeline.id: weblogs_downstream
  18. path.config: "/etc/logstash/conf.d/weblogs.conf"
  19. - pipeline.id: syslog_downstream
  20. path.config: "/etc/logstash/conf.d/syslog.conf"

我们接着在位置 /etc/logstash/conf.d 创建如下的两个文件:

weblogs.conf


  
  1. input { pipeline { address => weblogs } }
  2. filter {
  3. grok {
  4. match => { "message" => "%{COMBINEDAPACHELOG}" }
  5. }
  6. date {
  7. match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
  8. }
  9. }
  10. output {
  11. stdout {
  12. codec => rubydebug
  13. }
  14. elasticsearch { hosts => [ "localhost:9200" ] }
  15. }

syslog.conf


  
  1. input { pipeline { address => syslog } }
  2. filter {
  3. grok {
  4. match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} \[%{DATA}\] %{DATA} %{WORD:type} \"userText\":\"%{DATA:userText}\",\"prduction\":%{NUMBER:prduction:int},\"version\":%{NUMBER:version:int},\"bool\":%{DATA:bool},\{\"geoPoint\":\{\"location\":\"%{DATA:location}\",\"ip\":\"%{IP:ip}\",\"latitude\":%{BASE10NUM:latitude},\"longitude\":%{BASE10NUM:longitude},(\"optionalField\":%{BASE10NUM:optionalField},)?\}\}" }
  5. add_field => [ "read_es", "%{@timestamp}" ]
  6. }
  7. date {
  8. match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
  9. }
  10. mutate {
  11. remove_field => [ "message" ]
  12. remove_field => [ "syslog_timestamp" ]
  13. convert => [ "bool", "boolean"]
  14. rename => { "ip" => "[geoPoint][ip]" }
  15. rename => { "latitude" => "[geoPoint][latitude]" }
  16. rename => { "longitude" => "[geoPoint][longitude]" }
  17. rename => { "location" => "[geoPoint][location]" }
  18. }
  19. }
  20. output {
  21. stdout {
  22. codec => rubydebug
  23. }
  24. elasticsearch { hosts => [ "localhost:9200" ] }
  25. }

在配置完上面的文件后,你需要对 Logstash 进行重启:

service logstash restart

 


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