python监控rabbitmq的消息队列数量

[root@localhost chen]# cat b.py 
#!/usr/bin/python
# -*- coding: UTF-8 -*-


import json,time
import requests
from requests.auth import HTTPBasicAuth


def check_r(url='http://10.0.0.1:15672',username='xxxxx',password='yyyyyyyy',threshold=100000):
    alert_name = "rabbitmq-%s" % (url,)
    queue_url = 'http://10.1.0.7:15672/api/queues'
    res = requests.get(url=queue_url, auth=HTTPBasicAuth(username='xxxxxx', password='yyyyyyy'))
    if res.status_code == 200:
        queues = json.loads(res.text)
        msg = u"%s rabbitmq消息队列堆积异常:\n" % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),)
        for queue in queues:
            queue_count = int(queue.get("messages", 0))
            if queue_count > threshold:
                print queue
                print queue_count

check_r()

 

posted @ 2019-10-11 12:40  开始认识  阅读(2316)  评论(0编辑  收藏  举报