Back to index

munin  2.0.1
Beans.java
Go to the documentation of this file.
00001 package org.munin.plugin.jmx;
00002 
00003 import java.io.IOException;
00004 import java.io.PrintWriter;
00005 import java.net.MalformedURLException;
00006 import javax.management.MBeanServerConnection;
00007 import javax.management.ObjectName;
00008 import javax.management.MalformedObjectNameException;
00009 import javax.management.InstanceNotFoundException;
00010 import javax.management.MBeanAttributeInfo;
00011 import javax.management.MBeanInfo;
00012 import java.util.Set;
00013 import javax.management.JMException;
00014 import java.lang.management.ManagementFactory;
00015 
00016 import org.munin.plugin.jmx.Config;
00017 
00018 public class Beans {
00019 
00020     protected ObjectName name;
00021     protected Set<ObjectName> mbeans;
00022     protected Config config;
00023     protected MBeanServerConnection connection;
00024     protected String[] filter;
00025 
00026     public Beans(String args[]) {
00027         config = getConfig(args);
00028         filter = new String[args.length - 1];
00029         this.populateBeanList(args[0]);
00030         for(int i=1; i<args.length; i++) {
00031             filter[i-1]=args[i];
00032         }
00033         java.util.Arrays.sort(filter);
00034     }
00035 
00036     protected MBeanServerConnection getConnection() {
00037         if (connection == null) {
00038             try {
00039                 connection = BasicMBeanConnection.get(config);
00040             } catch (MalformedURLException e) {
00041                 throw new IllegalStateException(
00042                         "Failed to get MBean Server Connection", e);
00043             } catch (IOException e) {
00044                 throw new IllegalStateException(
00045                         "Failed to get MBean Server Connection", e);
00046             }
00047         }
00048         return connection;
00049     }
00050 
00051     protected void populateBeanList(String strName) {
00052         try {
00053             name = new ObjectName(strName);
00054         } catch (MalformedObjectNameException e) {
00055             throw new IllegalStateException("Failed to get provider class", e);
00056         }
00057 
00058         try {
00059             mbeans = getConnection().queryNames(name,null);
00060         } catch (IOException e) {
00061             throw new IllegalStateException("Failed to get provider class", e);
00062         }
00063     }
00064 
00065     protected void printBeans() {
00066         for (ObjectName bean : mbeans) {
00067             MBeanInfo info;
00068             try {
00069                 info = connection.getMBeanInfo(bean);
00070             } catch (Exception e) {
00071                 throw new IllegalStateException("Failed to get provider class", e);
00072             }
00073             MBeanAttributeInfo[] attributes = info.getAttributes();
00074             for (MBeanAttributeInfo attr : attributes) {
00075                 if (filter.length == 0 || java.util.Arrays.binarySearch(filter, attr.getName()) >= 0) {
00076                     Object val;
00077                     try {
00078                         val = connection.getAttribute(bean,attr.getName());
00079                     } catch (Exception e) {
00080                         val = e;
00081                     }
00082 
00083                     System.out.println(bean + "\t" + attr.getName() + "\t" + val);
00084                 }
00085             }
00086         }
00087     }
00088 
00089     private static Config getConfig(String[] args) {
00090         String prefix;
00091         if (args.length >= 2) {
00092             prefix = args[1];
00093         } else {
00094             prefix = null;
00095         }
00096         return new Config(prefix);
00097     }
00098 
00099 
00100     public static void main(String args[]) {
00101         Beans bs = new Beans(args);
00102         bs.printBeans();
00103     }
00104 }
00105 
00106 
00107 
00108 // vim: ts=4:et:ai