System Reboot Engineer System Reboot Engineer
首页
运维
编程

小布江

首页
运维
编程
  • Kubernetes

  • 日常

  • Prometheus

    • Alertmanager报警历史持久化
    • blackbox-exporter监测站点
    • 监控kafka小tips
    • 常用Exporter
    • 远程存储之VictoriaMetrics
    • Nginx-vts模块
    • alertmanager
      • VMagent
      • VMalert
    • Ci

    • 运维
    • Prometheus
    小布江
    2024-10-14
    目录

    alertmanager


    Alertmanager 主要用于接收 Prometheus 发送的告警信息,它支持丰富的告警通知渠道,而且很容易做到告警信息进行去重,降噪,分组等,是一款前卫的告警通知系统。通过在 Prometheus 中定义告警规则,Prometheus 会周期性的对告警规则进行计算,如果满足告警触发条件就会向 Alertmanager 发送告警信息。


    alertmanager workflow


    # 1 部署alertmanager

    # 1.1 指定配置文件
    [root@cce alertmanager]# vim alertmanager-config.yml
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: alert-config
      namespace: kube-mon
    data:
      config.yml: |-
        global:
          resolve_timeout: 5m
          smtp_smarthost: 'smtp.exmail.qq.com:465' # 腾讯企业邮箱的地址端口
          smtp_from: '[email protected]' # 发件人的邮箱
          smtp_auth_username: '[email protected]' # 邮箱地址
          smtp_auth_password: 'J3Kxxx' # smtp授权码,对应腾讯企业邮箱的生成的客户端密码
          smtp_require_tls: false
        # 所有报警信息进入后的根路由,用来设置报警的分发策略   
        route:
          group_by: ['alertname','instance','pod','namespace']
        # 当一个新的报警分组被创建后,需要等待至少 group_wait 时间来初始化通知,这种方式可以确保您能有足够的时间为同一分组来获取多个警报,然后一起触发这个报警信息。
          group_wait: 30s
          group_interval: 120s   # 相同的group之间发送告警通知的时间间隔   
          repeat_interval: 15m   # 如果一个报警信息已经发送成功了,等待 repeat_interval 时间来重新发送他们,不同类型告警发送频率需要具体配置
          receiver: 'email' # 默认的receiver:如果一个报警没有被一个route匹配,则发送给默认的接收器
          routes:
          - match:
              cluster: 'mt-prod' # lables带有 cluster=mt-prod的告警发送到mt的receiver
            receiver: 'mt'
        receivers:
        - name: 'mt'
          webhook_configs:
          - url: 'http://prometheusalert-kube-mon.svc.cluster.local:8080/prometheusalert?type=wx&tpl=prometheus-wx&wxurl=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx' # 发送企业微信,webhook配置下关键字
            send_resolved: true  # 接受告警恢复的通知
        - name: 'email'
          email_configs:
          - to: '[email protected]'
            send_resolved: true
        inhibit_rules: # 告警抑制
          - source_match:
              alertname: NodeDiskNotEnough
              severity: "critical"
            target_match:
              severity: "warning"
            equal:
              - instance        
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45

    分组机制可以将详细的告警信息合并成一个通知,在某些情况下,比如由于系统宕机导致大量的告警被同时触发,在这种情况下分组机制可以将这些被触发的告警合并为一个告警通知,避免一次性接受大量的告警通知,而无法对问题进行快速定位。


    # 1.2 配置 AlertManager 的容器
    [root@cce alertmanager]# vim alertmanager-deploy.yml
    ---
    kind: Deployment
    metadata:
      name: alertmanager
      namespace: kube-mon
      labels:
        app: alertmanager
    spec:
      selector:
        matchLabels:
          app: alertmanager
      template:
        metadata:
          labels:
            app: alertmanager
          annotations:
            prometheus.io/scrape: "true"
        spec:
          volumes:
            - name: alertcfg
              configMap:
                name: alert-config
            - name: alertmanager-storage
              persistentVolumeClaim:
                claimName: alertmanager
          containers:
            - name: alertmanager
              image: prom/alertmanager:v0.23.0
              imagePullPolicy: IfNotPresent
              args:
                - "--config.file=/etc/alertmanager/config.yml"
                - "--storage.path=/alertmanager"
              ports:
                - containerPort: 9093
                  name: http
              volumeMounts:
                - mountPath: "/etc/alertmanager"
                  name: alertcfg
                - mountPath: "/alertmanager" # 持久化静默的报警
                  name: alertmanager-storage  
              resources:
                requests:
                  cpu: 100m
                  memory: 256Mi
                limits:
                  cpu: 100m
                  memory: 256Mi
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: alertmanager
      namespace: kube-mon
      labels:
        app: alertmanager
    spec:
      selector:
        app: alertmanager
      ports:
        - name: web
          port: 9093
          targetPort: http
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63

    这里将上面创建的 alert-config 这个 ConfigMap 资源对象以 Volume 的形式挂载到 /etc/alertmanager 目录下去,然后在启动参数中指定了配置文件 --config.file=/etc/alertmanager/config.yml


    # 2 报警接收器

    # 2.1 腾讯邮箱配置

    image-20241014102823946


    image-20241014103033068

    设置>帐户>帐户安全>开启安全登录>客户端专用密码>保存


    # 2.3 部署 PrometheusAlert (opens new window),可以看官网文档详细输出
    ---
    apiVersion: v1
    data:
      app.conf: |
        #---------------------↓全局配置-----------------------
        appname = PrometheusAlert
        #登录用户名
        login_user=prometheusalert
        #登录密码
        login_password=prometheusalert
        #监听地址
        httpaddr = "0.0.0.0"
        #监听端口
        httpport = 8080
        runmode = dev
        #设置代理 proxy = http://123.123.123.123:8080
        proxy =
        #开启JSON请求
        copyrequestbody = true
        #告警消息标题
        title=PrometheusAlert
        #链接到告警平台地址
        GraylogAlerturl=http://graylog.org
        #钉钉告警 告警logo图标地址
        logourl=https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png
        #钉钉告警 恢复logo图标地址
        rlogourl=https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png
        #短信告警级别(等于3就进行短信告警) 告警级别定义 0 信息,1 警告,2 一般严重,3 严重,4 灾难
        messagelevel=3
        #电话告警级别(等于4就进行语音告警) 告警级别定义 0 信息,1 警告,2 一般严重,3 严重,4 灾难
        phonecalllevel=4
        #默认拨打号码(页面测试短信和电话功能需要配置此项)
        defaultphone=xxxxxxxx
        #故障恢复是否启用电话通知0为关闭,1为开启
        phonecallresolved=0
        #是否前台输出file or console
        logtype=file
        #日志文件路径
        logpath=logs/prometheusalertcenter.log
        #转换Prometheus,graylog告警消息的时区为CST时区(如默认已经是CST时区,请勿开启)
        prometheus_cst_time=0
        #数据库驱动,支持sqlite3,mysql,postgres如使用mysql或postgres,请开启db_host,db_port,db_user,db_password,db_name的注释
        db_driver=sqlite3
        #db_host=127.0.0.1
        #db_port=3306
        #db_user=root
        #db_password=root
        #db_name=prometheusalert
        #是否开启告警记录 0为关闭,1为开启
        AlertRecord=0
        #是否开启告警记录定时删除 0为关闭,1为开启
        RecordLive=0
        #告警记录定时删除周期,单位天
        RecordLiveDay=7
        # 是否将告警记录写入es7,0为关闭,1为开启
        alert_to_es=0
        # es地址,是[]string
        # beego.Appconfig.Strings读取配置为[]string,使用";"而不是","
        to_es_url=http://localhost:9200
        # to_es_url=http://es1:9200;http://es2:9200;http://es3:9200
        # es用户和密码
        # to_es_user=username
        # to_es_pwd=password
    
        #---------------------↓webhook-----------------------
        #是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启
        open-dingding=1
        #默认钉钉机器人地址
        #ddurl=https://oapi.dingtalk.com/robot/send?access_token=2297e449741e83159004b3e5fd624053c40757b21d13b48e2c6aa20aa4b83403
        ddurl=https://oapi.dingtalk.com/robot/send?access_token=9f49a2cde8eb200764da30e402b4b145db6c792c565d5cedc23f4db17ba5d5cd
        #是否开启 @所有人(0为关闭,1为开启)
        dd_isatall=0
    
        #是否开启微信告警通道,可同时开始多个通道0为关闭,1为开启
        open-weixin=1
        #默认企业微信机器人地址
        wxurl=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx
    
        #是否开启飞书告警通道,可同时开始多个通道0为关闭,1为开启
        open-feishu=0
        #默认飞书机器人地址
        fsurl=https://open.feishu.cn/open-apis/bot/hook/xxxxxxxxx
    
        #---------------------↓腾讯云接口-----------------------
        #是否开启腾讯云短信告警通道,可同时开始多个通道0为关闭,1为开启
        open-txdx=0
        #腾讯云短信接口key
        TXY_DX_appkey=xxxxx
        #腾讯云短信模版ID 腾讯云短信模版配置可参考 prometheus告警:{1}
        TXY_DX_tpl_id=xxxxx
        #腾讯云短信sdk app id
        TXY_DX_sdkappid=xxxxx
        #腾讯云短信签名 根据自己审核通过的签名来填写
        TXY_DX_sign=腾讯云
    
        #是否开启腾讯云电话告警通道,可同时开始多个通道0为关闭,1为开启
        open-txdh=0
        #腾讯云电话接口key
        TXY_DH_phonecallappkey=xxxxx
        #腾讯云电话模版ID
        TXY_DH_phonecalltpl_id=xxxxx
        #腾讯云电话sdk app id
        TXY_DH_phonecallsdkappid=xxxxx
    
        #---------------------↓华为云接口-----------------------
        #是否开启华为云短信告警通道,可同时开始多个通道0为关闭,1为开启
        open-hwdx=0
        #华为云短信接口key
        HWY_DX_APP_Key=xxxxxxxxxxxxxxxxxxxxxx
        #华为云短信接口Secret
        HWY_DX_APP_Secret=xxxxxxxxxxxxxxxxxxxxxx
        #华为云APP接入地址(端口接口地址)
        HWY_DX_APP_Url=https://rtcsms.cn-north-1.myhuaweicloud.com:10743
        #华为云短信模板ID
        HWY_DX_Templateid=xxxxxxxxxxxxxxxxxxxxxx
        #华为云签名名称,必须是已审核通过的,与模板类型一致的签名名称,按照自己的实际签名填写
        HWY_DX_Signature=华为云
        #华为云签名通道号
        HWY_DX_Sender=xxxxxxxxxx
    
        #---------------------↓阿里云接口-----------------------
        #是否开启阿里云短信告警通道,可同时开始多个通道0为关闭,1为开启
        open-alydx=0
        #阿里云短信主账号AccessKey的ID
        ALY_DX_AccessKeyId=xxxxxxxxxxxxxxxxxxxxxx
        #阿里云短信接口密钥
        ALY_DX_AccessSecret=xxxxxxxxxxxxxxxxxxxxxx
        #阿里云短信签名名称
        ALY_DX_SignName=阿里云
        #阿里云短信模板ID
        ALY_DX_Template=xxxxxxxxxxxxxxxxxxxxxx
    
        #是否开启阿里云电话告警通道,可同时开始多个通道0为关闭,1为开启
        open-alydh=0
        #阿里云电话主账号AccessKey的ID
        ALY_DH_AccessKeyId=xxxxxxxxxxxxxxxxxxxxxx
        #阿里云电话接口密钥
        ALY_DH_AccessSecret=xxxxxxxxxxxxxxxxxxxxxx
        #阿里云电话被叫显号,必须是已购买的号码
        ALY_DX_CalledShowNumber=xxxxxxxxx
        #阿里云电话文本转语音(TTS)模板ID
        ALY_DH_TtsCode=xxxxxxxx
    
        #---------------------↓容联云接口-----------------------
        #是否开启容联云电话告警通道,可同时开始多个通道0为关闭,1为开启
        open-rlydh=0
        #容联云基础接口地址
        RLY_URL=https://app.cloopen.com:8883/2013-12-26/Accounts/
        #容联云后台SID
        RLY_ACCOUNT_SID=xxxxxxxxxxx
        #容联云api-token
        RLY_ACCOUNT_TOKEN=xxxxxxxxxx
        #容联云app_id
        RLY_APP_ID=xxxxxxxxxxxxx
    
        #---------------------↓邮件配置-----------------------
        #是否开启邮件
        open-email=0
        #邮件发件服务器地址
        Email_host=smtp.qq.com
        #邮件发件服务器端口
        Email_port=465
        #邮件帐号
        Email_user=[email protected]
        #邮件密码
        Email_password=xxxxxx
        #邮件标题
        Email_title=运维告警
        #默认发送邮箱
        Default_emails=[email protected],[email protected]
    
        #---------------------↓七陌云接口-----------------------
        #是否开启七陌短信告警通道,可同时开始多个通道0为关闭,1为开启
        open-7moordx=0
        #七陌账户ID
        7MOOR_ACCOUNT_ID=Nxxx
        #七陌账户APISecret
        7MOOR_ACCOUNT_APISECRET=xxx
        #七陌账户短信模板编号
        7MOOR_DX_TEMPLATENUM=n
        #注意:七陌短信变量这里只用一个var1,在代码里写死了。
        #-----------
        #是否开启七陌webcall语音通知告警通道,可同时开始多个通道0为关闭,1为开启
        open-7moordh=0
        #请在七陌平台添加虚拟服务号、文本节点
        #七陌账户webcall的虚拟服务号
        7MOOR_WEBCALL_SERVICENO=xxx
        # 文本节点里被替换的变量,我配置的是text。如果被替换的变量不是text,请修改此配置
        7MOOR_WEBCALL_VOICE_VAR=text
    
        #---------------------↓telegram接口-----------------------
        #是否开启telegram告警通道,可同时开始多个通道0为关闭,1为开启
        open-tg=0
        #tg机器人token
        TG_TOKEN=xxxxx
        #tg消息模式 个人消息或者频道消息 0为关闭(推送给个人),1为开启(推送给频道)
        TG_MODE_CHAN=0
        #tg用户ID
        TG_USERID=xxxxx
        #tg频道name或者id, 频道name需要以@开始
        TG_CHANNAME=xxxxx
        #tg api地址, 可以配置为代理地址
        #TG_API_PROXY="https://api.telegram.org/bot%s/%s"
    
        #---------------------↓workwechat接口-----------------------
        #是否开启workwechat告警通道,可同时开始多个通道0为关闭,1为开启
        open-workwechat=0
        # 企业ID
        WorkWechat_CropID=xxxxx
        # 应用ID
        WorkWechat_AgentID=xxxx
        # 应用secret
        WorkWechat_AgentSecret=xxxx
        # 接受用户
        WorkWechat_ToUser="zhangsan|lisi"
        # 接受部门
        WorkWechat_ToParty="ops|dev"
        # 接受标签
        WorkWechat_ToTag=""
        # 消息类型, 暂时只支持markdown
        # WorkWechat_Msgtype = "markdown"
    
        #---------------------↓百度云接口-----------------------
        #是否开启百度云短信告警通道,可同时开始多个通道0为关闭,1为开启
        open-baidudx=0
        #百度云短信接口AK(ACCESS_KEY_ID)
        BDY_DX_AK=xxxxx
        #百度云短信接口SK(SECRET_ACCESS_KEY)
        BDY_DX_SK=xxxxx
        #百度云短信ENDPOINT(ENDPOINT参数需要用指定区域的域名来进行定义,如服务所在区域为北京,则为)
        BDY_DX_ENDPOINT=http://smsv3.bj.baidubce.com
        #百度云短信模版ID,根据自己审核通过的模版来填写(模版支持一个参数code:如prometheus告警:{code})
        BDY_DX_TEMPLATE_ID=xxxxx
        #百度云短信签名ID,根据自己审核通过的签名来填写
        TXY_DX_SIGNATURE_ID=xxxxx
    
        #---------------------↓百度Hi(如流)-----------------------
        #是否开启百度Hi(如流)告警通道,可同时开始多个通道0为关闭,1为开启
        open-ruliu=0
        #默认百度Hi(如流)机器人地址
        BDRL_URL=https://api.im.baidu.com/api/msg/groupmsgsend?access_token=xxxxxxxxxxxxxx
        #百度Hi(如流)群ID
        BDRL_ID=123456
        #---------------------↓bark接口-----------------------
        #是否开启telegram告警通道,可同时开始多个通道0为关闭,1为开启
        open-bark=0
        #bark默认地址, 建议自行部署bark-server
        BARK_URL=https://api.day.app
        #bark key, 多个key使用分割
        BARK_KEYS=xxxxx
        # 复制, 推荐开启
        BARK_COPY=1
        # 历史记录保存,推荐开启
        BARK_ARCHIVE=1
        # 消息分组
        BARK_GROUP=PrometheusAlert
    
        #---------------------↓语音播报-----------------------
        #语音播报需要配合语音播报插件才能使用
        #是否开启语音播报通道,0为关闭,1为开启
        open-voice=0
        VOICE_IP=127.0.0.1
        VOICE_PORT=9999
    
        #---------------------↓飞书机器人应用-----------------------
        #是否开启feishuapp告警通道,可同时开始多个通道0为关闭,1为开启
        open-feishuapp=1
        # APPID
        FEISHU_APPID=cli_xxxxxxxxxxxxx
        # APPSECRET
        FEISHU_APPSECRET=xxxxxxxxxxxxxxxxxxxxxx
        # 可填飞书 用户open_id、user_id、union_ids、部门open_department_id
        AT_USER_ID="xxxxxxxx"
      user.csv: |
        2019年4月10日,15888888881,小张,15999999999,备用联系人小陈,15999999998,备用联系人小赵
        2019年4月11日,15888888882,小李,15999999999,备用联系人小陈,15999999998,备用联系人小赵
        2019年4月12日,15888888883,小王,15999999999,备用联系人小陈,15999999998,备用联系人小赵
        2019年4月13日,15888888884,小宋,15999999999,备用联系人小陈,15999999998,备用联系人小赵
    kind: ConfigMap
    metadata:
      name: prometheus-alert-conf
      namespace: kube-mon
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: prometheus-alert
        alertname: prometheus-alert
      name: prometheus-alert
      namespace: kube-mon
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus-alert
          alertname: prometheus-alert
      template:
        metadata:
          labels:
            app: prometheus-alert
            alertname: prometheus-alert
        spec:
          containers:
          - image: feiyu563/prometheus-alert:latest
            name: prometheus-alert
            env:
            - name: TZ
              value: "Asia/Shanghai"
            ports:
            - containerPort: 8080
              name: http
            resources:
              limits:
                cpu: 200m
                memory: 200Mi
              requests:
                cpu: 100m
                memory: 100Mi
            volumeMounts:
            - name: prometheus-alert-conf-map
              mountPath: /app/conf/app.conf
              subPath: app.conf
            - name: prometheus-alert-conf-map
              mountPath: /app/user.csv
              subPath: user.csv
            - name: prometheus-alert-db
              mountPath: /app/db
          volumes:
          - name: prometheus-alert-conf-map
            configMap:
              name: prometheus-alert-conf
              items:
              - key: app.conf
                path: app.conf
              - key: user.csv
                path: user.csv
          - name: prometheus-alert-db
            persistentVolumeClaim:
              claimName: prometheusalert-pvc
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        alertname: prometheus-alert
      name: prometheus-alert
      namespace: kube-mon
      annotations:
        prometheus.io/scrape: 'true'
        prometheus.io/port: '8080'
    spec:
      ports:
      - name: http
        port: 8080
        targetPort: http
      selector:
        app: prometheus-alert
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    # 2.4 企微模版-PrometheusAlert
    {{ $externalURL := .externalURL -}}
    {{ $alerts := .alerts -}}
    {{ range $alert := $alerts -}}
      {{ $groupKey := printf "%s|%s" $alert.labels.alertname $alert.status -}}
      {{ if eq $alert.status "resolved" -}}
    <font color="green">Prometheus 恢复通知</font>
    告警名称:{{ $alert.labels.alertname }} 
    告警级别:{{ $alert.labels.severity }} 
    告警状态:{{ $alert.status }}
    开始时间:{{ GetCSTtime $alert.startsAt }} 
    结束时间:{{ GetCSTtime $alert.endsAt }} 
    {{ if $alert.labels.namespace -}}
    命名空间:{{ $alert.labels.namespace }} 
    {{ end -}}
    告警详情:**<font color="green">{{ $alert.annotations.description }}</font>**
      {{ else -}}
    <font color="#FF0000">Prometheus 告警通知</font>
    告警名称:{{ $alert.labels.alertname }} 
    告警级别:{{ $alert.labels.severity }} 
    告警状态:{{ $alert.status }}
    开始时间:{{ GetCSTtime $alert.startsAt }} 
    {{ if $alert.labels.namespace -}}
    命名空间:{{ $alert.labels.namespace }} 
    {{ end -}}
    告警详情:**<font color="red">{{ $alert.annotations.description }}</font>**
      {{ end -}}
    {{ end -}}
    -----------------------------------------------------------------------
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # 2.5 调试PrometheusAlert
    {
    	"receiver": "prometheus-alert-center",
    	"status": "firing",
    	"alerts": [{
    		"status": "firing",
    		"labels": {
    			"alertname": "TargetDown",
    			"index": "1",
    			"instance": "example-1",
    			"job": "example",
    			"level": "2",
    			"service": "example"
    		},
    		"annotations": {
    			"description": "target was down! example dev /example-1 was down for more than 120s.",
    			"level": "2",
    			"timestamp": "2020-05-21 02:58:07.829 +0000 UTC"
    		},
    		"startsAt": "2020-05-21T02:58:07.830216179Z",
    		"endsAt": "0001-01-01T00:00:00Z",
    		"generatorURL": "https://prometheus-alert-center/graph?g0.expr=up%7Bjob%21%3D%22kubernetes-pods%22%2Cjob%21%3D%22kubernetes-service-endpoints%22%7D+%21%3D+1\u0026g0.tab=1",
    		"fingerprint": "e2a5025853d4da64"
    	}],
    	"groupLabels": {
    		"instance": "example-1"
    	},
    	"commonLabels": {
    		"alertname": "TargetDown",
    		"index": "1",
    		"instance": "example-1",
    		"job": "example",
    		"level": "2",
    		"service": "example"
    	},
    	"commonAnnotations": {
    		"description": "target was down! example dev /example-1 was down for more than 120s.",
    		"level": "2",
    		"timestamp": "2020-05-21 02:58:07.829 +0000 UTC"
    	},
    	"externalURL": "https://prometheus-alert-center",
    	"version": "4",
    	"groupKey": "{}/{job=~\"^(?:.*)$\"}:{instance=\"example-1\"}"
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43

    image-20241014105445781


    image-20241014105523333


    # 2.6 飞书模版-PrometheusAlert
    {{ $externalURL := .externalURL -}}
    {{ $alerts := .alerts -}}
    {{ range $alert := $alerts -}}
      {{ $groupKey := printf "%s|%s" $alert.labels.alertname $alert.status -}}
      {{ if eq $alert.status "resolved" -}}
    </font>**<font color="green">Prometheus 告警恢复</font>  **
    告警名称:{{ $alert.labels.alertname }} 
    告警级别:{{ $alert.labels.severity }} 
    告警状态:{{ $alert.status }}
    开始时间:{{ GetCSTtime $alert.startsAt }} 
    结束时间:{{ GetCSTtime $alert.endsAt }} 
    {{ if $alert.labels.namespace -}}
    命名空间:{{ $alert.labels.namespace }} 
    {{ end -}}
    告警详情:**<font color="green">{{ $alert.annotations.description }}**
      {{ else -}}
    </font>**<font color="red">Prometheus 告警通知</font>  **
    告警名称:{{ $alert.labels.alertname }} 
    告警级别:{{ $alert.labels.severity }} 
    告警状态:{{ $alert.status }}
    开始时间:{{ GetCSTtime $alert.startsAt }} 
    {{ if $alert.labels.namespace -}}
    命名空间:{{ $alert.labels.namespace }} 
    {{ end -}}
    告警详情:**<font color="red">{{ $alert.annotations.description }}</font>**
      {{ end -}}
    {{ end -}}
    ------------------------------------------------------------------------------------------------------
    注: 之前遇到过飞书告警恢复了但是返回的卡片不是绿色,发现调试了下加上  告警状态:{{ $alert.status }}  就可以正常返回卡片颜色
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    # 2.7 Dingding自定义模版
    [root@prod-manage alertmanager]# cat webhook-dingtalk.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: webhook-dingtalk
      name: webhook-dingtalk
      namespace: kube-mon
      #需要和alertmanager在同一个namespace
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: webhook-dingtalk
      template:
        metadata:
          labels:
            app: webhook-dingtalk
        spec:
          containers:
          - image: registry.cn-hangzhou.aliyuncs.com/s-ops/webhook-dingtalk:v1
            imagePullPolicy: IfNotPresent
            name: webhook-dingtalk
            env:
            - name: TZ
              value: Asia/Shanghai
            args:
            - "https://oapi.dingtalk.com/robot/send?access_token=92ba58d77209ed9f4125bd2a80a0b3d2878eb6a2969a63839xxx"
            #上面创建的钉钉机器人hook
            ports:
            - containerPort: 8080
              protocol: TCP
            resources:
              requests:
                cpu: 100m
                memory: 100Mi
              limits:
                cpu: 500m
                memory: 500Mi
            livenessProbe:
              failureThreshold: 3
              initialDelaySeconds: 30
              periodSeconds: 10
              successThreshold: 1
              timeoutSeconds: 1
              tcpSocket:
                port: 8080
            readinessProbe:
              failureThreshold: 3
              initialDelaySeconds: 30
              periodSeconds: 10
              successThreshold: 1
              timeoutSeconds: 1
              httpGet:
                port: 8080
                path: /
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: webhook-dingtalk
      name: webhook-dingtalk
      namespace: kube-mon
    spec:
      ports:
      - name: http
        port: 80
        protocol: TCP
        targetPort: 8080
      selector:
        app: webhook-dingtalk
      type: ClusterIP
    -----------------------------------------------------------------------------
    alertmanager配置:
        receivers:
        - name: 'webhook'
          webhook_configs:
          - url: 'http://webhook-dingtalk/dingtalk/send/'
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    #alertmanager
    上次更新: 2025/04/25, 03:40:17
    Nginx-vts模块
    VMagent

    ← Nginx-vts模块 VMagent→

    最近更新
    01
    Harbor复制镜像
    04-15
    02
    CPU亲和
    04-10
    03
    开启telnet登录
    04-09
    更多文章>
    Theme by Vdoing
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式