#!/usr/bin/python3

import json
import argparse

# if any impacting changes to this plugin kindly increment the plugin version here.
PLUGIN_VERSION = 1

# Setting this to true will alert you when there is a communication problem while posting plugin data to server
HEARTBEAT = "true"

# Enter the host name configures for the Solr JMX
HOST_NAME = ""

# Enter the port configures for the Solr JMX
PORT = ""

# Enter the Broker name to be monitored from your Apache Solr
BROKER_NAME = ""

# Enter the Destination name to be monitored from your Apache Solr
DESTINATION_NAME = ""

URL = ""
QUERY = ""

result_json = {}

METRIC_UNITS = {
    "broker_memory_percent": "percent",
    "queue_memory_percent": "percent",
    "store_percent_usage": "percent",
    "temp_percent_usage": "percent",
    "average_enqueue_time": "milliseconds",
    "consumer_count": "",
    "dequeue_count": "messages",
    "dispatch_count": "messages",
    "enqueue_count": "messages",
    "expired_count":  "messages",
    "in_flight_count": "messages",
    "max_enqueue_time": "milliseconds",
    "min_enqueue_time": "milliseconds",
    "producer_count": "",
    "queue_size": "messages"
}

metric_map = {
    "broker_metrics": {
        "StorePercentUsage": "store_percent_usage",
        "TempPercentUsage": "temp_percent_usage",
        "MemoryPercentUsage": "broker_memory_percent_usage"
    },
    "queue_metrics": {
        "AverageEnqueueTime": "average_enqueue_time",
        "ConsumerCount": "consumer_count",
        "DequeueCount": "dequeue_count",
        "DispatchCount": "dispatch_count",
        "EnqueueCount": "enqueue_count",
        "ExpiredCount": "expired_count",
        "InFlightCount": "in_flight_count",
        "MemoryPercentUsage": "queue_memory_percent_usage",
        "MaxEnqueueTime": "max_enqueue_time",
        "MinEnqueueTime": "min_enqueue_time",
        "ProducerCount": "producer_count",
        "QueueSize": "queue_size"
    }
}


# JMX Query is executed and getting the Performance metric data after filtering the output
def mbean_attributes(jmxconnection, QUERY, javax, metric_arg):
    result = {}
    try:
        for metric in metric_arg:
            output = jmxconnection.getAttribute(javax.management.ObjectName(QUERY), metric)
            result[metric_arg[metric]] = output

    except Exception as e:
        result["status"] = 0
        result["msg"] = str(e)

    return result


# JMX url is defined and JMX connection is established, Query and metric keys are passed to process
def get_output():
    URL = "service:jmx:rmi:///jndi/rmi://" + HOST_NAME + ":" + PORT + "/jmxrmi"
    try:
        import jpype
        from jpype import java
        from jpype import javax

        jpype.startJVM(convertStrings=False)
        jhash = java.util.HashMap()
        jmxurl = javax.management.remote.JMXServiceURL(URL)
        jmxsoc = javax.management.remote.JMXConnectorFactory.connect(jmxurl, jhash)
        jmxconnection = jmxsoc.getMBeanServerConnection()

        QUERY = "org.apache.activemq:type=Broker,brokerName=" + BROKER_NAME
        result_json = mbean_attributes(jmxconnection, QUERY, javax, metric_map["broker_metrics"])

        QUERY = "org.apache.activemq:type=Broker,brokerName=" + BROKER_NAME + ",destinationType=Queue,destinationName=" + DESTINATION_NAME
        result_json.update(mbean_attributes(jmxconnection, QUERY, javax, metric_map["queue_metrics"]))

        result_json["broker_name"] = BROKER_NAME
        result_json["queue_name"] = DESTINATION_NAME

    except Exception as e:
        result_json["status"] = 0
        result_json["msg"] = str(e)

    return result_json


# arguments are parsed from activemq.cfg file and assigned with the variables
if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument('--host_name', help="activemq host_name", type=str)
    parser.add_argument('--port', help="activemq port", type=str)
    parser.add_argument('--broker_name', help="activemq broker_name", type=str)
    parser.add_argument('--destination_name', help="activemq destination_name", type=str)
    args = parser.parse_args()
    if args.host_name:
        HOST_NAME = args.host_name
    if args.port:
        PORT = args.port
    if args.broker_name:
        BROKER_NAME = args.broker_name
    if args.destination_name:
        DESTINATION_NAME = args.destination_name

    result_json = get_output()

    result_json['plugin_version'] = PLUGIN_VERSION
    result_json['heartbeat_required'] = HEARTBEAT
    result_json['units'] = METRIC_UNITS

    print(json.dumps(result_json, indent=4, sort_keys=True))

@@ -14,9 +14,8 @@
 thon
-3
 %0D%0A%0D%0A
@@ -152,9 +152,9 @@
 N = 
-1
+3
 %0D%0A%0D%0A
@@ -329,24 +329,33 @@
 for the 
-Solr JMX
+Activemq JMX host
 %0D%0AHOST_N
@@ -406,16 +406,25 @@
 the 
-Solr JMX
+Activemq JMX port
 %0D%0APO
@@ -485,25 +485,29 @@
 m your A
-pache Sol
+ctivemq broke
 r%0D%0ABROKE
@@ -581,18 +581,26 @@
 ur A
-pache Solr
+ctivemq Queue name
 %0D%0ADE
@@ -2625,24 +2625,41 @@
 _output():%0D%0A
+    result = %7B%7D%0D%0A
     URL = %22s
@@ -3207,21 +3207,16 @@
   result
-_json
  = mbean
@@ -3443,21 +3443,16 @@
   result
-_json
 .update(
@@ -3544,21 +3544,16 @@
   result
-_json
 %5B%22broker
@@ -3589,21 +3589,16 @@
   result
-_json
 %5B%22queue_
@@ -3668,21 +3668,16 @@
   result
-_json
 %5B%22status
@@ -3698,21 +3698,16 @@
   result
-_json
 %5B%22msg%22%5D 
@@ -3735,21 +3735,16 @@
 n result
-_json
 %0D%0A%0D%0A%0D%0A# 

Update activemq.py