发送警报邮件
使用第三方服务发送警报电子邮件,例如 SendGrid、Amazon Simple Email Service (SES)、Mailjet 或 Mailgun。要发送警报电子邮件,请完成以下步骤:
- 创建检查 用于识别要监控的数据和要警报的状态。
- 设置您首选的电子邮件服务(注册、获取API凭据和发送测试电子邮件):
- SendGrid:查看开始使用SendGrid API
- AWS简单电子邮件服务(SES):查看使用Amazon SES API。您的AWS SES请求,包括
url(端点)、身份验证和请求结构可能会有所不同。有关更多信息,请参见Amazon SES API请求和对Amazon SES API进行身份验证的请求。 - Mailjet:查看开始使用Mailjet
- Mailgun:查看Mailgun注册
- 创建一个警报电子邮件任务 以调用您的电子邮件服务并发送警报电子邮件。
在下面的过程中,我们使用 InfluxDB UI(用户界面)中的 Task 页面来创建一个任务。探索其他方式来 创建任务。
创建警报邮件任务
在 InfluxDB 用户界面中,选择左侧导航菜单中的 任务。
点击 创建任务。
在Name字段中,输入一个描述性名称,例如Send alert email,然后在Every字段中输入任务运行的频率,例如
10m。有关更多细节,例如使用cron语法或包括偏移量,请参见Task configuration options。在右侧面板中,在您的任务脚本中输入以下详细信息(请参见下面的示例):
- 导入Flux HTTP包。
- (可选) 将您的 API 密钥存储为秘密以便重用。 首先, 将您的 API 密钥添加为秘密, 然后导入 Flux InfluxDB Secrets 包。
- 查询
statuses测量值在_monitoring桶中以检索由您的检查生成的所有状态。 - 设置监视的时间范围;使用任务计划运行的相同间隔。例如,
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 requests 和 Authenticating requests to the Amazon SES API。我们建议使用 Signature Version 4 signing process 签名您的 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 reference。
下面的示例使用 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},
)