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
19
20
21 public class JvmSnmpMib
22 implements SnmpMib {
23
24
25
26
27 private final SnmpMib other;
28
29
30
31
32 private final SortedMap<SnmpOid, AttributeAccessor> mib = new TreeMap<SnmpOid, AttributeAccessor>();
33
34
35
36
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
63
64
65
66 public synchronized void open (final MBeanServer server) throws Exception {
67
68
69 putAttribute(server, JvmSnmpMib.jvmClassLoading + ".1.0", SnmpDataType.gauge32, "java.lang:type=ClassLoading", "LoadedClassCount", int.class, true);
70
71
72 putAttribute(server, JvmSnmpMib.jvmClassLoading + ".2.0", SnmpDataType.counter64, "java.lang:type=ClassLoading", "TotalLoadedClassCount", long.class, true);
73
74
75 putAttribute(server, JvmSnmpMib.jvmClassLoading + ".3.0", SnmpDataType.counter64, "java.lang:type=ClassLoading", "UnloadedClassCount", long.class, true);
76
77
78 putAttribute(server, JvmSnmpMib.jvmMemory + ".1.0", SnmpDataType.gauge32, "java.lang:type=Memory", "ObjectPendingFinalizationCount", int.class, true);
79
80
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
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
95 putAttribute(server, JvmSnmpMib.jvmThreading + ".1.0", SnmpDataType.gauge32, "java.lang:type=Threading", "ThreadCount", int.class, true);
96
97
98 putAttribute(server, JvmSnmpMib.jvmThreading + ".2.0", SnmpDataType.gauge32, "java.lang:type=Threading", "DaemonThreadCount", int.class, true);
99
100
101 putAttribute(server, JvmSnmpMib.jvmThreading + ".3.0", SnmpDataType.counter32, "java.lang:type=Threading", "PeakThreadCount", int.class, true);
102
103
104 putAttribute(server, JvmSnmpMib.jvmThreading + ".4.0", SnmpDataType.counter64, "java.lang:type=Threading", "TotalStartedThreadCount", long.class, true);
105
106
107 putAttribute(server, JvmSnmpMib.jvmThreading + ".5.0", SnmpDataType.counter64, "java.lang:type=Threading", "TotalStartedThreadCount", long.class, true);
108
109
110 putAttribute(server, JvmSnmpMib.jvmRuntime + ".1.0", SnmpDataType.octetString, "java.lang:type=Runtime", "Name", String.class, true);
111
112
113 putAttribute(server, JvmSnmpMib.jvmRuntime + ".2.0", SnmpDataType.octetString, "java.lang:type=Runtime", "VmName", String.class, true);
114
115
116 putAttribute(server, JvmSnmpMib.jvmRuntime + ".3.0", SnmpDataType.octetString, "java.lang:type=Runtime", "VmVendor", String.class, true);
117
118
119 putAttribute(server, JvmSnmpMib.jvmRuntime + ".4.0", SnmpDataType.octetString, "java.lang:type=Runtime", "VmVersion", String.class, true);
120
121
122 putAttribute(server, JvmSnmpMib.jvmRuntime + ".5.0", SnmpDataType.octetString, "java.lang:type=Runtime", "SpecName", String.class, true);
123
124
125 putAttribute(server, JvmSnmpMib.jvmRuntime + ".6.0", SnmpDataType.octetString, "java.lang:type=Runtime", "SpecVendor", String.class, true);
126
127
128 putAttribute(server, JvmSnmpMib.jvmRuntime + ".7.0", SnmpDataType.octetString, "java.lang:type=Runtime", "SpecVersion", String.class, true);
129
130
131 putAttribute(server, JvmSnmpMib.jvmRuntime + ".8.0", SnmpDataType.octetString, "java.lang:type=Runtime", "ManagementSpecVersion", String.class, true);
132
133
134 putAttribute(server, JvmSnmpMib.jvmRuntime + ".11.0", SnmpDataType.counter64, "java.lang:type=Runtime", "Uptime", long.class, true);
135
136
137 putAttribute(server, JvmSnmpMib.jvmRuntime + ".12.0", SnmpDataType.counter64, "java.lang:type=Runtime", "StartTime", long.class, true);
138
139
140 putAttribute(server, JvmSnmpMib.jvmOS + ".1.0", SnmpDataType.octetString, "java.lang:type=OperatingSystem", "Name", String.class, true);
141
142
143 putAttribute(server, JvmSnmpMib.jvmOS + ".2.0", SnmpDataType.octetString, "java.lang:type=OperatingSystem", "Arch", String.class, true);
144
145
146 putAttribute(server, JvmSnmpMib.jvmOS + ".3.0", SnmpDataType.octetString, "java.lang:type=OperatingSystem", "Version", String.class, true);
147
148
149 putAttribute(server, JvmSnmpMib.jvmOS + ".4.0", SnmpDataType.integer32, "java.lang:type=OperatingSystem", "AvailableProcessors", int.class, true);
150
151
152
153
154 }
155
156
157
158
159 public synchronized void close () {
160 this.mib.clear();
161 }
162
163
164
165
166
167
168
169
170
171
172
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
182
183
184
185
186
187
188
189
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
199
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
214
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
230
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
243
244
245 @Override
246 public final String toString () {
247 return "JvmSnmpMib[" + this.mib.values() + "]";
248 }
249
250 }