Alertmanager邮箱和企业微信的告警模板分享

前言

Prometheus监控功能十分强大,上手很快,而且是轻量级的。监控面板可以通过Grafana来展现,撘过ELK的都明白Kibana绘图是多么费劲,而Grafana社区提供非常丰富的仪表盘模板,引入十分轻松。至于告警可以借助Alertmanager来实现。在此分享一下Alertmanager邮箱和企业微信的告警模板。

效果展现

■ 邮箱

① 告警
在这里插入图片描述
② 消警
在这里插入图片描述

■ 企业微信

在这里插入图片描述

模板

■ 邮箱 —— email.tmpl

{
    
    {
    
     define "email.html" }}
{
    
    {
    
    - if gt (len .Alerts.Firing) 0 -}}
<h2>告警</h2>
<table border="5">
    <tr><td>报警项</td>
        <td>实例</td>
        <td>报警详情</td>
        <td>报警级别</td>
        <td>开始时间</td>
    </tr>
    {
    
    {
    
     range $i, $alert := .Alerts }}
        <tr><td>{
    
    {
    
     index $alert.Labels "alertname" }}</td>
            <td style="color:#32CD32" >{
    
    {
    
     index $alert.Labels "instance" }}</td>
            <td>{
    
    {
    
     index $alert.Annotations "description" }}</td>
            <td>{
    
    {
    
     $alert.Labels.severity }}</td>
            <td style="color:#FF7F50">{
    
    {
    
     $alert.StartsAt.Local.Format "2006-01-02 15:04:05" }}</td>
        </tr>
    {
    
    {
    
     end }}
</table>
{
    
    {
    
     end }}
{
    
    {
    
    - if gt (len .Alerts.Resolved) 0 -}}
<h2>已经恢复</h2>
<table border="5">
    <tr><td>报警项</td>
        <td>实例</td>
        <td>报警详情</td>
        <td>报警级别</td>
        <td>开始时间</td>
		<td>恢复时间</td>
    </tr>
    {
    
    {
    
     range $i, $alert := .Alerts }}
        <tr><td>{
    
    {
    
     index $alert.Labels "alertname" }}</td>
            <td style="color:#32CD32">{
    
    {
    
     index $alert.Labels "instance" }}</td>
            <td>{
    
    {
    
     index $alert.Annotations "description" }}</td>
            <td>{
    
    {
    
     $alert.Labels.severity }}</td>
            <td style="color:#FF7F50">{
    
    {
    
     $alert.StartsAt.Local.Format "2006-01-02 15:04:05" }}</td>
            <td style="color:#FF7F50">{
    
    {
    
     $alert.EndsAt.Local.Format "2006-01-02 15:04:05" }}</td>
        </tr>
    {
    
    {
    
     end }}
</table>
{
    
    {
    
     end }}{
    
    {
    
    - end }}

■ 企业微信 —— wechat.tmpl

{
    
    {
    
     define "wechat.message" }}
{
    
    {
    
    - if gt (len .Alerts.Firing) 0 -}}
{
    
    {
    
    - range $index, $alert := .Alerts -}}
{
    
    {
    
    - if eq $index 0 -}}
# 报警项: {
    
    { $alert.Labels.alertname }}
{
    
    {
    
    - end }}
> `**===告警详情===**` 
> 告警级别: {
    
    {
    
     $alert.Labels.severity }}
> 告警详情: <font color="comment">{
    
    {
    
     index $alert.Annotations "description" }}{
    
    {
    
     $alert.Annotations.message }}</font>
> 故障时间: <font color="warning">{
    
    {
    
     $alert.StartsAt.Local.Format "2006-01-02 15:04:05" }}</font>
> 故障实例: <font color="info">{
    
    {
    
     $alert.Labels.instance }}</font>
{
    
    {
    
    - end }}
{
    
    {
    
    - end }}
{
    
    {
    
    - if gt (len .Alerts.Resolved) 0 -}}
{
    
    {
    
    - range $index, $alert := .Alerts -}}
{
    
    {
    
    - if eq $index 0 -}}
# 恢复项: {
    
    { $alert.Labels.alertname }}
{
    
    {
    
    - end }}
> `**===恢复详情===**` 
> 告警级别: {
    
    {
    
     $alert.Labels.severity }}
> 告警详情: <font color="comment">{
    
    {
    
     index $alert.Annotations "description" }}{
    
    {
    
     $alert.Annotations.message }}</font>
> 故障时间: <font color="warning">{
    
    {
    
     $alert.StartsAt.Local.Format "2006-01-02 15:04:05" }}</font>
> 恢复时间: <font color="warning">{
    
    {
    
     $alert.EndsAt.Local.Format "2006-01-02 15:04:05" }}</font>
> 故障实例: <font color="info">{
    
    {
    
     $alert.Labels.instance }}</font>
{
    
    {
    
    - end }}
{
    
    {
    
    - end }}
{
    
    {
    
    - end }}

告警规则例子

groups:
- name: node_export_up
  rules:
  - alert: 存在监控节点下线
    expr: up < 1
    for: 10s    # 当一个监控项超过了阀值时,这个告警处于pengding状态,而pending状态维持for秒以后,就会切换为fire状态,也就是将告警信息发送给了alertmanager
    labels:
      severity: 可空闲处理
      action: text
      kind: node_export
    annotations:
      summary: "{
    
    { $labels.instance }} 已停止运行!"
      last: "{
    
    { $value }}"
      description: "{
    
    { $labels.instance }} 检测到异常停止!请及时处理!!!"
- name: redis_up
  rules:
  - alert: redis节点挂了
    expr: redis_up < 1
    for: 10s    # 当一个监控项超过了阀值时,这个告警处于pengding状态,而pending状态维持for秒以后,就会切换为fire状态,也就是将告警信息发送给了alertmanager
    labels:
      severity: 应及时处理
      action: text
      kind: node_export
    annotations:
      summary: "{
    
    { $labels.instance }} 已停止运行!"
      last: "{
    
    { $value }}"
      description: "{
    
    { $labels.instance }} 检测到异常停止!请及时处理!!!"
- name: mysql_up
  rules:
  - alert: mysql挂了
    expr: mysql_up < 1
    for: 10s    # 当一个监控项超过了阀值时,这个告警处于pengding状态,而pending状态维持for秒以后,就会切换为fire状态,也就是将告警信息发送给了alertmanager
    labels:
      severity: 紧急处理
      action: text
      kind: node_export
    annotations:
      summary: "{
    
    { $labels.instance }} 已停止运行!"
      last: "{
    
    { $value }}"
      description: "{
    
    { $labels.instance }} 检测到异常停止!请及时处理!!!"
  - alert: mysql慢sql太多
    expr: rate(mysql_global_status_slow_queries[1m]) > 0.15
    for: 10s    # 当一个监控项超过了阀值时,这个告警处于pengding状态,而pending状态维持for秒以后,就会切换为fire状态,也就是将告警信息发送给了alertmanager
    labels:
      severity: 紧急处理
      action: text
      kind: node_export
    annotations:
      summary: "{
    
    { $labels.instance }} 慢sql太多!"
      last: "{
    
    { $value }}"
      description: "{
    
    { $labels.instance }} 检测到异常!请及时处理!!!"

最后

如此即可实现可观的告警。

猜你喜欢

转载自blog.csdn.net/qq_24054301/article/details/118864520