EFK之filebeat用法进阶

接上一章节:https://blog.csdn.net/weixin_46546303/article/details/140279197?spm=1001.2014.3001.5501

一、filebeat module 输入流

1.filebeat module作用

Filebeat 模块的主要作用是简化日志数据的收集和处理过程。通过使用模块,你可以快速地配置 Filebeat 来收集特定类型的日志,并将其发送到 Elasticsearch 或 Logstash 进行进一步处理和分析。
官方文档:https://www.elastic.co/guide/en/beats/filebeat/7.17/filebeat-modules.html

2.使用 Filebeat 模块的步骤

在 Filebeat 安装目录中,有一个 modules.d 目录,其中包含各种模块的配置文件。你可以通过以下命令启用和配置模块:

filebeat modules enable <module_name>
#例如启用nginx模块
filebeat modules enable nginx

或者
root@xxx:~#  mv  /etc/filebeat/modules.d/nginx.yml.disabled   /etc/filebeat/module.d/nginx.yml    ##包安装在/etc/filebeat/module.d下

3.模块的使用

1)nginx module 处理nginx access log

nginx 日志拆分方案主要为:
  • 修改原生日志为json格式 (上一章已经讲过如果使用json解析nginx日志)
  • 借助filebat模块处理
  • 引入logstash
流程图及配置

在这里插入图片描述

开启nginx日志处理模块

官方参考文档:https://www.elastic.co/guide/en/beats/filebeat/7.17/filebeat-module-nginx.html

root@xxx:~#  mv /etc/filebeat/modules.d/nginx.yml.disabled   /etc/filebeat/modules.d/nginx.yml

root@xxx:~# filebeat   modules  list   ##检查已经启用的模块
Enabled:
nginx

root@xxx:~#  vim /etc/filebeat/modules.d/nginx.yml
# Module: nginx
# Docs: https://www.elastic.co/guide/en/beats/filebeat/7.17/filebeat-module-nginx.html

- module: nginx
  # Access logs
  access:
    enabled: true    ##启动aceesslog

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["/var/log/nginx/access.log"]   ##指定accesslog路径

  # Error logs
  error:
    enabled: false

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    #var.paths:

  # Ingress-nginx controller logs. This is disabled by default. It could be used in Kubernetes environments to parse ingress-nginx logs
  ingress_controller:
    enabled: false

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    #var.paths:
filebeat配置
root@xxx:~#  vim /etc/filebeat/filebeat.nginx.moudule_to.es.yml  ##编辑配置文件
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml   ##指定模块加载路径

  # Set to true to enable config reloading*
  reload.enabled: true   ##(是否支持热加载)

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200"]    ##指定es主机
  index: "ubuntu-xx-nginx-access-%{+yyyy.MM.dd}"  ##指定索引

setup.ilm.enabled: false   ##禁用ilm
setup.template.name: "ubuntu-xx-nginx"   ##设置索引模板名称
setup.template.pattern: "ubuntu-xx-nginx*" ##设置索引模模式
setup.template.overwrite: false    ##禁用模板重写
setup.template.settings:   ##索引模板配置项
  index.number_of_shards: 3   ##设置主分片
  index.number_of_replicas: 1  ##设置副本分片
查看kibana输出

在这里插入图片描述
备注:有50个可选字段进行组合输出

2)处理tomcat日志

使用模块处理 访问日志
filebeat配置文件
vim /etc/filebeat/config/tomcat_filebeat_modules.yml
tomcat_filebeat_modules.yml 
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml    ##指定模块加载路径

  # Set to true to enable config reloading*(是否支持热加载,不需要重启会自动重新加载)
  reload.enabled: true

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200"] 
  index: "ubuntu-xx-xx-access-tomcat-%{+yyyy.MM.dd}"

setup.ilm.enabled: false ##禁用ilm
setup.template.name: "ubuntu-xx-xx-tomcat"
setup.template.pattern: "ubuntu-xx-xx-tomcat*"
setup.template.overwrite: false
setup.template.settings:   
  index.number_of_shards: 3  ##设置主分片
  index.number_of_replicas: 1  ##设置副本分片
启用tomcat 模块
root@xxx~:# /etc/filebeat/modules.d# vim tomcat.yml 
# Module: tomcat
# Docs: https://www.elastic.co/guide/en/beats/filebeat/7.17/filebeat-module-tomcat.html  ##参考文档

- module: tomcat
  log:
    enabled: true
    var.paths:
      - "/var/log/tomcat7/*.txt"
    # Set which input to use between udp (default), tcp or file.
    # var.input: udp
    # var.syslog_host: localhost
    # var.syslog_port: 9501

    # Set paths for the log files when file input is used.
    # var.paths:
    #   - /var/log/tomcat/*.log
    # var.paths:
    #   - /var/log/tomcat7/*.txt

    # Toggle output of non-ECS fields (default true).
    # var.rsa_fields: true

    # Set custom timezone offset.
    # "local" (default) for system timezone.
    # "+02:00" for GMT+02:00
    # var.tz_offset: local

避坑点
  • 当运行filebeat时,发现没有采集到日志,也没有报错查询官方文档之后找到解决方案:
    https://www.elastic.co/guide/en/beats/filebeat/7.17/filebeat-module-tomcat.html
var.input
读取消息的输入, udp 、file、tcp默认udp为udp。

**var.paths
读取文件的路径。需要是一个列表。仅当var.input设置为时才有效file。**

var.syslog_host
监听基于 UDP 或 TCP 的系统日志流量的地址。默认为localhost。设置为0.0.0.0可绑定到所有可用接口。

var.syslog_port
监听系统日志流量的端口。默认为9501
1024 以下的端口需要 Filebeat 以 root 身份运行。

var.tz_offset
默认情况下,日志中的日期时间将被解释为相对于运行 Filebeat 的主机中配置的时区。如果从不同时区的主机提取日志,请使用此字段设置时区偏移量,以便正确解析日期时间。有效值的格式为 ±HH:mm,-07:00例如UTC-7。

var.rsa_fields
用于控制向事件添加非 ECS 字段的标志。默认为 true,这将导致rsa添加 ECS 和自定义字段。

var.keep_raw_fields
用于控制将原始解析器字段添加到事件的标志。此字段位于 下rsa.raw。默认值为 false。
重新修改模块配置
  • 重新修改tomcat.yml
# Module: tomcat
# Docs: https://www.elastic.co/guide/en/beats/filebeat/7.17/filebeat-module-tomcat.html

- module: tomcat
  log:
    enabled: true
    var.input: file
    var.paths:
      - "/var/log/tomcat7/*.txt"
    # Set which input to use between udp (default), tcp or file.
    # var.input: udp
    # var.syslog_host: localhost
    # var.syslog_port: 9501

    # Set paths for the log files when file input is used.
    # var.paths:
    #   - /var/log/tomcat/*.log
    # var.paths:
    #   - /var/log/tomcat7/*.txt

    # Toggle output of non-ECS fields (default true).
    # var.rsa_fields: true

    # Set custom timezone offset.
    # "local" (default) for system timezone.
    # "+02:00" for GMT+02:00
    # var.tz_offset: local
tomcat module解释
tomcat 模块三种输入方式

在这里插入图片描述

TCP、UDP 需要指定:
  • var.syslog_host
    监听基于 UDP 或 TCP 的系统日志流量的地址。默认为localhost。设置为0.0.0.0可绑定到所有可用接口。

  • var.syslog_port
    监听系统日志流量的端口。默认为9501
    1024 以下的端口需要 Filebeat 以 root 身份运行

  • 基于udp/tcp监听

- module: tomcat
  log:
    enabled: true
    var.input: udp/tcp
    var.syslog_host: localhost
    var.syslog_port: 9501
启动filebeat:
netstat  -nulp |grep 95
udp        0      0 127.0.0.1:9501          0.0.0.0:*                           155499/filebeat
查看kibana输出发现针对tomcat模块,实际并没有太多字段的可选字段

在这里插入图片描述

自定义tomcat access_log json
修改tomcat配置logfromat
  • 修改tomcat配置文件
vim /etc/tomcat7/server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="access_log_" suffix=""
               pattern="{&quot;Time&quot;:&quot;%t&quot;,&quot;Status&quot;:&quot;%s&quot;,&quot;Userinfo&quot;:&quot;%u&quot;,&quot;Client_ip&quot;:&quot;%a&quot;,&quot;Request_Proto&quot;:&quot;%H&quot;,&quot;Method&quot;:&quot;%m&quo
t;,&quot;Request_URI&quot;:&quot;%U&quot;,&quot;query&quot;:&quot;%q&quot;,&quot;Respone_time&quot;:&quot;%D&quot;,&quot;Request_thread_name&quot;:&quot;%l&quot;,&quot;User_Agent&quot;:&quot;%{User-Agent}i&quot;,&quot;Referer&quot;:&quo
t;%{Referer}i&quot;}"/>

备注:json格式写法和nginx\apache有区别:{"KEY":"VALUE"},多个用逗号隔开

{"Time":"[24/Jul/2024:20:29:22 +0800]","Status":"200","Userinfo":"-","Client_ip":"10.17.11.225","Request_Proto":"HTTP/1.1","Method":"GET","Request_URI":"/","query":"","Respone_time":"1","Request_thread_name":"-","User_Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36","Referer":"-"}
filebeat配置

filebeat配置和nginx access log 配置类似,此处不在展示

tomcat catalina日志多行查询
多行日志处理,将符合条件的行合并成一行显示

仅适用于filestream输入,而不适用于log输入

parsers:
- multiline:
    type: pattern
    pattern: '^\['
    negate: true
    match: after

如果仍然使用的log输入,则无需使用parsers。

multiline.type: pattern
multiline.pattern: '^\['
multiline.negate: true
multiline.match: after
参数解释
  • multiline.type
    定义要使用的聚合方法。默认值为pattern。另一个选项是count允许您聚合固定数量的行。
  • multiline.pattern
    指定要匹配的正则表达式模式。请注意,Filebeat 支持的正则表达式模式与 Logstash 支持的模式略有不同。请参阅正则表达式支持以获取受支持的正则表达式模式列表。根据您配置其他多行选项的方式,与指定正则表达式匹配的行将被视为上一行的延续或新多行事件的开始。您可以设置选项negate以否定模式。
  • multiline.negate
    定义模式是否被否定。默认值为false。
  • multiline.match
    指定 Filebeat 如何将匹配的行组合成事件。设置为after或before。这些设置的行为取决于您为 指定的内容

在这里插入图片描述

总结:
false after  :匹配行放在非匹配行之后
false   before:匹配行放在非匹配行之前
true    after:  不匹配的行,放到匹配的行之后
true    before: 不匹配的行放在匹配行之前
测试案例
  • 测试文件:
vim /tmp/testfile
a
c
d
b
b
a
a
c
b
c
b
  • 示例一:
    false after
    #按行读取,遇到非匹配行就换行,遇到匹配行就继续读取,直到遇见新的非匹配行,将非匹配行作为行首,最后一个匹配行为行位的区别合并成一行输出;匹配行放到非匹配行之后,合并成一行。即从第一行开始读取,如果第二行是非匹配行,第一行则作为单独一行展示;如果第二行及后面行是匹配行,则将第二行和后面匹配行合并成一行展示,直到遇到新的非匹配行出现就换行。最终输出每行的开头都是非匹配行;

cat multi.filebeat.out.yml

filebeat.inputs:
- type: log
  paths: 
    - "/tmp/testfile"
  tags: ["testing"] 
  multiline.type: pattern
  multiline.pattern: '^b'
  multiline.negate: false
  multiline.match: after   ####匹配行放到非匹配行之后

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200"]

输出结果:
在这里插入图片描述

  • 示例二:
    false before
    #按行读取,遇到匹配行,就以新匹配作为行首继续向下读取,直到遇到第一个非匹配行就结束,将此区间的行合并成一行;如果连续非匹配行,则非匹配行都作为单独行展示;
    即:从第一行往下读取,如果第二行、及后面的行都是非匹配行,这读取的这些行都识别成单独一行展示,如果遇到匹配的行,则以匹配的行为开始向下匹配,如果后续行都为匹配行就合并成一行,直到遇到新的非匹配行出现,就以该非匹配行为截止,将匹配行-新遇到的非匹配行合并成一行展示,换行继续向下读取;
    cat multi.filebeat.out.yml
filebeat.inputs:
- type: log
  paths: 
    - "/tmp/testfile"
  tags: ["testing"] 
  multiline.type: pattern
  multiline.pattern: '^b'
  multiline.negate: false
  multiline.match: before   ####匹配行放在不匹配行之前

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200"]

输出结果:
在这里插入图片描述

  • 示例三:
    true after
    #非匹配行放到匹配行之后,即日志收集每一行开头都是以匹配行为开头,往下处理合并成一行输出,直至遇到新匹配行在另起新行。如果文件首行为非匹配行,则往下读取,内容合并成一行,直到匹配行出现在另起一行;

cat multi.filebeat.out.yml

filebeat.inputs:
- type: log
  paths: 
    - "/tmp/testfile"
  tags: ["testing"] 
  multiline.type: pattern
  multiline.pattern: '^b'
  multiline.negate: true
  multiline.match: after   ####不匹配的行放在匹配行之后

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200"]

输出结果:
在这里插入图片描述

  • 示例四:
    true before
    #按行读取,遇到匹配行就将匹配行作为合并行的行尾,和之前的非匹配行合并成一行输出;

cat multi.filebeat.out.yml

filebeat.inputs:
- type: log
  paths: 
    - "/tmp/testfile"
  tags: ["testing"] 
  multiline.type: pattern
  multiline.pattern: '^b'
  multiline.negate: true
  multiline.match: after   ####不匹配的行放在匹配行之前

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200"]

输出结果:
在这里插入图片描述

多行输出配置示例:
root@ubuntu-xx:/var/log/tomcat7# cat /etc/filebeat/config/tomcat_filebeat_mutil.yml 
filebeat.inputs:
- type: log
  paths:
    - "/var/log/tomcat7/catalina.out"
  tags: ["applog"]
#  parsers:      ###此格式仅适用于filestream,###
#    - multiline:
#      type: pattern
#      pattern: '^Jan|^Feb|^Mar|^Apr|^May|^Jun|^Jul|^Aug|^Sep|^Oct|^Nov|^Dec'
#      negate: true
#      match: after

  multiline.type: pattern
  multiline.pattern: '(^Jan|^Feb|^Mar|^Apr|^May|^Jun|^Jul|^Aug|^Sep|^Oct|^Nov|^Dec)'
  multiline.negate: true
  multiline.match: after

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200:9200"] 
  index: "ubuntu-8-136-tomcat-applog-%{+yyyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: "ubuntu-xx-tomcat"
setup.template.pattern: "ubuntu-xx-tomcat*"
setup.template.overwrite: false
setup.template.settings:
  index.number_of_shards: 3
  index.number_of_replicas: 1

####https://www.elastic.co/guide/en/beats/filebeat/7.17/multiline-examples.html

二、KQL使用方式:


简单查询:

field:value
根据字段和值进行简单的精确匹配查询。

逻辑操作符:

field1:value1 AND field2:value2
field1:value1 OR field2:value2
NOT field:value
使用逻辑操作符进行AND、OR和NOT的组合查询。

通配符:

field:va*
field:v?lue
使用通配符进行模糊匹配查询,*代表多个字符,?代表一个字符。

范围查询:

field:[value1 TO value2]
根据字段值的范围进行查询。

子查询:

(field1:value1 OR field2:value2) AND field3:value3
使用括号进行子查询,可以构建复杂的查询逻辑。

字段存在性查询:

_exists_:field
查询指定字段存在的文档。

三、filebeat日志过滤:

参考官方文档:https://www.elastic.co/guide/en/beats/filebeat/7.17/filebeat-input-log.html

1.exclude_lines:

匹配Filebeat 排除的行的正则表达式列表。Filebeat 会删除与列表中的正则表达式匹配的所有行。默认情况下,不会删除任何行。空行将被忽略;如果还指定了多行设置,则每个多行消息将合并为一行,然后用 进行过滤exclude_line

filebeat.inputs:
- type: log
  ...
  exclude_lines: ['^DBG'] ##排除以^DBG开头的行,且区分大小写,支持通配符,特殊字符需要加转义符

2.include_lines:

正则表达式列表,用于匹配您希望 Filebeat 包含的行。Filebeat 仅导出与列表中的正则表达式匹配的行。默认情况下,将导出所有行。空行将被忽略;如果还指定了多行设置,则每个多行消息将合并为一行,然后再按 进行过滤include_lines;

filebeat.inputs:
- type: log
  ...
  include_lines: ['^ERR', '^WARN']

如果同时定义了include_lines和exclude_lines,Filebeat会先执行include_lines,然后执行exclude_lines。这两个选项的定义顺序并不重要。 include_lines 选项将始终在 exclude_lines 选项之前执行,即使在配置文件中exclude_lines 出现在 include_lines 之前。

filebeat.inputs:
- type: log
  ...
  include_lines: ['^ERROR']
  exclude_lines: ["404"]

filebeat input 之filestream(文件流) 【log输入流在7.16之后弃用】

- ndjson:
    target: ""
    add_error_key: true
filebeat.inputs:
- type: filestream
  paths:
    - "/var/log/nginx/access.log"
  tags: ["access"]
  id: nginx-access-0001
  fields:
    log_type: "access"
  parsers:
    - ndjson:
        add_error_key: true
        keys_under_root: true
  

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200"] 
  index: "ubuntu-8-136-nginx-access-%{+yyyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: "ubuntu-xx-xx-nginx"
setup.template.pattern: "ubuntu-xx-xx-nginx*"
setup.template.overwrite: false
setup.template.settings:
  index.number_of_shards: 3
  index.number_of_replicas: 1

3.多行匹配:

parsers:
    - ndjson:
        add_error_key: true
        keys_under_root: true
    - multiline:
        type: pattern
        pattern: '^\['
        negate: true
        match: after  

四、作业:收集服务器日志信息,多日志集合一配置文件

1.作业配置

filebeat.inputs:
- type: filestream
  id: dpkg_log-0001
  paths:
    - "/var/log/dpkg.log"
  tags: ["sys","dpkg"]
  fields:
    log_type: "sys"
    name: "dpkglog"

- type: filestream
  id: command_log-0001
  paths:
    - "/var/log/dpkg.log"
  tags: ["sys","command"]
  fields:
    log_type: "sys"
    name: "commandlog"

- type: filestream
  id:  auth_log-0001
  paths:
    - "/var/log/auth.log"
  tags: ["sys","auth"]
  fields:
    log_type: "sys"
    name: "authlog"

- type: filestream
  id:  kern_log-0001
  paths:
    - "/var/log/kern.log.1"
  tags: ["sys","kern"]
  fields:
    log_type: "sys"
    name: "kernlog"

- type: filestream
  id: kibana_log-0001
  paths:
    - "/var/log/kibana/kibana.log"
  tags: ["app","kibana"]
  fields:
    log_type: "app"
    name: "kibanalog"
  parsers: 
    - ndjson:
        keys_under_root: true
        add_error_key: true
        message_key: message


- type: filestream
  id:  samba_log-0001
  paths:
    - "/var/log/samba/log.nmbd"
  tags: ["app","samba"]
  fields:
    log_type: "app"
    name: "sambalog"
  parsers:
    - multiline:    
        type: pattern    ##这里我想使用counter类型,但是似乎有问题,如果配置type为counter,运行会提示没有这个类型,当我修改成count可用,但是提示count不能为空,查询之后得知counter似乎并不支持filebeat 7.17.3
        pattern:  '^\['
        negate:  true
        match:  after

- type: filestream
  id: sys_log-0001
  paths:
    - "/var/log/syslog"
  tags: ["sys","sys"]
  fields:
    log_type: "sys"
    name: "syslog"

- type: filestream
  id:  tomcat_access_log-0001
  paths:
    - "/var/log/tomcat7/access_log.*"
  tags: ["app","tomcat"]
  fields:
    log_type: "app"
    name: "tomcat_accesslog"
  parsers:
    - ndjson:
        keys_under_root: true
        add_error_key: true

- type: filestream
  id:  tomcat_catalina_log-0001
  paths:
    - "/var/log/tomcat7/catalina.out"
  tags: ["app","tomcat"]
  fields:
    log_type: "app"
    name: "tomcat_log"
  parsers:
    - multiline:
        type: pattern
        pattern: '^Jan|^Feb|^Mar|^Apr|^May|^Jun|^Jul|^Aug|^Sep|^Oct|^Nov|^Dec'
        negate: true
        match: after

output.elasticsearch:
  hosts: ["http://xx.xx.xx.xx:9200","http://xx.xx.xx.xx:9200","http://xx.xx.xx.xx:9200"] 
  index: "ubuntu-8-136-%{[fields.name]}-%{+yyyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: "ubuntu-xx.xx"
setup.template.pattern: "ubuntu-xx.xx*"
setup.template.overwrite: false
setup.template.settings:
  index.number_of_shards: 3
  index.number_of_replicas: 1

2.作业解析

1)问题分析

现象

centos现象:一个配置文件收集实例超过4个,写入es是可能会出现部分数据无法写入的问题

2)解决方案:

拆成多个filebeat实例

运行多个实例,需要指定数据路径,即

filebeat -e -c xxxxx .yml  --path.data=/xxxxx

当运行实例时,会在数据路径检查是否存在filebeat.lock,如果存在会报冲突

日志聚合
  • 流程图
    在这里插入图片描述

  • 使用rsyslog将日志聚合输入到一个文件
    centos:
    vim /etc/rsyslog.conf

    ###TCP 系统日志
    $ModLoad imtcp
    $InputTCPServerRun 514

    ###日志聚合
    # *.* @xx.xx.xx.xx:514 放在远程服务器
    *.* /var/log/merge_system.log

重启服务测试

systemctl restart syslog

logger "111111"

五、filebeat其他output:

1、file

数据流走向

在这里插入图片描述

配置信息

将文件输出将转储到一个文件中,其中每个输出均采用 JSON 格式。目前,此输出用于测试,但它可以用作 Logstash 的输入。

filebeat.inputs:
- type: tcp
  max_message_size: 10MiB
  host: "localhost:9000"
  max_connections: 50

output.file:
  path: "/tmp/filebeat"     ##指定输出目标目录,必须
  filename: filebeat        ##指定存储文件名
  rotate_every_kb: 10000    ##每个文件的最大大小(以千字节为单位)。达到此大小时,文件将被轮换。默认值为 10240 KB
  number_of_files: 7    ##path目录下可保存的最大文件数。达到此文件数时,将删除最旧的文件,其余文件将从最后移到最前面。文件数必须介于 2 到 1024 之间。默认值为 7
  permissions: 0600    ##用于文件创建的权限。默认值为 0600

2、redis

数据流走向

在这里插入图片描述

配置信息

filebeat.inputs:
- type: filestream
  id: redis-test-01
  paths:
    - "/tmp/filebeat/filebeat"
  parsers:
    ndjson:
      keys_under_root: true
      add_error_key: true


output.redis:
  hosts: ["localhost"]   ##指定redis主机
  password: "your_paaswd"   ##指定redis passwd
  key: "filebeat"     ##指定key的值
  db: 0      ##指定链接数据库编号
  timeout: 5   ##指定超时时间

3.tcp log

数据流走向

在这里插入图片描述

也可用nc xxx  9000

tcp日志详解

参数参考文档详见:https://www.elastic.co/guide/en/beats/filebeat/7.17/filebeat-input-tcp.html

什么是tcp日志

TCP日志(TCP log)是指记录与TCP(传输控制协议)相关的网络活动的日志。这些日志通常用于监控、调试和分析网络通信。TCP日志可以包含以下信息:

  • 连接建立和终止:记录TCP连接的建立(如三次握手)和终止(如四次挥手)的时间和状态。
  • 数据传输:记录通过TCP连接传输的数据包,包括发送和接收的数据字节数。
  • 错误和警告:记录在TCP连接中发生的错误、超时、重传等异常情况。
  • 连接状态:记录TCP连接的状态变化,如从SYN_SENT到ESTABLISHED再到CLOSED。
常见的TCP日志记录工具和方法
  • 系统日志(syslog, rsyslog, syslog-ng):许多操作系统的系统日志服务可以配置为记录TCP连接信息。
  • 网络分析工具(如tcpdump, Wireshark):这些工具可以捕获并分析网络流量,包括TCP流量,并生成详细的日志或报告。
  • Web服务器日志(如Apache, Nginx):这些服务器通常记录与TCP连接相关的访问日志。
  • 防火墙和路由器日志:网络设备通常记录与流量过滤和路由相关的TCP连接信息。

案例分析

  • 创建新的所引如果和已经存在的所以模板
filebeat.inputs:
- type: tcp    ##指定输入类型为tcp 
  max_message_size: 10MiB    ##通过 TCP 接收的消息的最大值。默认值为20MiB
  host: "0.0.0.0:9000"        ##用于侦听事件流的主机和 TCP 端口

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200"] 
  index: "ubuntu-8-tcplog-%{+yyyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: "ubuntu-8"
setup.template.pattern: "ubuntu-8*"
setup.template.overwrite: false
setup.template.settings:
  index.number_of_shards: 3
  • 启动filebeat示例

测试:

telnet  xxxx 9000
aaaa
  • 终端提示:
2024-07-27T22:37:10.843+0800    ERROR   [publisher_pipeline_output]     pipeline/output.go:154  Failed to connect to backoff(elasticsearch(http://xx.xx.xx.xx:9200)): Connection marked as failed because the onConnect callback failed: error loading template: failed to load template: couldn't load template: 400 Bad Request: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"index template [ubuntu-8] has index patterns [ubuntu-8*] matching patterns from existing templates [ubuntu-8-136] with patterns (ubuntu-8-136 => [ubuntu-8-136*]) that have the same priority [150], multiple index templates may not match during index creation, please use a different priority"}],"type":"illegal_argument_exception","reason":"index template [ubuntu-8] has index patterns [ubuntu-8*] matching patterns from existing templates [ubuntu-8-136] with patterns (ubuntu-8-136 => [ubuntu-8-136*]) that have the same priority [150], multiple index templates may not match during index creation, please use a different priority"},"status":400}. Response body: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"index template [ubuntu-8] has index patterns [ubuntu-8*] matching patterns from existing templates [ubuntu-8-136] with patterns (ubuntu-8-136 => [ubuntu-8-136*]) that have the same priority [150], multiple index templates may not match during index creation, please use a different priority"}],"type":"illegal_argument_exception","reason":"index template [ubuntu-8] has index patterns [ubuntu-8*] matching patterns from existing templates [ubuntu-8-136] with patterns (ubuntu-8-136 => [ubuntu-8-136*]) that have the same priority [150], multiple index templates may not match during index creation, please use a different priority"},"status":400}

提示不能加载模板,和已经存在的模板ubuntu-8-136冲突
在这里插入图片描述

  • 尝试修改索引模板将范围缩小
filebeat.inputs:
- type: tcp
  host: "0.0.0.0:9000"

output.elasticsearch:
  hosts: ["http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200","http://10.xx.xx.xx:9200"] 
  index: "ubuntu-8-tcplog-%{+yyyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: "ubuntu-8-136-tcplog"
setup.template.pattern: "ubuntu-8-136-tcplog*"
setup.template.overwrite: false
setup.template.settings:
  index.number_of_shards: 3
  • 分析
    重新测试还是得到和已经存在的索引模板冲突

  • 得出结论:
    新建实例中的索引模板名称可以使用和已经存在的所以模板相同的名字,但是不能使用比已经存在索引模板名称范围更大或者更小的作为新模板的名字

  • 16
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值