1 20 package org.jboss.jmx.adaptor.snmp.agent; 21 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 25 import javax.management.Notification ; 26 import javax.management.ObjectName ; 27 28 import org.jboss.system.ListenerServiceMBeanSupport; 29 import org.jboss.system.server.ServerConfig; 30 import org.opennms.protocols.snmp.SnmpAgentSession; 31 import org.opennms.protocols.snmp.SnmpPeer; 32 import org.opennms.protocols.snmp.SnmpSMI; 33 34 50 public class SnmpAgentService extends ListenerServiceMBeanSupport 51 implements SnmpAgentServiceMBean 52 { 53 54 public static final int SNMPV1 = 1; 55 public static final int SNMPV2 = 2; 56 57 58 public static final String DEFAULT_READ_COMMUNITY = "public"; 59 public static final String DEFAULT_WRITE_COMMUNITY = "private"; 60 61 63 64 private Clock clock = null; 65 66 67 private Counter trapCounter = null; 68 69 70 private String managersResName = null; 71 72 73 private String notificationMapResName = null; 74 75 76 private String requestHandlerResName = null; 77 78 79 private String trapFactoryClassName = null; 80 81 82 private String requestHandlerClassName = null; 83 84 85 private String readCommunity = DEFAULT_READ_COMMUNITY; 86 87 88 private String writeCommunity = DEFAULT_WRITE_COMMUNITY; 89 90 91 private int numberOfThreads = 1; 92 93 94 private int port = 1161; 95 96 97 private int snmpVersion = SNMPV1; 98 99 100 private InetAddress bindAddress; 101 102 103 private ObjectName timerName = null; 104 105 106 private int heartBeatPeriod = 60; 107 108 109 private boolean dynamicSubscriptions = true; 110 111 112 private Heartbeat heartbeat = null; 113 114 115 private TrapEmitter trapEmitter = null; 116 117 118 private SnmpAgentSession agentSession = null; 119 120 121 private RequestHandler requestHandler; 122 123 125 128 public SnmpAgentService() 129 { 130 } 132 133 135 140 public int getHeartBeatPeriod() 141 { 142 return this.heartBeatPeriod; 143 } 144 145 150 public void setHeartBeatPeriod(int heartBeatPeriod) 151 { 152 this.heartBeatPeriod = heartBeatPeriod; 153 } 154 155 161 public long getInstantiationTime() 162 { 163 return this.clock.instantiationTime(); 164 } 165 166 171 public long getUptime() 172 { 173 return this.clock.uptime(); 174 } 175 176 181 public long getTrapCount() 182 { 183 return this.trapCounter.peek(); 184 } 185 186 191 public void setManagersResName(String managersResName) 192 { 193 this.managersResName = managersResName; 194 } 195 196 201 public String getManagersResName() 202 { 203 return this.managersResName; 204 } 205 206 211 public void setNotificationMapResName(String notificationMapResName) 212 { 213 this.notificationMapResName = notificationMapResName; 214 } 215 216 221 public String getNotificationMapResName() 222 { 223 return this.notificationMapResName; 224 } 225 226 231 public void setTrapFactoryClassName(String name) 232 { 233 this.trapFactoryClassName = name; 234 } 235 236 241 public String getTrapFactoryClassName() 242 { 243 return this.trapFactoryClassName; 244 } 245 246 251 public void setTimerName(ObjectName timerName) 252 { 253 this.timerName = timerName; 254 } 255 256 261 public ObjectName getTimerName() 262 { 263 return this.timerName; 264 } 265 266 271 public void setBindAddress(String bindAddress) 272 throws UnknownHostException 273 { 274 this.bindAddress = toInetAddress(bindAddress); 275 } 276 277 282 public String getBindAddress() 283 { 284 String address = null; 285 286 if (this.bindAddress != null) 287 address = this.bindAddress.getHostAddress(); 288 289 return address; 290 } 291 292 297 public void setNumberOfThreads(int numberOfThreads) 298 { 299 if (numberOfThreads > 0 && numberOfThreads <= 12) 300 { 301 this.numberOfThreads = numberOfThreads; 302 } 303 } 304 305 310 public int getNumberOfThreads() 311 { 312 return numberOfThreads; 313 } 314 315 320 public void setPort(int port) 321 { 322 if (port >= 0) 323 { 324 this.port = port; 325 } 326 } 327 328 333 public int getPort() 334 { 335 return port; 336 } 337 338 343 public void setSnmpVersion(int snmpVersion) 344 { 345 switch (snmpVersion) 346 { 347 case SNMPV2: 348 this.snmpVersion = SNMPV2; 349 break; 350 351 default: 352 this.snmpVersion = SNMPV1; 353 break; 354 } 355 } 356 357 362 public int getSnmpVersion() 363 { 364 return snmpVersion; 365 } 366 367 372 public void setReadCommunity(String readCommunity) 373 { 374 if (readCommunity != null && readCommunity.length() > 0) 375 { 376 this.readCommunity = readCommunity; 377 } 378 } 379 380 384 public void setWriteCommunity(String writeCommunity) 385 { 386 if (writeCommunity != null && writeCommunity.length() > 0) 387 { 388 this.writeCommunity = writeCommunity; 389 } 390 } 391 392 397 public void setRequestHandlerClassName(String requestHandlerClassName) 398 { 399 this.requestHandlerClassName = requestHandlerClassName; 400 } 401 402 407 public String getRequestHandlerClassName() 408 { 409 return requestHandlerClassName; 410 } 411 412 417 public void setRequestHandlerResName(String requestHandlerResName) 418 { 419 this.requestHandlerResName = requestHandlerResName; 420 } 421 422 427 public String getRequestHandlerResName() 428 { 429 return requestHandlerResName; 430 } 431 432 437 public void setDynamicSubscriptions(boolean dynamicSubscriptions) 438 { 439 this.dynamicSubscriptions = dynamicSubscriptions; 440 } 441 442 447 public boolean getDynamicSubscriptions() 448 { 449 return this.dynamicSubscriptions; 450 } 451 452 454 459 public void reconfigureRequestHandler() throws Exception 460 { 461 if (requestHandler instanceof Reconfigurable) 462 ((Reconfigurable)requestHandler).reconfigure(getRequestHandlerResName()); 463 else 464 throw new UnsupportedOperationException ("Request handler is not Reconfigurable"); 465 } 466 467 469 472 protected void startService() 473 throws Exception 474 { 475 this.clock = new Clock(); 477 this.trapCounter = new Counter(0); 478 479 482 log.debug("Instantiating trap emitter ..."); 483 this.trapEmitter = new TrapEmitter(this.getTrapFactoryClassName(), 484 this.trapCounter, 485 this.clock, 486 this.getManagersResName(), 487 this.getNotificationMapResName()); 488 489 log.debug("Starting trap emitter ..."); 491 this.trapEmitter.start(); 492 493 this.heartbeat = new Heartbeat(this.getServer(), 495 this.getTimerName(), 496 this.getHeartBeatPeriod()); 497 498 log.debug("Starting heartbeat controller ..."); 499 heartbeat.start(); 500 501 super.subscribe(this.dynamicSubscriptions); 503 504 log.debug("Starting snmp agent ..."); 506 startAgent(); 507 508 log.info("SNMP agent going active"); 509 510 this.sendNotification(new Notification (EventTypes.COLDSTART, this, 512 getNextNotificationSequenceNumber())); 513 } 514 515 518 protected void stopService() 519 throws Exception 520 { 521 super.unsubscribe(); 523 524 log.debug("Stopping heartbeat controller ..."); 525 this.heartbeat.stop(); 526 this.heartbeat = null; 528 log.debug("Stopping trap emitter ..."); 529 this.trapEmitter.stop(); 530 this.trapEmitter = null; 531 532 log.debug("Stopping snmp agent ..."); 533 this.agentSession.close(); 534 this.agentSession = null; 535 536 log.info("SNMP agent stopped"); 537 } 538 539 541 544 public void handleNotification2(Notification n, Object handback) 545 { 546 if (log.isTraceEnabled()) { 547 log.trace("Received notification: <" + n + "> Payload " + 548 "TS: <" + n.getTimeStamp() + "> " + 549 "SN: <" + n.getSequenceNumber() + "> " + 550 "T: <" + n.getType() + ">"); 551 } 552 553 try { 554 this.trapEmitter.send(n); 555 } 556 catch (Exception e) { 557 log.error("Sending trap", e); 558 } 559 } 560 561 563 566 private void startAgent() 567 throws Exception 568 { 569 InetAddress address = this.bindAddress != null ? this.bindAddress : 571 toInetAddress(System.getProperty(ServerConfig.SERVER_BIND_ADDRESS)); 572 573 SnmpPeer peer = new SnmpPeer(address, this.port); 575 576 peer.getParameters().setReadCommunity(this.readCommunity); 578 peer.getParameters().setWriteCommunity(this.writeCommunity); 579 peer.getParameters().setVersion(this.snmpVersion == SNMPV2 ? SnmpSMI.SNMPV2 : SnmpSMI.SNMPV1); 580 581 requestHandler = (RequestHandler)Class.forName( 583 this.requestHandlerClassName, true, this.getClass().getClassLoader()).newInstance(); 584 requestHandler.initialize(this.requestHandlerResName, this.getServer(), this.log, this.clock); 585 586 this.agentSession = this.numberOfThreads > 1 588 ? new SnmpAgentSession(requestHandler, peer, this.numberOfThreads) 589 : new SnmpAgentSession(requestHandler, peer); 590 } 591 592 595 private InetAddress toInetAddress(String host) 596 throws UnknownHostException 597 { 598 if (host == null || host.length() == 0) 599 return null; 600 else 601 return InetAddress.getByName(host); 602 } 603 604 } 605 | Popular Tags |