前提条件Prometheus+Grafana+alertmanager+ 邮件
钉钉告警安装
下载
去官网下载prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v2.1.0/prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
上传
mkdir prometheus-webhook-dingtalk && cd prometheus-webhook-dingtalk && rz -be
tar zxvf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
mv prometheus-webhook-dingtalk-2.1.0.linux-amd64 /usr/local/
ln -s /usr/local/prometheus-webhook-dingtalk-2.1.0.linux-amd64/ /usr/local/prometheus-webhook-dingtalk
配置config
拷贝个模板文件
cp config.example.yml config.yml
url 和 secret 是我们创建告警机器人的 webook 和 "加签"
配置模板
[root@zcsmaster1 prometheus-webhook-dingtalk]# pwd
/usr/local/prometheus-webhook-dingtalk
[root@zcsmaster1 prometheus-webhook-dingtalk]# vim contrib/templates/legacy/template.tmpl
contrib/templates/legacy/template.tmpl
{{ define "default.__text_alert_list" }}{{ range . }}
#### \[{{ .Labels.severity | upper }}\] {{ .Annotations.summary }}
**Description:** {{ .Annotations.description }}
**Graph:** [<d83d><dcc8>]({{ .GeneratorURL }})
**Details:**
{{ range .Labels.SortedPairs }}{{ if and (ne (.Name) "severity") (ne (.Name) "summary") }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}{{ end }}
{{ end }}{{ end }}
{{/* Default */}}
{{ define "default.title" }}{{ template "__subject" . }}{{ end }}
{{ define "default.content" }}#### \[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}\] **[{{ index .GroupLabels "alertname" }}]({{ template "__alertmanagerURL" . }})**
{{ if gt (len .Alerts.Firing) 0 -}}
**Alerts Firing**
{{ template "default.__text_alert_list" .Alerts.Firing }}
{{ range .AtMobiles }}@{{ . }}{{ end }}
{{- end }}
{{ if gt (len .Alerts.Resolved) 0 -}}
**Alerts Resolved**
{{ template "default.__text_alert_list" .Alerts.Resolved }}
{{ range .AtMobiles }}@{{ . }}{{ end }}
{{- end }}
{{- end }}
{{/* Legacy */}}
{{ define "legacy.title" }}{{ template "__subject" . }}{{ end }}
{{ define "legacy.content" }}#### \[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}\] **[{{ index .GroupLabels "alertname" }}]({{ template "__alertmanagerURL" . }})**
{{ template "__text_alert_list" .Alerts.Firing }}
{{- end }}
{{/* Following names for compatibility */}}
{{ define "ding.link.title" }}{{ template "default.title" . }}{{ end }}
{{ define "ding.link.content" }}{{ template "default.content" . }}{{ end }}
配置启动服务
echo '
[Unit]
Description=prometheus-webhook-dingtalk
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus-webhook-dingtalk/prometheus-webhook-dingtalk \
--config.file='/usr/local/prometheus-webhook-dingtalk/config.yml'
Restart=on-failure
RestartSec=60s
[Install]
WantedBy=multi-user.target
' > /usr/lib/systemd/system/prometheus-webhook-dingtalk.service
赋权
useradd prometheus
chown prometheus.prometheus /usr/local/prometheus-webhook-dingtalk -R
启动
systemctl enable prometheus-webhook-dingtalk.service
systemctl start prometheus-webhook-dingtalk.service
确认开启了
ps -aux|grep prometheus-webhook-dingtalk
观察日志
journalctl -u prometheus-webhook-dingtalk.service -f
测试告警
首先修改altermanger 配置
重启
kubectl apply -f alertmanager-cm.yaml
kubectl get pod -n ccse
kubectl delete pod prometheus-server-6d8f4447dd-gfprw -n ccse