架構圖
通過定時任務采集解析MQ XML資料存儲到MYSQL資料庫中,目前MQ積累值超過100時,說明消費異常,通過企業微信報警,MQ.php可查詢曆史記錄。
預覽
告警頁面
查詢頁面,顯示最後1000行資料
系統組成
MQchecktouch.py 初始化資料庫
MQcheck.py 監控主程式
MQ.php 曆史記錄查詢程式
初始化資料庫
首先手動建立庫和使用者,通過MQchecktouch.py初始化,生成表
import mysql.connector
mqdb = mysql.connector.connect(
host="127.0.0.1",
user="mquser",
passwd="mqpasswd",
database="mq"
)
mqcursor = mqdb.cursor()
mqcursor.execute("CREATE TABLE mqdata (id INT AUTO_INCREMENT PRIMARY KEY,time VARCHAR(255), name VARCHAR(255), number VARCHAR(255))")
mqcursor.execute("CREATE TABLE configkey (name VARCHAR(255), config VARCHAR(255))")
insert_sql = "INSERT INTO configkey (name, config) VALUES ('config','1')"
mqcursor.execute(insert_sql)
mqdb.commit()
mqcursor.close()
初始化後的資料庫
監控主程式
通過定時任務運作主程式
import requests,time
import xml.etree.cElementTree as ET
import mysql.connector
def get_mqxml():
mqreq = requests.post(url='http://你的MQ位址:8161/admin/xml/queues.jsp', auth=('admin', 'admin'))
mqxml = ET.fromstring(mqreq.content.decode())
mqname_list = []
mqnumbers_list = []
for queue in mqxml.iter('queue'):
mqname_list.append(queue.get("name"))
mqnumbers_list.append(queue.find('stats').get("size"))
return mqname_list,mqnumbers_list
def post_weixin(num,mqnumber):
if num == 0:
data = "6啊,MQ恢複了,目前累計值:"
else:
data = "G了,MQ目前累計值:"
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你自己的企業微信機器人'
body = {
"msgtype": "news",
"news": {
"articles": [
{
"title": "MQ監控機器人",
"description": data+str(mqnumber),
"url": "90apt.com",
"picurl": "你自己的圖檔"
}
]
}
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=body)
print(response.text)
print(response.status_code)
def save_mysql():
total = 0
mqdb = mysql.connector.connect(
host="127.0.0.1",
user="mquser",
passwd="mqpasswd",
database="mq"
)
mqcursor = mqdb.cursor()
nowtime = time.asctime()
for ele in range(0, len(mqnumbers_list)):
sql = "INSERT INTO mqdata (time ,name, number) VALUES (%s, %s, %s)"
val = (nowtime, mqname_list[ele], mqnumbers_list[ele])
mqcursor.execute(sql, val)
total = total + int(mqnumbers_list[ele])
mqdb.commit()
print(total)
if total > 10:
getconfig_sql = "SELECT * FROM configkey WHERE name ='config'"
mqcursor.execute(getconfig_sql)
mqconfig = mqcursor.fetchall()[0][1]
if mqconfig == "0":
updateconfig_sql = "UPDATE configkey SET config = '1' WHERE name = 'config'"
mqcursor.execute(updateconfig_sql)
mqdb.commit()
mqcursor.close()
post_weixin(1,total)
else:
None
else:
getconfig_sql = "SELECT * FROM configkey WHERE name ='config'"
mqcursor.execute(getconfig_sql)
mqconfig = mqcursor.fetchall()[0][1]
if mqconfig == "1":
updateconfig_sql = "UPDATE configkey SET config = '0' WHERE name = 'config'"
mqcursor.execute(updateconfig_sql)
post_weixin(0, total)
mqdb.commit()
mqcursor.close()
else:
None
mqname_list = get_mqxml()[0]
mqnumbers_list = get_mqxml()[1]
save_mysql()
曆史記錄查詢程式
<?php
$con=mysqli_connect("localhost","mquser","mqpasswd","mq");
// 檢測連接配接
if (mysqli_connect_errno())
{
echo "連接配接失敗: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM mqdata order by time desc limit 1,1000");
while($row = mysqli_fetch_array($result))
{
echo $row['time'] . " " . $row['name'] . " " . $row['number'];
echo "<br>";
}
$conn = null;
?>
完畢