private String createAlias(Server server) { String alias; if (server.getAlias() != null) { alias = server.getAlias(); } else { alias = server.getHost() + "_" + server.getPort(); alias = StringUtils.cleanupStr(alias); } return alias; }
@Override protected void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception { for (Result result : results) { log.debug("Query result: [{}]", result); Map<String, Object> resultValues = result.getValues(); for (Entry<String, Object> values : resultValues.entrySet()) { Object value = values.getValue(); if (isNumeric(value)) { Map<String, Object> map = new HashMap<String, Object>(); map.put("serverAlias", server.getAlias()); map.put("server", server.getHost()); map.put("port", server.getPort()); map.put("objDomain", result.getObjDomain()); map.put("className", result.getClassName()); map.put("typeName", result.getTypeName()); map.put("attributeName", result.getAttributeName()); map.put("key", values.getKey()); map.put("keyAlias", result.getKeyAlias()); map.put("value", Double.parseDouble(value.toString())); map.put("timestamp", result.getEpoch()); log.debug( "Insert into Elastic: Index: [{}] Type: [{}] Map: [{}]", indexName, ELASTIC_TYPE_NAME, map); Index index = new Index.Builder(map).index(indexName).type(ELASTIC_TYPE_NAME).build(); JestResult addToIndex = jestClient.execute(index); if (!addToIndex.isSucceeded()) { throw new ElasticWriterException( String.format( "Unable to write entry to elastic: %s", addToIndex.getErrorMessage())); } } else { log.warn( "Unable to submit non-numeric value to Elastic: [{}] from result [{}]", value, result); } } } }
public static void main(String[] args) throws Exception { Server server = Server.builder().setHost("w2").setPort("1105").build(); JMXConnector conn = null; try { conn = server.getServerConnection(); MBeanServerConnection mbeanServer = conn.getMBeanServerConnection(); TreeWalker3 tw = new TreeWalker3(); tw.walkTree(mbeanServer, server); } catch (IOException e) { log.error( "Problem processing queries for server: " + server.getHost() + ":" + server.getPort(), e); } finally { if (conn != null) { conn.close(); } } }
@Before public void initMetrics() { server = Server.builder().setHost("localhost").setPort("9876").build(); query = Query.builder().build(); result = new Result( 1, "attributeName", "className", "objDomain", null, "typeName", ImmutableMap.of("key", (Object) 1)); results = ImmutableList.of(result); }
public void walkTree(MBeanServerConnection connection, Server server) throws Exception { // key here is null, null returns everything! Set<ObjectName> mbeans = connection.queryNames(null, null); Map<String, String> output = newHashMap(); for (ObjectName name : mbeans) { MBeanInfo info = connection.getMBeanInfo(name); MBeanAttributeInfo[] attrs = info.getAttributes(); Query.Builder queryBuilder = Query.builder().setObj(name.getCanonicalName()); ResultCapture resultCapture = new ResultCapture(); queryBuilder.addOutputWriter(resultCapture); for (MBeanAttributeInfo attrInfo : attrs) { queryBuilder.addAttr(attrInfo.getName()); } Query query = queryBuilder.build(); try { Iterable<Result> results = server.execute(query); query.runOutputWritersForQuery(server, results); } catch (AttributeNotFoundException anfe) { log.error("Error", anfe); } for (Result result : resultCapture.results) { output.put(result.getTypeName(), query.getAttr().toString()); } } for (Entry<String, String> entry : output.entrySet()) { log.debug(entry.getKey()); log.debug(entry.getValue()); log.debug("-----------------------------------------"); } }
@Test public void testMerge() throws Exception { Query q1 = new Query(); q1.addAttr("foo"); q1.addAttr("bar"); q1.addKey("key1"); q1.addKey("key2"); q1.setObj("obj"); q1.setResultAlias("alias"); // same as q1 Query q2 = new Query(); q2.addAttr("foo"); q2.addAttr("bar"); q2.addKey("key1"); q2.addKey("key2"); q2.setObj("obj"); q2.setResultAlias("alias"); // different than q1 and q2 Query q3 = new Query(); q3.addAttr("foo"); q3.addAttr("bar"); q3.addKey("key1"); q3.addKey("key2"); q3.setObj("obj3"); q3.setResultAlias("alias"); Server s1 = new Server(); s1.setAlias("alias"); s1.setHost("host"); s1.setPort("8004"); s1.setCronExpression("cron"); s1.setNumQueryThreads(Integer.valueOf(123)); s1.setPassword("pass"); s1.setUsername("user"); s1.addQuery(q1); s1.addQuery(q2); // same as s1 Server s2 = new Server(); s2.setAlias("alias"); s2.setHost("host"); s2.setPort("8004"); s2.setCronExpression("cron"); s2.setNumQueryThreads(Integer.valueOf(123)); s2.setPassword("pass"); s2.setUsername("user"); s2.addQuery(q1); s2.addQuery(q2); Server s3 = new Server(); s3.setAlias("alias"); s3.setHost("host3"); s3.setPort("8004"); s3.setCronExpression("cron"); s3.setNumQueryThreads(Integer.valueOf(123)); s3.setPassword("pass"); s3.setUsername("user"); s3.addQuery(q1); s3.addQuery(q2); s3.addQuery(q3); List<Server> existing = new ArrayList<Server>(); existing.add(s1); List<Server> adding = new ArrayList<Server>(); adding.add(s2); JmxUtils.mergeServerLists(existing, adding); // should only have one server with 1 query since we just added the same // server and same query. Assert.assertTrue(existing.size() == 1); Assert.assertTrue(existing.get(0).getQueries().size() == 1); for (Server server : existing) { System.out.println(server); } adding.add(s3); JmxUtils.mergeServerLists(existing, adding); Assert.assertTrue(existing.size() == 2); Assert.assertTrue(existing.get(0).getQueries().size() == 1); // q1 and // q2 // are // equal Assert.assertTrue(existing.get(1).getQueries().size() == 2); // q1 and // q2 // are // equal, // q3 is // different s2.addQuery(q3); JmxUtils.mergeServerLists(existing, adding); Assert.assertTrue(existing.size() == 2); Assert.assertTrue(existing.get(0).getQueries().size() == 2); // q1 and // q2 // are // equal, // q3 is // different Assert.assertTrue(existing.get(1).getQueries().size() == 2); // q1 and // q2 // are // equal, // q3 is // different }
public static void main(String[] args) throws Exception { Server.Builder serverBuilder = Server.builder().setHost("w2").setPort("1105").setAlias("w2_activemq_1105"); GraphiteWriter gw = GraphiteWriter.builder() .addTypeName("destinationName") .addTypeName("Destination") .setDebugEnabled(true) .setHost(GW_HOST) .setPort(2003) .build(); Query q = Query.builder() .setObj( "org.apache.activemq:BrokerName=localhost,Type=Subscription,clientId=*,consumerId=*,destinationName=*,destinationType=Queue,persistentMode=Non-Durable") .addAttr("PendingQueueSize") .addAttr("DispatchedQueueSize") .addAttr("EnqueueCounter") .addAttr("DequeueCounter") .addAttr("MessageCountAwaitingAcknowledge") .addAttr("DispachedCounter") .addOutputWriterFactory(gw) .build(); serverBuilder.addQuery(q); Query q2 = Query.builder() .setObj( "org.apache.activemq:BrokerName=localhost,Destination=ActiveMQ.Advisory.Consumer.Queue.*,Type=Topic") .addAttr("QueueSize") .addAttr("MaxEnqueueTime") .addAttr("MinEnqueueTime") .addAttr("AverageEnqueueTime") .addAttr("InFlightCount") .addAttr("ConsumerCount") .addAttr("ProducerCount") .addAttr("DispatchCount") .addAttr("DequeueCount") .addAttr("EnqueueCount") .addAttr("Subscriptions") .addOutputWriterFactory(gw) .build(); serverBuilder.addQuery(q2); Query q3 = Query.builder() .setObj("org.apache.activemq:BrokerName=localhost,Destination=*,Type=Queue") .addAttr("QueueSize") .addAttr("MaxEnqueueTime") .addAttr("MinEnqueueTime") .addAttr("AverageEnqueueTime") .addAttr("InFlightCount") .addAttr("ConsumerCount") .addAttr("ProducerCount") .addAttr("DispatchCount") .addAttr("DequeueCount") .addAttr("EnqueueCount") .addAttr("Subscriptions") .addOutputWriterFactory(gw) .build(); serverBuilder.addQuery(q3); Query q4 = Query.builder() .setObj("org.apache.activemq:BrokerName=localhost,Destination=*,Type=Topic") .addAttr("QueueSize") .addAttr("MaxEnqueueTime") .addAttr("MinEnqueueTime") .addAttr("AverageEnqueueTime") .addAttr("InFlightCount") .addAttr("ConsumerCount") .addAttr("ProducerCount") .addAttr("DispatchCount") .addAttr("DequeueCount") .addAttr("EnqueueCount") .addAttr("Subscriptions") .addOutputWriterFactory(gw) .build(); serverBuilder.addQuery(q4); Query q5 = Query.builder() .setObj("org.apache.activemq:BrokerName=localhost,Type=Broker") .addOutputWriterFactory(gw) .build(); serverBuilder.addQuery(q5); Query q6 = Query.builder() .setObj("java.lang:type=Memory") .addAttr("HeapMemoryUsage") .addAttr("NonHeapMemoryUsage") .addOutputWriterFactory(gw) .build(); serverBuilder.addQuery(q6); Query q7 = Query.builder() .setObj("java.lang:type=Threading") .addAttr("DaemonThreadCount") .addAttr("PeakThreadCount") .addAttr("ThreadCount") .addAttr("CurrentThreadCpuTime") .addAttr("CurrentThreadUserTime") .addAttr("TotalStartedThreadCount") .addOutputWriterFactory(gw) .build(); serverBuilder.addQuery(q7); Query q8 = Query.builder() .setObj("java.lang:name=*,type=GarbageCollector") .addKey("committed") .addKey("init") .addKey("max") .addKey("used") .addKey("duration") .addKey("CollectionCount") .addKey("CollectionTime") .addOutputWriterFactory(gw) .build(); serverBuilder.addQuery(q8); Query q9 = Query.builder() .setObj("java.lang:type=MemoryPool,name=*") .addOutputWriterFactory( GraphiteWriter.builder() .addTypeName("name") .setDebugEnabled(true) .setHost(GW_HOST) .setPort(2003) .build()) .build(); serverBuilder.addQuery(q9); JmxProcess process = new JmxProcess(serverBuilder.build()); new JsonPrinter(System.out).prettyPrint(process); Injector injector = JmxTransModule.createInjector(new JmxTransConfiguration()); JmxTransformer transformer = injector.getInstance(JmxTransformer.class); transformer.executeStandalone(process); }
@SuppressFBWarnings( value = "DMI_HARDCODED_ABSOLUTE_FILENAME", justification = "Path to RRD binary is hardcoded as this is example code") public static void main(String[] args) throws Exception { File outputFile = new File("target/w2-TEST.rrd"); if (!outputFile.exists() && !outputFile.createNewFile()) { throw new IOException("Could not create output file"); } RRDToolWriter gw = RRDToolWriter.builder() .setTemplateFile(new File("memorypool-rrd-template.xml")) .setOutputFile(outputFile) .setBinaryPath(new File("/opt/local/bin")) .setDebugEnabled(true) .setGenerate(true) .addTypeName("Destination") .build(); JmxProcess process = new JmxProcess( Server.builder() .setHost("w2") .setPort("1105") .setAlias("w2_activemq_1105") .addQuery( Query.builder() .setObj("org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=*") .addAttr("QueueSize") .addAttr("MaxEnqueueTime") .addAttr("MinEnqueueTime") .addAttr("AverageEnqueueTime") .addAttr("InFlightCount") .addAttr("ConsumerCount") .addAttr("ProducerCount") .addAttr("DispatchCount") .addAttr("DequeueCount") .addAttr("EnqueueCount") .addOutputWriter(gw) .build()) .addQuery( Query.builder() .setObj("org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=*") .addAttr("QueueSize") .addAttr("MaxEnqueueTime") .addAttr("MinEnqueueTime") .addAttr("AverageEnqueueTime") .addAttr("InFlightCount") .addAttr("ConsumerCount") .addAttr("ProducerCount") .addAttr("DispatchCount") .addAttr("DequeueCount") .addAttr("EnqueueCount") .addOutputWriter(gw) .build()) .build()); jsonPrinter.prettyPrint(process); Injector injector = JmxTransModule.createInjector(new JmxTransConfiguration()); JmxTransformer transformer = injector.getInstance(JmxTransformer.class); transformer.executeStandalone(process); }