温馨提示×

ubuntu filebeat能否自定义标签

小樊
41
2025-12-13 01:08:12
栏目: 智能运维

Ubuntu 上 Filebeat 自定义标签指南

可行性与作用Ubuntu 上的 Filebeat 支持通过配置为事件添加自定义 tagsfields,用于后续在 Elasticsearch/KibanaLogstash 中做过滤、路由与聚合。常见做法是给不同日志路径设置不同标签,便于按标签分流或创建索引。

配置方式

  • 全局或输入级标签:在 filebeat.yml 的 filebeat.inputs 中为某类日志统一打标签;也可用 processors 动态添加。示例:
    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /var/log/myapp/*.log
      tags: ["myapp", "production"]
      fields:
        app_name: "myapp"
        env: "prod"
      fields_under_root: false   # 为 true 时将 fields 提升到事件根层级
    
    # 使用处理器添加/修改标签与字段
    processors:
    - add_fields:
        target: ""
        fields:
          processed: true
    - decode_json_fields:
        fields: ["message"]
        target: "json_content"
        overwrite_keys: true
    
    要点:tags 是字符串数组;fields 是键值对;fields_under_root 控制是否放在根层级。

按标签分流示例

  • Logstash 条件路由:在 Logstash 中按标签选择不同的 Elasticsearch 索引。
    input {
      redis {
        host => "192.168.3.65"
        port => 46379
        password => "password"
        db => 3
        data_type => "list"
        key => "0388"
      }
    }
    output {
      if "psych-log-0388" in [tags] {
        elasticsearch {
          hosts => ["192.168.3.59:9200"]
          index => "psych-log-0388-%{+YYYY.MM.dd}"
        }
      }
      if "bert-log-0388" in [tags] {
        elasticsearch {
          hosts => ["192.168.3.59:9200"]
          index => "bert-log-0388-%{+YYYY.MM.dd}"
        }
      }
    }
    
    要点:在 Filebeat 侧给不同日志路径分别打上如 psych-log-0388bert-log-0388 的标签,Logstash 即可据此分流到不同索引。

验证与生效

  • 语法检查与查看输出:
    sudo filebeat test config -e
    sudo filebeat -e -c /etc/filebeat/filebeat.yml
    
    建议先用控制台输出(如 output.console)验证事件是否携带期望的 tags/fields,再切回生产输出。修改后重启服务:
    sudo systemctl restart filebeat
    sudo systemctl enable filebeat
    
    要点:test config 检查语法;-e 输出到控制台便于调试;重启使配置生效。

0