Discord 事件处理器
Discord 是一个流行的聊天服务,主要面向游戏玩家以及游戏之外寻求免费解决方案的团队。要配置 Kapacitor 以向 Discord 发送告警消息,请设置适用的配置选项。
配置
Discord 事件处理器的配置以及默认选项值在您的 kapacitor.conf
中设置。以下是一个配置示例
[[discord]]
enabled = false
default = true
url = "https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
workspace = "guild-channel"
timestamp = true
username = "Kapacitor"
avatar-url = "https://influxdata.github.io/branding/img/downloads/influxdata-logo--symbol--pool-alpha.png"
embed-title = "Kapacitor Alert"
global = false
state-changes-only = false
ssl-ca = "/path/to/ca.crt"
ssl-cert = "/path/to/cert.crt"
ssl-key = "/path/to/private-key.key"
insecure-skip-verify = false
可以通过重复
[[discord]]
部分来配置多个 Discord 客户端。workspace
充当每个配置的 Discord 客户端的唯一标识符。
enabled
设置为 true
以启用 Discord 事件处理器。
default
如果指定了多个 Discord 客户端配置,请将一个配置标识为默认配置。
workspace
Discord 工作区 ID。设置此字符串以标识此特定 Discord 配置。例如,Discord 频道和它所属的公会名称,例如 <guild>-<channel>
。
timestamp
布尔值,表示时间戳是否应显示在嵌入中。
url
Discord webhook URL。这可以通过在频道设置中添加 webhook 来获得 - 有关完整指南,请参阅 Webhooks 简介。Discord 将为您提供 webhook URL。
username
设置 Discord 机器人用户名以覆盖生成 webhook 时设置的用户名。
avatar-url
设置指定头像的 URL 以覆盖生成 webhook 时设置的头像。
embed-title
设置要在告警嵌入中显示的标题。如果为空,则不设置标题。
global
设置为 true
以将所有告警发送到 Discord,而无需在 TICKscript 中显式指定 Discord。
state-changes-only
将所有告警设置为仅状态更改模式,这意味着只有在告警状态更改时才会发送告警。仅当 global
为 true
时适用。
ssl-ca
设置证书颁发机构文件的路径。
ssl-cert
设置主机证书文件的路径。
ssl-key
设置证书私钥文件的路径。
insecure-skip-verify
设置为 true
以使用 SSL 但跳过链和主机验证。如果使用自签名证书,则这是必要的。
选项
在处理程序文件中或在 TICKscript 中使用 .discord()
时,设置以下 Discord 事件处理程序选项。
名称 | 类型 | 描述 |
---|---|---|
workspace | string | 指定在有多个 Discord 配置时要使用的 Discord 配置。 |
timestamp | bool | 指定是否在嵌入页脚中显示时间戳。如果为空,则使用配置中的选择。 |
username | string | Discord 机器人的用户名。如果为空,则使用配置中的用户名。 |
avatar-url | string | 用作 webhook 头像的图像 URL。如果为空,则使用配置中的 URL。 |
embed-title | string | 发布到 webhook 的告警嵌入的标题。如果为空,则使用配置中设置的标题。 |
示例:处理程序文件
id: handler-id
topic: topic-name
kind: discord
options:
workspace: 'guild-channel'
username: 'Kapacitor'
avatar-url: 'https://influxdata.github.io/branding/img/downloads/influxdata-logo--symbol--pool-alpha.png'
timestamp: true
embed-title: 'Kapacitor Alert'
示例:TICKscript
|alert()
// ...
.discord()
.workspace('guild-channel')
.username('Kapacitor')
.avatarUrl('https://influxdata.github.io/branding/img/downloads/influxdata-logo--symbol--pool-alpha.png')
.timestamp(true)
.embedTitle('Kapacitor Alert')
设置 Guild
要允许 Kapacitor 向 Discord 发送告警,请从 Discord 获取 webhook url - 请参阅 Webhooks 简介。然后,将生成的 webhook URL 添加为您的 kapacitor.conf
的 [[discord]]
配置部分中的 url
。
使用 Discord 事件处理器
在一个或多个 Discord 事件处理器在您的 kapacitor.conf
中启用和配置后,在您的 TICKscript 中使用 .discord()
属性向 Discord 发送告警,或定义一个订阅主题并将发布的告警发送到 Discord 的 Discord 处理程序。
为了避免每个告警间隔都发布消息,请使用 AlertNode.StateChangesOnly,以便只有告警状态更改的事件才会发送到 Discord。
请参阅下面的示例,了解在 kapacitor.conf
中定义的示例 Discord 配置
kapacitor.conf 中的 Discord 设置
[[discord]]
enabled = true
default = true
url = "https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
workspace = "guild-alerts"
timestamp = true
username = "AlertBot"
avatar-url = "https://influxdata.github.io/branding/img/downloads/influxdata-logo--symbol--pool-alpha.png"
embed-title = "Alert"
global = false
state-changes-only = false
[[discord]]
enabled = true
default = false
url = "https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
workspace = "guild-errors"
timestamp = true
username = "StatsBot"
avatar-url = "https://influxdata.github.io/branding/img/downloads/influxdata-logo--symbol--pool-alpha.png"
embed-title = "Errors"
global = false
state-changes-only = false
从 TICKscript 向 Discord 发送告警
在您的 TICKscript 中使用 .discord()
事件处理程序发送告警。例如,此配置将在空闲 CPU 使用率降至 20% 以下时,向 Discord 频道发送消息“Hey, check your CPU”的告警。
discord-cpu-alert.tick
stream
|from()
.measurement('cpu')
|alert()
.warn(lambda: "usage_idle" < 20)
.stateChangesOnly()
.message('Hey, check your CPU')
.discord()
.embedTitle('Uh Oh!')
从定义的处理程序向 Discord 发送告警
添加一个订阅 cpu
的 Discord 处理程序,方法是创建一个 TICKscript,将告警消息发布到主题。例如,此配置将发送消息为“Hey, check your CPU”的告警。添加了一个订阅 cpu
主题并将所有告警消息发布到 Discord 的 Discord 处理程序。
创建一个 TICKscript,将告警消息发布到主题。下面的 TICKscript 在空闲 CPU 使用率降至 5% 以下时,向 cpu
主题发送严重告警消息。
cpu_alert.tick
stream
|from()
.measurement('cpu')
|alert()
.crit(lambda: "usage_idle" < 5)
.stateChangesOnly()
.message('Hey, check your CPU')
.topic('cpu')
添加并启用 TICKscript
kapacitor define cpu_alert -tick cpu_alert.tick
kapacitor enable cpu_alert
创建一个处理程序文件,该文件订阅 cpu
主题并使用 Discord 事件处理程序向 Discord 发送告警。此处理程序使用非默认的 Discord 处理程序“critical-alerts”,该处理程序将消息发送到 Discord 中的 #critical-alerts 频道。
discord_cpu_handler.yaml
id: discord-cpu-alert
topic: cpu
kind: discord
options:
workspace: 'guild-alerts'
embed-title: 'Hey, Listen!'
添加处理程序
kapacitor define-topic-handler discord_cpu_handler.yaml
使用多个 Discord 配置
Kapacitor 可以使用多个 Discord 集成,每个集成都由 workspace
配置的值标识。下面的 TICKscript 说明了如何使用多个 Discord 集成。
在 上面的 kapacitor.conf
中,有两个 Discord 配置;一个用于告警,另一个用于每日统计信息。每个 Discord 配置的 workspace
配置都充当唯一标识符。
以下 TICKscript 将告警发送到 alerts
Discord 工作区。
discord-cpu-alert.tick
stream
|from()
.measurement('cpu')
|alert()
.crit(lambda: "usage_idle" < 5)
.stateChangesOnly()
.message('Hey, I think the machine is on fire.')
.discord()
.workspace('alerts')
.embedTitle('AAAAAAAAAAAAAAAAAAAAAA')
错误率也存储在同一个 InfluxDB 实例中,我们希望将 500
错误的每日报告发送到 error-reports
Discord 工作区。以下 TICKscript 收集 500
错误事件并将它们发布到 500-errors
主题。
500_errors.tick
stream
|from()
.measurement('errors')
.groupBy('500')
|alert()
.info(lambda: 'count' > 0)
.noRecoveries()
.topic('500-errors')
下面是一个聚合处理程序,它订阅 500-errors
主题,聚合 24 小时内 500 个错误的数量,然后将聚合消息发布到 500-errors-24h
主题。
500_errors_24h.yaml
id: 500-errors-24h
topic: 500-errors
kind: aggregate
options:
interval: 24h
topic: 500-errors-24h
message: '{{ .Count }} 500 errors last 24 hours.'
最后,但并非最不重要的一点,是一个 Discord 处理程序,它订阅 500-errors-24h
主题并将聚合计数消息发布到 error-reports
Discord 工作区
discord_500_errors_daily.yaml
id: discord-500-errors-daily
topic: 500-errors-24h
kind: discord
options:
workspace: guild-errors
此页面是否对您有帮助?
感谢您的反馈!