Created
December 16, 2014 11:09
-
-
Save mikecroft/02890ad5a76f001c3f7e to your computer and use it in GitHub Desktop.
JMX monitoring for ActiveMQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.co.c2b2.activemq.monitor; | |
/** | |
* | |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
* | |
* Copyright (c) 2014 C2B2 Consulting Limited. All rights reserved. | |
* | |
* The contents of this file are subject to the terms of the Common Development | |
* and Distribution License("CDDL") (collectively, the "License"). You | |
* may not use this file except in compliance with the License. You can | |
* obtain a copy of the License at | |
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
* When distributing the software, include this License Header Notice in each | |
* file and include the License file at packager/legal/LICENSE.txt. | |
* | |
* | |
* Owner: C2B2 | |
* Author: Mike Croft | |
* Date: 02/12/14 | |
* | |
*/ | |
import javax.management.*; | |
import javax.management.remote.JMXConnector; | |
import javax.management.remote.JMXConnectorFactory; | |
import javax.management.remote.JMXServiceURL; | |
import java.io.*; | |
public class Monitor { | |
public static void main (String args[]){ | |
String host = "localhost"; | |
String port = "1099"; | |
String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi"; | |
try { | |
getData(url); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void getData(String url) throws IOException { | |
String brokerDataFile = "broker.csv"; | |
String queueDataFile = "queue-"; | |
JMXServiceURL serviceUrl = new JMXServiceURL(url); | |
JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceUrl, null); | |
try { | |
MBeanServerConnection mbeanConn = jmxConnector.getMBeanServerConnection(); | |
ObjectName brokerMBean = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker"); | |
String[] brokerAttributes = {"BrokerName" | |
, "MemoryPercentUsage" | |
, "StorePercentUsage" | |
, "TempPercentUsage" | |
, "TotalConsumerCount" | |
, "TotalDequeueCount" | |
, "TotalEnqueueCount" | |
, "TotalMessageCount" | |
}; | |
// Get broker details | |
getAttributes(mbeanConn, brokerMBean, brokerAttributes, brokerDataFile); | |
// A list of Queue names is stored as a broker attribute | |
ObjectName[] queues = (ObjectName[]) mbeanConn.getAttribute(brokerMBean, "Queues"); | |
String [] queueAttributes = {"Name" | |
, "AverageEnqueueTime" | |
, "CursorFull" | |
, "CursorMemoryUsage" | |
, "CursorPercentUsage" | |
, "DequeueCount" | |
, "EnqueueCount" | |
, "InFlightCount" | |
, "MemoryPercentUsage" | |
, "ProducerCount" | |
, "QueueSize" | |
}; | |
for (ObjectName queueMBean : queues){ | |
getAttributes(mbeanConn, queueMBean, queueAttributes, queueDataFile + mbeanConn.getAttribute(queueMBean, "Name") + ".csv"); | |
} | |
} catch (Exception e){ | |
e.printStackTrace(); | |
} finally { | |
jmxConnector.close(); | |
} | |
} | |
private static void getAttributes(MBeanServerConnection mbeanConn, ObjectName mBean, String[] attributes, String fileName) throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException, IOException { | |
String data = ""; | |
String titles = ""; | |
File f = new File(fileName); | |
if (!f.exists()) { | |
// Titles | |
for (String attr : attributes){ titles += attr + ",";} | |
appendToFile(fileName, titles); | |
} | |
// Data | |
for (String attr : attributes){ | |
data += mbeanConn.getAttribute(mBean, attr) + ","; | |
} | |
appendToFile(fileName, data); | |
} | |
private static void appendToFile(String fileName, String data) { | |
try { | |
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true))); | |
out.println(data); | |
out.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment