1、环境介绍
grafana版本:9.4.1
建议:使用grafana alerting来做告警的话,一定要使用9.4以后得版本,以前的版本功能都不完善
目前公司的日志采集使用的是PLG (promtail、loki、grafana)架构,日志告警这块没有使用loki自带的告警功能,而是使用 grafana alerting 来做日志告警
需求:按照告警级别来区分发送到不同的告警渠道
告警级别 | 告警渠道 |
---|---|
Alert2 | Slack |
Alert1 | Slack、微信 |
Alert0 | Slack、微信、电话告警 |
2、告警功能
- 告警规则
- 通知渠道
- 通知渠道
- 通知模板
- 通知策略
- 路由规则
- 静音计时
- 静默
- 团队
- 管理员
3、创建通知模板
创建标题模板:title
{{ define "title" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
日志告警: {{ $alert.Labels.team }}团队{{ $alert.Labels.env }}环境{{ $alert.Labels.job }}服务触发告警,请及时处理!
{{- end }}
{{- end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
告警恢复: {{ $alert.Labels.team }}团队{{ $alert.Labels.env }}环境{{ $alert.Labels.job }}服务告警恢复,请知晓!
{{- end }}
{{- end }}
{{- end }}
{{- end }}
创建通知模板:common
{{ define "common" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
===========================
告警类型: {{ $alert.Labels.alertname }}
告警级别: {{ $alert.Labels.severity }}
告警服务: {{ $alert.Labels.job }}
告警详情: {{ $alert.Annotations.description }}
告警时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
抑制告警: {{ $alert.SilenceURL }}
===========================
{{- end }}
{{- end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
===========================
告警类型: {{ $alert.Labels.alertname }}
告警级别: {{ $alert.Labels.severity }}
告警服务: {{ $alert.Labels.job }}
告警详情: {{ $alert.Annotations.description }}
告警时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
恢复时间: {{ ($alert.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
===========================
{{- end }}
{{- end }}
{{- end }}
{{- end }}
4、创建通知渠道
添加通知渠道,具体参数如下
Slack通知配置如下:
Name:Slack
Integration:Slack
Webhook URL:你的Slack地址
Title:{{ template “title” . }}
Text Body:{{ template “common” . }}
微信通知配置如下:
Name:WeChat
Integration:WeCom
Agent ID:你的企业微信中应用ID
Corp ID:你的企业微信账号ID
Secret:你的企业微信中应用Secret
Message Type:Text
Title:{{ template “title” . }}
Text Body:{{ template “common” . }}
电话通知配置如下:
Name:Phone
Integration:Webhook
URL:你的Webhook接口地址
HTTP Method:POST
Title:{{ template “title” . }}
Text Body:{{ template “common” . }}
4、静音计时
静音计时可以设置在什么时间段(例如:晚上)忽略告警、忽略不重要的告警等
5、创建路由规则
根策略 - 所有警报的默认设置,所有警报都将转到默认通知渠道,除非您在特定路由区域中设置其他匹配器。
说明:经过测试目前版本的根策略,多个通知渠道的时候,无法正常收到告警
故可以在具体路由中,再创建一个匹配所有的策略,如下图:
匹配所有标签路由规则—> Slack、微信、电话告警级别
匹配特定标签的路由规则—> 微信、电话告警级别
匹配特定标签的路由规则—> 电话告警级别
6、告警规则
创建告警规则
评论区