正在加载

Slack 操作

Elastic Stack Serverless

使用 slack 操作向 Slack 团队的频道或用户发送消息。要发送 Slack 消息,您需要在 elasticsearch.yml配置至少一个 Slack 帐户

您可以在 actions 数组中配置 Slack 操作。特定于操作的属性使用 slack 关键字指定。

以下代码段显示了一个简单的 slack 操作定义

"actions" : {
  "notify-slack" : {
    "transform" : { ... },
    "throttle_period" : "5m",
    "slack" : {
      "message" : {
        "to" : [ "#admins", "@chief-admin" ],
        "text" : "Encountered  {{ctx.payload.hits.total}} errors in the last 5 minutes (facepalm)"
      }
    }
  }
}
  1. 您要将消息发送到的频道和用户。
  2. 消息的内容。

除了发送简单的基于文本的消息之外,您还可以使用 Slack 附件机制来发送格式化的消息。 Watcher 利用 Slack 附件使您能够从执行上下文有效负载中动态填充模板化消息。

以下代码段显示了一个标准消息附件

"actions" : {
  "notify-slack" : {
    "throttle_period" : "5m",
    "slack" : {
      "account" : "team1",
      "message" : {
        "from" : "watcher",
        "to" : [ "#admins", "@chief-admin" ],
        "text" : "System X Monitoring",
        "attachments" : [
          {
            "title" : "Errors Found",
            "text" : "Encountered  {{ctx.payload.hits.total}} errors in the last 5 minutes (facepalm)",
            "color" : "danger"
          }
        ]
      }
    }
  }
}

要定义从有效负载动态填充的附件模板,您需要在 watch 操作中指定 dynamic_attachments。例如,动态附件可以引用有效负载中的直方图桶,并为每个桶构建一个附件。

在以下示例中,watch 输入执行带有日期直方图聚合和 Slack 操作的搜索

  1. 将有效负载转换为列表,其中列表中的每个项目都包含月份、该月份的用户计数以及表示与该计数关联的情绪的颜色(危险或不良)。
  2. 定义引用转换生成的列表中项目的附件模板。
"input" : {
  "search" : {
    "request" : {
      "body" : {
        "aggs" : {
          "users_per_month" : {
            "date_histogram" : {
              "field" : "@timestamp",
              "interval" : "month"
            }
          }
        }
      }
    }
  }
},
...
"actions" : {
  "notify-slack" : {
    "throttle_period" : "5m",
    "transform" : {
      "script" : {
        "source" : "['items': ctx.payload.aggregations.users_per_month.buckets.collect(bucket -> ['count': bucket.doc_count, 'name': bucket.key_as_string, 'color': bucket.doc_count < 100 ? 'danger' : 'good'])]",
        "lang" : "painless"
      }
    },
    "slack" : {
      "account" : "team1",
      "message" : {
        "from" : "watcher",
        "to" : [ "#admins", "@chief-admin" ],
        "text" : "System X Monitoring",
        "dynamic_attachments" : {
          "list_path" : "ctx.payload.items"
          "attachment_template" : {
            "title" : "{{month}}",
            "text" : "Users Count: {{count}}",
            "color" : "{{color}}"
          }
        }
      }
    }
  }
}
  1. 操作的转换生成的列表。
  2. 参数占位符引用转换生成的列表中每个项目的属性。
名称 必需 描述
message.from 要在 Slack 消息中显示的发送者姓名。覆盖传入 Webhook 的配置名称。
message.to 您要将消息发送到的频道和用户。频道名称必须以 # 开头,用户名必须以 @ 开头。接受字符串值或字符串值数组。
message.icon 要在 Slack 消息中显示的图标。覆盖传入 Webhook 的配置图标。接受指向图像的公共 URL。
message.text 消息内容。
message.attachments Slack 消息附件。消息附件使您能够创建格式更丰富的消息。指定的数组定义在 Slack 附件文档中。
message.dynamic_attachments 可以根据当前 watch 有效负载动态填充的 Slack 消息附件。有关更多信息,请参阅使用附件格式化 Slack 消息
proxy.host 要使用的代理主机(仅与 proxy.port 结合使用)
proxy.port 要使用的代理端口(仅与 proxy.host 结合使用)

您可以在 elasticsearch.ymlxpack.notification.slack 命名空间中配置 Watcher 可以用来与 Slack 通信的帐户。

您需要一个具有传入 Webhook 功能的 Slack 应用程序来配置 Slack 帐户。使用生成的 Webhook URL 在 Elasticsearch 中设置您的 Slack 帐户。

要配置 Slack 帐户,您至少需要在 Elasticsearch 密钥库中指定帐户名称和 Webhook URL(请参阅安全设置

bin/elasticsearch-keystore add xpack.notification.slack.account.monitoring.secure_url
警告

您不能再使用elasticsearch.yml设置配置 Slack 帐户。请改用 Elasticsearch 的安全密钥库方法。

您可以为Slack 通知属性指定默认值

xpack.notification.slack:
  account:
    monitoring:
      message_defaults:
        from: x-pack
        to: notifications
        icon: http://example.com/images/watcher-icon.jpg
        attachment:
          fallback: "X-Pack Notification"
          color: "#36a64f"
          title: "X-Pack Notification"
          title_link: "https://elastic.ac.cn/guide/en/x-pack/current/index.html"
          text: "One of your watches generated this notification."
          mrkdwn_in: "pretext, text"

要通知多个频道,请在 Slack 中为每个频道创建一个 Webhook URL,并在 Elasticsearch 中创建多个 Slack 帐户(每个 Webhook URL 一个)。

如果您配置了多个 Slack 帐户,则需要配置一个默认帐户,或者在 slack 操作中指定应该使用哪个帐户发送通知。

xpack.notification.slack:
  default_account: team1
  account:
    team1:
      ...
    team2:
      ...
© . All rights reserved.