Back to index

munin  2.0.1
AbstractMultiGraphsProvider.java
Go to the documentation of this file.
00001 package org.munin.plugin.jmx;
00002 
00003 import java.io.PrintWriter;
00004 import java.io.StringWriter;
00005 import java.util.Collections;
00006 import java.util.LinkedHashSet;
00007 import java.util.Map;
00008 import java.util.Set;
00009 import java.util.regex.Matcher;
00010 import java.util.regex.Pattern;
00011 
00016 public abstract class AbstractMultiGraphsProvider extends
00017               AbstractGraphsProvider {
00018        private String prefix = "";
00019 
00020        protected AbstractMultiGraphsProvider(Config config) {
00021               super(config);
00022               setPrefix(config.getPrefix());
00023        }
00024 
00025        protected abstract Map<String, AbstractGraphsProvider> getProviders();
00026 
00027        public String getPrefix() {
00028               return prefix;
00029        }
00030 
00031        public void setPrefix(String prefix) {
00032               this.prefix = prefix;
00033        }
00034 
00035        @Override
00036        public void printConfig(PrintWriter out) {
00037               Map<String, AbstractGraphsProvider> providers = getProviders();
00038               for (Map.Entry<String, AbstractGraphsProvider> entry : providers
00039                             .entrySet()) {
00040                      out.println("multigraph " + getPrefix() + entry.getKey());
00041                      entry.getValue().printConfig(out);
00042                      out.println();
00043               }
00044        }
00045 
00046        @Override
00047        public void printValues(PrintWriter out) {
00048               Map<String, AbstractGraphsProvider> providers = getProviders();
00049               for (Map.Entry<String, AbstractGraphsProvider> entry : providers
00050                             .entrySet()) {
00051                      StringWriter sw = new StringWriter();
00052                      PrintWriter pw = new PrintWriter(sw);
00053                      entry.getValue().printValues(pw);
00054                      pw.close();
00055                      String values = sw.toString().trim();
00056                      if (values.length() != 0) {
00057                             out.println("multigraph " + getPrefix() + entry.getKey());
00058                             out.println(values);
00059                             out.println();
00060                      }
00061               }
00062        }
00063 
00081        protected void addWithAlias(Map<String, AbstractGraphsProvider> providers,
00082                      AbstractGraphsProvider provider, String dataName, String... aliases) {
00083               providers.put(dataName, noGraph(provider));
00084               AbstractGraphsProvider aliasProvider = aliasFor(provider, dataName);
00085               for (String alias : aliases) {
00086                      providers.put(alias, aliasProvider);
00087               }
00088        }
00089 
00095        public AbstractMultiGraphsProvider multigraph(final String name,
00096                      final AbstractGraphsProvider provider) {
00097               return multigraph(name, provider, config);
00098        }
00099 
00100        public static AbstractMultiGraphsProvider multigraph(final String name,
00101                      final AbstractGraphsProvider provider, Config config) {
00102               return new AbstractMultiGraphsProvider(config) {
00103                      @Override
00104                      protected Map<String, AbstractGraphsProvider> getProviders() {
00105                             return Collections.singletonMap(name, provider);
00106                      }
00107               };
00108        }
00109 
00114        public AbstractGraphsProvider noGraph(final AbstractGraphsProvider provider) {
00115               return noGraph(provider, config);
00116        }
00117 
00118        public static AbstractGraphsProvider noGraph(
00119                      final AbstractGraphsProvider provider, Config config) {
00120               return new AbstractGraphsProvider(config) {
00121 
00122                      @Override
00123                      public void printConfig(PrintWriter out) {
00124                             provider.printConfig(out);
00125                             out.println("graph no");
00126                      }
00127 
00128                      @Override
00129                      public void printValues(PrintWriter out) {
00130                             provider.printValues(out);
00131                      }
00132               };
00133        }
00134 
00145        public AbstractGraphsProvider aliasFor(
00146                      final AbstractGraphsProvider provider, final String origName) {
00147               return aliasFor(provider, getPrefix() + origName, config);
00148        }
00149 
00150        private static final Pattern FIELD_CONFIG_PATTERN = Pattern.compile(
00151                      "^([a-zA-Z0-9_]+)\\.", Pattern.MULTILINE);
00152        private static final Pattern GRAPH_ORDER_PATTERN = Pattern.compile(
00153                      "^graph_order .*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
00154 
00167        public static AbstractGraphsProvider aliasFor(
00168                      final AbstractGraphsProvider provider, final String origName,
00169                      final Config config) {
00170               return new AbstractGraphsProvider(config) {
00171 
00172                      @Override
00173                      public void printConfig(PrintWriter out) {
00174                             StringWriter stringWriter = new StringWriter();
00175                             provider.printConfig(new PrintWriter(stringWriter));
00176                             String config = stringWriter.toString();
00177                             Set<String> fields = new LinkedHashSet<String>();
00178                             String graphConfig = null;
00179                             String fieldConfig = null;
00180                             // remove existing graph_order directive
00181                             config = GRAPH_ORDER_PATTERN.matcher(config).replaceAll("");
00182                             Matcher matcher = FIELD_CONFIG_PATTERN.matcher(config);
00183                             while (matcher.find()) {
00184                                    if (graphConfig == null) {
00185                                           graphConfig = config.substring(0, matcher.start())
00186                                                         .replaceFirst("\n+$", "");
00187                                           fieldConfig = config.substring(matcher.start());
00188                                    }
00189                                    fields.add(matcher.group(1));
00190                             }
00191                             out.println(graphConfig);
00192                             out.print("graph_order");
00193                             for (String field : fields) {
00194                                    out.print(" " + field + "=" + origName + "." + field);
00195                             }
00196                             out.println();
00197                             out.println("update no");
00198                             out.println(fieldConfig);
00199                      }
00200 
00201                      @Override
00202                      public void printValues(PrintWriter out) {
00203                             // don't print values, as we've got "update no" set
00204                      }
00205               };
00206        }
00207 }