Example #1
0
  public SampleAgent(Map args) {
    configFile = (String) ((List) args.get("c")).get(0);
    bootCounterFile = new File((String) ((List) args.get("bc")).get(0));

    server = new DefaultMOServer();
    MOServer[] moServers = new MOServer[] {server};
    InputStream configInputStream =
        SampleAgent.class.getResourceAsStream("SampleAgentConfig.properties");
    if (args.containsKey("cfg")) {
      try {
        configInputStream = new FileInputStream((String) ArgumentParser.getValue(args, "cfg", 0));
      } catch (FileNotFoundException ex1) {
        ex1.printStackTrace();
      }
    }
    final Properties props = new Properties();
    try {
      props.load(configInputStream);
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    MOInputFactory configurationFactory =
        new MOInputFactory() {
          public MOInput createMOInput() {
            return new PropertyMOInput(props, SampleAgent.this);
          }
        };
    InputStream tableSizeLimitsInputStream =
        SampleAgent.class.getResourceAsStream("SampleAgentTableSizeLimits.properties");
    if (args.containsKey("ts")) {
      try {
        tableSizeLimitsInputStream =
            new FileInputStream((String) ArgumentParser.getValue(args, "ts", 0));
      } catch (FileNotFoundException ex1) {
        ex1.printStackTrace();
      }
    }
    tableSizeLimits = new Properties();
    try {
      tableSizeLimits.load(tableSizeLimitsInputStream);
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    MessageDispatcher messageDispatcher = new MessageDispatcherImpl();
    addListenAddresses(messageDispatcher, (List) args.get("address"));
    agent =
        new AgentConfigManager(
            new OctetString(MPv3.createLocalEngineID()),
            messageDispatcher,
            null,
            moServers,
            ThreadPool.create("SampleAgent", 3),
            configurationFactory,
            new DefaultMOPersistenceProvider(moServers, configFile),
            new EngineBootsCounterFile(bootCounterFile));
  }
Example #2
0
 public MockSnmpAgent(final File confFile, final URL moFile) {
   super(
       BOOT_COUNT_FILE,
       confFile,
       new CommandProcessor(
           new OctetString(MPv3.createLocalEngineID(new OctetString("MOCKAGENT")))));
   m_moLoader.set(new PropertiesBackedManagedObject());
   m_moFile.set(moFile);
   agent.setWorkerPool(ThreadPool.create("RequestPool", 4));
 }
    /** This method will listen for traps and response pdu's from SNMP agent. */
    public synchronized void listen(TransportIpAddress address) throws IOException {
      AbstractTransportMapping transport;

      if (address instanceof TcpAddress) {
        transport = new DefaultTcpTransportMapping((TcpAddress) address);
      } else {
        transport = new DefaultUdpTransportMapping((UdpAddress) address);
      }

      ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);
      MessageDispatcher mtDispatcher =
          new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());

      // add message processing models
      mtDispatcher.addMessageProcessingModel(new MPv1());
      mtDispatcher.addMessageProcessingModel(new MPv2c());

      // add all security protocols
      SecurityProtocols.getInstance().addDefaultProtocols();
      SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());

      // Create Target
      CommunityTarget target = new CommunityTarget();
      target.setCommunity(new OctetString("public"));

      Snmp snmp = new Snmp(mtDispatcher, transport);
      snmp.addCommandResponder(this);

      transport.listen();
      logger.info("Listening on " + address);

      try {
        this.wait();
      } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
      }
    }
 public SNMP4JAgent(String address) {
   // 不需要这两个文件一定存在
   configFile = "files/SampleAgent.cfg";
   bootCounterFile = new File("files/SampleAgent.bc");
   server = new DefaultMOServer();
   MOServer[] moServers = new MOServer[] {server};
   // 读properties文件
   final Properties props = new Properties();
   FileInputStream configInputStream = null;
   try {
     configInputStream = new FileInputStream("files/AgentConfig.properties");
     props.load(configInputStream);
   } catch (IOException e) {
     e.printStackTrace();
   }
   // 注入file
   MOInputFactory configurationFactory =
       new MOInputFactory() {
         public MOInput createMOInput() {
           return new PropertyMOInput(props, SNMP4JAgent.this);
         }
       };
   List<String> addressList = new ArrayList<String>();
   addressList.add(address);
   MessageDispatcher messageDispatcher = new MessageDispatcherImpl();
   addListenAddresses(messageDispatcher, addressList);
   agent =
       new AgentConfigManager(
           new OctetString(MPv3.createLocalEngineID()),
           messageDispatcher,
           null,
           moServers,
           ThreadPool.create("SampleAgent", 3),
           configurationFactory,
           new DefaultMOPersistenceProvider(moServers, configFile),
           new EngineBootsCounterFile(bootCounterFile));
 }