Esempio n. 1
0
  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("-----------------------------------------");
    }
  }
 @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);
 }
Esempio n. 3
0
 /** 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);
       }
     }
   }
 }
Esempio n. 4
0
  @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
  }
Esempio n. 5
0
  @Test
  public void checkEmptyTypeNamesAreIgnored() throws Exception {
    Server server = serverWithNoQuery();
    // Set useObjDomain to true
    Query query =
        Query.builder()
            .setUseObjDomainAsKey(true)
            .setAllowDottedKeys(true)
            .setObj("\"yammer.metrics\":name=\"uniqueName\",type=\"\"")
            .build();

    Result result =
        new Result(
            System.currentTimeMillis(),
            "Attribute",
            "com.yammer.metrics.reporting.JmxReporter$Counter",
            "yammer.metrics",
            null,
            "name=\"uniqueName\",type=\"\"",
            ImmutableMap.of("Attribute", (Object) 0));

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    ArrayList<String> typeNames = new ArrayList<String>();
    typeNames.add("name");
    typeNames.add("type");
    GraphiteWriter writer = getGraphiteWriter(out, typeNames);

    writer.doWrite(server, query, of(result));

    // check that the empty type "type" is ignored when allowDottedKeys is true
    assertThat(out.toString())
        .startsWith("servers.host_example_net_4321.yammer.metrics.uniqueName.Attribute 0 ");

    // check that this also works when literal " characters aren't included in the JMX ObjectName
    query =
        Query.builder()
            .setUseObjDomainAsKey(true)
            .setAllowDottedKeys(true)
            .setObj("yammer.metrics:name=uniqueName,type=")
            .build();

    out = new ByteArrayOutputStream();
    writer = getGraphiteWriter(out, typeNames);

    writer.doWrite(server, query, of(result));
    assertThat(out.toString())
        .startsWith("servers.host_example_net_4321.yammer.metrics.uniqueName.Attribute 0 ");

    // check that the empty type "type" is ignored when allowDottedKeys is false
    query =
        Query.builder()
            .setUseObjDomainAsKey(true)
            .setAllowDottedKeys(false)
            .setObj("\"yammer.metrics\":name=\"uniqueName\",type=\"\"")
            .build();

    out = new ByteArrayOutputStream();
    writer = getGraphiteWriter(out, typeNames);

    writer.doWrite(server, query, of(result));
    assertThat(out.toString())
        .startsWith("servers.host_example_net_4321.yammer_metrics.uniqueName.Attribute 0 ");
  }
Esempio n. 6
0
  /** Parse and validate settings. */
  @Override
  public void validateSetup(Query query) throws ValidationException {

    // Parse and validate host setting
    host = getStringSetting(HOST, DEFAULT_HOST);
    if (host == null) throw new ValidationException("Host can't be null", query);

    // Parse and validate port setting
    port = getIntegerSetting(PORT, DEFAULT_PORT);

    // Parse and validate addressing mode setting
    try {
      addressingMode = UDPAddressingMode.valueOf(getStringSetting(ADDRESSING_MODE, ""));
    } catch (IllegalArgumentException iae) {
      try {
        addressingMode = UDPAddressingMode.getModeForAddress(host);
      } catch (UnknownHostException uhe) {
        addressingMode = DEFAULT_ADDRESSING_MODE;
      } catch (IOException ioe) {
        addressingMode = DEFAULT_ADDRESSING_MODE;
      }
    }

    // Parse and validate TTL setting
    ttl = getIntegerSetting(TTL, DEFAULT_TTL);

    // Parse and validate protocol version setting
    v31 = getBooleanSetting(V31, DEFAULT_V31);

    // Parse and validate unit setting
    units = getStringSetting(UNITS, DEFAULT_UNITS);

    // Parse and validate slope setting
    slope = GMetricSlope.valueOf(getStringSetting(SLOPE, DEFAULT_SLOPE.name()));

    // Parse and validate tmax setting
    tmax = getIntegerSetting(TMAX, DEFAULT_TMAX);

    // Parse and validate dmax setting
    dmax = getIntegerSetting(DMAX, DEFAULT_DMAX);

    // Parse and validate group name setting
    groupName = getStringSetting(GROUP_NAME, DEFAULT_GROUP_NAME);

    // Determine the spoofed hostname
    spoofedHostName = getSpoofedHostName(query.getServer().getHost(), query.getServer().getAlias());

    log.debug(
        "Validated Ganglia metric ["
            + HOST
            + ": "
            + host
            + ", "
            + PORT
            + ": "
            + port
            + ", "
            + ADDRESSING_MODE
            + ": "
            + addressingMode
            + ", "
            + TTL
            + ": "
            + ttl
            + ", "
            + V31
            + ": "
            + v31
            + ", "
            + UNITS
            + ": '"
            + units
            + "', "
            + SLOPE
            + ": "
            + slope
            + ", "
            + TMAX
            + ": "
            + tmax
            + ", "
            + DMAX
            + ": "
            + dmax
            + ", "
            + SPOOF_NAME
            + ": "
            + spoofedHostName
            + ", "
            + GROUP_NAME
            + ": '"
            + groupName
            + "']");
  }
Esempio n. 7
0
  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);
  }
Esempio n. 8
0
  @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);
  }
Esempio n. 9
-1
  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();
  }