View Javadoc

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  	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  		super();
40  		this.other = other;
41  	}
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  		putAttribute(server, JvmSnmpMib.jvmClassLoading + ".1.0", SnmpDataType.gauge32, "java.lang:type=ClassLoading", "LoadedClassCount", int.class, true);
70  
71  		// jvmClassesTotalLoadedCount
72  		putAttribute(server, JvmSnmpMib.jvmClassLoading + ".2.0", SnmpDataType.counter64, "java.lang:type=ClassLoading", "TotalLoadedClassCount", long.class, true);
73  
74  		// jvmClassesUnloadedCount
75  		putAttribute(server, JvmSnmpMib.jvmClassLoading + ".3.0", SnmpDataType.counter64, "java.lang:type=ClassLoading", "UnloadedClassCount", long.class, true);
76  
77  		// jvmMemoryPendingFinalCount
78  		putAttribute(server, JvmSnmpMib.jvmMemory + ".1.0", SnmpDataType.gauge32, "java.lang:type=Memory", "ObjectPendingFinalizationCount", int.class, true);
79  
80  		// jvmMemoryHeap
81  		putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".10.0", "java.lang:type=Memory", "HeapMemoryUsage", "init", SnmpDataType.counter64, long.class);
82  		putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".11.0", "java.lang:type=Memory", "HeapMemoryUsage", "used", SnmpDataType.counter64, long.class);
83  		putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".12.0", "java.lang:type=Memory", "HeapMemoryUsage", "committed", SnmpDataType.counter64,
84  				long.class);
85  		putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".13.0", "java.lang:type=Memory", "HeapMemoryUsage", "max", SnmpDataType.counter64, long.class);
86  
87  		// jvmMemoryNonHeap
88  		putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".20.0", "java.lang:type=Memory", "NonHeapMemoryUsage", "init", SnmpDataType.counter64, long.class);
89  		putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".21.0", "java.lang:type=Memory", "NonHeapMemoryUsage", "used", SnmpDataType.counter64, long.class);
90  		putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".22.0", "java.lang:type=Memory", "NonHeapMemoryUsage", "committed", SnmpDataType.counter64,
91  				long.class);
92  		putCompositeDataAttribute(server, JvmSnmpMib.jvmMemory + ".23.0", "java.lang:type=Memory", "NonHeapMemoryUsage", "max", SnmpDataType.counter64, long.class);
93  
94  		// jvmThreadCount
95  		putAttribute(server, JvmSnmpMib.jvmThreading + ".1.0", SnmpDataType.gauge32, "java.lang:type=Threading", "ThreadCount", int.class, true);
96  
97  		// jvmThreadDaemonCount
98  		putAttribute(server, JvmSnmpMib.jvmThreading + ".2.0", SnmpDataType.gauge32, "java.lang:type=Threading", "DaemonThreadCount", int.class, true);
99  
100 		// jvmThreadPeakCount
101 		putAttribute(server, JvmSnmpMib.jvmThreading + ".3.0", SnmpDataType.counter32, "java.lang:type=Threading", "PeakThreadCount", int.class, true);
102 
103 		// jvmThreadTotalStartedCount
104 		putAttribute(server, JvmSnmpMib.jvmThreading + ".4.0", SnmpDataType.counter64, "java.lang:type=Threading", "TotalStartedThreadCount", long.class, true);
105 
106 		// jvmThreadContentionMonitoring
107 		putAttribute(server, JvmSnmpMib.jvmThreading + ".5.0", SnmpDataType.counter64, "java.lang:type=Threading", "TotalStartedThreadCount", long.class, true);
108 
109 		// jvmRTName
110 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".1.0", SnmpDataType.octetString, "java.lang:type=Runtime", "Name", String.class, true);
111 
112 		// jvmRTVMName
113 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".2.0", SnmpDataType.octetString, "java.lang:type=Runtime", "VmName", String.class, true);
114 
115 		// jvmRTVMVendor
116 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".3.0", SnmpDataType.octetString, "java.lang:type=Runtime", "VmVendor", String.class, true);
117 
118 		// jvmRTVMVersion
119 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".4.0", SnmpDataType.octetString, "java.lang:type=Runtime", "VmVersion", String.class, true);
120 
121 		// jvmRTSpecName
122 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".5.0", SnmpDataType.octetString, "java.lang:type=Runtime", "SpecName", String.class, true);
123 
124 		// jvmRTSpecVendor
125 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".6.0", SnmpDataType.octetString, "java.lang:type=Runtime", "SpecVendor", String.class, true);
126 
127 		// jvmRTSpecVersion
128 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".7.0", SnmpDataType.octetString, "java.lang:type=Runtime", "SpecVersion", String.class, true);
129 
130 		// jvmRTManagementSpecVersion
131 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".8.0", SnmpDataType.octetString, "java.lang:type=Runtime", "ManagementSpecVersion", String.class, true);
132 
133 		// jvmRTUptimeMs
134 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".11.0", SnmpDataType.counter64, "java.lang:type=Runtime", "Uptime", long.class, true);
135 
136 		// jvmRTStartTimeMs
137 		putAttribute(server, JvmSnmpMib.jvmRuntime + ".12.0", SnmpDataType.counter64, "java.lang:type=Runtime", "StartTime", long.class, true);
138 
139 		// jvmOSName
140 		putAttribute(server, JvmSnmpMib.jvmOS + ".1.0", SnmpDataType.octetString, "java.lang:type=OperatingSystem", "Name", String.class, true);
141 
142 		// jvmOSArch
143 		putAttribute(server, JvmSnmpMib.jvmOS + ".2.0", SnmpDataType.octetString, "java.lang:type=OperatingSystem", "Arch", String.class, true);
144 
145 		// jvmOSVersion
146 		putAttribute(server, JvmSnmpMib.jvmOS + ".3.0", SnmpDataType.octetString, "java.lang:type=OperatingSystem", "Version", String.class, true);
147 
148 		// jvmOSProcessorCount
149 		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 	}
155 
156 	/**
157 	 * Closes the connection with the JMX agent.
158 	 */
159 	public synchronized void close () {
160 		this.mib.clear();
161 	}
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 		final SnmpOid oid = SnmpOid.newInstance(oidAsStr);
177 		this.mib.put(oid, new JvmAttributeAccessor(server, new ObjectName(mBeanName), oid, attributeName, snmpDataType, jmxDataType, !readonly));
178 	}
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 		final SnmpOid oid = SnmpOid.newInstance(oidAsStr);
194 		this.mib.put(oid, new CompositeDataAttributeAccessor(oid, server, new ObjectName(mBeanName), attributeName, attributeKey, snmpDataType, jmxDataType));
195 	}
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 		synchronized (this.mib) {
204 			node = this.mib.get(oid);
205 		}
206 		if (node == null) {
207 			node = this.other.find(oid);
208 		}
209 		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 		final Iterator<Entry<SnmpOid, AttributeAccessor>> entryIterator = nextSet(oid).entrySet().iterator();
219 		if (entryIterator.hasNext()) {
220 			node = entryIterator.next().getValue();
221 		}
222 		else {
223 			node = null;
224 		}
225 		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 		final SortedMap<SnmpOid, AttributeAccessor> nodeMap = new TreeMap<SnmpOid, AttributeAccessor>();
234 		synchronized (this.mib) {
235 			nodeMap.putAll(this.mib.tailMap(SnmpOid.newInstance(oid.getOid(), 0)));
236 		}
237 		nodeMap.putAll(this.other.nextSet(oid));
238 		return nodeMap;
239 	}
240 
241 	/*
242 	 * {@inheritDoc}
243 	 * @see java.lang.Object#toString()
244 	 */
245 	@Override
246 	public final String toString () {
247 		return "JvmSnmpMib[" + this.mib.values() + "]";
248 	}
249 
250 }