Back to index

munin  2.0.1
AbstractGraphsProvider.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 
00007 import javax.management.MBeanServerConnection;
00008 
00009 public abstract class AbstractGraphsProvider {
00010        protected final Config config;
00011        private MBeanServerConnection connection;
00012 
00013        protected AbstractGraphsProvider(final Config config) {
00014               if (config == null) {
00015                      throw new IllegalArgumentException("config must not be null");
00016               }
00017               this.config = config;
00018        }
00019 
00020        protected MBeanServerConnection getConnection() {
00021               if (connection == null) {
00022                      try {
00023                             connection = BasicMBeanConnection.get(config);
00024                      } catch (MalformedURLException e) {
00025                             throw new IllegalStateException(
00026                                           "Failed to get MBean Server Connection", e);
00027                      } catch (IOException e) {
00028                             throw new IllegalStateException(
00029                                           "Failed to get MBean Server Connection", e);
00030                      }
00031               }
00032               return connection;
00033        }
00034 
00035        public abstract void printConfig(PrintWriter out);
00036 
00037        public abstract void printValues(PrintWriter out);
00038 
00039        protected void printGraphConfig(PrintWriter out, String title,
00040                      String vlabel, String info, String args, boolean update,
00041                      boolean graph) {
00042               out.println("graph_title " + title);
00043               out.println("graph_vlabel " + vlabel);
00044               if (info != null && info.length() > 0) {
00045                      out.println("graph_info " + info);
00046               }
00047               if (args != null && args.length() > 0) {
00048                      out.println("graph_args " + args);
00049               }
00050               out.println("graph_category " + config.getCategory());
00051               if (!update) {
00052                      out.println("update no");
00053               }
00054               if (!graph) {
00055                      out.println("graph no");
00056               }
00057        }
00058 
00059        protected void printFieldConfig(PrintWriter out, String name, String label,
00060                      String info, String type, String draw, String colour, double min,
00061                      double max) {
00062               printFieldAttribute(out, name, "label", label);
00063               printFieldAttribute(out, name, "info", info);
00064               printFieldAttribute(out, name, "type", type);
00065               if (!Double.isNaN(min)) {
00066                      printFieldAttribute(out, name, "min", min);
00067               }
00068               if (!Double.isNaN(max)) {
00069                      printFieldAttribute(out, name, "max", max);
00070               }
00071               printFieldAttribute(out, name, "draw", draw);
00072               printFieldAttribute(out, name, "colour", colour);
00073        }
00074 
00075        protected void printFieldAttribute(PrintWriter out, String fieldName,
00076                      String attributeName, Object value) {
00077               if (value != null) {
00078                      String stringValue = String.valueOf(value);
00079                      if (stringValue.length() > 0) {
00080                             out.println(fieldName + "." + attributeName + " " + value);
00081                      }
00082               }
00083        }
00084 
00085        static void runGraph(String[] args) {
00086               String providerClassName = Thread.currentThread().getStackTrace()[2].getClassName();
00087               Class<? extends AbstractGraphsProvider> providerClass;
00088               try {
00089                      providerClass = Class.forName(providerClassName).asSubclass(AbstractGraphsProvider.class);
00090               } catch (ClassNotFoundException e) {
00091                      throw new IllegalStateException("Failed to get provider class", e);
00092               }
00093               Config config = getConfig(args);
00094               AbstractGraphsProvider provider = getProvider(providerClass, config);
00095               runGraph(provider, config, args);
00096        }
00097 
00098        private static AbstractGraphsProvider getProvider(
00099                      Class<? extends AbstractGraphsProvider> providerClass, Config config) {
00100               try {
00101                      return providerClass.getConstructor(Config.class).newInstance(
00102                                    config);
00103               } catch (NoSuchMethodException e) {
00104                      // just try default constructor
00105                      try {
00106                             return providerClass.newInstance();
00107                      } catch (Exception e1) {
00108                             throw new IllegalArgumentException(
00109                                           "Can't instantiate provider with default constructor: "
00110                                                         + providerClass, e);
00111                      }
00112               } catch (Exception e) {
00113                      throw new IllegalArgumentException(
00114                                    "Can't instantiate provider with constructor accepting Config object: "
00115                                                  + providerClass, e);
00116               }
00117        }
00118 
00119        private static Config getConfig(String[] args) {
00120               String prefix;
00121               if (args.length >= 2) {
00122                      prefix = args[1];
00123               } else {
00124                      prefix = null;
00125               }
00126               return new Config(prefix);
00127        }
00128 
00129        private static void runGraph(final AbstractGraphsProvider provider,
00130                      Config config, String[] args) {
00131               try {
00132                      PrintWriter out = new PrintWriter(System.out);
00133                      if (args[0].equals("config")) {
00134                             provider.printConfig(out);
00135                             if (config.isDirtyConfg()) {
00136                                    provider.printValues(out);
00137                             }
00138                      } else {
00139                             provider.printValues(out);
00140                      }
00141                      out.flush();
00142               } catch (Exception e) {
00143                      e.printStackTrace(System.err);
00144               }
00145        }
00146 
00147        protected static String toFieldName(String name) {
00148               return name.replaceAll("[^A-Za-z0-9_]", "_");
00149        }
00150 }