zoukankan      html  css  js  c++  java
  • Logstash——条件判断

    前言

    有时您只想在特定条件下过滤或输出事件。为此,您可以使用条件(conditional)。比如在elk系统中想要添加一个type类型的关键字来根据不同的条件赋值,最后好做统计。

    条件语法

    if EXPRESSION {
      ...
    } else if EXPRESSION {
      ...
    } else {
      ...
    }

    比较操作

    • 相等: ==, !=, <, >, <=, >=
    • 正则: =~(匹配正则), !~(不匹配正则)
    • 包含: in(包含), not in(不包含)

    布尔操作

      - and(与), or(或), nand(非与), xor(非或)

    一元运算符

    表达式可能很长且很复杂。表达式可以包含其他表达式,您可以使用!来取消表达式,并且可以使用括号(...)对它们进行分组。

    • - !(取反)
    • - ()(复合表达式)
    • !()(对复合表达式结果取反)

    示例

    如若action是login则mutate filter删除secret字段:

    filter {
      if [action] == "login" {
        mutate { remove_field => "secret" }
      }
    }

    若是正则匹配,成功后添加自定义字段:

    filter {
        if [message] =~ /w+s+/w+(/learner/course/)/ {
            mutate {
                add_field => { "learner_type" => "course" }
            }
        }
    }

    在一个条件里指定多个表达式:

    output {
      # Send production errors to pagerduty
      if [loglevel] == "ERROR" and [deployment] == "production" {
        pagerduty {
        ...
        }
      }
    }

    在in条件,可以比较字段值:

    filter {
      if [foo] in [foobar] {
        mutate { add_tag => "field in field" }
      }
      if [foo] in "foo" {
        mutate { add_tag => "field in string" }
      }
      if "hello" in [greeting] {
        mutate { add_tag => "string in field" }
      }
      if [foo] in ["hello", "world", "foo"] {
        mutate { add_tag => "field in list" }
      }
      if [missing] in [alsomissing] {
        mutate { add_tag => "shouldnotexist" }
      }
      if !("foo" in ["hello", "world"]) {
        mutate { add_tag => "shouldexist" }
      }
    }

    not in示例:

    output {
      if "_grokparsefailure" not in [tags] {
        elasticsearch { ... }
      }
    }

    参考资料:

    官方文档-conditionals

    ELK logstash 配置语法(24th)

  • 相关阅读:
    cPanel设置自定义404错误页
    jquery鼠标移入某区域屏蔽鼠标滚轮 滚动滚动条
    阻止子元素继承父元素事件(郁闷我一晚上的问题!)
    告别码农,成为真正的程序员
    PHP中数组合并的两种方法及区别介绍
    理解OAuth 2.0[摘]
    mysql之触发器trigger 详解
    ThinkPHP Where 条件中使用表达式
    linux下利用curl监控网页shell脚本
    XUtils 3 使用
  • 原文地址:https://www.cnblogs.com/caoweixiong/p/12579608.html
Copyright © 2011-2022 走看看