PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jboss-5.1.0/varia/src/main/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 604 lines | 267 code | 90 blank | 247 comment | 20 complexity | 54f80d90cceb4a1727ee373811c00994 MD5 | raw file
  1. /*
  2. * Copyright (c) 2003, Intracom S.A. - www.intracom.com
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * This package and its source code is available at www.jboss.org
  19. */
  20. package org.jboss.jmx.adaptor.snmp.agent;
  21. import java.net.InetAddress;
  22. import java.net.UnknownHostException;
  23. import javax.management.Notification;
  24. import javax.management.ObjectName;
  25. import org.jboss.bootstrap.spi.ServerConfig;
  26. import org.jboss.system.ListenerServiceMBeanSupport;
  27. import org.opennms.protocols.snmp.SnmpAgentSession;
  28. import org.opennms.protocols.snmp.SnmpPeer;
  29. import org.opennms.protocols.snmp.SnmpSMI;
  30. /**
  31. * <tt>SnmpAgentService</tt> is an MBean class implementing an SNMP agent.
  32. *
  33. * It allows to send V1 or V2 traps to one or more SNMP managers defined
  34. * by their IP address, listening port number and expected SNMP version.
  35. *
  36. * It support mapping SNMP get/set requests JMX mbean attribute get/sets.
  37. *
  38. * @jmx:mbean
  39. * extends="org.jboss.system.ListenerServiceMBean"
  40. *
  41. * @author <a href="mailto:spol@intracom.gr">Spyros Pollatos</a>
  42. * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
  43. * @author <a href="mailto:krishnaraj@ieee.org">Krishnaraj S</a>
  44. * @version $Revision: 80636 $
  45. */
  46. public class SnmpAgentService extends ListenerServiceMBeanSupport
  47. implements SnmpAgentServiceMBean
  48. {
  49. /** Supported versions */
  50. public static final int SNMPV1 = 1;
  51. public static final int SNMPV2 = 2;
  52. /** Default communities */
  53. public static final String DEFAULT_READ_COMMUNITY = "public";
  54. public static final String DEFAULT_WRITE_COMMUNITY = "private";
  55. // Private data --------------------------------------------------
  56. /** Time keeping*/
  57. private Clock clock = null;
  58. /** Trap counter */
  59. private Counter trapCounter = null;
  60. /** Name of the file containing SNMP manager specifications */
  61. private String managersResName = null;
  62. /** Name of resource file containing notification to trap mappings */
  63. private String notificationMapResName = null;
  64. /** Name of resource file containing get/set mappings */
  65. private String requestHandlerResName = null;
  66. /** Name of the trap factory class to be utilised */
  67. private String trapFactoryClassName = null;
  68. /** Name of request handler implementation class */
  69. private String requestHandlerClassName = null;
  70. /** The SNMP read community to use */
  71. private String readCommunity = DEFAULT_READ_COMMUNITY;
  72. /** The SNMP write community to use */
  73. private String writeCommunity = DEFAULT_WRITE_COMMUNITY;
  74. /** Controls the request processing thread pool */
  75. private int numberOfThreads = 1;
  76. /** Agent listening port */
  77. private int port = 1161;
  78. /** Agent SNMP protocol version */
  79. private int snmpVersion = SNMPV1;
  80. /** The interface to bind, useful for multi-homed hosts */
  81. private InetAddress bindAddress;
  82. /** Name of the utilised timer MBean */
  83. private ObjectName timerName = null;
  84. /** Heartbeat emission period (in seconds) and switch */
  85. private int heartBeatPeriod = 60;
  86. /** Dynamic subscriptions flag */
  87. private boolean dynamicSubscriptions = true;
  88. /** Reference to heartbeat emission controller */
  89. private Heartbeat heartbeat = null;
  90. /** The trap emitting subsystem*/
  91. private TrapEmitter trapEmitter = null;
  92. /** the snmp agent session for handling get/set requests */
  93. private SnmpAgentSession agentSession = null;
  94. /** the request handler instance handling get/set requests */
  95. private RequestHandler requestHandler;
  96. // Constructors --------------------------------------------------
  97. /**
  98. * Default CTOR
  99. */
  100. public SnmpAgentService()
  101. {
  102. // empty
  103. }
  104. // Attributes ----------------------------------------------------
  105. /**
  106. * Gets the heartbeat switch
  107. *
  108. * @jmx:managed-attribute
  109. */
  110. public int getHeartBeatPeriod()
  111. {
  112. return this.heartBeatPeriod;
  113. }
  114. /**
  115. * Sets the heartbeat period (in seconds) switch
  116. *
  117. * @jmx:managed-attribute
  118. */
  119. public void setHeartBeatPeriod(int heartBeatPeriod)
  120. {
  121. this.heartBeatPeriod = heartBeatPeriod;
  122. }
  123. /**
  124. * Returns the difference, measured in milliseconds, between the
  125. * instantiation time and midnight, January 1, 1970 UTC.
  126. *
  127. * @jmx:managed-attribute
  128. */
  129. public long getInstantiationTime()
  130. {
  131. return this.clock.instantiationTime();
  132. }
  133. /**
  134. * Returns the up-time
  135. *
  136. * @jmx:managed-attribute
  137. */
  138. public long getUptime()
  139. {
  140. return this.clock.uptime();
  141. }
  142. /**
  143. * Returns the current trap counter reading
  144. *
  145. * @jmx:managed-attribute
  146. */
  147. public long getTrapCount()
  148. {
  149. return this.trapCounter.peek();
  150. }
  151. /**
  152. * Sets the name of the file containing SNMP manager specifications
  153. *
  154. * @jmx:managed-attribute
  155. */
  156. public void setManagersResName(String managersResName)
  157. {
  158. this.managersResName = managersResName;
  159. }
  160. /**
  161. * Gets the name of the file containing SNMP manager specifications
  162. *
  163. * @jmx:managed-attribute
  164. */
  165. public String getManagersResName()
  166. {
  167. return this.managersResName;
  168. }
  169. /**
  170. * Sets the name of the file containing the notification/trap mappings
  171. *
  172. * @jmx:managed-attribute
  173. */
  174. public void setNotificationMapResName(String notificationMapResName)
  175. {
  176. this.notificationMapResName = notificationMapResName;
  177. }
  178. /**
  179. * Gets the name of the file containing the notification/trap mappings
  180. *
  181. * @jmx:managed-attribute
  182. */
  183. public String getNotificationMapResName()
  184. {
  185. return this.notificationMapResName;
  186. }
  187. /**
  188. * Sets the utilised trap factory name
  189. *
  190. * @jmx:managed-attribute
  191. */
  192. public void setTrapFactoryClassName(String name)
  193. {
  194. this.trapFactoryClassName = name;
  195. }
  196. /**
  197. * Gets the utilised trap factory name
  198. *
  199. * @jmx:managed-attribute
  200. */
  201. public String getTrapFactoryClassName()
  202. {
  203. return this.trapFactoryClassName;
  204. }
  205. /**
  206. * Sets the utilised timer MBean name
  207. *
  208. * @jmx:managed-attribute
  209. */
  210. public void setTimerName(ObjectName timerName)
  211. {
  212. this.timerName = timerName;
  213. }
  214. /**
  215. * Gets the utilised timer MBean name
  216. *
  217. * @jmx:managed-attribute
  218. */
  219. public ObjectName getTimerName()
  220. {
  221. return this.timerName;
  222. }
  223. /**
  224. * Sets the agent bind address
  225. *
  226. * @jmx:managed-attribute
  227. */
  228. public void setBindAddress(String bindAddress)
  229. throws UnknownHostException
  230. {
  231. this.bindAddress = toInetAddress(bindAddress);
  232. }
  233. /**
  234. * Gets the agent bind address
  235. *
  236. * @jmx:managed-attribute
  237. */
  238. public String getBindAddress()
  239. {
  240. String address = null;
  241. if (this.bindAddress != null)
  242. address = this.bindAddress.getHostAddress();
  243. return address;
  244. }
  245. /**
  246. * Sets the number of threads in the agent request processing thread pool
  247. *
  248. * @jmx:managed-attribute
  249. */
  250. public void setNumberOfThreads(int numberOfThreads)
  251. {
  252. if (numberOfThreads > 0 && numberOfThreads <= 12)
  253. {
  254. this.numberOfThreads = numberOfThreads;
  255. }
  256. }
  257. /**
  258. * Gets the number of threads in the agent requests processing thread pool
  259. *
  260. * @jmx:managed-attribute
  261. */
  262. public int getNumberOfThreads()
  263. {
  264. return numberOfThreads;
  265. }
  266. /**
  267. * Sets the agent listening port number
  268. *
  269. * @jmx:managed-attribute
  270. */
  271. public void setPort(int port)
  272. {
  273. if (port >= 0)
  274. {
  275. this.port = port;
  276. }
  277. }
  278. /**
  279. * Gets the agent listening port number
  280. *
  281. * @jmx:managed-attribute
  282. */
  283. public int getPort()
  284. {
  285. return port;
  286. }
  287. /**
  288. * Sets the snmp protocol version
  289. *
  290. * @jmx:managed-attribute
  291. */
  292. public void setSnmpVersion(int snmpVersion)
  293. {
  294. switch (snmpVersion)
  295. {
  296. case SNMPV2:
  297. this.snmpVersion = SNMPV2;
  298. break;
  299. default:
  300. this.snmpVersion = SNMPV1;
  301. break;
  302. }
  303. }
  304. /**
  305. * Gets the snmp protocol version
  306. *
  307. * @jmx:managed-attribute
  308. */
  309. public int getSnmpVersion()
  310. {
  311. return snmpVersion;
  312. }
  313. /**
  314. * Sets the read community (no getter)
  315. *
  316. * @jmx:managed-attribute
  317. */
  318. public void setReadCommunity(String readCommunity)
  319. {
  320. if (readCommunity != null && readCommunity.length() > 0)
  321. {
  322. this.readCommunity = readCommunity;
  323. }
  324. }
  325. /**
  326. * Sets the write community (no getter)
  327. * @jmx:managed-attribute
  328. */
  329. public void setWriteCommunity(String writeCommunity)
  330. {
  331. if (writeCommunity != null && writeCommunity.length() > 0)
  332. {
  333. this.writeCommunity = writeCommunity;
  334. }
  335. }
  336. /**
  337. * Sets the RequestHandler implementation class
  338. *
  339. * @jmx:managed-attribute
  340. */
  341. public void setRequestHandlerClassName(String requestHandlerClassName)
  342. {
  343. this.requestHandlerClassName = requestHandlerClassName;
  344. }
  345. /**
  346. * Gets the RequestHandler implementation class
  347. *
  348. * @jmx:managed-attribute
  349. */
  350. public String getRequestHandlerClassName()
  351. {
  352. return requestHandlerClassName;
  353. }
  354. /**
  355. * Sets the resource file name containing get/set mappings
  356. *
  357. * @jmx:managed-attribute
  358. */
  359. public void setRequestHandlerResName(String requestHandlerResName)
  360. {
  361. this.requestHandlerResName = requestHandlerResName;
  362. }
  363. /**
  364. * Gets the resource file name containing get/set mappings
  365. *
  366. * @jmx:managed-attribute
  367. */
  368. public String getRequestHandlerResName()
  369. {
  370. return requestHandlerResName;
  371. }
  372. /**
  373. * Enables/disables dynamic subscriptions
  374. *
  375. * @jmx:managed-attribute
  376. */
  377. public void setDynamicSubscriptions(boolean dynamicSubscriptions)
  378. {
  379. this.dynamicSubscriptions = dynamicSubscriptions;
  380. }
  381. /**
  382. * Gets the dynamic subscriptions status
  383. *
  384. * @jmx:managed-attribute
  385. */
  386. public boolean getDynamicSubscriptions()
  387. {
  388. return this.dynamicSubscriptions;
  389. }
  390. // Operations ----------------------------------------------------
  391. /**
  392. * Reconfigures the RequestHandler, reponsible for handling get requests etc.
  393. *
  394. * @jmx:managed-operation
  395. */
  396. public void reconfigureRequestHandler() throws Exception
  397. {
  398. if (requestHandler instanceof Reconfigurable)
  399. ((Reconfigurable)requestHandler).reconfigure(getRequestHandlerResName());
  400. else
  401. throw new UnsupportedOperationException("Request handler is not Reconfigurable");
  402. }
  403. // Lifecycle operations ------------------------------------------
  404. /**
  405. * Perform service start-up
  406. */
  407. protected void startService()
  408. throws Exception
  409. {
  410. // initialize clock and trapCounter
  411. this.clock = new Clock();
  412. this.trapCounter = new Counter(0);
  413. // Notification subscription are handled by
  414. // ListenerServiceMBeanSupport baseclass
  415. log.debug("Instantiating trap emitter ...");
  416. this.trapEmitter = new TrapEmitter(this.getTrapFactoryClassName(),
  417. this.trapCounter,
  418. this.clock,
  419. this.getManagersResName(),
  420. this.getNotificationMapResName());
  421. // Start trap emitter
  422. log.debug("Starting trap emitter ...");
  423. this.trapEmitter.start();
  424. // Get the heartbeat going
  425. this.heartbeat = new Heartbeat(this.getServer(),
  426. this.getTimerName(),
  427. this.getHeartBeatPeriod());
  428. log.debug("Starting heartbeat controller ...");
  429. heartbeat.start();
  430. // subscribe for notifications, with the option for dynamic subscriptions
  431. super.subscribe(this.dynamicSubscriptions);
  432. // initialise the snmp agent
  433. log.debug("Starting snmp agent ...");
  434. startAgent();
  435. log.info("SNMP agent going active");
  436. // Send the cold start!
  437. this.sendNotification(new Notification(EventTypes.COLDSTART, this,
  438. getNextNotificationSequenceNumber()));
  439. }
  440. /**
  441. * Perform service shutdown
  442. */
  443. protected void stopService()
  444. throws Exception
  445. {
  446. // unsubscribe for notifications
  447. super.unsubscribe();
  448. log.debug("Stopping heartbeat controller ...");
  449. this.heartbeat.stop();
  450. this.heartbeat = null; // gc
  451. log.debug("Stopping trap emitter ...");
  452. this.trapEmitter.stop();
  453. this.trapEmitter = null;
  454. log.debug("Stopping snmp agent ...");
  455. this.agentSession.close();
  456. this.agentSession = null;
  457. log.info("SNMP agent stopped");
  458. }
  459. // Notification handling -----------------------------------------
  460. /**
  461. * All notifications are intercepted here and are routed for emission.
  462. */
  463. public void handleNotification2(Notification n, Object handback)
  464. {
  465. if (log.isTraceEnabled()) {
  466. log.trace("Received notification: <" + n + "> Payload " +
  467. "TS: <" + n.getTimeStamp() + "> " +
  468. "SN: <" + n.getSequenceNumber() + "> " +
  469. "T: <" + n.getType() + ">");
  470. }
  471. try {
  472. this.trapEmitter.send(n);
  473. }
  474. catch (Exception e) {
  475. log.error("Sending trap", e);
  476. }
  477. }
  478. // Private -------------------------------------------------------
  479. /**
  480. * Start the embedded agent
  481. */
  482. private void startAgent()
  483. throws Exception
  484. {
  485. // cater for possible global -b option, if no override has been specified
  486. InetAddress address = this.bindAddress != null ? this.bindAddress :
  487. toInetAddress(System.getProperty(ServerConfig.SERVER_BIND_ADDRESS));
  488. // the listening address
  489. SnmpPeer peer = new SnmpPeer(address, this.port);
  490. // set community strings and protocol version
  491. peer.getParameters().setReadCommunity(this.readCommunity);
  492. peer.getParameters().setWriteCommunity(this.writeCommunity);
  493. peer.getParameters().setVersion(this.snmpVersion == SNMPV2 ? SnmpSMI.SNMPV2 : SnmpSMI.SNMPV1);
  494. // Instantiate and initialize the RequestHandler implementation
  495. requestHandler = (RequestHandler)Class.forName(
  496. this.requestHandlerClassName, true, this.getClass().getClassLoader()).newInstance();
  497. requestHandler.initialize(this.requestHandlerResName, this.getServer(), this.log, this.clock);
  498. // Instantiate the AgentSession with an optional thread pool
  499. this.agentSession = this.numberOfThreads > 1
  500. ? new SnmpAgentSession(requestHandler, peer, this.numberOfThreads)
  501. : new SnmpAgentSession(requestHandler, peer);
  502. }
  503. /**
  504. * Safely convert a host string to InetAddress or null
  505. */
  506. private InetAddress toInetAddress(String host)
  507. throws UnknownHostException
  508. {
  509. if (host == null || host.length() == 0)
  510. return null;
  511. else
  512. return InetAddress.getByName(host);
  513. }
  514. }