jolokia api提供了一种通过HTTP访问JMX获得AMQ后台数据的一种方式,即Restful Api
#!/usr/bin/env python # -*- coding:utf-8 -*- import requests import json import re import time import sys n = 1 while True: queues = requests.get("http://172.16.1.15:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=activemq_server01/Queues",auth=("admin","admin")) listQueue = json.loads(queues.content)["value"] for queue in listQueue: data = queue['objectName'].encode('unicode-escape').split(r',')[1] queueName = data.split(r'=')[1] enqueue1 = requests.get("http://172.16.1.15:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=activemq_server01,destinationType=Queue,destinationName="+queueName+"/EnqueueCount",auth=("admin","admin")) dequeue1 = requests.get( "http://172.16.1.15:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=activemq_server01,destinationType=Queue,destinationName=" + queueName + "/DequeueCount", auth=("admin", "admin")) queueSize1 = requests.get( "http://172.16.1.15:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=activemq_server01,destinationType=Queue,destinationName=" + queueName + "/QueueSize", auth=("admin", "admin")) consumer1 = requests.get( "http://172.16.1.15:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=activemq_server01,destinationType=Queue,destinationName=" + queueName + "/ConsumerCount", auth=("admin", "admin")) enqueue = json.loads(enqueue1.content)['value'] dequeue = json.loads(dequeue1.content)['value'] queueSize = json.loads(queueSize1.content)['value'] consumer = json.loads(consumer1.content)['value'] print("第%d次采集:%-20s 待消费 %-5d 入队量 %-5d 出队量 %-5d 消费者 %-5d"% (n,queueName,queueSize,enqueue,dequeue,consumer)) n=n+1 time.sleep(5)