1 20 21 package org.snmp4j.agent; 22 23 import java.io.*; 24 25 import org.snmp4j.*; 26 import org.snmp4j.agent.io.*; 27 import org.snmp4j.agent.mo.snmp.*; 28 import org.snmp4j.agent.mo.snmp4j.*; 29 import org.snmp4j.log.*; 30 import org.snmp4j.mp.*; 31 import org.snmp4j.security.*; 32 import org.snmp4j.smi.*; 33 import org.snmp4j.transport.*; 34 35 45 public abstract class BaseAgent implements Runnable , MOPersistenceProvider { 46 47 private static final LogAdapter logger = 48 LogFactory.getLogger(BaseAgent.class); 49 50 public static final int STATE_CREATED = 0; 51 public static final int STATE_INIT_STARTED = 10; 52 public static final int STATE_INIT_FINISHED = 20; 53 public static final int STATE_RUNNING = 40; 54 public static final int STATE_STOPPED = 30; 55 56 protected SNMPv2MIB snmpv2MIB; 57 protected SnmpFrameworkMIB snmpFrameworkMIB; 58 protected SnmpTargetMIB snmpTargetMIB; 59 protected SnmpNotificationMIB snmpNotificationMIB; 60 protected SnmpProxyMIB snmpProxyMIB; 61 protected SnmpCommunityMIB snmpCommunityMIB; 62 protected Snmp4jLogMib snmp4jLogMIB; 63 protected Snmp4jConfigMib snmp4jConfigMIB; 64 protected UsmMIB usmMIB; 65 protected VacmMIB vacmMIB; 66 protected DefaultMOServer server; 67 protected Snmp session; 68 protected TransportMapping[] transportMappings; 69 protected MessageDispatcherImpl dispatcher; 70 protected CommandProcessor agent; 71 72 protected MPv3 mpv3; 73 protected USM usm; 74 75 protected File configFile; 76 protected File bootCounterFile; 77 protected NotificationOriginator notificationOriginator; 78 protected ProxyForwarder defaultProxyForwarder; 79 80 protected OctetString sysDescr = 81 new OctetString("SNMP4J-Agent - "+ 82 System.getProperty("os.name","")+ 83 " - "+System.getProperty("os.arch")+ 84 " - "+System.getProperty("os.version")); 85 protected OID sysOID = new OID("1.3.6.1.4.1.4976"); 86 protected Integer32 sysServices = new Integer32(10); 87 88 protected int agentState = STATE_CREATED; 89 90 protected OctetString defaultContext; 91 92 97 protected BaseAgent() { 98 this.server = new DefaultMOServer(); 99 } 100 101 115 protected BaseAgent(File bootCounterFile, 116 File configFile, 117 CommandProcessor commandProcessor) { 118 this(); 119 this.configFile = configFile; 120 this.bootCounterFile = bootCounterFile; 121 this.agent = commandProcessor; 122 } 123 124 132 public void init() throws IOException { 133 agentState = STATE_INIT_STARTED; 134 initTransportMappings(); 135 initMessageDispatcher(); 136 server.addContext(new OctetString()); 137 snmpv2MIB = new SNMPv2MIB(sysDescr, sysOID, sysServices); 138 139 dispatcher.addCounterListener(snmpv2MIB); 141 agent.addCounterListener(snmpv2MIB); 142 snmpFrameworkMIB = 143 new SnmpFrameworkMIB((USM) 144 mpv3.getSecurityModel(SecurityModel.SECURITY_MODEL_USM), 145 dispatcher.getTransportMappings()); 146 usmMIB = new UsmMIB(usm, SecurityProtocols.getInstance()); 147 usm.addUsmUserListener(usmMIB); 148 149 vacmMIB = new VacmMIB(server); 150 snmpTargetMIB = new SnmpTargetMIB(session); 151 snmpNotificationMIB = new SnmpNotificationMIB(); 152 snmpCommunityMIB = new SnmpCommunityMIB(snmpTargetMIB); 153 154 snmp4jLogMIB = new Snmp4jLogMib(); 155 snmp4jConfigMIB = new Snmp4jConfigMib(snmpv2MIB.getSysUpTime()); 156 snmp4jConfigMIB.setSnmpCommunityMIB(snmpCommunityMIB); 157 snmp4jConfigMIB.setDefaultConfigFile(configFile); 158 snmp4jConfigMIB.setPersistenceProvider( 159 Snmp4jConfigMib.Snmp4jCfgStorageFormatEnum.binary, this); 160 snmpProxyMIB = new SnmpProxyMIB(); 161 notificationOriginator = 162 new NotificationOriginatorImpl(session, vacmMIB, 163 snmpv2MIB.getSysUpTime(), 164 snmpTargetMIB, snmpNotificationMIB); 165 setupDefaultProxyForwarder(); 166 addUsmUser(usm); 168 addCommunities(snmpCommunityMIB); 170 addViews(vacmMIB); 171 addNotificationTargets(snmpTargetMIB, snmpNotificationMIB); 172 173 registerSnmpMIBs(); 174 } 175 176 186 protected OctetString getContext(MOGroup mibGroup) { 187 return getDefaultContext(); 188 } 189 190 193 protected void registerSnmpMIBs() { 194 try { 195 snmpTargetMIB.registerMOs(server, getContext(snmpTargetMIB)); 196 snmpNotificationMIB.registerMOs(server, getContext(snmpNotificationMIB)); 197 vacmMIB.registerMOs(server, getContext(vacmMIB)); 198 usmMIB.registerMOs(server, getContext(usmMIB)); 199 snmpv2MIB.registerMOs(server, getContext(snmpv2MIB)); 200 snmpFrameworkMIB.registerMOs(server, getContext(snmpFrameworkMIB)); 201 snmpCommunityMIB.registerMOs(server, getContext(snmpCommunityMIB)); 202 snmp4jLogMIB.registerMOs(server, getContext(snmp4jLogMIB)); 203 snmp4jConfigMIB.registerMOs(server, getContext(snmp4jConfigMIB)); 204 snmpProxyMIB.registerMOs(server, getContext(snmpProxyMIB)); 205 registerManagedObjects(); 206 } 207 catch (DuplicateRegistrationException ex) { 208 ex.printStackTrace(); 209 } 210 } 211 212 215 protected void unregisterSnmpMIBs() { 216 snmpTargetMIB.unregisterMOs(server, getContext(snmpTargetMIB)); 217 snmpNotificationMIB.unregisterMOs(server, getContext(snmpNotificationMIB)); 218 vacmMIB.unregisterMOs(server, getContext(vacmMIB)); 219 usmMIB.unregisterMOs(server, getContext(usmMIB)); 220 snmpv2MIB.unregisterMOs(server, getContext(snmpv2MIB)); 221 snmpFrameworkMIB.unregisterMOs(server, getContext(snmpFrameworkMIB)); 222 snmpCommunityMIB.unregisterMOs(server, getContext(snmpCommunityMIB)); 223 snmp4jLogMIB.unregisterMOs(server, getContext(snmp4jLogMIB)); 224 snmp4jConfigMIB.unregisterMOs(server, getContext(snmp4jConfigMIB)); 225 snmpProxyMIB.unregisterMOs(server, getContext(snmpProxyMIB)); 226 unregisterManagedObjects(); 227 } 228 229 232 protected abstract void registerManagedObjects(); 233 234 237 protected abstract void unregisterManagedObjects(); 238 239 243 protected void setupDefaultProxyForwarder() { 244 defaultProxyForwarder = new ProxyForwarderImpl(session, snmpProxyMIB, 245 snmpTargetMIB); 246 agent.addProxyForwarder(defaultProxyForwarder, 247 null, ProxyForwarder.PROXY_TYPE_ALL); 248 ((ProxyForwarderImpl)defaultProxyForwarder).addCounterListener(snmpv2MIB); 249 } 250 251 257 public void loadConfig(int importMode) { 258 try { 259 FileInputStream fis = null; 260 fis = new FileInputStream(configFile); 261 loadConfig(fis, importMode); 262 fis.close(); 263 } 264 catch (FileNotFoundException ex) { 265 logger.error(ex); 266 } 267 catch (IOException ex) { 268 logger.error(ex); 269 } 270 } 271 272 281 public void loadConfig(InputStream configFile, int importMode) 282 throws IOException 283 { 284 ObjectInputStream ois = new ObjectInputStream(configFile); 285 DefaultMOInput is = new DefaultMOInput(ois); 286 is.setOverwriteMode(importMode); 287 MOServerPersistence p = new MOServerPersistence(server); 288 p.loadData(is); 289 ois.close(); 290 } 291 292 296 public void saveConfig() { 297 FileOutputStream fos = null; 298 try { 299 fos = new FileOutputStream(configFile); 300 saveConfig(fos); 301 fos.flush(); 302 fos.close(); 303 } 304 catch (FileNotFoundException ex) { 305 logger.error(ex); 306 } 307 catch (IOException ex) { 308 logger.error(ex); 309 } 310 finally { 311 if (fos != null) { 312 try { 313 fos.close(); 314 } 315 catch (IOException ex1) { 316 logger.warn(ex1); 317 } 318 } 319 } 320 } 321 322 331 public void saveConfig(OutputStream configFile) throws IOException { 332 ObjectOutputStream oos = new ObjectOutputStream(configFile); 333 DefaultMOOutput os = new DefaultMOOutput(oos); 334 MOServerPersistence p = new MOServerPersistence(server); 335 p.saveData(os); 336 oos.flush(); 337 } 338 339 343 protected void addShutdownHook() { 344 Runtime.getRuntime().addShutdownHook(new Thread () { 345 public void run() { 346 saveConfig(); 347 } 348 }); 349 } 350 351 356 protected void finishInit() { 357 if (agentState < STATE_INIT_STARTED) { 358 logger.fatal("Agent initialization finish is called before "+ 359 "initialization, current state is "+agentState); 360 } 361 agent.setNotificationOriginator(notificationOriginator); 362 agent.addMOServer(server); 363 agent.setCoexistenceProvider(snmpCommunityMIB); 364 agent.setVacm(vacmMIB); 365 dispatcher.addCommandResponder(agent); 366 agentState = STATE_INIT_FINISHED; 367 } 368 369 protected void sendColdStartNotification() { 370 notificationOriginator.notify(new OctetString(), SnmpConstants.coldStart, 371 new VariableBinding[0]); 372 } 373 374 378 public void run() { 379 if (agentState < STATE_INIT_FINISHED) { 380 logger.fatal("Agent is run uninitialized, current state is "+agentState); 381 } 382 if (agentState == STATE_STOPPED) { 383 initSnmpSession(); 384 } 385 try { 386 session.listen(); 387 } 388 catch (IOException iox) { 389 logger.error(iox); 390 } 391 } 392 393 398 public void stop() { 399 if (agentState != STATE_RUNNING) { 400 logger.error("Agent is stopped although it is not running, "+ 401 "current state is "+agentState); 402 } 403 try { 404 session.close(); 405 } 406 catch (IOException ex) { 407 logger.warn("Closing agent session threw IOException: "+ex.getMessage()); 408 } 409 session = null; 410 agentState = STATE_STOPPED; 411 } 412 413 417 protected void initMessageDispatcher() { 418 dispatcher = new MessageDispatcherImpl(); 419 mpv3 = new MPv3(agent.getContextEngineID().getValue()); 420 usm = new USM(SecurityProtocols.getInstance(), 421 agent.getContextEngineID(), 422 updateEngineBoots()); 423 SecurityModels.getInstance().addSecurityModel(usm); 424 SecurityProtocols.getInstance().addDefaultProtocols(); 425 dispatcher.addMessageProcessingModel(new MPv1()); 426 dispatcher.addMessageProcessingModel(new MPv2c()); 427 dispatcher.addMessageProcessingModel(mpv3); 428 initSnmpSession(); 429 } 430 431 protected void initSnmpSession() { 432 session = new Snmp(dispatcher); 433 for (int i=0; i<transportMappings.length; i++) { 434 session.addTransportMapping(transportMappings[i]); 435 } 436 } 437 438 443 protected int updateEngineBoots() { 444 int boots = getEngineBoots(); 445 boots++; 446 if (boots <= 0) { 447 boots = 1; 448 } 449 setEngineBoots(boots); 450 return boots; 451 } 452 453 458 protected int getEngineBoots() { 459 FileInputStream fis = null; 460 try { 461 fis = new FileInputStream(bootCounterFile); 462 ObjectInputStream ois = new ObjectInputStream(fis); 463 int boots = ois.readInt(); 464 if (logger.isInfoEnabled()) { 465 logger.info("Engine boots is: "+boots); 466 } 467 return boots; 468 } 469 catch (FileNotFoundException ex) { 470 logger.warn("Could not find boot counter file: "+bootCounterFile); 471 } 472 catch (IOException iox) { 473 if (logger.isDebugEnabled()) { 474 iox.printStackTrace(); 475 } 476 logger.error("Failed to read boot counter: "+iox.getMessage()); 477 } 478 finally { 479 if (fis != null) { 480 try { 481 fis.close(); 482 } 483 catch (IOException ex1) { 484 logger.warn(ex1); 485 } 486 } 487 } 488 return 0; 489 } 490 491 protected void setEngineBoots(int engineBoots) { 492 FileOutputStream fos = null; 493 try { 494 fos = new FileOutputStream(bootCounterFile); 495 ObjectOutputStream oos = new ObjectOutputStream(fos); 496 oos.writeInt(engineBoots); 497 oos.close(); 498 if (logger.isInfoEnabled()) { 499 logger.info("Wrote boot counter: " + engineBoots); 500 } 501 } 502 catch (FileNotFoundException fnfex) { 503 logger.error("Boot counter configuration file not found: "+ 504 fnfex.getMessage()); 505 } 506 catch (IOException iox) { 507 logger.error("Failed to write boot counter: "+iox.getMessage()); 508 } 509 finally { 510 if (fos != null) { 511 try { 512 fos.close(); 513 } 514 catch (IOException ex1) { 515 logger.warn(ex1); 516 } 517 } 518 } 519 } 520 521 526 protected abstract void addUsmUser(USM usm); 527 528 536 protected abstract void addNotificationTargets(SnmpTargetMIB targetMIB, 537 SnmpNotificationMIB notificationMIB); 538 539 544 protected abstract void addViews(VacmMIB vacmMIB); 545 546 552 protected abstract void addCommunities(SnmpCommunityMIB communityMIB); 553 554 558 protected void initTransportMappings() throws IOException { 559 transportMappings = new TransportMapping[1]; 560 transportMappings[0] = 561 new DefaultUdpTransportMapping(new UdpAddress("0.0.0.0/161")); 562 } 563 564 public NotificationOriginator getNotificationOriginator() { 565 return notificationOriginator; 566 } 567 568 public void setDefaultProxyForwarder(ProxyForwarder defaultProxyForwarder) { 569 this.defaultProxyForwarder = defaultProxyForwarder; 570 } 571 572 public void setSysDescr(OctetString sysDescr) { 573 this.sysDescr = sysDescr; 574 } 575 576 public void setSysOID(OID sysOID) { 577 this.sysOID = sysOID; 578 } 579 580 public void setSysServices(Integer32 sysServices) { 581 this.sysServices = sysServices; 582 } 583 584 public void setAgent(CommandProcessor agent) { 585 this.agent = agent; 586 } 587 588 public void setBootCounterFile(File bootCounterFile) { 589 this.bootCounterFile = bootCounterFile; 590 } 591 592 public void setConfigFile(File configFile) { 593 this.configFile = configFile; 594 } 595 596 607 public void setDefaultContext(OctetString defaultContext) { 608 this.defaultContext = defaultContext; 609 } 610 611 public ProxyForwarder getDefaultProxyForwarder() { 612 return defaultProxyForwarder; 613 } 614 615 public OctetString getSysDescr() { 616 return sysDescr; 617 } 618 619 public OID getSysOID() { 620 return sysOID; 621 } 622 623 public Integer32 getSysServices() { 624 return sysServices; 625 } 626 627 public CommandProcessor getAgent() { 628 return agent; 629 } 630 631 public File getBootCounterFile() { 632 return bootCounterFile; 633 } 634 635 public File getConfigFile() { 636 return configFile; 637 } 638 639 public Snmp4jConfigMib getSnmp4jConfigMIB() { 640 return snmp4jConfigMIB; 641 } 642 643 public Snmp4jLogMib getSnmp4jLogMIB() { 644 return snmp4jLogMIB; 645 } 646 647 public SnmpCommunityMIB getSnmpCommunityMIB() { 648 return snmpCommunityMIB; 649 } 650 651 public SnmpFrameworkMIB getSnmpFrameworkMIB() { 652 return snmpFrameworkMIB; 653 } 654 655 public SnmpNotificationMIB getSnmpNotificationMIB() { 656 return snmpNotificationMIB; 657 } 658 659 public SnmpProxyMIB getSnmpProxyMIB() { 660 return snmpProxyMIB; 661 } 662 663 public SnmpTargetMIB getSnmpTargetMIB() { 664 return snmpTargetMIB; 665 } 666 667 public SNMPv2MIB getSnmpv2MIB() { 668 return snmpv2MIB; 669 } 670 671 public UsmMIB getUsmMIB() { 672 return usmMIB; 673 } 674 675 public VacmMIB getVacmMIB() { 676 return vacmMIB; 677 } 678 679 public Snmp getSession() { 680 return session; 681 } 682 683 public DefaultMOServer getServer() { 684 return server; 685 } 686 687 public MPv3 getMPv3() { 688 return mpv3; 689 } 690 691 public USM getUsm() { 692 return usm; 693 } 694 695 702 public int getAgentState() { 703 return agentState; 704 } 705 706 719 public OctetString getDefaultContext() { 720 return defaultContext; 721 } 722 } 723 | Popular Tags |