文档文档

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]] 配置时,它充当标识符。端点仅用作标识符。它们不会附加到 HTTP POST URL。

url

将发布警报数据的 URL。

headers

设置要应用到 POST 请求的额外头信息。

basic-auth

设置要应用到 POST 请求的认证凭据。

alert-template

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

alert-template-file

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

row-template

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

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 指定。
headers字符串到字符串的映射设置要应用到 POST 请求的额外头信息。
capture‑responsebool如果 HTTP 状态码不是 2xx 代码,则读取并记录 HTTP 响应。
timeout持续时间HTTP 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 事件处理器可以在 TICKscripts 和事件处理器文件中使用,将警报和 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 发布警报

下面的 TICKscripts 使用 .post() 事件处理器,在闲置 CPU 使用率低于 10% 时发布消息:“嘿,检查你的 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”发送到 cpu 主题。添加了一个 post 事件处理器,该处理器订阅 cpu 主题,并将所有警报消息发布到 kapacitor.conf 中定义的 URL 和端点。

创建将警报消息发布到主题的 TICKscript。当空闲 CPU 使用率低于 10% 时,下面的 TICKscript 将警报消息发送到 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 主体。它们仅与 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。
.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 主体。它们仅与 httpPost 事件处理器一起使用,因为它们一次消耗一行数据。模板可以在 kapacitor.conf 中使用 row-template 配置内联定义,或者定义在单独的文件中并使用 row-template-file 配置进行引用。

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

字段描述
.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}}

此页面是否有帮助?

感谢您的反馈!


InfluxDB 3.8 新特性

InfluxDB 3.8 和 InfluxDB 3 Explorer 1.6 的主要增强功能。

查看博客文章

InfluxDB 3.8 现已适用于 Core 和 Enterprise 版本,同时发布了 InfluxDB 3 Explorer UI 的 1.6 版本。本次发布着重于操作成熟度,以及如何更轻松地部署、管理和可靠地运行 InfluxDB。

更多信息,请查看

InfluxDB Docker 的 latest 标签将指向 InfluxDB 3 Core

在 **2026 年 2 月 3 日**,InfluxDB Docker 镜像的 latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。

如果使用 Docker 来安装和运行 InfluxDB,latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。例如,如果使用 Docker 运行 InfluxDB v2,请将 latest 版本标签替换为 Docker pull 命令中的特定版本标签 — 例如

docker pull influxdb:2