author | mchung |
Tue, 17 Jan 2012 15:55:40 -0800 | |
changeset 11530 | a9d059c15b80 |
parent 5506 | 202f599c92aa |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.management.snmp; |
|
27 |
||
28 |
import com.sun.jmx.snmp.daemon.SnmpAdaptorServer; |
|
29 |
import com.sun.jmx.snmp.InetAddressAcl; |
|
30 |
import com.sun.jmx.snmp.IPAcl.SnmpAcl; |
|
31 |
import sun.management.snmp.jvmmib.JVM_MANAGEMENT_MIB; |
|
32 |
import sun.management.snmp.jvminstr.JVM_MANAGEMENT_MIB_IMPL; |
|
33 |
import sun.management.snmp.jvminstr.NotificationTarget; |
|
34 |
import sun.management.snmp.jvminstr.NotificationTargetImpl; |
|
35 |
import sun.management.snmp.util.MibLogger; |
|
36 |
import sun.management.snmp.util.JvmContextFactory; |
|
37 |
||
38 |
import sun.management.Agent; |
|
39 |
import sun.management.AgentConfigurationError; |
|
40 |
import static sun.management.AgentConfigurationError.*; |
|
41 |
import sun.management.FileSystem; |
|
42 |
||
43 |
import java.util.List; |
|
44 |
import java.util.ArrayList; |
|
45 |
import java.util.Enumeration; |
|
46 |
import java.util.Properties; |
|
47 |
||
48 |
import java.io.IOException; |
|
49 |
import java.io.File; |
|
50 |
import java.io.FileInputStream; |
|
51 |
||
52 |
import java.net.InetAddress; |
|
53 |
import java.net.UnknownHostException; |
|
54 |
||
55 |
/** |
|
56 |
* This class initializes and starts the SNMP Adaptor for JSR 163 SNMP |
|
57 |
* Monitoring. |
|
58 |
**/ |
|
59 |
public final class AdaptorBootstrap { |
|
60 |
||
61 |
private static final MibLogger log = new MibLogger(AdaptorBootstrap.class); |
|
62 |
||
63 |
/** |
|
64 |
* Default values for SNMP configuration properties. |
|
65 |
**/ |
|
66 |
public static interface DefaultValues { |
|
67 |
public static final String PORT="161"; |
|
68 |
public static final String CONFIG_FILE_NAME="management.properties"; |
|
69 |
public static final String TRAP_PORT="162"; |
|
70 |
public static final String USE_ACL="true"; |
|
71 |
public static final String ACL_FILE_NAME="snmp.acl"; |
|
72 |
public static final String BIND_ADDRESS="localhost"; |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Names of SNMP configuration properties. |
|
77 |
**/ |
|
78 |
public static interface PropertyNames { |
|
79 |
public static final String PORT="com.sun.management.snmp.port"; |
|
80 |
public static final String CONFIG_FILE_NAME= |
|
81 |
"com.sun.management.config.file"; |
|
82 |
public static final String TRAP_PORT= |
|
83 |
"com.sun.management.snmp.trap"; |
|
84 |
public static final String USE_ACL= |
|
85 |
"com.sun.management.snmp.acl"; |
|
86 |
public static final String ACL_FILE_NAME= |
|
87 |
"com.sun.management.snmp.acl.file"; |
|
88 |
public static final String BIND_ADDRESS= |
|
89 |
"com.sun.management.snmp.interface"; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* We keep a reference - so that we can possibly call |
|
94 |
* terminate(). As of now, terminate() is only called by unit tests |
|
95 |
* (makes it possible to run several testcases sequentially in the |
|
96 |
* same JVM). |
|
97 |
**/ |
|
98 |
private SnmpAdaptorServer adaptor; |
|
99 |
private JVM_MANAGEMENT_MIB_IMPL jvmmib; |
|
100 |
||
101 |
private AdaptorBootstrap(SnmpAdaptorServer snmpas, |
|
102 |
JVM_MANAGEMENT_MIB_IMPL mib) { |
|
103 |
jvmmib = mib; |
|
104 |
adaptor = snmpas; |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Compute the full path name for a default file. |
|
109 |
* @param basename basename (with extension) of the default file. |
|
110 |
* @return ${JRE}/lib/management/${basename} |
|
111 |
**/ |
|
112 |
private static String getDefaultFileName(String basename) { |
|
113 |
final String fileSeparator = File.separator; |
|
114 |
return System.getProperty("java.home") + fileSeparator + "lib" + |
|
115 |
fileSeparator + "management" + fileSeparator + basename; |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Retrieve the Trap Target List from the ACL file. |
|
120 |
**/ |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
121 |
@SuppressWarnings("unchecked") |
2 | 122 |
private static List<NotificationTarget> getTargetList(InetAddressAcl acl, |
123 |
int defaultTrapPort) { |
|
124 |
final ArrayList<NotificationTarget> result = |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
125 |
new ArrayList<>(); |
2 | 126 |
if (acl != null) { |
127 |
if (log.isDebugOn()) |
|
128 |
log.debug("getTargetList",Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.processing")); |
|
129 |
||
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
130 |
final Enumeration td = acl.getTrapDestinations(); |
2 | 131 |
for (; td.hasMoreElements() ;) { |
132 |
final InetAddress targetAddr = (InetAddress)td.nextElement(); |
|
133 |
final Enumeration tc = |
|
134 |
acl.getTrapCommunities(targetAddr); |
|
135 |
for (;tc.hasMoreElements() ;) { |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
136 |
final String community = (String)tc.nextElement(); |
2 | 137 |
final NotificationTarget target = |
138 |
new NotificationTargetImpl(targetAddr, |
|
139 |
defaultTrapPort, |
|
140 |
community); |
|
141 |
if (log.isDebugOn()) |
|
142 |
log.debug("getTargetList", |
|
143 |
Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.adding", |
|
144 |
target.toString())); |
|
145 |
result.add(target); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
return result; |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Initializes and starts the SNMP Adaptor Server. |
|
154 |
* If the com.sun.management.snmp.port property is not defined, |
|
155 |
* simply return. Otherwise, attempts to load the config file, and |
|
156 |
* then calls {@link #initialize(java.lang.String, java.util.Properties)}. |
|
157 |
* |
|
158 |
**/ |
|
159 |
public static synchronized AdaptorBootstrap initialize() { |
|
160 |
||
161 |
// Load a new properties |
|
162 |
final Properties props = Agent.loadManagementProperties(); |
|
163 |
if (props == null) return null; |
|
164 |
||
165 |
final String portStr = props.getProperty(PropertyNames.PORT); |
|
166 |
||
167 |
return initialize(portStr,props); |
|
168 |
} |
|
169 |
||
170 |
/** |
|
171 |
* Initializes and starts the SNMP Adaptor Server. |
|
172 |
**/ |
|
173 |
public static synchronized |
|
174 |
AdaptorBootstrap initialize(String portStr, Properties props) { |
|
175 |
||
176 |
// Get port number |
|
177 |
if (portStr.length()==0) portStr=DefaultValues.PORT; |
|
178 |
final int port; |
|
179 |
try { |
|
180 |
port = Integer.parseInt(portStr); |
|
181 |
} catch (NumberFormatException x) { |
|
182 |
throw new AgentConfigurationError(INVALID_SNMP_PORT, x, portStr); |
|
183 |
} |
|
184 |
||
185 |
if (port < 0) { |
|
186 |
throw new AgentConfigurationError(INVALID_SNMP_PORT, portStr); |
|
187 |
} |
|
188 |
||
189 |
// Get trap port number |
|
190 |
final String trapPortStr = |
|
191 |
props.getProperty(PropertyNames.TRAP_PORT, |
|
192 |
DefaultValues.TRAP_PORT); |
|
193 |
||
194 |
final int trapPort; |
|
195 |
try { |
|
196 |
trapPort = Integer.parseInt(trapPortStr); |
|
197 |
} catch (NumberFormatException x) { |
|
198 |
throw new AgentConfigurationError(INVALID_SNMP_TRAP_PORT, x, trapPortStr); |
|
199 |
} |
|
200 |
||
201 |
if (trapPort < 0) { |
|
202 |
throw new AgentConfigurationError(INVALID_SNMP_TRAP_PORT, trapPortStr); |
|
203 |
} |
|
204 |
||
205 |
// Get bind address |
|
206 |
final String addrStr = |
|
207 |
props.getProperty(PropertyNames.BIND_ADDRESS, |
|
208 |
DefaultValues.BIND_ADDRESS); |
|
209 |
||
210 |
// Get ACL File |
|
211 |
final String defaultAclFileName = |
|
212 |
getDefaultFileName(DefaultValues.ACL_FILE_NAME); |
|
213 |
final String aclFileName = |
|
214 |
props.getProperty(PropertyNames.ACL_FILE_NAME, |
|
215 |
defaultAclFileName); |
|
216 |
final String useAclStr = |
|
217 |
props.getProperty(PropertyNames.USE_ACL,DefaultValues.USE_ACL); |
|
218 |
final boolean useAcl = |
|
219 |
Boolean.valueOf(useAclStr).booleanValue(); |
|
220 |
||
221 |
if (useAcl) checkAclFile(aclFileName); |
|
222 |
||
223 |
AdaptorBootstrap adaptor = null; |
|
224 |
try { |
|
225 |
adaptor = getAdaptorBootstrap(port, trapPort, addrStr, |
|
226 |
useAcl, aclFileName); |
|
227 |
} catch (Exception e) { |
|
228 |
throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.getMessage()); |
|
229 |
} |
|
230 |
return adaptor; |
|
231 |
} |
|
232 |
||
233 |
private static AdaptorBootstrap getAdaptorBootstrap |
|
234 |
(int port, int trapPort, String bindAddress, boolean useAcl, |
|
235 |
String aclFileName) { |
|
236 |
||
237 |
final InetAddress address; |
|
238 |
try { |
|
239 |
address = InetAddress.getByName(bindAddress); |
|
240 |
} catch (UnknownHostException e) { |
|
241 |
throw new AgentConfigurationError(UNKNOWN_SNMP_INTERFACE, e, bindAddress); |
|
242 |
} |
|
243 |
if (log.isDebugOn()) { |
|
244 |
log.debug("initialize", |
|
245 |
Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.starting" + |
|
246 |
"\n\t" + PropertyNames.PORT + "=" + port + |
|
247 |
"\n\t" + PropertyNames.TRAP_PORT + "=" + trapPort + |
|
248 |
"\n\t" + PropertyNames.BIND_ADDRESS + "=" + address + |
|
249 |
(useAcl?("\n\t" + PropertyNames.ACL_FILE_NAME + "=" |
|
250 |
+ aclFileName):"\n\tNo ACL")+ |
|
251 |
"")); |
|
252 |
} |
|
253 |
||
254 |
final InetAddressAcl acl; |
|
255 |
try { |
|
256 |
acl = useAcl ? new SnmpAcl(System.getProperty("user.name"),aclFileName) |
|
257 |
: null; |
|
258 |
} catch (UnknownHostException e) { |
|
259 |
throw new AgentConfigurationError(UNKNOWN_SNMP_INTERFACE, e, e.getMessage()); |
|
260 |
} |
|
261 |
||
262 |
// Create adaptor |
|
263 |
final SnmpAdaptorServer adaptor = |
|
264 |
new SnmpAdaptorServer(acl, port, address); |
|
265 |
adaptor.setUserDataFactory(new JvmContextFactory()); |
|
266 |
adaptor.setTrapPort(trapPort); |
|
267 |
||
268 |
// Create MIB |
|
269 |
// |
|
270 |
final JVM_MANAGEMENT_MIB_IMPL mib = new JVM_MANAGEMENT_MIB_IMPL(); |
|
271 |
try { |
|
272 |
mib.init(); |
|
273 |
} catch (IllegalAccessException x) { |
|
274 |
throw new AgentConfigurationError(SNMP_MIB_INIT_FAILED, x, x.getMessage()); |
|
275 |
} |
|
276 |
||
277 |
// Configure the trap destinations. |
|
278 |
// |
|
279 |
mib.addTargets(getTargetList(acl,trapPort)); |
|
280 |
||
281 |
||
282 |
// Start Adaptor |
|
283 |
// |
|
284 |
try { |
|
285 |
// Will wait until the adaptor starts or fails to start. |
|
286 |
// If the adaptor fails to start, a CommunicationException or |
|
287 |
// an InterruptedException is thrown. |
|
288 |
// |
|
289 |
adaptor.start(Long.MAX_VALUE); |
|
290 |
} catch (Exception x) { |
|
291 |
Throwable t=x; |
|
292 |
if (x instanceof com.sun.jmx.snmp.daemon.CommunicationException) { |
|
293 |
final Throwable next = t.getCause(); |
|
294 |
if (next != null) t = next; |
|
295 |
} |
|
296 |
throw new AgentConfigurationError(SNMP_ADAPTOR_START_FAILED, t, |
|
297 |
address + ":" + port, |
|
298 |
"(" + t.getMessage() + ")"); |
|
299 |
} |
|
300 |
||
301 |
// double check that adaptor is actually started (should always |
|
302 |
// be active, so that exception should never be thrown from here) |
|
303 |
// |
|
304 |
if (!adaptor.isActive()) { |
|
305 |
throw new AgentConfigurationError(SNMP_ADAPTOR_START_FAILED, |
|
306 |
address + ":" + port); |
|
307 |
} |
|
308 |
||
309 |
try { |
|
310 |
// Add MIB to adaptor |
|
311 |
// |
|
312 |
adaptor.addMib(mib); |
|
313 |
||
314 |
// Add Adaptor to the MIB |
|
315 |
// |
|
316 |
mib.setSnmpAdaptor(adaptor); |
|
317 |
} catch (RuntimeException x) { |
|
318 |
new AdaptorBootstrap(adaptor,mib).terminate(); |
|
319 |
throw x; |
|
320 |
} |
|
321 |
||
322 |
log.debug("initialize", |
|
323 |
Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.initialize1")); |
|
324 |
log.config("initialize", |
|
325 |
Agent.getText("jmxremote.AdaptorBootstrap.getTargetList.initialize2", |
|
326 |
address.toString(), java.lang.Integer.toString(adaptor.getPort()))); |
|
327 |
return new AdaptorBootstrap(adaptor,mib); |
|
328 |
} |
|
329 |
||
330 |
private static void checkAclFile(String aclFileName) { |
|
331 |
if (aclFileName == null || aclFileName.length()==0) { |
|
332 |
throw new AgentConfigurationError(SNMP_ACL_FILE_NOT_SET); |
|
333 |
} |
|
334 |
final File file = new File(aclFileName); |
|
335 |
if (!file.exists()) { |
|
336 |
throw new AgentConfigurationError(SNMP_ACL_FILE_NOT_FOUND, aclFileName); |
|
337 |
} |
|
338 |
if (!file.canRead()) { |
|
339 |
throw new AgentConfigurationError(SNMP_ACL_FILE_NOT_READABLE, aclFileName); |
|
340 |
} |
|
341 |
||
342 |
FileSystem fs = FileSystem.open(); |
|
343 |
try { |
|
344 |
if (fs.supportsFileSecurity(file)) { |
|
345 |
if (!fs.isAccessUserOnly(file)) { |
|
346 |
throw new AgentConfigurationError(SNMP_ACL_FILE_ACCESS_NOT_RESTRICTED, |
|
347 |
aclFileName); |
|
348 |
} |
|
349 |
} |
|
350 |
} catch (IOException e) { |
|
351 |
throw new AgentConfigurationError(SNMP_ACL_FILE_READ_FAILED, aclFileName); |
|
352 |
||
353 |
} |
|
354 |
} |
|
355 |
||
356 |
||
357 |
/** |
|
358 |
* Get the port on which the adaptor is bound. |
|
359 |
* Returns 0 if the adaptor is already terminated. |
|
360 |
* |
|
361 |
**/ |
|
362 |
public synchronized int getPort() { |
|
363 |
if (adaptor != null) return adaptor.getPort(); |
|
364 |
return 0; |
|
365 |
} |
|
366 |
||
367 |
/** |
|
368 |
* Stops the adaptor server. |
|
369 |
**/ |
|
370 |
public synchronized void terminate() { |
|
371 |
if (adaptor == null) return; |
|
372 |
||
373 |
// Terminate the MIB (deregister NotificationListener from |
|
374 |
// MemoryMBean) |
|
375 |
// |
|
376 |
try { |
|
377 |
jvmmib.terminate(); |
|
378 |
} catch (Exception x) { |
|
379 |
// Must not prevent to stop... |
|
380 |
// |
|
381 |
log.debug("jmxremote.AdaptorBootstrap.getTargetList.terminate", |
|
382 |
x.toString()); |
|
383 |
} finally { |
|
384 |
jvmmib=null; |
|
385 |
} |
|
386 |
||
387 |
// Stop the adaptor |
|
388 |
// |
|
389 |
try { |
|
390 |
adaptor.stop(); |
|
391 |
} finally { |
|
392 |
adaptor = null; |
|
393 |
} |
|
394 |
} |
|
395 |
||
396 |
} |