Back to index

munin  2.0.1
MultigraphMemory.java
Go to the documentation of this file.
00001 package org.munin.plugin.jmx;
00002 
00003 import java.io.IOException;
00004 import java.lang.management.ManagementFactory;
00005 import java.util.LinkedHashMap;
00006 import java.util.Map;
00007 import java.util.Set;
00008 import java.util.TreeSet;
00009 
00010 import javax.management.MalformedObjectNameException;
00011 import javax.management.ObjectName;
00012 
00013 import org.munin.plugin.jmx.AbstractMemoryUsageProvider.UsageType;
00014 
00015 public class MultigraphMemory extends AbstractMultiGraphsProvider {
00016        private static final String PREFIX = "_memory";
00017 
00018        public MultigraphMemory(Config config) {
00019               super(config);
00020               // can't set the PREFIX itself, because we also need to use config
00021               // without that prefix for the legacy data storage graphs
00022        }
00023 
00024        @Override
00025        protected Map<String, AbstractGraphsProvider> getProviders() {
00026               Map<String, AbstractGraphsProvider> providers = new LinkedHashMap<String, AbstractGraphsProvider>();
00027               providers.put(PREFIX, new MemoryAllocatedTotal(config));
00028               addWithAlias(providers, new MemoryAllocatedHeap(config),
00029                             "_MemoryAllocatedHeap", PREFIX + ".heap");
00030               addWithAlias(providers, new MemoryAllocatedNonHeap(config),
00031                             "_MemoryAllocatedNonHeap", PREFIX + ".non_heap");
00032               addWithAlias(providers, new GarbageCollectionInfo.Time(config),
00033                             "_GCTime", PREFIX + ".gc_time");
00034               addWithAlias(providers, new GarbageCollectionInfo.Count(config),
00035                             "_GCCount", PREFIX + ".gc_count");
00036 
00037               try {
00038                      addPoolProviders(providers);
00039               } catch (Exception e) {
00040                      e.printStackTrace();
00041               }
00042 
00043               return providers;
00044        }
00045 
00046        private void addPoolProviders(Map<String, AbstractGraphsProvider> providers)
00047                      throws MalformedObjectNameException, IOException {
00048               ObjectName gcName = new ObjectName(
00049                             ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*");
00050 
00051               // ensure memory pools are sorted consistently
00052               Set<ObjectName> mbeans = new TreeSet<ObjectName>(getConnection()
00053                             .queryNames(gcName, null));
00054               for (ObjectName objName : mbeans) {
00055                      String poolName = objName.getKeyProperty("name");
00056 
00057                      MemoryPoolUsageProvider usage = new MemoryPoolUsageProvider(config,
00058                                    objName, UsageType.USAGE);
00059                      MemoryPoolUsageProvider peak = new MemoryPoolUsageProvider(config,
00060                                    objName, UsageType.PEAK);
00061                      MemoryPoolUsageProvider postGC = new MemoryPoolUsageProvider(
00062                                    config, objName, UsageType.POST_GC);
00063 
00064                      String legacyKey = "_" + getLegacyPoolKey(poolName);
00065                      String key = PREFIX + ".pool_" + toFieldName(poolName);
00066                      addWithAlias(providers, usage, legacyKey + "Usage", key, key
00067                                    + ".usage");
00068                      addWithAlias(providers, peak, legacyKey + "Peak", key + ".peak");
00069                      addWithAlias(providers, postGC, legacyKey + "UsagePostGC", key
00070                                    + ".postGC");
00071               }
00072        }
00073 
00074        private String getLegacyPoolKey(final String poolName) {
00075               LegacyPool legacyPool = LegacyPool.getLegacyPool(poolName);
00076               String poolKey;
00077               if (legacyPool != null) {
00078                      poolKey = legacyPool.getName();
00079               } else {
00080                      poolKey = toFieldName(poolName);
00081               }
00082               return "Memory" + poolKey;
00083        }
00084 
00085        public static void main(String[] args) {
00086               runGraph(args);
00087        }
00088 }