温馨提示×

ubuntu filebeat能否支持多种日志源

小樊
40
2025-12-13 01:06:14
栏目: 智能运维

支持多种日志源的方式Ubuntu 上,Filebeat 可以同时采集多种日志源。做法是在配置文件 /etc/filebeat/filebeat.ymlfilebeat.inputs 数组中定义多个输入项(每个项可设置不同的 type 与参数);同一实例可并行启用 log、syslog、tcp、udp、container/journald 等多种输入类型。若需面向容器与宿主机混合环境,常见组合是:容器日志用 container,系统日志用 journald,业务文件日志用 log

快速配置示例

  • 同时采集系统日志与 Nginx 访问日志,并按日志类型写入不同索引
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/syslog
    - /var/log/auth.log
  fields:
    log_type: system
  fields_under_root: true

- type: log
  enabled: true
  paths:
    - /var/log/nginx/*.access.log
  fields:
    log_type: nginx_access
  fields_under_root: true
  processors:
    - dissect:
        tokenizer: '%{client_ip} - %{ident} [%{timestamp}] "%{method} %{request} HTTP/%{http_version}" %{status} %{body_bytes_sent} "%{referrer}" "%{user_agent}"'
        field: "message"
        target_prefix: "http"

output.elasticsearch:
  hosts: ["http://es-host:9200"]
  indices:
    - index: "filebeat-system-%{+yyyy.MM.dd}"
      when.equals:
        log_type: "system"
    - index: "filebeat-nginx-%{+yyyy.MM.dd}"
      when.equals:
        log_type: "nginx_access"

要点:

  • 通过 fields 给不同来源打标签,再用 indices.when.equals 写入不同索引,便于检索与配额管理。
  • 多行日志(如 Java 堆栈)可在对应输入项中添加 multiline 配置进行合并。

同时采集文件与网络端口

  • 文件日志 + TCP 接收(适合接收本地或远程应用主动推送的日志)
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/app/*.log
  fields:
    source: app_file

- type: tcp
  host: "0.0.0.0:9000"
  fields:
    source: app_tcp
  processors:
    - decode_json_fields:
        fields: ["message"]
        target: ""
        overwrite_keys: true

output.elasticsearch:
  hosts: ["http://es-host:9200"]
  index: "filebeat-mixed-%{+yyyy.MM.dd}"

说明:

  • tcp 输入可监听端口接收日志流;配合 decode_json_fields 可直接解析 JSON 消息。
  • 如需 UDP,可新增一个 type: udp 输入项,方法与 tcp 类似。

注意事项

  • 同一 Filebeat 实例通常只配置 一个输出(不能同时写多个 output);如需多路投递,建议使用 LogstashKafka 作为中间层进行分发。
  • 容器场景优先使用 container 输入采集 /var/lib/docker/containers//.log;宿主机系统日志建议用 journald 输入,更完整且与 systemd 单元关联。
  • 多行日志务必在对应输入项中配置 multiline,否则堆栈会被拆行,影响解析与检索。

0