提交 94540a45 authored 作者: fwang's avatar fwang

init

上级
流水线 #872 已取消 于阶段
FROM python:3.7.10-slim
WORKDIR /root/terminal-check/
ADD ./ ./
RUN pip install -r requirements.txt -i https://pypi.douban.com/simple/
ENTRYPOINT ["python","main.py"]
```shell
docker build -t ilabservice-registry.cn-hangzhou.cr.aliyuncs.com/basic/monitor-check-queue:1.0 .
docker push ilabservice-registry.cn-hangzhou.cr.aliyuncs.com/basic/monitor-check-queue:1.0
```
\ No newline at end of file
import pika
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests,json
# rabbitmq 队列参数
# rabbitmq_host = "rabbitmq-rabbitmq-haproxy-1"
rabbitmq_host = "172.16.0.65"
rabbitmq_port = 5672
rabbitmq_username = "ilabservice"
rabbitmq_password = "iLabServiceOps123456"
# 钉钉自定义机器人参数
dingtalk_webhook = "https://oapi.dingtalk.com/robot/send?access_token=abf0534b1d64ffd2263f44548f4fc62ec0a5be79bfe158ffb93d263f155a314a"
dingtalk_secret = "SEC53fcfc4dc3b289b6d59bbbf3070d369179c45f55c9cd8b7f9c402581fb04158c"
# 检测队列, name: 队列名称 threshold: 报警阈值
check_queue_confs = [
{
"name": "PRE_MERGER_QUEUE",
"threshold": 20
},
{
"name": "MQTT_QUEUE",
"threshold": 50
}
]
# 发送简单钉钉消息
def send_dingtalk_msg(content:str):
timestamp = str(round(time.time() * 1000))
secret_enc = dingtalk_secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, dingtalk_secret)
# print(string_to_sign)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
webhook_url=dingtalk_webhook+'&timestamp='+timestamp+'&sign='+sign
print(webhook_url)
#定义数据类型
headers={'Content-Type':'application/json'}
data={"msgtype":"text","text":{"content": content}, "at":{"isAtAll":True}}
#发送post请求
res=requests.post(webhook_url,data=json.dumps(data),headers=headers)
print(res.text)
# 创建连接
connect_params = pika.ConnectionParameters(
host=rabbitmq_host,
port=rabbitmq_port,
heartbeat=None,
credentials=pika.credentials.PlainCredentials(username=rabbitmq_username, password=rabbitmq_password),
)
connection = pika.BlockingConnection(connect_params)
channel = connection.channel()
need_alarm = False
alarm_content = "队列消息堆积告警\n"
# 根据队列配置,巡检所有队列的消息数量,超过阈值的则进行报警到钉钉群中
for queue_conf in check_queue_confs:
queue_name = queue_conf["name"]
alarm_threshold = queue_conf["threshold"]
print("queue_conf:", queue_conf)
queue = channel.queue_declare(queue=queue_name, durable=True)
print("queue:", queue_name, "The current queue msg number:" , queue.method.message_count)
if queue.method.message_count >= alarm_threshold:
need_alarm = True
alarm_content += "-----------------------------\n队列名称:" + queue_name + "\n堆积消息数量:" + str(queue.method.message_count) + "\n告警数量阈值:" + str(alarm_threshold) + "\n";
alarm_content += "将会导致报警不及时或无法报警,请相关人员及时关注!"
if need_alarm:
send_dingtalk_msg(alarm_content)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论