Skip to content

Commit

Permalink
1.1:
Browse files Browse the repository at this point in the history
- report prefixing,
- cli agent
  • Loading branch information
tcurdt committed Mar 7, 2012
1 parent 699ae64 commit 5f82e99
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>org.vafer</groupId>
<artifactId>jmx2any</artifactId>
<name>jmx2any</name>
<version>1.0</version>
<version>1.1</version>
<description>
</description>
<url>http://github.com/tcurdt/jmx2any</url>
Expand Down
2 changes: 1 addition & 1 deletion src/examples/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node:
id: test
delay: 1s
delay: 5s

output:
ganglia:
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/org/vafer/jmx/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ public final class Agent {

private final String filename;
private final ScheduledThreadPoolExecutor executor;
private final boolean console;
private final boolean all;

public Agent(String filename) {
public Agent(String filename, boolean console, boolean all) {
this.filename = filename;
this.console = console;
this.all = all;
this.executor = new ScheduledThreadPoolExecutor(1);
this.executor.setThreadFactory(new ThreadFactory() {
public Thread newThread(Runnable r) {
Expand All @@ -28,14 +32,17 @@ public Thread newThread(Runnable r) {
public void start() {
try {
final Exporter exporter = new Exporter();
final Exporter.Config config = exporter.load(filename, false, false);
final Exporter.Config config = exporter.load(filename, console, all);
executor.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
exporter.output(config);
} catch (Exception e) {
System.err.println("jmx2any: " + e.getMessage());
}
if (console) {
System.out.println("-");
}
}
}, config.initialDelay, config.repeatDelay, TimeUnit.MILLISECONDS);
} catch (Exception e) {
Expand All @@ -49,7 +56,7 @@ public void stop() {

public static void premain(String args, Instrumentation inst) {
System.out.println("Starting jmx2any agent");
new Agent(args).start();
new Agent(args, false, false).start();
}

// public static void main(String[] args) throws Exception {
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/org/vafer/jmx/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ public Config load(String configfile, boolean console, boolean all) throws Excep
output = OutputFactory.createOutput(outputMap);
}

Enums enums = new Enums();
Set<String> metrics = new TreeSet<String>();
Set<String> reports = flattenAsStringSet(outputMap, "report");
for(String report : reports) {
Enums enums = new Enums();
Set<String> metrics = new HashSet<String>();

Map reportMap = (Map) configMap.get(report);
if (reportMap == null) throw new Exception("No such report named \"" + report + "\"");

Expand All @@ -84,6 +85,11 @@ public Config load(String configfile, boolean console, boolean all) throws Excep
}
queries.addAll(flattenAsStringSet(reportMap, "query"));

String prefix = (String) reportMap.get("prefix");
if (prefix == null) {
prefix = "";
}

Map metricsMap = (Map) reportMap.get("metrics");
metrics.addAll(metricsMap.keySet());
for(Object metric : metricsMap.keySet()) {
Expand All @@ -92,19 +98,19 @@ public Config load(String configfile, boolean console, boolean all) throws Excep
Map enumsMap = (Map) metricMap.get("enum");
for(Object replacement : enumsMap.keySet()) {
enums.setMapping(
String.valueOf(metric),
prefix + String.valueOf(metric),
Pattern.compile(String.valueOf(enumsMap.get(replacement))),
Integer.parseInt(String.valueOf(replacement))
);
}
}
}
}

if (all) {
pipes.add(new ConverterPipe(output, enums));
} else {
pipes.add(new OutputFilter(new ConverterPipe(output, enums), metrics));
if (all) {
pipes.add(new ConverterPipe(output, prefix, enums));
} else {
pipes.add(new OutputFilter(new ConverterPipe(output, prefix, enums), metrics));
}
}
}

Expand Down
22 changes: 17 additions & 5 deletions src/main/java/org/vafer/jmx/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@ public final class Main {
@Parameter(names = "-config", description = "path to config file", required = true)
private String configPath = "/etc/jmx2any.yml";

@Parameter(names = "-print", description = "print to output")
private boolean print = false;
@Parameter(names = "-console", description = "print to console")
private boolean console = false;

@Parameter(names = "-all", description = "do not filter")
private boolean all = false;

@Parameter(names = "-agent", description = "run agent")
private boolean agent = false;

private void run() throws Exception {
Exporter exporter = new Exporter();
Exporter.Config config = exporter.load(configPath, print, all);
exporter.output(config);
if (agent) {
new Agent(configPath, console, all).start();
// just running the agent until the jvm is terminated
while(true) {
Thread.sleep(5*1000);
}
} else {
Exporter exporter = new Exporter();
Exporter.Config config = exporter.load(configPath, console, all);
exporter.output(config);
}
}

public static void main(String[] args) throws Exception {
Expand All @@ -29,6 +40,7 @@ public static void main(String[] args) throws Exception {
cli.usage();
System.exit(1);
}

m.run();
}
}
6 changes: 4 additions & 2 deletions src/main/java/org/vafer/jmx/pipe/ConverterPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public final class ConverterPipe implements JmxPipe {
private final static DefaultFormatter formatter = new DefaultFormatter();

private final Output output;
private final String prefix;
private final Enums enums;

public ConverterPipe(Output output, Enums enums) {
public ConverterPipe(Output output, String prefix, Enums enums) {
this.output = output;
this.prefix = prefix;
this.enums = enums;
}

Expand All @@ -38,7 +40,7 @@ public void output(String node, JmxQuery.JmxAttribute metric) throws IOException
final String attribute = formatter.attributeName(metric.getBeanName(), metric.getAttributeName());
try {
final Object value = metric.getAttributeValue();
flatten(node, attribute, value);
flatten(node, prefix + attribute, value);
} catch(Exception e) {
System.err.println(String.format("Failed to read attribute %s [%s]", attribute, e.getMessage()));
}
Expand Down

0 comments on commit 5f82e99

Please sign in to comment.