使用Logstash发送电子邮件通知

6qftjkof  于 5个月前  发布在  Logstash
关注(0)|答案(1)|浏览(89)

我使用的是ELK版本8.10.2-1的Kong API Gateway日志,我想配置Logstash邮件插件,根据HTTP状态码发送邮件通知。
在ELK Kong中,我有一个名为[response.status]的字段,其中包含HTTP状态码,如200,401和500。
我已经配置了Logstash来根据[response.status]字段的值添加一个自定义标记,但不幸的是,该标记无法成功工作。
如何根据[response.status]字段的值配置/添加标签?

Logstash配置文件:

input {
  udp {
    port => 9000
  }
}

filter {
  json {
    source => "message"

    add_tag => ["kong"]

  }

##
if [response.status] == "401" {
    mutate {
      add_tag => [ "http_error_401" ]
    }
  }
##

}

output {
  elasticsearch {
    hosts => ["https://xx.xx.xx.xx:9200" , "https://xx.xx.xx.xx:9200" , "https://xx.xx.xx.xx:9200"]
    user => "elastic_user"
    password => "${elastic_password}"
    ssl => true
    ssl_certificate_verification => false
    cacert => "/etc/logstash/http_ca.crt"
    ilm_rollover_alias => "kong"
    ilm_pattern => "{now/d}-000001"
    ilm_policy => "kong-index-policy-example"

  }
if "http_error_401" in [tags] {
    email {
      to => "[email protected]"
      from => "[email protected]"
      subject => "API-Error at  %{@timestamp}"
      body => "Tags: %{tags}\\n\\Content:\\n%{message}"
      via => "smtp"
      address => "mail.example.com"
      port => 25

    }
  }
}

字符串

wlsrxk51

wlsrxk511#

解决方案:if [response.status] == "401"更新为if [response][status] == "401"或/和if [response][status] == 401

以下是解释:

[响应.状态]

Logstash中的语法if [response.status] == "401"不正确,因为它试图使用点表示法引用嵌套字段。Logstash主要对嵌套字段使用方括号表示法。[response.status]:这被解释为名为“response.status”的单个字段,而不是嵌套字段“response”和“status”。

**[response][status]**Logstash中的语法[response][status]被称为字段引用语法,用于访问事件数据中嵌套的字段。通过将其与[response][status]组合,你正在指定“response”中嵌套字段“status”的完整路径field。此语法允许Logstash正确解释和访问条件语句或其他处理逻辑中的嵌套字段。

如果你想检查的话,这里是官方文档:https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html
这里有一个例子,如果你想测试:

logstash conf:

input {
  stdin {codec => json}
}
filter {
  if [response][status] == 401 {
    mutate {
      add_tag => [ "http_error_401" ]
    }
  }
}
output {
  if "http_error_401" in [tags] {
  stdout {}
  }
}

字符串

data:

{"response":{"status":401}}

运行logstash

./logstash-7.16.2/bin/logstash -f email.conf

输出:

{"response":{"status":401}}
{
          "host" => "musab-mac.local",
          "tags" => [
        [0] "http_error_401"
    ],
      "response" => {
        "status" => 401
    },
      "@version" => "1",
    "@timestamp" => 2023-12-18T14:27:40.534Z
}

相关问题