章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • ansible系列文章

  • Kubernetes笔记

  • elk

    • elk安装
    • docker-compose安装elk
    • filebeat-log相关配置指南
    • logstash提取日志字段到kibana仪表盘展示
    • 深入理解 ELK 中 Logstash 的底层原理
    • filebeat及logstash配置
      • es索引定期删除脚本
      • logstash线上配置文件
      • elk报错问题总结
      • es备份和还原
      • ES集群与角色规划
      • ES8.8集群与Kibana部署
      • ElasticSearch可视化工具介绍
      • 使用docker构建ElasticSearch集群
    • jenkins

    • GitLabCI_CD

    • 专题
    • elk
    章工运维
    2023-04-20
    目录

    filebeat及logstash配置

    记录下filebeat及logstash配置语法。

    # 配置filebeat收集nginx日志及java日志

    filebeat.inputs:
    - type: log
      enabled: True
      fields:
        log_type: nginx-access
        project_name: shsp
        log_topic: common_nginx
      fields_under_root: true
      paths:
        - /apps/usr/nginx/logs/access.log
    
    - type: log
      enabled: True
      fields:
        project_name: shsp
        log_type: nginx-error
        log_topic: common_nginx
      fields_under_root: true
      paths:
        - /apps/usr/nginx/logs/error.log
    
    
    - type: log
      enabled: True
      multiline.pattern: '^[[:space:]]+|^Caused by:'
      multiline.negate: false
      multiline.match: after
      fields:
        project_name: shsp
        log_type: app_log
        log_topic: app_all
      fields_under_root: true
      paths:
        - /apps/usr/appdata/logs/*.log
    
    output.kafka:
      hosts: ["192.168.20.2:9092", "192.168.20.3:9092", "192.168.20.4:9092"]
      topic: '%{[log_topic]}'
      partition.round_robin:
        reachable_only: false
      required_acks: 1
      compression: gzip
      max_message_bytes: 1000000
    
    processors:
      - drop_fields:
          fields: ['ecs', 'beat', 'input', '@version', 'agent']
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47

    # logstash处理nginx日志

    input {
        kafka {
            bootstrap_servers => "192.168.20.2:9092,192.168.20.3:9092,192.168.20.4:9092"
            topics => ["common_nginx"]
            codec => json { charset => "UTF-8" }
            group_id => "standard"
            consumer_threads => 8
        }
    }
    filter {
       if [log_type] == "nginx-access" {
            grok {
               match => { "message" => ["%{IPORHOST:[access][remote_ip]} - %{DATA:[access][user_name]} \[%{HTTPDATE:[access][time]}\] \"%{WORD:[access][method]} %{DATA:[access][url]} HTTP/%{NUMBER:[access][http_version]}\" %{NUMBER:[access][response_code]} %{NUMBER:[access][body_sent][bytes]} \"%{DATA:[access][referrer]}\" \"%{DATA:[access][agent]}\""] }
            remove_field => "message"
            }
            mutate {
                add_field => { "read_timestamp" => "%{@timestamp}" }
            }
            date {
                match => [ "[access][time]", "dd/MMM/YYYY:H:m:s Z" ]
                remove_field => "[access][time]"
            }
            useragent {
                source => "[access][agent]"
                target => "[access][user_agent]"
                remove_field => "[access][agent]"
            }
            geoip {
                source => "[access][remote_ip]"
                target => "[geoip]"
            }
        } 
        else if [log_type] == "nginx-error" {
            grok {
                match => { "message" => ["%{DATA:[error][time]} \[%{DATA:[error][level]}\] %{NUMBER:[error][pid]}#%{NUMBER:[error][tid]}: (\*%{NUMBER:[error][connection_id]} )?%{GREEDYDATA:[error][message]}"] }
                remove_field => "message"
            }
            mutate {
                rename => { "@timestamp" => "read_timestamp" }
            }
            date {
                match => [ "[error][time]", "YYYY/MM/dd H:m:s"]
                remove_field => "[error][time]"
            }
        }
    }
    
    output {
        #stdout { 
        #    codec => rubydebug
        #}
    
        elasticsearch {
            hosts => [ "192.168.20.11:9200","192.168.20.12:9200","192.168.20.13:9200" ]
            user => "elastic"
            password => "abcd"
            index => "logstash-nginx-%{project_name}-%{+YYYY.MM.dd}"
            codec => json { charset => "UTF-8" }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60

    # logstash处理java日志

    input {
        kafka {
            bootstrap_servers => "192.168.20.2:9092,192.168.20.3:9092,192.168.20.4:9092"
            topics => ["app_all"]
            codec => json { charset => "UTF-8" }
            group_id => "standard"
            consumer_threads => 8
        }
    }
    filter {
        mutate {
                    add_field => { "log_path" => "%{[log][file][path]}" }
                    add_field => { "host_name" => "%{[host][name]}"}
            }
            mutate {
                split => ["[log][file][path]", "/"]
            }
        mutate {
                  split => ["[log][file][path][-1]", "."]
        }
            mutate {
                    add_field => { "log_name" => "%{[log][file][path][-1][0]}" }
            }
            mutate{
                remove_field => ["log", "host"]
    
            }  
    }
    
    output {
        #stdout { 
        #    codec => rubydebug
        #}
    
        elasticsearch {
            hosts => [ "192.168.20.11:9200","192.168.20.12:9200","192.168.20.13:9200" ]
            user => "elastic"
            password => "abcd"
            index => "logstash-app_%{project_name}-%{+YYYY.MM.dd}"
            codec => json { charset => "UTF-8" }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42

    # filebeat配置fields中字段介绍

    在 Filebeat 的配置文件中,fields 配置项允许你添加自定义字段,以便更好地描述、分类或标记日志事件。

    fields 配置项通常位于 Filebeat 配置文件的 filebeat.inputs 部分,如下所示:

    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /var/log/*.log
      fields:
        app_name: my_app
        environment: production
    
    1
    2
    3
    4
    5
    6
    7
    8

    在这个例子中,我们向每个日志事件添加了两个自定义字段:app_name 和 environment。这些字段在后续处理和分析日志数据时非常有用,可以帮助我们根据应用名称或环境对日志进行过滤、查询和聚合。

    在 Filebeat 中,你可以添加任意数量的自定义字段,以满足你对日志数据的标记和分类需求。这些字段在 Filebeat 将日志数据发送到目标时保留,并可以在日志处理过程中被使用。

    fields_under_root介绍

    在 Filebeat 配置文件中,fields_under_root 是一个布尔选项,用于控制自定义字段(通过 fields 配置项添加)是作为顶层字段还是子级字段添加到日志事件中。默认情况下,fields_under_root 选项的值为 false,这意味着自定义字段将作为子级字段添加到事件中。

    如果将 fields_under_root 设置为 true,则自定义字段将添加到事件的顶层。这样的设置可能对于与其他系统的集成和兼容性非常重要,因为某些系统可能要求在特定的顶层字段中存储一些元数据。

    以下是一个配置示例:

    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /var/log/*.log
      fields:
        app_name: my_app
        environment: production
      fields_under_root: true
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    在这个例子中,fields_under_root 被设置为 true,所以 app_name 和 environment 这两个自定义字段将直接作为顶层字段添加到日志事件中。如果将 fields_under_root 设置为 false 或不设置,那么这些字段将作为子级字段添加到事件中,如 fields.app_name 和 fields.environment。

    微信 支付宝
    上次更新: 2023/04/21, 08:57:47

    ← 深入理解 ELK 中 Logstash 的底层原理 es索引定期删除脚本→

    最近更新
    01
    shell脚本模块集合
    05-13
    02
    生活小技巧(认知版)
    04-29
    03
    生活小技巧(防骗版)
    04-29
    更多文章>
    Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式