一、采集日志多行合并

配置示例:

filebeat.inputs:
 - type: log
  enabled: true
  encoding: GB2312
  paths:
    - D:\software\eclipse\logs\SCC-node1.log
  multiline.pattern: ^\[dic-log]
  multiline.negate: true
  multiline.match: after
  #multiline.timeout: 5s

配置说明:

  • encoding是指定编码集,避免中文乱码
  • paths是采集日志文件的地址,可配置成指定文件,也可配置成通配形式,例:\logs*.log
  • multiline下一些配置即为多行合并采集的一些参数
  • multiline.pattern:正则表达式,匹配那些日志符合合并的条件
  • multiline.negate:该配置为是否取反的意思,默认为false
  • multiline.match:取值为after或before,表示匹配到的这一行日志是合并到上一条日志还是下一条日志,若为after,这将匹配到当行日志合并到上一行日志后面作为一条日志;若为before,这合并到下一行日志前面作为一条日志
  • multiline.timeout: 表示超时时间,如果超过timeout还没有新的一行日志产生,则自动结束当前的多行、形成一条日志发出去。

特别说明一下multiline.pattern和multiline.negate的配合使用

1、multiline.negate默认为false即逻辑不取反,这时只有符合multiline.pattern匹配条件的日志才会被合并

例如配置如下:

multiline.pattern:'^[[:space:]]+'
multiline.negate: false
multiline.match: after

此时以空格开头的日志会被合并到上一行后面,如以下的日志采集可使用

Caused by: java.io.IOException: null
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:105) ~[amqp-client-4.0.3.jar:4.0.3]
	at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:101) ~[amqp-client-4.0.3.jar:4.0.3]

我们需要把后面两行以空格开头的日志合并到第一行上,整体在一起算一条完整的日志

2、 multiline.negate为true,表示逻辑取反,这是只有不符合multiline.pattern匹配条件的日志才会被合并

例如配置如下:

multiline.pattern: ^\[dic-log]
multiline.negate: true
multiline.match: after

此时不以[dic-log]开头的日志会被合并到上一行后面,以[dic-log]开头的日志行被认为是一条完整的日志

二、日志输出到Elasticsearch中自定义index

filebeat输出到Elasticsearch默认的index 是filebeat+时间,这显然不符合我们的实际使用情况,这里就需要我们自定义index了,自定义index配置如下:

setup.template.name: "zhang-local"
setup.template.pattern: "zhang-local-*"
output.elasticsearch:
	# Array of hosts to connect to.
	hosts: ["192.168.128.179:9200"]
	index: zhang-local-%{+yyyy.MM.dd}

先配置setup.template的name和pattern,然后在output.elasticsearch下加index配置,

特别说明:三个配置的前缀都要相同,否则为报错!

三、Filebeat结合Ingest Node提取特定字段

filebeat输出到Elasticsearch的日志,仅为一条字符串,若我们想处理这条日志,而又不想使用logstash,就可以先elasticsearch发送http请求添加ingest的pipeline 管道规则,使其输出到Elasticsearch的日志完成我们想要的预处理

1、 请检查Ingest node是否启动,配置在elasticsearch.yml中,默认ingest node是启动着的。(此步可忽略,若后遇报错再行检查)
2、 创建自己的管道规则,创建文件eop-pipeline.json
{
        "description": "eop-pipeline",
        "processors": [{
                        "grok": {
                                "field": "message",
                                "patterns": ["\\[dic-log]\\|#\\|%{GREEDYDATA:applicationName}\\|#\\|%{GREEDYDATA:agentId}\\|#\\|%{TIMESTAMP_IS
O8601:date}\\|#\\|%{LOGLEVEL:level}\\|#\\|%{GREEDYDATA:buseness}\\|#\\|%{GREEDYDATA:busParams}\\|#\\|%{GREEDYDATA:position}\\|#\\|"]
                        }
                }

        ]
}

ps:patterns配置使用grok语法,配置方式可自行百度

3、 向elasticsearch发送http请求添加ingest的pipeline 管道规则
curl -H 'Content-Type: application/json' -XPUT 'http://192.168.128.179:9200/_ingest/pipeline/eop-pipeline' -d@eop-pipeline.json

测试是否添加成功:curl http://127.0.0.1:9200/_ingest/pipeline
返回报文中有你配置的eop-pipeline.json即为成功

ps:上述curl为liunx方式,任何http的put请求都可以,若在window发送可用soapui,方式为put,报文为eop-pipeline.json中的内容

4、 配置Filebeat即可使用管道,添加pipeline配置,示例配置如下:
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["192.168.128.179:9200"]
  index: zhang-local-%{+yyyy.MM.dd}
  pipelines:
    - pipeline: "eop-pipeline"