Coverage Report - net.sf.snmpadaptor4j.core.JvmSnmpMib
 
Classes in this File Line Coverage Branch Coverage Complexity
JvmSnmpMib
6 %
4/62
0 %
0/6
1,222
 
 1  
 package net.sf.snmpadaptor4j.core;
 2  
 
 3  
 import java.util.Iterator;
 4  
 import java.util.SortedMap;
 5  
 import java.util.TreeMap;
 6  
 import java.util.Map.Entry;
 7  
 import javax.management.MBeanServer;
 8  
 import javax.management.ObjectName;
 9  
 import javax.management.openmbean.CompositeData;
 10  
 import net.sf.snmpadaptor4j.api.SnmpMib;
 11  
 import net.sf.snmpadaptor4j.api.AttributeAccessor;
 12  
 import net.sf.snmpadaptor4j.core.accessor.CompositeDataAttributeAccessor;
 13  
 import net.sf.snmpadaptor4j.core.accessor.JvmAttributeAccessor;
 14  
 import net.sf.snmpadaptor4j.object.SnmpDataType;
 15  
 import net.sf.snmpadaptor4j.object.SnmpOid;
 16  
 
 17  
 /**
 18  
  * Object representing the <b>M</b>anagement <b>I</b>nformation <b>B</b>ase (MIB) for attributes of JVM.
 19  
  * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
 20  
  */
 21  
 public class JvmSnmpMib
 22  
                 implements SnmpMib {
 23  
 
 24  
         /**
 25  
          * Other object representing the MIB.
 26  
          */
 27  
         private final SnmpMib other;
 28  
 
 29  
         /**
 30  
          * <b>M</b>anagement <b>I</b>nformation <b>B</b>ase (MIB).
 31  
          */
 32  7
         private final SortedMap<SnmpOid, AttributeAccessor> mib = new TreeMap<SnmpOid, AttributeAccessor>();
 33  
 
 34  
         /**
 35  
          * Constructor.
 36  
          * @param other Other object representing the MIB.
 37  
          */
 38  
         public JvmSnmpMib (final SnmpMib other) {
 39  7
                 super();
 40  7
                 this.other = other;
 41  7
         }
 42  
 
 43  
         private static final String jvmMgtMIB = "1.3.6.1.4.1.42.2.145.3.163.1";
 44  
 
 45  
         private static final String jvmMgtMIBObjects = jvmMgtMIB + ".1";
 46  
 
 47  
         private static final String jvmMgtMIBNotifications = jvmMgtMIB + ".2";
 48  
 
 49  
         private static final String jvmMgtMIBConformance = jvmMgtMIB + ".3";
 50  
 
 51  
         private static final String jvmClassLoading = jvmMgtMIBObjects + ".1";
 52  
 
 53  
         private static final String jvmMemory = jvmMgtMIBObjects + ".2";
 54  
 
 55  
         private static final String jvmThreading = jvmMgtMIBObjects + ".3";
 56  
 
 57  
         private static final String jvmRuntime = jvmMgtMIBObjects + ".4";
 58  
 
 59  
         private static final String jvmOS = jvmMgtMIBObjects + ".5";
 60  
 
 61  
         /**
 62  
          * Opens the connection with the JMX agent.
 63  
          * @param server JMX agent.
 64  
          * @throws Exception Exception if an error occurred.
 65  
          */
 66  
         public synchronized void open (final MBeanServer server) throws Exception {
 67  
 
 68  
                 // jvmClassesLoadedCount
 69  0
                 putAttribute(server, JvmSnmpMib.jvmClassLoading + ".1.0", SnmpDataType.gauge32, "java.lang:type=ClassLoading", "LoadedClassCount", int.class, true);
 70  
 
 71  
                 // jvmClassesTotalLoadedCount
 72  0
                 putAttribute(server, JvmSnmpMib.jvmClassLoading + ".2.0", SnmpDataType.counter64, "java.lang:type=ClassLoading", "TotalLoadedClassCount", long.class, true);
 73  
 
 74  
                 // jvmClassesUnloadedCount
 75  0
                 putAttribute(server, JvmSnmpMib.jvmClassLoading + ".3.0", SnmpDataType.counter64, "java.lang:type=ClassLoading", "UnloadedClassCount", long.class, true);
 76  
 
 77  
                 // jvmMemoryPendingFinalCount
 78  0
                 putAttribute(server, JvmSnmpMib.jvmMemory + ".1.0", SnmpDataType.gauge32, "java.lang:type=Memory", "ObjectPendingFinalizationCount", int.class, true);
 79  
 
 80  
                 // jvmMemoryHeap
 81  0
                 putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".10.0", "java.lang:type=Memory", "HeapMemoryUsage", "init", SnmpDataType.counter64, long.class);
 82  0
                 putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".11.0", "java.lang:type=Memory", "HeapMemoryUsage", "used", SnmpDataType.counter64, long.class);
 83  0
                 putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".12.0", "java.lang:type=Memory", "HeapMemoryUsage", "committed", SnmpDataType.counter64,
 84  
                                 long.class);
 85  0
                 putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".13.0", "java.lang:type=Memory", "HeapMemoryUsage", "max", SnmpDataType.counter64, long.class);
 86  
 
 87  
                 // jvmMemoryNonHeap
 88  0
                 putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".20.0", "java.lang:type=Memory", "NonHeapMemoryUsage", "init", SnmpDataType.counter64, long.class);
 89  0
                 putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".21.0", "java.lang:type=Memory", "NonHeapMemoryUsage", "used", SnmpDataType.counter64, long.class);
 90  0
                 putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".22.0", "java.lang:type=Memory", "NonHeapMemoryUsage", "committed", SnmpDataType.counter64,
 91  
                                 long.class);
 92  0
                 putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".23.0", "java.lang:type=Memory", "NonHeapMemoryUsage", "max", SnmpDataType.counter64, long.class);
 93  
 
 94  
                 // jvmThreadCount
 95  0
                 putAttribute(server, JvmSnmpMib.jvmThreading + ".1.0", SnmpDataType.gauge32, "java.lang:type=Threading", "ThreadCount", int.class, true);
 96  
 
 97  
                 // jvmThreadDaemonCount
 98  0
                 putAttribute(server, JvmSnmpMib.jvmThreading + ".2.0", SnmpDataType.gauge32, "java.lang:type=Threading", "DaemonThreadCount", int.class, true);
 99  
 
 100  
                 // jvmThreadPeakCount
 101  0
                 putAttribute(server, JvmSnmpMib.jvmThreading + ".3.0", SnmpDataType.counter32, "java.lang:type=Threading", "PeakThreadCount", int.class, true);
 102  
 
 103  
                 // jvmThreadTotalStartedCount
 104  0
                 putAttribute(server, JvmSnmpMib.jvmThreading + ".4.0", SnmpDataType.counter64, "java.lang:type=Threading", "TotalStartedThreadCount", long.class, true);
 105  
 
 106  
                 // jvmThreadContentionMonitoring
 107  0
                 putAttribute(server, JvmSnmpMib.jvmThreading + ".5.0", SnmpDataType.counter64, "java.lang:type=Threading", "TotalStartedThreadCount", long.class, true);
 108  
 
 109  
                 // jvmRTName
 110  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".1.0", SnmpDataType.octetString, "java.lang:type=Runtime", "Name", String.class, true);
 111  
 
 112  
                 // jvmRTVMName
 113  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".2.0", SnmpDataType.octetString, "java.lang:type=Runtime", "VmName", String.class, true);
 114  
 
 115  
                 // jvmRTVMVendor
 116  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".3.0", SnmpDataType.octetString, "java.lang:type=Runtime", "VmVendor", String.class, true);
 117  
 
 118  
                 // jvmRTVMVersion
 119  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".4.0", SnmpDataType.octetString, "java.lang:type=Runtime", "VmVersion", String.class, true);
 120  
 
 121  
                 // jvmRTSpecName
 122  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".5.0", SnmpDataType.octetString, "java.lang:type=Runtime", "SpecName", String.class, true);
 123  
 
 124  
                 // jvmRTSpecVendor
 125  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".6.0", SnmpDataType.octetString, "java.lang:type=Runtime", "SpecVendor", String.class, true);
 126  
 
 127  
                 // jvmRTSpecVersion
 128  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".7.0", SnmpDataType.octetString, "java.lang:type=Runtime", "SpecVersion", String.class, true);
 129  
 
 130  
                 // jvmRTManagementSpecVersion
 131  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".8.0", SnmpDataType.octetString, "java.lang:type=Runtime", "ManagementSpecVersion", String.class, true);
 132  
 
 133  
                 // jvmRTUptimeMs
 134  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".11.0", SnmpDataType.counter64, "java.lang:type=Runtime", "Uptime", long.class, true);
 135  
 
 136  
                 // jvmRTStartTimeMs
 137  0
                 putAttribute(server, JvmSnmpMib.jvmRuntime + ".12.0", SnmpDataType.counter64, "java.lang:type=Runtime", "StartTime", long.class, true);
 138  
 
 139  
                 // jvmOSName
 140  0
                 putAttribute(server, JvmSnmpMib.jvmOS + ".1.0", SnmpDataType.octetString, "java.lang:type=OperatingSystem", "Name", String.class, true);
 141  
 
 142  
                 // jvmOSArch
 143  0
                 putAttribute(server, JvmSnmpMib.jvmOS + ".2.0", SnmpDataType.octetString, "java.lang:type=OperatingSystem", "Arch", String.class, true);
 144  
 
 145  
                 // jvmOSVersion
 146  0
                 putAttribute(server, JvmSnmpMib.jvmOS + ".3.0", SnmpDataType.octetString, "java.lang:type=OperatingSystem", "Version", String.class, true);
 147  
 
 148  
                 // jvmOSProcessorCount
 149  0
                 putAttribute(server, JvmSnmpMib.jvmOS + ".4.0", SnmpDataType.integer32, "java.lang:type=OperatingSystem", "AvailableProcessors", int.class, true);
 150  
 
 151  
                 // TODO
 152  
                 // see http://docs.oracle.com/javase/1.5.0/docs/guide/management/JVM-MANAGEMENT-MIB.mib
 153  
 
 154  0
         }
 155  
 
 156  
         /**
 157  
          * Closes the connection with the JMX agent.
 158  
          */
 159  
         public synchronized void close () {
 160  0
                 this.mib.clear();
 161  0
         }
 162  
 
 163  
         /**
 164  
          * Adds a new JVM attribute.
 165  
          * @param server JMX agent.
 166  
          * @param oidAsStr Object Identifier of attribute as {@link String}.
 167  
          * @param snmpDataType SNMP data type of MIB node.
 168  
          * @param mBeanName MBean name.
 169  
          * @param attributeName Attribute name.
 170  
          * @param jmxDataType Data type of JMX attribute.
 171  
          * @param readonly <code>TRUE</code> if the attribute is read only.
 172  
          * @throws Exception Exception if an error occurred.
 173  
          */
 174  
         public final void putAttribute (final MBeanServer server, final String oidAsStr, final SnmpDataType snmpDataType, final String mBeanName,
 175  
                         final String attributeName, final Class<?> jmxDataType, final boolean readonly) throws Exception {
 176  0
                 final SnmpOid oid = SnmpOid.newInstance(oidAsStr);
 177  0
                 this.mib.put(oid, new JvmAttributeAccessor(server, new ObjectName(mBeanName), oid, attributeName, snmpDataType, jmxDataType, !readonly));
 178  0
         }
 179  
 
 180  
         /**
 181  
          * Adds a new JVM attribute to {@link CompositeData}.
 182  
          * @param server JMX agent.
 183  
          * @param oidAsStr Object Identifier of attribute as {@link String}.
 184  
          * @param mBeanName MBean name.
 185  
          * @param attributeName Attribute name of {@link CompositeData}.
 186  
          * @param attributeKey Attribute key of value.
 187  
          * @param snmpDataType SNMP data type of MIB node.
 188  
          * @param jmxDataType Data type of JMX attribute.
 189  
          * @throws Exception Exception if an error occurred.
 190  
          */
 191  
         public final void putCompositeDataAttribute (final MBeanServer server, final String oidAsStr, final String mBeanName, final String attributeName,
 192  
                         final String attributeKey, final SnmpDataType snmpDataType, final Class<?> jmxDataType) throws Exception {
 193  0
                 final SnmpOid oid = SnmpOid.newInstance(oidAsStr);
 194  0
                 this.mib.put(oid, new CompositeDataAttributeAccessor(oid, server, new ObjectName(mBeanName), attributeName, attributeKey, snmpDataType, jmxDataType));
 195  0
         }
 196  
 
 197  
         /*
 198  
          * {@inheritDoc}
 199  
          * @see net.sf.snmpadaptor4j.api.SnmpMib#find(net.sf.snmpadaptor4j.object.SnmpOid)
 200  
          */
 201  
         public final AttributeAccessor find (final SnmpOid oid) {
 202  
                 AttributeAccessor node;
 203  0
                 synchronized (this.mib) {
 204  0
                         node = this.mib.get(oid);
 205  0
                 }
 206  0
                 if (node == null) {
 207  0
                         node = this.other.find(oid);
 208  
                 }
 209  0
                 return node;
 210  
         }
 211  
 
 212  
         /*
 213  
          * {@inheritDoc}
 214  
          * @see net.sf.snmpadaptor4j.api.SnmpMib#next(net.sf.snmpadaptor4j.object.SnmpOid)
 215  
          */
 216  
         public final AttributeAccessor next (final SnmpOid oid) {
 217  
                 AttributeAccessor node;
 218  0
                 final Iterator<Entry<SnmpOid, AttributeAccessor>> entryIterator = nextSet(oid).entrySet().iterator();
 219  0
                 if (entryIterator.hasNext()) {
 220  0
                         node = entryIterator.next().getValue();
 221  
                 }
 222  
                 else {
 223  0
                         node = null;
 224  
                 }
 225  0
                 return node;
 226  
         }
 227  
 
 228  
         /*
 229  
          * {@inheritDoc}
 230  
          * @see net.sf.snmpadaptor4j.api.SnmpMib#nextSet(net.sf.snmpadaptor4j.object.SnmpOid)
 231  
          */
 232  
         public final SortedMap<SnmpOid, AttributeAccessor> nextSet (final SnmpOid oid) {
 233  0
                 final SortedMap<SnmpOid, AttributeAccessor> nodeMap = new TreeMap<SnmpOid, AttributeAccessor>();
 234  0
                 synchronized (this.mib) {
 235  0
                         nodeMap.putAll(this.mib.tailMap(SnmpOid.newInstance(oid.getOid(), 0)));
 236  0
                 }
 237  0
                 nodeMap.putAll(this.other.nextSet(oid));
 238  0
                 return nodeMap;
 239  
         }
 240  
 
 241  
         /*
 242  
          * {@inheritDoc}
 243  
          * @see java.lang.Object#toString()
 244  
          */
 245  
         @Override
 246  
         public final String toString () {
 247  0
                 return "JvmSnmpMib[" + this.mib.values() + "]";
 248  
         }
 249  
 
 250  
 }