文档文档

Post (HTTP) 事件处理程序

post 事件处理程序将 JSON 编码的数据发布到 HTTP 端点。

配置

post 事件处理程序的配置以及默认的 选项 值在您的 kapacitor.conf 中设置。以下是一个配置示例

kapacitor.conf 中的 Post 设置

[[httppost]]
  endpoint = "example"
  url = "http://example.com/path"
  headers = { Example = "your-key" }
  basic-auth = { username = "my-user", password = "my-pass" }
  alert-template = "{{.Message}}:{{range .Data.Series}}{{.Tags}},{{range .Values}}{{.}}{{end}}{{end}}"
  alert-template-file = "/path/to/template/file"
  row-template = "{{.Name}} host={{index .Tags \"host\"}}{{range .Values}} {{index . "time"}} {{index . "value"}}{{end}}"
  row-template-file = "/path/to/template/file"

endpoint

配置的 HTTP POST 端点的名称,当存在多个 [[httppost]] 配置时,它充当 [[httppost]] 配置的标识符。端点仅是标识符。它们不会附加到 HTTP POST URL。

url

将向其发布警报数据的 URL。

headers

要在 POST 请求上设置的额外标头值集。

basic-auth

要在 POST 请求上设置的身份验证凭据集。

alert-template

用于构建自定义 HTTP 主体的警报模板。警报模板仅与 post alert 处理程序一起使用,因为它们会消耗警报数据。跳到 警报模板

alert-template-file

警报模板文件的绝对路径。跳到 警报模板

row-template

用于构建自定义 HTTP 主体的行模板。行模板仅与 httpPost 节点 管道节点一起使用,因为它们一次消耗一行。跳到 行模板

row-template-file

行模板文件的绝对路径。跳到 行模板

使用环境变量定义配置选项

endpointurlheaders 配置选项可以使用环境变量定义

KAPACITOR_HTTPPOST_0_ENDPOINT = "example"
KAPACITOR_HTTPPOST_0_URL = "http://example.com/path"
KAPACITOR_HTTPPOST_0_HEADERS_Example1 = "header1"
KAPACITOR_HTTPPOST_0_HEADERS_Example2 = "header2"

配置和使用多个 HTTP POST 端点

kapacitor.conf 支持多个 [[httppost]] 部分。每个部分的 endpoint 配置选项充当该特定配置的唯一标识符。要将特定的 [[httppost]] 配置与 Post 警报处理程序一起使用,请在您的 post 警报处理程序文件您的 TICKscript 中指定端点。

kapacitor.conf

[[httppost]]
  endpoint = "endpoint1"
  url = "http://example-1.com/path"
  # ...

[[httppost]]
  endpoint = "endpoint2"
  url = "http://example-2.com/path"
  # ...

也可以使用环境变量添加多个 HTTP POST 端点配置。变量值使用每个变量键中的数字分组在一起。

KAPACITOR_HTTPPOST_0_ENDPOINT = "example0"
KAPACITOR_HTTPPOST_0_URL = "http://example-0.com/path"
KAPACITOR_HTTPPOST_0_HEADERS_Example1 = "header1"
KAPACITOR_HTTPPOST_0_HEADERS_Example2 = "header2"

KAPACITOR_HTTPPOST_1_ENDPOINT = "example1"
KAPACITOR_HTTPPOST_1_URL = "http://example-1.com/path"
KAPACITOR_HTTPPOST_1_HEADERS_Example1 = "header1"
KAPACITOR_HTTPPOST_1_HEADERS_Example2 = "header2"

选项

以下 post 事件处理程序选项可以在 处理程序文件 中设置,或者在使用 TICKscript 中的 .post() 时设置。

名称类型描述
urlendpoint将向其发布警报数据的 URL。
endpointendpoint字符串
headers要使用的 HTTP POST 端点的名称(在 kapacitor.conf 中配置)。不能代替 URL 指定。要在 POST 请求上设置的额外标头值集。
headers字符串到字符串的映射capture‑response
布尔值如果 HTTP 状态代码不是 2xx 代码,则读取并记录 HTTP 响应。timeout
duration字符串到字符串的映射HTTP POST 的超时时间。

skipSSLVerification

id: handler-id
topic: topic-name
kind: post
options:
  # Using the 'example' endpoint configured in the kapacitor.conf
  endpoint: example

布尔值

id: handler-id
topic: topic-name
kind: post
options:
  # Defining post options "inline"
  url: http://example.com/path
  headers:
    'Example1': 'example1'
    'Example2': 'example2'
  capture-response: true
  timeout: 10s
  skipSSLVerification: true

禁用 POST 请求的 SSL 验证。

|alert()
  // ...  
  // Using the 'example' endpoint configured in the kapacitor.conf
  .post()
    .endpoint('example')

示例:处理程序文件 - 使用预配置的端点

|alert()
  // ...
  // Defining post options "inline"
  .post('https://example.com/path')
    .header('Example1', 'example1')
    .header('Example2', 'example2')
    .captureResponse()
    .timeout(10s)
    .skipSSLVerification()

示例:处理程序文件 - “内联”定义 post 选项

示例:TICKscript - 使用预配置的端点

示例:TICKscript - “内联”定义 post 选项

[[httppost]]
  endpoint = "api-alert"
  url = "http://mydomain.com/api/alerts"
  headers = { From = "alerts@mydomain.com" }
  alert-template = "{{.Message}}:{{range .Data.Series}}{{.Tags}},{{range .Values}}{{.}}{{end}}{{end}}"

使用 Post 事件处理程序

post 事件处理程序可以在 TICKscript 和处理程序文件中使用,以将警报和 HTTP POST 数据发布到 HTTP 端点。以下示例处理警报,并使用在 kapacitor.conf 中定义的相同 [[httppost]] 配置

kapacitor.conf 中的 HTTP POST 设置

stream
  |from()
    .measurement('cpu')
  |alert()
    .crit(lambda: "usage_idle" < 10)
    .message('Hey, check your CPU')
    .post()
      .endpoint('api-alerts')

从 TICKscript 发布警报

kapacitor.conf 中的 HTTP POST 设置

stream
  |from()
    .measurement('cpu')
  |alert()
    .crit(lambda: "usage_idle" < 10)
    .message('Hey, check your CPU')
    .post('https://example.com/path')
      .header('Example1', 'example1')
      .header('Example2', 'example2')
      .captureResponse()
      .timeout(10s)
      .skipSSLVerification()

以下 TICKscript 使用 .post() 事件处理程序发布消息“Hey, check your CPU”,每当空闲 CPU 使用率降至 10% 以下时。

post-cpu-alert.tick

如果您不想使用在 kapacitor.conf 中定义的 [[httppost]] 设置,您可以内联指定您的 post 选项。

从定义的处理程序发布警报

stream
  |from()
    .measurement('cpu')
  |alert()
    .crit(lambda: "usage_idle" < 10)
    .message('Hey, check your CPU')
    .topic('cpu')

以下设置将警报发送到 cpu 主题,消息为“Hey, check your CPU”。添加了一个 post 处理程序,该处理程序订阅 cpu 主题并将所有警报消息发布到在 kapacitor.conf 中定义的 url 和端点。

kapacitor define cpu_alert -tick cpu_alert.tick
kapacitor enable cpu_alert

创建一个 TICKscript,将警报消息发布到主题。以下 TICKscript 在任何时候空闲 CPU 使用率降至 10% 以下时,都会向 cpu 主题发送警报消息。

cpu_alert.tick

id: post-cpu-alert
topic: cpu
kind: post
options:
  url: 'http://example.com/path'
  headers:
    'From': 'alert@mydomain.com'

添加并启用 TICKscript

kapacitor define-topic-handler post_cpu_handler.yaml

创建一个处理程序文件,该文件订阅 cpu 主题并使用 post 事件处理程序将警报发布到 HTTP 端点。

post_cpu_handler.yaml

添加处理程序

Post 模板

post 事件处理程序允许您使用警报和行模板自定义 POST 的内容和结构。

警报模板描述
警报模板用于构建自定义 HTTP 主体。它们仅与 post alert 处理程序一起使用,因为它们会消耗警报数据。模板以内联方式在 kapacitor.conf 中使用 alert-template 配置定义,或在单独的文件中使用 alert-template-file 配置引用。警报模板使用 Golang Template 并且可以访问以下字段
字段.ID
警报的唯一 ID。.Message
警报的消息。.Details
警报的详细信息。.Time
警报事件发生的时间。.Duration
警报事件的持续时间。.Level
警报级别,即 INFO、WARN 或 CRITICAL。.Data
触发警报的数据。.PreviousLevel

警报的先前级别,即 INFO、WARN 或 CRITICAL。

kapacitor.conf

[[httppost]]
  endpoint = "host"
  url = "host={{index .ID \"host\"}}{{index . "time"}}{{end}}}"
  alert-template = "{{.Message}}:{{range .Data.Series}}{{.Tags}},{{range .Values}}{{.}}{{end}}{{end}}"

.Recoverable

kapacitor.conf

[[httppost]]
  endpoint = "host"
  url = "host={{index .ID \"host\"}}{{index . "time"}}{{end}}}"
  alert-template-file = "/etc/templates/alert.html"

指示警报是否可自动恢复。

{{.Message}}:{{range .Data.Series}}{{.Tags}},{{range .Values}}{{.}}{{end}}{{end}}

内联警报模板

警报模板文件

/etc/templates/alert.html

警报模板描述
行模板行模板用于构建自定义 HTTP 主体。它们仅与 httpPost 处理程序一起使用,因为它们一次消耗一行。模板以内联方式在 kapacitor.conf 中使用 row-template 配置定义,或在单独的文件中使用 row-template-file 配置引用。
行模板使用 Golang Template 并且可以访问以下字段.Name
数据流的指标名称.Tags

数据上的标签映射。

kapacitor.conf

[[httppost]]
  endpoint = "host"
  url = "host={{index .Tags \"host\"}}{{range .Values}} {{index . "time"}} {{index . "value"}}{{end}}"
  row-template = '{{.Name}} host={{index .Tags "host"}}{{range .Values}} {{index . "time"}} {{index . "value"}}{{end}}'

.Values

kapacitor.conf

[[httppost]]
  endpoint = "host"
  url = "host={{index .Tags \"host\"}}{{range .Values}} {{index . "time"}} {{index . "value"}}{{end}}"
  row-template-file = "/etc/templates/row.html"

值列表;每个值都是一个映射,其中包含点的“time”键以及点上所有其他字段的键。

{{.Name}} host={{index .Tags \"host\"}}{{range .Values}} {{index . "time"}} {{index . "value"}}{{end}}

内联行模板

此页面是否对您有帮助?


请告诉我们如何改进

Flux 的未来

Flux 即将进入维护模式。您可以继续像现在这样使用它,而无需对代码进行任何更改。

阅读更多

现已全面上市