文档文档

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 Body 的警报模板。警报模板仅与 post alert 处理器一起使用,因为它们会消耗警报数据。跳至 警报模板

alert-template-file

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

row-template

用于构建自定义 HTTP Body 的行模板。行模板仅与 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() 时设置。

名称类型描述
urlstring警报数据将发布到的 URL。
endpointstring要使用的 HTTP POST 端点的名称(在 kapacitor.conf 中配置)。不能代替 URL 指定。
headersstring 到 string 的映射要在 POST 请求上设置的额外标头值集。
capture‑responsebool如果 HTTP 状态代码不是 2xx 代码,则读取并记录 HTTP 响应。
timeoutdurationHTTP POST 的超时时间。
skipSSLVerificationbool禁用 POST 请求的 SSL 验证。

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

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

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

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

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

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

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

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

使用 Post 事件处理器

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

kapacitor.conf 中的 HTTP 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}}"

从 TICKscript 发布警报

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

post-cpu-alert.tick

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

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

post-cpu-alert.tick

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()

从定义的处理器发布警报

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

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

cpu_alert.tick

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

添加并启用 TICKscript

kapacitor define cpu_alert -tick cpu_alert.tick
kapacitor enable cpu_alert

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

post_cpu_handler.yaml

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

添加处理器

kapacitor define-topic-handler post_cpu_handler.yaml

Post 模板

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

警报模板

警报模板用于构建自定义 HTTP Body。它们仅与 post alert 处理器一起使用,因为它们会消耗警报数据。模板可以在 kapacitor.conf 中使用 alert-template 配置内联定义,也可以在单独的文件中定义,并使用 alert-template-file 配置引用。

警报模板使用 Golang 模板,并且可以访问以下字段

字段描述
.ID警报的唯一 ID。
.Message警报的消息。
.Details警报的详细信息。
.Time警报事件发生的时间。
.Duration警报事件的持续时间。
.Level警报级别,即 INFO、WARN 或 CRITICAL。
.Data触发警报的数据。
.PreviousLevel警报的先前级别,即 INFO、WARN 或 CRITICAL。
.Recoverable指示警报是否可自动恢复。

内联警报模板

kapacitor.conf

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

警报模板文件

kapacitor.conf

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

/etc/templates/alert.html

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

行模板

行模板用于构建自定义 HTTP Body。它们仅与 httpPost 处理器一起使用,因为它们一次消耗一行。模板可以在 kapacitor.conf 中使用 row-template 配置内联定义,也可以在单独的文件中定义,并使用 row-template-file 配置引用。

行模板使用 Golang 模板,并且可以访问以下字段

字段描述
.Name数据流的指标名称
.Tags数据上的标签映射。
.Values值列表;每个值都是一个映射,其中包含点的“time”键以及点上所有其他字段的键。

内联行模板

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}}'

行模板文件

kapacitor.conf

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

/etc/templates/row.html

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

此页内容对您有帮助吗?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。您可以继续像目前一样使用它,而无需更改任何代码。

阅读更多

InfluxDB 3 开源版本现已推出 Public Alpha

InfluxDB 3 开源版本现已可用于 alpha 测试,根据 MIT 或 Apache 2 许可获得许可。

我们正在发布作为 alpha 版本一部分的两种产品。

InfluxDB 3 Core 是我们新的开源产品。它是用于时间序列和事件数据的最新数据引擎。InfluxDB 3 Enterprise 是一个商业版本,它建立在 Core 的基础上,增加了历史查询功能、读取副本、高可用性、可扩展性和细粒度的安全性。

有关如何入门的更多信息,请查看