Esempio n. 1
0
  private PDU sendRequestV1V2(PDU pdu, int version) throws Exception {
    PDU response;
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
    target.setVersion(version);
    if (m_timeout > 0) {
      target.setTimeout(m_timeout);
    }

    TransportMapping<UdpAddress> transport = null;
    try {
      transport = new DefaultUdpTransportMapping();
      Snmp snmp = new Snmp(transport);
      transport.listen();

      ResponseEvent e = snmp.send(pdu, target);
      response = e.getResponse();
    } finally {
      if (transport != null) {
        transport.close();
      }
    }
    return response;
  }
Esempio n. 2
0
  public static MockSnmpAgent createAgentAndRun(URL moFile, String bindAddress)
      throws InterruptedException {
    setupLogging();
    try {
      InputStream in = moFile.openStream();
      if (in == null) {
        throw new IllegalArgumentException(
            "could not get InputStream mock object resource; does it exist?  Resource: " + moFile);
      }
      in.close();

    } catch (IOException e) {
      throw new RuntimeException(
          "Got IOException while checking for existence of mock object file: " + e, e);
    }

    final MockSnmpAgent agent = new MockSnmpAgent(new File("/dev/null"), moFile, bindAddress);
    Thread thread = new Thread(agent, agent.getClass().getSimpleName() + '-' + agent.hashCode());
    thread.start();

    try {
      while (!agent.isRunning() && thread.isAlive()) {
        Thread.sleep(10);
      }
    } catch (final InterruptedException e) {
      s_log.warn("MockSnmpAgent: Agent interrupted while starting: " + e.getLocalizedMessage());
      thread.interrupt();
      agent.shutDownAndWait();
      throw e;
    }

    if (!thread.isAlive()) {
      agent.m_running.set(false);
      agent.m_stopped.set(true);
      throw new IllegalStateException(
          "MockSnmpAgent: agent failed to start on address " + bindAddress, agent.m_failure.get());
    }

    if (System.getProperty(PROPERTY_SLEEP_ON_CREATE) != null) {
      long sleep = Long.parseLong(System.getProperty(PROPERTY_SLEEP_ON_CREATE));
      Thread.sleep(sleep);
    }

    return agent;
  }
Esempio n. 3
0
  private PDU sendRequestV3(PDU pdu) throws IOException {
    PDU response;

    OctetString userId = new OctetString("opennmsUser");
    OctetString pw = new OctetString("0p3nNMSv3");

    UserTarget target = new UserTarget();
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setSecurityName(userId);
    target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
    target.setVersion(SnmpConstants.version3);
    if (m_timeout > 0) {
      target.setTimeout(m_timeout);
    } else {
      target.setTimeout(5000);
    }

    TransportMapping<UdpAddress> transport = null;
    try {
      USM usm =
          new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
      SecurityModels.getInstance().addSecurityModel(usm);
      transport = new DefaultUdpTransportMapping();
      Snmp snmp = new Snmp(transport);

      UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw);
      snmp.getUSM().addUser(userId, user);

      transport.listen();

      ResponseEvent e = snmp.send(pdu, target);
      response = e.getResponse();
    } finally {
      if (transport != null) {
        transport.close();
      }
    }
    return response;
  }
Esempio n. 4
0
  @Before
  public void setUp() throws Exception {
    // Create a global USM that all client calls will use
    SNMP4JSettings.setEnterpriseID(5813);
    m_usm =
        new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(m_usm);

    try {
      m_agent =
          MockSnmpAgent.createAgentAndRun(
              classPathResource("penrose-lldp-mib.properties"),
              str(InetAddress.getLocalHost()) + "/0");
    } catch (Throwable e) {
      m_agent =
          MockSnmpAgent.createAgentAndRun(
              classPathResource("penrose-lldp-mib.properties"),
              str(InetAddressUtils.ONE_TWENTY_SEVEN) + "/0");
    }

    m_requestedVarbinds = new ArrayList<AnticipatedRequest>();
  }
Esempio n. 5
0
  @Before
  public void setUp() throws Exception {
    // Create a global USM that all client calls will use
    MPv3.setEnterpriseID(5813);
    m_usm =
        new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(m_usm);

    m_agent =
        MockSnmpAgent.createAgentAndRun(
            classPathResource("penrose-lldp-mib.properties"), "127.0.0.1/1691");

    m_requestedVarbinds = new ArrayList<AnticipatedRequest>();
  }
Esempio n. 6
0
  @Test
  public void testGetNextMultipleVarbinds() throws Exception {

    request(".1.0.8802.1.1.2.1.3.1")
        .andExpect(".1.0.8802.1.1.2.1.3.1.0", SMIConstants.SYNTAX_INTEGER32, new Integer32(4));

    doGetNext();

    m_agent.getUsm().setEngineBoots(15);

    byte[] hexString =
        new byte[] {(byte) 0x80, (byte) 0x71, (byte) 0x1F, (byte) 0x8F, (byte) 0xAF, (byte) 0xC0};
    request(".1.0.8802.1.1.2.1.3.1")
        .andExpect(".1.0.8802.1.1.2.1.3.1.0", SMIConstants.SYNTAX_INTEGER32, new Integer32(4));
    request(".1.0.8802.1.1.2.1.3.2")
        .andExpect(
            ".1.0.8802.1.1.2.1.3.2.0",
            SMIConstants.SYNTAX_OCTET_STRING,
            new OctetString(hexString));
    request(".1.0.8802.1.1.2.1.3.3")
        .andExpect(
            ".1.0.8802.1.1.2.1.3.3.0",
            SMIConstants.SYNTAX_OCTET_STRING,
            new OctetString("penrose-mx480".getBytes()));

    doGetNext();

    // This statement breaks the internal state of the SNMP4J agent
    // m_agent.getUsm().setLocalEngine(m_agent.getUsm().getLocalEngineID(), 15, 200);
    m_agent.getUsm().removeEngineTime(m_usm.getLocalEngineID());
    m_usm.removeEngineTime(m_agent.getUsm().getLocalEngineID());

    request(".1.0.8802.1.1.2.1.3.1")
        .andExpect(".1.0.8802.1.1.2.1.3.1.0", SMIConstants.SYNTAX_INTEGER32, new Integer32(4));
    doGetNext();
  }
Esempio n. 7
0
  public static void main(String[] args) throws UnknownHostException, MalformedURLException {
    LogFactory.setLogFactory(new ConsoleLogFactory());
    AgentConfigData agentConfig = parseCli(args);
    if (agentConfig == null) {
      System.err.println("Could not parse configuration.");
      System.exit(1);
    }
    String listenSpec =
        agentConfig.getListenAddr().getHostAddress() + "/" + agentConfig.getListenPort();

    try {
      MockSnmpAgent.createAgentAndRun(agentConfig.getMoFile(), listenSpec);
    } catch (InterruptedException e) {
      System.exit(0);
    }
  }
Esempio n. 8
0
 @After
 public void tearDown() throws Exception {
   if (m_agent != null) {
     m_agent.shutDownAndWait();
   }
 }