admin管理员组

文章数量:1582707

qq告警

一. 获取安全码

首先如果我们想要使用qq邮箱接收报警信息,需要先去qq邮箱上获取你自己的一个安全码,如下图

 

开启后你会获取到一个安全码,用于其他程序去发送邮件到你的邮箱,下面开始配置告警

二. 配置alertmanager发送邮件

注意修改为你的配置,不然过两天我邮箱炸了┭┮﹏┭┮  (3台主机都要写入哦)

#指定将收到的告警发送给谁
cat > /etc/alertmanager/alertmanager.yml <<EOF
global:
  resolve_timeout: 5m
  smtp_from: '2665483426@qq'            #发送人的邮箱,自定义
  smtp_smarthost: 'smtp.qq:465'               
  smtp_auth_username: '2665483426@qq'  #你自己的邮箱
  smtp_auth_password: 'vxehxwuwnwlreaih1'   #这个是上面获取安全吗的值,不是你邮箱的密码
  smtp_require_tls: false
  smtp_hello: 'qq'

route:
  group_by: ['alertname']
  group_wait: 5s
  group_interval: 5s
  repeat_interval: 5m
  receiver: 'email'

receivers:
- name: 'email'
  email_configs:
  - to: '2665483426@qq'            #收件人
    send_resolved: true
EOF

说明

#全局配置
global:
  resolve_timeout: 5m     #当告警的状态由"firing"变为"resolve"的以后还要呆多长时间,才宣布告警解除
  smtp_from: '2665483426@qq'          #发件人邮箱
  smtp_smarthost: 'smtp.qq:465'       #qq邮箱smtp端口端口
  smtp_auth_username: '2665483426@qq'   #邮箱地址
  smtp_auth_password: 'vxehxwuwnwlreaih1'    #邮箱安全码
  smtp_require_tls: false                   #不携带证书请求
  smtp_hello: 'qq'

#路由配置
route:            
  group_by: ['alertname']    #告警应该根据那些标签进行分组
                             #这里匹配的是key,当相同标签的告警一起来的时候分为同一组
  group_wait: 5s             #一组的告警发出前要等待多少秒,这个是为了把更多的告警一个批次发出去
  group_interval: 5s         #同一组的多批次告警间隔多少秒后,才能发出
  repeat_interval: 5m        #重复的告警要等待多久后才能再次发出去
  receiver: 'email'           #指定路由到email的路由

#发送配置
receivers:                         #定义谁接收告警,(接收路由的告警)
- name: 'email'                    #名称对应route中的receiver参数
  email_configs:                   #采用邮箱机制
  - to: '2665483426@qq'        #发送到那里的邮箱
    send_resolved: true            #是否发送状态恢复的邮件
EOF

重启服务

#因为是集群模式,这里要给3台添加配置并重启
systemctl restart alertmanager

三. 测试--触发告警

上面我们定义的规则是判断up指标中job=node的值是否为0

 

#job=node所对应的服务的服务是node-exporter
#我们这里停止一台node-exporter服务来测试
systemctl stop node-exporter

触发报警该规则会变更为Pending状态 ,等待15s如果没有恢复则会进入Firing状态

当进入Firing状态后,会将告警信息发送到alertmanager中

当你看到这条时,alertmanager已经拿到了告警数据,然后通过route路由到告警配置中,给配置中的邮箱进行报警

(这里我登陆我们接收报警的邮箱)

 

163邮箱

开通协议

https://help.mail.163/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2a5feb28b66796d3b

开启后你会获取到一个安全码,用于其他程序去发送邮件到你的邮箱,下面开始配置告警

配置alertmanager发送邮件
#指定将收到的告警发送给谁
cat > /etc/alertmanager/alertmanager.yml <<EOF
global:
  resolve_timeout: 5m
  smtp_from: '111@163'            #发送人的邮箱,自定义
  smtp_smarthost: 'smtp.163:465'            #163邮箱服务器地址:port   
  smtp_auth_username: '111@163'  #你自己的邮箱
  smtp_auth_password: 'VAXHCJANINDVWNCR'   #这个是上面获取安全吗的值,不是你邮箱的密码
  smtp_require_tls: false
  smtp_hello: '163'
 
route:
  group_by: ['alertname']
  group_wait: 5s
  group_interval: 5s
  repeat_interval: 5m
  receiver: 'email'
 
receivers:
- name: 'email'
  email_configs:
  - to: '111@163'            #收件人
    send_resolved: true
EOF

说明

#全局配置
global:
  resolve_timeout: 5m       #当告警的状态由"firing"变为"resolve"的以后还要呆多长时间,才宣布告警解除
  smtp_from: 'gaoyanlinux@163'            #发送人的邮箱,写自己的邮箱。
  smtp_smarthost: 'smtp.163:25'            #163邮箱服务器地址:port   
  smtp_auth_username: 'gaoyanlinux@163'  #你自己的邮箱
  smtp_auth_password: 'VAXHCJANINDVWNCR'   #这个是上面获取安全吗的值,不是你邮箱的密码
  smtp_require_tls: false                    #不携带证书请求
  smtp_hello: '163'                        
 
route:
  group_by: ['alertname']          #告警应该根据那些标签进行分组, #这里匹配的是key,当相同标签的告警一起来的时候分为同一组
  group_wait: 5s            #一组的告警发出前要等待多少秒,这个是为了把更多的告警一个批次发出去
  group_interval: 5s          #同一组的多批次告警间隔多少秒后,才能发出
  repeat_interval: 5m        #重复的告警要等待多久后才能再次发出去
  receiver: 'email'           #指定路由到email的路由,接收者,与下面的receivers保持一致即可
 
receivers:                        #定义谁接收告警,(接收路由的告警)
- name: 'email'                      #名称对应route中的receiver参数
  email_configs:                     #采用邮箱机制
  - to: 'gaoyanlinux@163'            #收件人邮箱
    send_resolved: true
  - to: 'gy19920113@qq'            #收件人
    send_resolved: true                 #是否发送状态恢复的邮件
systemctl restart alertmanager

企业微信告警

需要搭建webhook服务来接受Alertmanager发送的告警信息

使用 webhook 服务通常需要你自己搭建一个服务来接收 Alertmanager 发送的告警信息,并根据接收到的信息执行相应的动作,比如发送微信消息

 

1. 搭建 Webhook 服务

pip install Flask
编写代码

接下来,编写一个简单的 Flask 应用来接收 Alertmanager 发送的告警信息,并通过微信企业号发送消息。

from flask import Flask, request
import requests
import json
​
app = Flask(__name__)
​
@app.route('/wechat', methods=['POST'])
def wechat():
    data = request.json
​
    # 解析告警信息
    if 'alerts' in data and len(data['alerts']) > 0:
        alert = data['alerts'][0]
        status = alert['status']
        labels = alert['labels']
        annotations = alert['annotations']
​
        message = f"Status: {status}\n"
        message += f"AlertName: {labels.get('alertname')}\n"
        message += f"Severity: {labels.get('severity')}\n"
        message += f"Summary: {annotations.get('summary')}\n"
​
        # 构造微信消息
        wechat_message = {
            "msgtype": "text",
            "text": {
                "content": message,
                "mentioned_mobile_list": ["@all"]
            }
        }
​
        # 发送消息到微信           #企业微信机器人的key机密码
        response = requests.post(
            "https://qyapi.weixin.qq/cgi-bin/webhook/send?key=af62060f-50ae-40e1-893b-6d2ade343471",
            json=wechat_message
        )
​
        if response.status_code == 200:
            return 'Message sent successfully'
        else:
            return 'Failed to send message'
​
    return 'No alerts found'
​
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8090, debug=True)

2. 配置和部署服务

配置服务

在上面的代码中,你需要替换 key=your-wechat-bot-key 为实际的微信企业号 webhook 的 key。你可以通过企业微信的管理后台获取这个 key。

部署服务
你可以选择在本地或远程服务器上部署这个服务。以下是几种常见的部署方法:
​
1.本地部署:在本地计算机上运行 Flask 应用。
2.云服务器:将应用部署到云服务器(如 AWS EC2、阿里云 ECS 等)。
3.容器化部署:使用 Docker 容器化应用,并部署到 Kubernetes 或 Docker Swarm 等集群

这里用容器方式运行,本地部署直接启动服务就行

1.创建 Dockerfile:

FROM python:3.8-slim

# 设置 pip 镜像源,可以不设置
#RUN echo "[global]" >> /root/.pip/pip.conf && echo "index-url = https://pypi.tuna.tsinghua.edu/simple" >> /root/.pip/pip.conf
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY ./app.py  .
CMD ["python", "app.py"]

2.创建 requirements.txt

Flask==2.0.3
requests==2.27.1
werkzeug==2.0.3
#指定好版本,不然有的模块之间不兼容

3.构建 Docker 镜像:

docker build -t my-wechat-webhook .
docker run -it -d  -p 8090:8090 --name my-wechat-webhook   my-wechat-webhook:latest

测试服务

 curl -X POST -H "Content-Type: application/json" -d '{"status":"firing","alerts":[{"labels":{"alertname":"DiskUsage","instance":"localhost:9100"},"annotations":{"summary":"Instance localhost:9100 Disk Usage is high (75%)"}}]}' http://101.42.26.214:8090/wechat

配置 Alertmanager

确保 Alertmanager 的配置文件指向你的 webhook 服务:

将之前的邮件报警配置备份,换成企业微信报警

vim /etc/alertmanger/alertmanager.yml
global:
  resolve_timeout: 5m

route:
  group_by: ['alertname', 'cluster', 'service', 'instance']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 3h
  receiver: 'wechat-webhook'

receivers:
- name: 'wechat-webhook'
  webhook_configs:
  - url: 'http://101.42.26.214:8090/wechat'
    send_resolved: true
#这里我们先注释掉
#templates:    #这是一个列表,用于指定一个或多个模板文件的位置
#  - '/etc/alertmanager/templates/*.tmpl'   #这些模板文件用于定义如何格式化和生成告警的通知消息。模板文件通常使用 Go 语言的标准模板引擎来定义告警消息的格式。模板文件可以包含变量、条件判断、循环等结构,用于动态生成告警消息。

systemctl restart alertmanager.service

本文标签: 邮件企业prometheusqq