Search in sources :

Example 1 with SnmpAgentAddress

use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.

the class MockSnmpStrategyTest method setUp.

@Before
public void setUp() throws Exception {
    m_strategy = new MockSnmpStrategy();
    MockSnmpStrategy.setDataForAddress(new SnmpAgentAddress(m_agentAddress, m_agentPort), new ClassPathResource("loadSnmpDataTest.properties"));
    m_oldProperty = System.getProperty("org.opennms.snmp.strategyClass");
    System.setProperty("org.opennms.snmp.strategyClass", m_strategy.getClass().getName());
}
Also used : SnmpAgentAddress(org.opennms.netmgt.snmp.SnmpAgentAddress) ClassPathResource(org.springframework.core.io.ClassPathResource) Before(org.junit.Before)

Example 2 with SnmpAgentAddress

use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.

the class JUnitSnmpAgentExecutionListener method handleSnmpAgent.

private void handleSnmpAgent(final TestContext testContext, final JUnitSnmpAgent config, boolean useMockSnmpStrategy, MockSnmpDataProvider provider) throws IOException, UnknownHostException, InterruptedException {
    if (config == null)
        return;
    String factoryClassName = "unknown";
    try {
        final SnmpAgentConfigFactory factory = testContext.getApplicationContext().getBean("snmpPeerFactory", SnmpAgentConfigFactory.class);
        factoryClassName = factory.getClass().getName();
    } catch (final Exception e) {
    // ignore
    }
    if (!factoryClassName.contains("ProxySnmpAgentConfigFactory")) {
        LOG.warn("SNMP Peer Factory ({}) is not the ProxySnmpAgentConfigFactory -- did you forget to include applicationContext-proxy-snmp.xml?", factoryClassName);
    }
    LOG.debug("handleSnmpAgent(testContext, {}, {})", config, useMockSnmpStrategy);
    String host = config.host();
    if (host == null || "".equals(host)) {
        /*
             * NOTE: This call produces different results on different platforms so make
             * sure your client code is aware of this. If you use the {@link ProxySnmpAgentConfigFactory}
             * by including the <code>classpath:/META-INF/opennms/applicationContext-proxy-snmp.xml</code>
             * Spring context, you probably won't need to deal with this. It will override the
             * SnmpPeerFactory with the correct values.
             * 
             * Linux: 127.0.0.1
             * Mac OS: primary external interface
             */
        host = InetAddressUtils.getLocalHostAddressAsString();
    //host = "127.0.0.1";
    }
    final ResourceLoader loader = new DefaultResourceLoader();
    final Resource resource = loader.getResource(config.resource());
    // NOTE: The default value for config.port is specified inside {@link JUnitSnmpAgent}
    final InetAddress hostAddress = addr(host);
    final int port = config.port();
    final SnmpAgentAddress agentAddress = new SnmpAgentAddress(hostAddress, port);
    final SnmpAgentConfigProxyMapper mapper = SnmpAgentConfigProxyMapper.getInstance();
    if (useMockSnmpStrategy) {
        // since it's all virtual, the "mapped" port just points to the real agent address
        mapper.addProxy(hostAddress, agentAddress);
    } else {
        MockSnmpAgent agent = null;
        try {
            agent = MockSnmpAgent.createAgentAndRun(resource.getURL(), str(InetAddress.getLocalHost()) + "/0");
        } catch (Throwable e) {
            agent = MockSnmpAgent.createAgentAndRun(resource.getURL(), str(InetAddressUtils.ONE_TWENTY_SEVEN) + "/0");
        }
        SnmpAgentAddress listenAddress = new SnmpAgentAddress(agent.getInetAddress(), agent.getPort());
        mapper.addProxy(hostAddress, listenAddress);
        testContext.setAttribute(IPADDRESS_KEY, listenAddress.getAddress());
        testContext.setAttribute(PORT_KEY, listenAddress.getPort());
        LOG.debug("using MockSnmpAgent on {} for 'real' address {}", listenAddress, agentAddress);
        provider.addAgent(agentAddress, agent);
    }
    provider.setDataForAddress(agentAddress, resource);
}
Also used : SnmpAgentAddress(org.opennms.netmgt.snmp.SnmpAgentAddress) ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) MockSnmpAgent(org.opennms.mock.snmp.MockSnmpAgent) SnmpAgentConfigFactory(org.opennms.netmgt.config.api.SnmpAgentConfigFactory) Resource(org.springframework.core.io.Resource) InetAddress(java.net.InetAddress) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 3 with SnmpAgentAddress

use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.

the class ProxySnmpAgentConfigFactory method getAgentConfig.

@Override
public SnmpAgentConfig getAgentConfig(final InetAddress address, String location) {
    final SnmpAgentConfigProxyMapper mapper = SnmpAgentConfigProxyMapper.getInstance();
    final SnmpAgentAddress agentAddress = mapper.getAddress(address);
    final String addressString = str(address);
    if (agentAddress == null) {
        LOG.debug("No agent address mapping found for {}!  Try adding a @JUnitSnmpAgent(host=\"{}\", resource=\"...\" entry...", addressString, addressString);
        return super.getAgentConfig(address, location);
    }
    final SnmpAgentConfig config = new SnmpAgentConfig(agentAddress.getAddress());
    config.setProxyFor(address);
    config.setPort(agentAddress.getPort());
    LOG.debug("proxying {} -> {}", addressString, agentAddress);
    return config;
}
Also used : SnmpAgentAddress(org.opennms.netmgt.snmp.SnmpAgentAddress) SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig)

Example 4 with SnmpAgentAddress

use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.

the class ProvisionerIT method testProvisionerServiceRescanAfterAddingSnmpNms7838.

// fail if we take more than five minutes
@Test(timeout = 300000)
// Start the test with an empty SNMP agent
@JUnitSnmpAgent(host = "198.51.100.201", port = 161, resource = "classpath:/snmpwalk-empty.properties")
public void testProvisionerServiceRescanAfterAddingSnmpNms7838() throws Exception {
    importFromResource("classpath:/requisition_then_scan2.xml", Boolean.TRUE.toString());
    final List<OnmsNode> nodes = getNodeDao().findAll();
    final OnmsNode node = nodes.get(0);
    runPendingScans();
    m_nodeDao.flush();
    assertEquals(1, getInterfaceDao().countAll());
    // Make sure that the OnmsIpInterface doesn't have an ifIndex
    assertNull(getInterfaceDao().get(node, "198.51.100.201").getIfIndex());
    // Make sure that no OnmsSnmpInterface records exist for the node
    assertNull(getSnmpInterfaceDao().findByNodeIdAndIfIndex(node.getId(), 4));
    assertNull(getSnmpInterfaceDao().findByNodeIdAndIfIndex(node.getId(), 5));
    LOG.info("******************** ADDING SNMP DATA ********************");
    // Add some SNMP data to the agent
    m_mockSnmpDataProvider.setDataForAddress(new SnmpAgentAddress(addr("198.51.100.201"), 161), new DefaultResourceLoader().getResource("classpath:/snmpTestData3.properties"));
    // Rescan
    m_mockEventIpcManager.sendEventToListeners(nodeUpdated(node.getId()));
    runPendingScans();
    m_nodeDao.flush();
    // Make sure that a second interface was added from the SNMP agent data
    assertEquals(2, getInterfaceDao().countAll());
    // Verify the ifIndex entries
    assertEquals(5, getInterfaceDao().get(node, "198.51.100.201").getIfIndex().intValue());
    assertEquals(5, getInterfaceDao().get(node, "198.51.100.201").getSnmpInterface().getIfIndex().intValue());
    assertEquals(4, getInterfaceDao().get(node, "198.51.100.204").getIfIndex().intValue());
    assertEquals(4, getInterfaceDao().get(node, "198.51.100.204").getSnmpInterface().getIfIndex().intValue());
}
Also used : SnmpAgentAddress(org.opennms.netmgt.snmp.SnmpAgentAddress) OnmsNode(org.opennms.netmgt.model.OnmsNode) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Test(org.junit.Test) JUnitSnmpAgent(org.opennms.core.test.snmp.annotations.JUnitSnmpAgent)

Example 5 with SnmpAgentAddress

use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.

the class MockSnmpStrategy method createWalker.

@Override
public SnmpWalker createWalker(final SnmpAgentConfig agentConfig, final String name, final CollectionTracker tracker) {
    LOG.debug("createWalker({}/{}, {}, {})", InetAddrUtils.str(agentConfig.getAddress()), agentConfig.getPort(), name, tracker.getClass().getName());
    final SnmpAgentAddress aa = new SnmpAgentAddress(agentConfig.getAddress(), agentConfig.getPort());
    final PropertyOidContainer oidContainer = getOidContainer(aa);
    return new MockSnmpWalker(aa, agentConfig.getVersion(), oidContainer, name, tracker, agentConfig.getMaxVarsPerPdu(), agentConfig.getRetries());
}
Also used : SnmpAgentAddress(org.opennms.netmgt.snmp.SnmpAgentAddress)

Aggregations

SnmpAgentAddress (org.opennms.netmgt.snmp.SnmpAgentAddress)8 Test (org.junit.Test)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 InetAddress (java.net.InetAddress)2 JUnitSnmpAgent (org.opennms.core.test.snmp.annotations.JUnitSnmpAgent)2 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Before (org.junit.Before)1 JUnitSnmpAgents (org.opennms.core.test.snmp.annotations.JUnitSnmpAgents)1 MockSnmpAgent (org.opennms.mock.snmp.MockSnmpAgent)1 SnmpAgentConfigFactory (org.opennms.netmgt.config.api.SnmpAgentConfigFactory)1 EventAnticipator (org.opennms.netmgt.dao.mock.EventAnticipator)1 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)1 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1 ResourceLoader (org.springframework.core.io.ResourceLoader)1