发送警报邮件
使用第三方服务(例如 SendGrid, Amazon Simple Email Service (SES), Mailjet, 或 Mailgun)发送警报邮件。 要发送警报邮件,请完成以下步骤
- 创建一个检查以识别要监控的数据和要发出警报的状态。
- 设置你首选的电子邮件服务(注册、检索 API 凭据并发送测试邮件)
- SendGrid: 请参阅 SendGrid API 入门
- AWS Simple Email Service (SES): 请参阅 使用 Amazon SES API。 你的 AWS SES 请求,包括
url
(端点)、身份验证和请求的结构可能会有所不同。 有关更多信息,请参阅 Amazon SES API 请求 和 验证对 Amazon SES API 的请求。 - Mailjet: 请参阅 Mailjet 入门
- Mailgun: 请参阅 Mailgun 注册
- 创建一个警报邮件任务以调用你的电子邮件服务并发送警报邮件。
在下面的过程中,我们使用 InfluxDB UI(用户界面)中的 Task 页面来创建任务。 探索其他 创建任务 的方法。
创建一个警报邮件任务
在 InfluxDB UI 中,选择左侧导航菜单中的 Tasks。
点击 创建任务。
在 Name 字段中,输入一个描述性名称,例如 Send alert email,然后在 Every 字段中输入任务的运行频率,例如
10m
。 有关更多详细信息,例如使用 cron 语法或包含偏移量,请参阅 任务配置选项。在右侧面板中,在你的任务脚本中输入以下详细信息(请参阅下面的示例)
- 导入 Flux HTTP 包。
- (可选)将你的 API 密钥存储为密钥以供重用。 首先,将你的 API 密钥添加为密钥,然后导入 Flux InfluxDB 密钥包。
- 查询
_monitoring
存储桶中的statuses
测量以检索你的检查生成的所有状态。 - 设置要监控的时间范围;使用与任务计划运行的间隔相同的时间间隔。 例如,
range (start: -task.every)
。 - 设置要发出警报的
_level
,例如crit
、warn
、info
或ok
。 - 使用
map()
函数评估使用http.post()
发送警报的标准。 - 指定你的电子邮件服务
url
(端点),包括适用的请求headers
,并验证你的请求data
格式是否遵循你的电子邮件服务指定的格式。
示例
下面的示例使用 SendGrid API 在自上次任务运行以来发生超过 3 个严重状态时发送警报邮件。
import "http"
import "json"
// Import the Secrets package if you store your API key as a secret.
// For detail on how to do this, see Step 4 above.
import "influxdata/influxdb/secrets"
// Retrieve the secret if applicable. Otherwise, skip this line
// and add the API key as the Bearer token in the Authorization header.
SENDGRID_APIKEY = secrets.get(key: "SENDGRID_APIKEY")
numberOfCrits = from(bucket: "_monitoring")
|> range(start: -task.every)
|> filter(fn: (r) => r._measurement == "statuses" and r._level == "crit")
|> count()
numberOfCrits
|> map(
fn: (r) => if r._value > 3 then
{r with _value: http.post(
url: "https://api.sendgrid.com/v3/mail/send",
headers: {"Content-Type": "application/json", "Authorization": "Bearer ${SENDGRID_APIKEY}"},
data: json.encode(
v: {
"personalizations": [
{
"to": [
{
"email": "jane.doe@example.com"
}
]
}
],
"from": {
"email": "john.doe@example.com"
},
"subject": "InfluxDB critical alert",
"content": [
{
"type": "text/plain",
"value": "There have been ${r._value} critical statuses."
}
]
}
)
)}
else
{r with _value: 0},
)
下面的示例使用 AWS SES API v2 在自上次任务运行以来发生超过 3 个严重状态时发送警报邮件。
你的 AWS SES 请求,包括 url
(端点)、身份验证和请求的结构可能会有所不同。 有关更多信息,请参阅 Amazon SES API 请求 和 验证对 Amazon SES API 的请求。 我们建议使用 Signature Version 4 签名过程 对你的 AWS API 请求进行签名。
import "http"
import "json"
// Import the Secrets package if you store your API credentials as secrets.
// For detail on how to do this, see Step 4 above.
import "influxdata/influxdb/secrets"
// Retrieve the secrets if applicable. Otherwise, skip this line
// and add the API key as the Bearer token in the Authorization header.
AWS_AUTH_ALGORITHM = secrets.get(key: "AWS_AUTH_ALGORITHM")
AWS_CREDENTIAL = secrets.get(key: "AWS_CREDENTIAL")
AWS_SIGNED_HEADERS = secrets.get(key: "AWS_SIGNED_HEADERS")
AWS_CALCULATED_SIGNATURE = secrets.get(key: "AWS_CALCULATED_SIGNATURE")
numberOfCrits = from(bucket: "_monitoring")
|> range(start: -task.every)
|> filter(fn: (r) => r.measurement == "statuses" and r._level == "crit")
|> count()
numberOfCrits
|> map(
fn: (r) => if r._value > 3 then
{r with _value: http.post(
url: "https://email.your-aws-region.amazonaws.com/sendemail/v2/email/outbound-emails",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer ${AWS_AUTH_ALGORITHM}${AWS_CREDENTIAL}${AWS_SIGNED_HEADERS}${AWS_CALCULATED_SIGNATURE}"},
data: json.encode(v: {
"Content": {
"Simple": {
"Body": {
"Text": {
"Charset": "UTF-8",
"Data": "There have been ${r._value} critical statuses."
}
},
"Subject": {
"Charset": "UTF-8",
"Data": "InfluxDB critical alert"
}
}
},
"Destination": {
"ToAddresses": [
"john.doe@example.com"
]
}
}
)
)}
else
{r with _value: 0},
)
有关请求语法的详细信息,请参阅 SendEmail API v2 参考。
下面的示例使用 Mailjet Send API 在自上次任务运行以来发生超过 3 个严重状态时发送警报邮件。
要查看你的 Mailjet API 凭据,请登录 Mailjet 并打开 API 密钥管理页面。
import "http"
import "json"
// Import the Secrets package if you store your API keys as secrets.
// For detail on how to do this, see Step 4 above.
import "influxdata/influxdb/secrets"
// Retrieve the secrets if applicable. Otherwise, skip this line
// and add the API keys as Basic credentials in the Authorization header.
MAILJET_APIKEY = secrets.get(key: "MAILJET_APIKEY")
MAILJET_SECRET_APIKEY = secrets.get(key: "MAILJET_SECRET_APIKEY")
numberOfCrits = from(bucket: "_monitoring")
|> range(start: -task.every)
|> filter(fn: (r) => r.measurement == "statuses" and "r.level" == "crit")
|> count()
numberOfCrits
|> map(
fn: (r) => if r._value > 3 then
{r with
_value: http.post(
url: "https://api.mailjet.com/v3.1/send",
headers: {
"Content-type": "application/json",
"Authorization": "Basic ${MAILJET_APIKEY}:${MAILJET_SECRET_APIKEY}"
},
data: json.encode(
v: {
"Messages": [
{
"From": {"Email": "jane.doe@example.com"},
"To": [{"Email": "john.doe@example.com"}],
"Subject": "InfluxDB critical alert",
"TextPart": "There have been ${r._value} critical statuses.",
"HTMLPart": "<h3>${r._value} critical statuses</h3><p>There have been ${r._value} critical statuses.",
},
],
},
),
),
}
else
{r with _value: 0},
)
下面的示例使用 Mailgun API 在自上次任务运行以来发生超过 3 个严重状态时发送警报邮件。
要查看你的 Mailgun API 密钥,请登录 Mailjet 并打开 账户安全 - API 安全。 Mailgun 要求通过 Mailgun 指定一个域。 当你首次设置帐户时,会自动为你创建一个域。 你必须在你的 url
端点中包含此域(例如,https://api.mailgun.net/v3/YOUR_DOMAIN
或 https://api.eu.mailgun.net/v3/YOUR_DOMAIN
。 如果你使用的是 Mailgun 的免费版本,则可以为你的域设置最多五个授权收件人(接收电子邮件警报)。 要查看你的 Mailgun 域,请登录 Mailgun 并查看 域名页面。
import "http"
import "json"
// Import the Secrets package if you store your API key as a secret.
// For detail on how to do this, see Step 4 above.
import "influxdata/influxdb/secrets"
// Retrieve the secret if applicable. Otherwise, skip this line
// and add the API key as the Bearer token in the Authorization header.
MAILGUN_APIKEY = secrets.get(key: "MAILGUN_APIKEY")
numberOfCrits = from(bucket: "_monitoring")
|> range(start: -task.every)
|> filter(fn: (r) => r["_measurement"] == "statuses")
|> filter(fn: (r) => r["_level"] == "crit")
|> count()
numberOfCrits
|> map(
fn: (r) => if r._value > 1 then
{r with _value: http.post(
url: "https://api.mailgun.net/v3/YOUR_DOMAIN/messages",
headers: {
"Content-type": "application/json",
"Authorization": "Basic api:${MAILGUN_APIKEY}"
},
data: json.encode(v: {
"from": "Username <mailgun@YOUR_DOMAIN_NAME>",
"to": "email@example.com",
"subject": "InfluxDB critical alert",
"text": "There have been ${r._value} critical statuses."
}
)
)}
else
{r with _value: 0},
)
此页内容是否对您有帮助?
感谢您的反馈!
支持与反馈
感谢您成为我们社区的一份子! 我们欢迎并鼓励您提供关于 InfluxDB 和本文档的反馈和错误报告。 要获得支持,请使用以下资源
拥有年度或支持合同的客户可以联系 InfluxData 支持。