需要将 Slack 和寻呼机职责集成到 Prometheus 并定义自定义规则



我在GCP kubernetes环境中配置了Prometheus&Grafana,使用 https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus/manifests 中提供的KB

。一切都运行良好,我的集群详细信息显示在 Grafana 中。现在我想为 Prometheus 配置警报,并需要集成到我的 Slack 频道。如果有人对此有任何想法,请告诉我。

提前致谢

使用 prometheus 运算符,我花了一段时间才弄清楚 alertmanager 配置作为秘密存储在 https://github.com/coreos/prometheus-operator/blob/master/contrib/kube-prometheus/manifests/alertmanager-secret.yaml

您需要对其进行解码,编辑,编码和应用

echo "Imdsb2JhbCI6IAogICJyZXNvbHZlX3RpbWVvdXQiOiAiNW0iCiJyZWNlaXZlcnMiOiAKLSAibmFtZSI6ICJudWxsIgoicm91dGUiOiAKICAiZ3JvdXBfYnkiOiAKICAtICJqb2IiCiAgImdyb3VwX2ludGVydmFsIjogIjVtIgogICJncm91cF93YWl0IjogIjMwcyIKICAicmVjZWl2ZXIiOiAibnVsbCIKICAicmVwZWF0X2ludGVydmFsIjogIjEyaCIKICAicm91dGVzIjogCiAgLSAibWF0Y2giOiAKICAgICAgImFsZXJ0bmFtZSI6ICJEZWFkTWFuc1N3aXRjaCIKICAgICJyZWNlaXZlciI6ICJudWxsIg==" | base64 --decode 

对 - 所以。您需要做一些事情。

首先下载并运行警报管理器 - 您可以在此处下载它 - 您可以运行简单的配置。

然后,您需要将警报管理器添加到您的普罗米修斯配置中。

prometheus.yml样品

alerting:
alertmanagers:
alerting:
alertmanagers:
- scheme: http
static_configs:
- targets:
- "localhost:9093"
- scheme: http
static_configs:
- targets:
- "localhost:9093"

假设您已经运行了规则,并且现在想要将其集成到 slack 中,那么在警报管理器配置文件中,您需要添加

global:
slack_api_url: '...'

route:
repeat_interval: 5s
receiver: slack-alert # replace this field
group_by:
- WebsiteStaus
- InstanceDownTime
receivers:
- name: 'slack-alert'
slack_configs:
- channel: '#some-channel'
username: 'prometheus-bot'
send_resolved: true
# title: '{{ range .Alerts }} {{ .Annotations.summary }} {{ end }}'
# text: '<!channel> n {{ range .Alerts }} {{ .Annotations.description }} n {{ end }}'
title: '{{ .CommonAnnotations.summary }}'
text: '{{ .CommonAnnotations.description }}'

只是关于.CommonAnnotations.Annotations的说明.CommonAnnotations是指单个警报,而Annotations是指多个警报事件。因此,如果警报规则同时触发两个警报,则需要使用CommonAnnotations

如果您没有设置任何规则,这里有一个示例规则,我用它来在网站出现故障时提醒我。

groups:
# ============================================================================
# Website alerts
# --------------
# ============================================================================
- name: WebsiteStaus
rules:
# Check if the probe_success was successful
- alert: SiteDown
expr: probe_success == 0
for: 5s
labels:
severity: page
annotations:
summary: A website has gone down!
description: '<{{ $labels.instance }}|{{ $labels.instance }}> failed to probe'

最新更新