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("-----------------------------------------"); } }
/** Send query result values to Ganglia. */ @Override public void doWrite(Query query) throws Exception { for (final Result result : query.getResults()) { if (result.getValues() != null) { for (final Map.Entry<String, Object> resultValue : result.getValues().entrySet()) { final String name = JmxUtils.getKeyString2(query, result, resultValue, getTypeNames(), null); final String value = resultValue.getValue().toString(); log.debug("Sending Ganglia metric {}={}", name, value); new GMetric(host, port, addressingMode, ttl, v31, null, spoofedHostName) .announce( name, value, getType(resultValue.getValue()), units, slope, tmax, dmax, groupName); } } } }
@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); } } } }
private void serialize(Query query, OutputStream outputStream) throws IOException { JsonGenerator g = jsonFactory.createJsonGenerator(outputStream, JsonEncoding.UTF8); g.useDefaultPrettyPrinter(); g.writeStartObject(); g.writeStringField("name", "jmxtrans"); g.writeStringField("type", "metric"); g.writeStringField("handler", sensuhandler); StringBuffer jsonoutput = new StringBuffer(); List<String> typeNames = getTypeNames(); for (Result result : query.getResults()) { Map<String, Object> resultValues = result.getValues(); if (resultValues != null) { for (Map.Entry<String, Object> values : resultValues.entrySet()) { if (NumberUtils.isNumeric(values.getValue())) { Object value = values.getValue(); jsonoutput .append(JmxUtils.getKeyString(query, result, values, typeNames, null)) .append(" ") .append(value) .append(" ") .append(TimeUnit.SECONDS.convert(result.getEpoch(), TimeUnit.MILLISECONDS)) .append(System.getProperty("line.separator")); } } } } g.writeStringField("output", jsonoutput.toString()); g.writeEndObject(); g.flush(); g.close(); }