def run(): monitor = Monitor() while True: try: monitor.check() except Exception, e: log.error(e) time.sleep(ConfUtil.getCycle())
def index(): try: addrList = ConfUtil.getMQAddrList() addr = request.args.get('addr', addrList[0]) queues = MQService.getQueues(addr) addrList.remove(addr) addrList.insert(0, addr) except Exception, e: log.error(e)
def getQueues(addr): queueUrl = 'http://' + addr.strip() + '/api/queues' res = requests.get(queueUrl, auth=(ConfUtil.getMQUsername(), ConfUtil.getMQPassword())) queues = [] index = 0 for queue in json.loads(res.text): queueName = queue.get('name') msgTotal = queue.get('messages', 0) dict = {} dict['queue'] = queueName dict['msgTotal'] = msgTotal index += 1 dict['index'] = index doc = MonitorDao.findQueue(addr, queueName) dict['mailList'] = '' dict['msgTotalMax'] = '' if doc != None: dict['mailList'] = ','.join(doc.get('mailList')) dict['msgTotalMax'] = doc.get('msgTotalMax') queues.append(dict) return queues
def check(self): mailContent = {} for mail in ConfUtil.getGlobalMailList(): mailContent[mail.strip()] = [] for addr in self.__addrList: overviewUrl = 'http://' + addr.strip() + '/api/overview' try: res = requests.get(overviewUrl, auth=(self.__username, self.__password)) except ConnectionError, e: for mail in ConfUtil.getGlobalMailList(): mailContent[mail].append(addr + ' is dead.') continue queueUrl = 'http://' + addr.strip() + '/api/queues' res = requests.get(queueUrl, auth=(self.__username, self.__password)) for queue in json.loads(res.text): queueName = queue.get('name') if queueName in ConfUtil.getIgnoreQueueList(): continue messages = queue.get('messages', 0) doc = MonitorDao.findQueue(addr, queueName) if doc == None: if messages > ConfUtil.getGlobalMsgTotalMax(): alarm = addr + ', ' + queueName + ', ' + 'msgTotal is ' + str(messages) + '(' + str(ConfUtil.getGlobalMsgTotalMax()) + ').' for mail in ConfUtil.getGlobalMailList(): mailContent[mail].append(alarm) else: try: msgTotalMax = 0 if len(doc.get('msgTotalMax')) > 0: msgTotalMax = (int)(doc.get('msgTotalMax')) except Exception, e: log.error(doc) mailList = doc.get('mailList') if messages > msgTotalMax: alarm = addr + ', ' + queueName + ', ' + 'msgTotal is ' + str(messages) + '(' + str(msgTotalMax) + ').' for mail in mailList: if mailContent.get(mail) == None: mailContent[mail] = [] mailContent[mail.strip()].append(alarm)
def check(self): mailContent = {} for mail in ConfUtil.getGlobalMailList(): mailContent[mail.strip()] = [] for addr in self.__addrList: overviewUrl = 'http://' + addr.strip() + '/api/overview' try: res = requests.get(overviewUrl, auth=(self.__username, self.__password)) except ConnectionError, e: for mail in ConfUtil.getGlobalMailList(): mailContent[mail].append(addr + ' is dead.') continue queueUrl = 'http://' + addr.strip() + '/api/queues' res = requests.get(queueUrl, auth=(self.__username, self.__password)) for queue in json.loads(res.text): queueName = queue.get('name') if queueName in ConfUtil.getIgnoreQueueList(): continue messages = queue.get('messages', 0) doc = MonitorDao.findQueue(addr, queueName) if doc == None: if messages > ConfUtil.getGlobalMsgTotalMax(): alarm = addr + ', ' + queueName + ', ' + 'msgTotal is ' + str( messages) + '(' + str( ConfUtil.getGlobalMsgTotalMax()) + ').' for mail in ConfUtil.getGlobalMailList(): mailContent[mail].append(alarm) else: try: msgTotalMax = 0 if len(doc.get('msgTotalMax')) > 0: msgTotalMax = (int)(doc.get('msgTotalMax')) except Exception, e: log.error(doc) mailList = doc.get('mailList') if messages > msgTotalMax: alarm = addr + ', ' + queueName + ', ' + 'msgTotal is ' + str( messages) + '(' + str(msgTotalMax) + ').' for mail in mailList: if mailContent.get(mail) == None: mailContent[mail] = [] mailContent[mail.strip()].append(alarm)
def __init__(self): self.__addrList = ConfUtil.getMQAddrList() self.__username = ConfUtil.getMQUsername() self.__password = ConfUtil.getMQPassword()
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on Sep 27, 2015 @author: mountain ''' from app.util import ConfUtil import smtplib import requests from email.mime.text import MIMEText from email.header import Header from app.util.LogUtil import log fromAddr = ConfUtil.getFromAddr() smtpServer = ConfUtil.getSmtpServer() smtpPort = ConfUtil.getSmtpPort() password = ConfUtil.getMailPassword() url = ConfUtil.getPostUrl() def sendMail(toAddr, content): server = smtplib.SMTP(smtpServer, smtpPort) server.set_debuglevel(1) server.login(fromAddr, password) message = MIMEText(content, 'plain', 'utf-8') message['Subject'] = Header(u'RabbitMQ报警', 'utf-8').encode() server.sendmail(fromAddr, toAddr, message.as_string()) server.quit() def sendMultiMail(data): print data
#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on Sep 24, 2015 @author: mountain ''' from pymongo import MongoClient from app.util import ConfUtil client = MongoClient(ConfUtil.getMongoHost(), ConfUtil.getMongoPort()) db = client.mq_monitor queue = db.queue user = db.user def saveQueue(data): queue.update({'queue': data.get('queue')}, data, upsert=True) def findQueue(addr, queueName): return queue.find_one({'queue': queueName, 'addr': addr}) if __name__ == '__main__': print findQueue('c') == None
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on Sep 27, 2015 @author: mountain ''' from app.util import ConfUtil import smtplib import requests from email.mime.text import MIMEText from email.header import Header from app.util.LogUtil import log fromAddr = ConfUtil.getFromAddr() smtpServer = ConfUtil.getSmtpServer() smtpPort = ConfUtil.getSmtpPort() password = ConfUtil.getMailPassword() def sendMail(toAddr, content): server = smtplib.SMTP(smtpServer, smtpPort) server.set_debuglevel(1) server.login(fromAddr, password) message = MIMEText(content, 'plain', 'utf-8') message['Subject'] = Header(u'RabbitMQ报警', 'utf-8').encode() server.sendmail(fromAddr, toAddr, message.as_string()) server.quit() def sendMultiMail(data):