Permalink
Browse files

white space cleanup,

close jmx connection in finally,
support for composites
multi-query support
  • Loading branch information...
1 parent bb75bd0 commit c5389afa33ff4453ea070ef94cb4083cb4e673cc @tcurdt committed Jan 19, 2012
View
@@ -6,3 +6,5 @@ target
eclipse
old
bin
+.idea
+*.iml
View
@@ -56,7 +56,7 @@ Sometimes it can be useful to track String values by mapping them into an enum a
java -jar jmx2munin.jar \
-url service:jmx:rmi:///jndi/rmi://localhost:7199/jmxrmi \
-query "org.apache.cassandra.*:*" \
- list
+ -list
It should output a list of possible candidates. This can now be turned into a enum configuration file:
View
@@ -5,7 +5,7 @@
<groupId>org.vafer</groupId>
<artifactId>jmx2munin</artifactId>
<name>jmx2munin</name>
- <version>1.0</version>
+ <version>1.1</version>
<description>
Munin plugin to access JMX information
</description>
@@ -37,13 +37,13 @@
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
- <version>1.17</version>
+ <version>1.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <version>4.5</version>
+ <version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
@@ -53,15 +53,17 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
<configuration>
- <source>1.5</source>
- <target>1.5</target>
+ <source>1.6</source>
+ <target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
+ <version>2.11</version>
<configuration>
<forkMode>never</forkMode>
<includes>
@@ -14,7 +14,7 @@
public final class Enums {
private TreeMap<String, LinkedHashMap<Integer, Pattern>> sections = new TreeMap<String, LinkedHashMap<Integer, Pattern>>();
-
+
public boolean load(String filePath) throws IOException {
BufferedReader input = null;
LinkedHashMap<Integer, Pattern> section = new LinkedHashMap<Integer, Pattern>();
@@ -46,7 +46,7 @@ public boolean load(String filePath) throws IOException {
}
} finally {
if (input != null) {
- input.close();
+ input.close();
}
}
return false;
@@ -59,9 +59,9 @@ public static String id(ObjectName beanName, String attributeName) {
sb.append(beanName.getKeyProperty("type"));
sb.append(':');
sb.append(attributeName);
- return sb.toString();
+ return sb.toString();
}
-
+
public Number resolve(String id, String value) {
LinkedHashMap<Integer, Pattern> section = sections.get(id);
if (section == null) {
@@ -71,7 +71,7 @@ public Number resolve(String id, String value) {
if (entry.getValue().matcher(value).matches()) {
return entry.getKey();
}
- }
+ }
return null;
}
}
@@ -19,34 +19,39 @@
import javax.management.remote.JMXServiceURL;
public final class Query {
-
+
public void run(String url, String expression, Filter filter, Output output) throws IOException, MalformedObjectNameException, InstanceNotFoundException, ReflectionException, IntrospectionException, AttributeNotFoundException, MBeanException {
- JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(url));
- MBeanServerConnection connection = connector.getMBeanServerConnection();
- final Collection<ObjectInstance> mbeans = connection.queryMBeans(new ObjectName(expression), null);
+ JMXConnector connector = null;
+ try {
+ connector = JMXConnectorFactory.connect(new JMXServiceURL(url));
+ MBeanServerConnection connection = connector.getMBeanServerConnection();
+ final Collection<ObjectInstance> mbeans = connection.queryMBeans(new ObjectName(expression), null);
- for(ObjectInstance mbean : mbeans) {
- final ObjectName mbeanName = mbean.getObjectName();
- final MBeanInfo mbeanInfo = connection.getMBeanInfo(mbeanName);
- final MBeanAttributeInfo[] attributes = mbeanInfo.getAttributes();
- for (final MBeanAttributeInfo attribute : attributes) {
- if (attribute.isReadable()) {
- if (filter.include(mbeanName, attribute.getName())) {
- final String attributeName = attribute.getName();
- try {
- output.output(
- mbean.getObjectName(),
- attributeName,
- connection.getAttribute(mbeanName, attributeName)
- );
- } catch(Exception e) {
- // System.err.println("Failed to read " + mbeanName + "." + attributeName);
- }
+ for(ObjectInstance mbean : mbeans) {
+ final ObjectName mbeanName = mbean.getObjectName();
+ final MBeanInfo mbeanInfo = connection.getMBeanInfo(mbeanName);
+ final MBeanAttributeInfo[] attributes = mbeanInfo.getAttributes();
+ for (final MBeanAttributeInfo attribute : attributes) {
+ if (attribute.isReadable()) {
+ if (filter.include(mbeanName, attribute.getName())) {
+ final String attributeName = attribute.getName();
+ try {
+ output.output(
+ mbean.getObjectName(),
+ attributeName,
+ connection.getAttribute(mbeanName, attributeName)
+ );
+ } catch(Exception e) {
+ // System.err.println("Failed to read " + mbeanName + "." + attributeName);
+ }
+ }
}
}
}
-
+ } finally {
+ if (connector != null) {
+ connector.close();
+ }
}
- connector.close();
}
}
@@ -2,51 +2,63 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.TreeSet;
import javax.management.ObjectName;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeType;
public final class Value {
public interface Listener {
public void value(ObjectName beanName, String attributeName, String value);
public void value(ObjectName beanName, String attributeName, Number value);
}
-
+
public static void flatten(ObjectName beanName, String attributeName, Object value, Listener listener) {
if (value instanceof Number) {
listener.value(beanName, attributeName, (Number) value);
-
+
} else if (value instanceof String) {
listener.value(beanName, attributeName, (String) value);
} else if (value instanceof Set) {
final Set set = (Set) value;
- flatten(beanName, attributeName + ".size", set.size(), listener);
- for(Object entry : set) {
+ flatten(beanName, attributeName + ".size", set.size(), listener);
+ for(Object entry : set) {
flatten(beanName, attributeName + "[" + entry + "]", 1, listener);
}
} else if (value instanceof List) {
final List list = (List)value;
- listener.value(beanName, attributeName + ".size", list.size());
+ listener.value(beanName, attributeName + ".size", list.size());
for(int i = 0; i<list.size(); i++) {
flatten(beanName, attributeName + "[" + i + "]", list.get(i), listener);
}
-
+
} else if (value instanceof Map) {
final Map<?,?> map = (Map<?,?>) value;
- listener.value(beanName, attributeName + ".size", map.size());
+ listener.value(beanName, attributeName + ".size", map.size());
for(Map.Entry<?, ?> entry : map.entrySet()) {
- flatten(beanName, attributeName + "[" + entry.getKey() + "]", entry.getValue(), listener);
- }
-
+ flatten(beanName, attributeName + "[" + entry.getKey() + "]", entry.getValue(), listener);
+ }
+
+ } else if (value instanceof CompositeData) {
+
+ CompositeData composite = (CompositeData) value;
+ CompositeType type = composite.getCompositeType();
+ TreeSet<String> keysSet = new TreeSet<String>(type.keySet());
+ for(String key : keysSet) {
+ flatten(beanName, attributeName + "[" + key + "]", composite.get(key), listener);
+ }
+
} else {
- // System.err.println("Failed to convert " + beanName + "." + attributeName);
+ // System.err.println("Failed to convert " + beanName + "." + attributeName);
}
}
}
@@ -4,25 +4,21 @@
import java.util.List;
import java.util.Locale;
-import org.vafer.jmx.Enums;
-import org.vafer.jmx.Filter;
-import org.vafer.jmx.ListOutput;
-import org.vafer.jmx.NoFilter;
-import org.vafer.jmx.Query;
+import org.vafer.jmx.*;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
public final class Munin {
- @Parameter(description = "")
- private List<String> args = new ArrayList<String>();
-
+ @Parameter(names = "-list", description = "show as list")
+ private boolean list;
+
@Parameter(names = "-url", description = "jmx url", required = true)
private String url;
@Parameter(names = "-query", description = "query expression", required = true)
- private String query;
+ private List<String> queries = new ArrayList<String>();
@Parameter(names = "-enums", description = "file string to enum config")
private String enumsPath;
@@ -42,21 +38,25 @@ private void run() throws Exception {
if (enumsPath != null) {
enums.load(enumsPath);
}
-
- final String cmd = args.toString().toLowerCase(Locale.US);
- if ("[list]".equals(cmd)) {
- new Query().run(url, query, filter, new ListOutput());
+
+ final Output output;
+ if (list) {
+ output = new ListOutput();
} else {
- new Query().run(url, query, filter, new MuninOutput(enums));
+ output = new MuninOutput(enums);
+ }
+
+ for(String query : queries) {
+ new Query().run(url, query, filter, output);
}
}
public static void main(String[] args) throws Exception {
Munin m = new Munin();
-
+
JCommander cli = new JCommander(m);
try {
- cli.parse(args);
+ cli.parse(args);
} catch(Exception e) {
cli.usage();
System.exit(1);
@@ -25,11 +25,11 @@ public static String attributeName(ObjectName bean, String attribute) {
sb.append(fieldname(beanString(bean)));
sb.append('_');
sb.append(fieldname(attribute));
- return sb.toString().toLowerCase(Locale.US);
+ return sb.toString().toLowerCase(Locale.US);
}
-
+
private static String fieldname(String s) {
- return s.replaceAll("[^A-Za-z0-9]", "_");
+ return s.replaceAll("[^A-Za-z0-9]", "_").replaceAll("_+", "_").replaceAll("_$", "");
}
private static String beanString(ObjectName beanName) {
@@ -54,24 +54,24 @@ private static String beanString(ObjectName beanName) {
ArrayList<String> keys = new ArrayList(properties.keySet());
Collections.sort(keys);
-
+
for(String key : keys) {
sb.append('.');
sb.append(properties.get(key));
}
-
+
return sb.toString();
// return beanName.getCanonicalName();
}
-
+
public void output(ObjectName beanName, String attributeName, Object value) {
Value.flatten(beanName, attributeName, value, new Value.Listener() {
public void value(ObjectName beanName, String attributeName, String value) {
final Number v = enums.resolve(Enums.id(beanName, attributeName), value);
if (v != null) {
value(beanName, attributeName, v);
} else {
- value(beanName, attributeName, Double.NaN);
+ value(beanName, attributeName, Double.NaN);
}
}
public void value(ObjectName beanName, String attributeName, Number value) {
@@ -83,11 +83,11 @@ public void value(ObjectName beanName, String attributeName, Number value) {
final NumberFormat f = NumberFormat.getInstance();
f.setMaximumFractionDigits(2);
f.setGroupingUsed(false);
- v = f.format(value);
+ v = f.format(value);
}
-
+
System.out.println(attributeName(beanName, attributeName) + ".value " + v);
}
- });
+ });
}
}

0 comments on commit c5389af

Please sign in to comment.