Search in sources :

Example 1 with IncludeRange

use of org.opennms.netmgt.config.discovery.IncludeRange in project opennms by OpenNMS.

the class DiscoveryConfigFactoryTest method testSingleIPExclude.

@Test
public void testSingleIPExclude() throws Exception {
    final DiscoveryConfiguration conf = new DiscoveryConfiguration();
    IncludeRange ir = new IncludeRange();
    ir.setBegin("192.168.0.1");
    ir.setEnd("192.168.0.254");
    conf.addIncludeRange(ir);
    ExcludeRange er = new ExcludeRange();
    er.setBegin("192.168.0.100");
    er.setEnd("192.168.0.100");
    conf.addExcludeRange(er);
    DiscoveryConfigFactory factory = new DiscoveryConfigFactory(conf);
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.0.1")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.0.2")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.0.99")));
    assertTrue(factory.isExcluded(InetAddressUtils.addr("192.168.0.100")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.0.101")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.0.151")));
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) ExcludeRange(org.opennms.netmgt.config.discovery.ExcludeRange) Test(org.junit.Test)

Example 2 with IncludeRange

use of org.opennms.netmgt.config.discovery.IncludeRange in project opennms by OpenNMS.

the class DiscoveryConfigFactoryTest method testMultipleExcludes.

@Test
public void testMultipleExcludes() throws Exception {
    final DiscoveryConfiguration conf = new DiscoveryConfiguration();
    IncludeRange ir = new IncludeRange();
    ir.setBegin("192.168.0.1");
    ir.setEnd("192.168.0.254");
    conf.addIncludeRange(ir);
    ir = new IncludeRange();
    ir.setBegin("192.168.2.1");
    ir.setEnd("192.168.2.255");
    conf.addIncludeRange(ir);
    Specific s = new Specific();
    s.setAddress("192.168.1.1");
    conf.addSpecific(s);
    s = new Specific();
    s.setAddress("192.168.4.1");
    conf.addSpecific(s);
    ExcludeRange er = new ExcludeRange();
    er.setBegin("192.168.0.100");
    er.setEnd("192.168.0.150");
    conf.addExcludeRange(er);
    er = new ExcludeRange();
    er.setBegin("192.168.2.200");
    er.setEnd("192.168.4.254");
    conf.addExcludeRange(er);
    final DiscoveryConfigFactory factory = new DiscoveryConfigFactory(conf);
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.0.1")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.0.2")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.0.99")));
    assertTrue(factory.isExcluded(InetAddressUtils.addr("192.168.0.100")));
    assertTrue(factory.isExcluded(InetAddressUtils.addr("192.168.0.140")));
    assertTrue(factory.isExcluded(InetAddressUtils.addr("192.168.0.150")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.0.151")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.1.1")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.2.1")));
    assertFalse(factory.isExcluded(InetAddressUtils.addr("192.168.2.100")));
    assertTrue(factory.isExcluded(InetAddressUtils.addr("192.168.2.200")));
    assertTrue(factory.isExcluded(InetAddressUtils.addr("192.168.2.220")));
    assertTrue(factory.isExcluded(InetAddressUtils.addr("192.168.2.255")));
    assertTrue(factory.isExcluded(InetAddressUtils.addr("192.168.4.1")));
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) Specific(org.opennms.netmgt.config.discovery.Specific) ExcludeRange(org.opennms.netmgt.config.discovery.ExcludeRange) Test(org.junit.Test)

Example 3 with IncludeRange

use of org.opennms.netmgt.config.discovery.IncludeRange in project opennms by OpenNMS.

the class DiscoveryConfigurationLocationMigratorOfflineTest method testRemoveAttribute.

@Test
public void testRemoveAttribute() throws Exception {
    final DiscoveryConfigurationLocationMigratorOffline task = new DiscoveryConfigurationLocationMigratorOffline();
    task.preExecute();
    task.execute();
    task.postExecute();
    final File configFile = new File(m_tempFolder.getRoot(), "etc/discovery-configuration.xml");
    final DiscoveryConfiguration discoveryConfiguration = JaxbUtils.unmarshal(DiscoveryConfiguration.class, new FileReader(configFile));
    Assert.assertNotNull(discoveryConfiguration);
    Assert.assertEquals(3, discoveryConfiguration.getIncludeRanges().size());
    Assert.assertEquals(3, discoveryConfiguration.getExcludeRanges().size());
    Assert.assertEquals(3, discoveryConfiguration.getSpecifics().size());
    Assert.assertEquals(3, discoveryConfiguration.getIncludeUrls().size());
    int pittsboroLocation = 0;
    int oldDefaultLocation = 0;
    int newDefaultLocation = 0;
    int nullLocation = 0;
    for (IncludeRange e : discoveryConfiguration.getIncludeRanges()) {
        if (DiscoveryConfigurationLocationMigratorOffline.NEW_DEFAULT_LOCATION.equals(e.getLocation().orElse(null))) {
            newDefaultLocation++;
        }
        if (DiscoveryConfigurationLocationMigratorOffline.OLD_DEFAULT_LOCATION.equals(e.getLocation().orElse(null))) {
            oldDefaultLocation++;
        }
        if (!e.getLocation().isPresent()) {
            nullLocation++;
        }
        if ("pittsboro".equals(e.getLocation().orElse(null))) {
            pittsboroLocation++;
        }
    }
    Assert.assertEquals(1, newDefaultLocation);
    Assert.assertEquals(1, pittsboroLocation);
    Assert.assertEquals(1, nullLocation);
    Assert.assertEquals(0, oldDefaultLocation);
    pittsboroLocation = 0;
    oldDefaultLocation = 0;
    newDefaultLocation = 0;
    nullLocation = 0;
    for (Specific e : discoveryConfiguration.getSpecifics()) {
        if (DiscoveryConfigurationLocationMigratorOffline.NEW_DEFAULT_LOCATION.equals(e.getLocation().orElse(null))) {
            newDefaultLocation++;
        }
        if (DiscoveryConfigurationLocationMigratorOffline.OLD_DEFAULT_LOCATION.equals(e.getLocation().orElse(null))) {
            oldDefaultLocation++;
        }
        if (!e.getLocation().isPresent()) {
            nullLocation++;
        }
        if ("pittsboro".equals(e.getLocation().orElse(null))) {
            pittsboroLocation++;
        }
    }
    Assert.assertEquals(1, newDefaultLocation);
    Assert.assertEquals(1, pittsboroLocation);
    Assert.assertEquals(1, nullLocation);
    Assert.assertEquals(0, oldDefaultLocation);
    pittsboroLocation = 0;
    oldDefaultLocation = 0;
    newDefaultLocation = 0;
    nullLocation = 0;
    for (IncludeUrl e : discoveryConfiguration.getIncludeUrls()) {
        if (DiscoveryConfigurationLocationMigratorOffline.NEW_DEFAULT_LOCATION.equals(e.getLocation().orElse(null))) {
            newDefaultLocation++;
        }
        if (DiscoveryConfigurationLocationMigratorOffline.OLD_DEFAULT_LOCATION.equals(e.getLocation().orElse(null))) {
            oldDefaultLocation++;
        }
        if (!e.getLocation().isPresent()) {
            nullLocation++;
        }
        if ("pittsboro".equals(e.getLocation().orElse(null))) {
            pittsboroLocation++;
        }
    }
    Assert.assertEquals(1, newDefaultLocation);
    Assert.assertEquals(1, pittsboroLocation);
    Assert.assertEquals(1, nullLocation);
    Assert.assertEquals(0, oldDefaultLocation);
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) IncludeUrl(org.opennms.netmgt.config.discovery.IncludeUrl) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) FileReader(java.io.FileReader) Specific(org.opennms.netmgt.config.discovery.Specific) File(java.io.File) Test(org.junit.Test)

Example 4 with IncludeRange

use of org.opennms.netmgt.config.discovery.IncludeRange in project opennms by OpenNMS.

the class DiscoveryIntegrationIT method testDiscovery.

@Test
public void testDiscovery() throws Exception {
    // Add a range of localhost IP addresses to ping
    IncludeRange range = new IncludeRange();
    range.setBegin("127.0.5.1");
    range.setEnd("127.0.5.254");
    range.setTimeout(5000l);
    range.setRetries(0);
    range.setLocation(CUSTOM_LOCATION);
    DiscoveryConfiguration config = m_discoveryConfig.getConfiguration();
    // Start immediately
    config.setInitialSleepTime(0l);
    // Discover 255 address ~= 10 seconds
    config.setPacketsPerSecond(25.5);
    // Add a discovery range to the config
    config.clearIncludeRanges();
    config.addIncludeRange(range);
    // Don't actually save the config or we'll overwrite the
    // opennms-base-assembly XML file
    // m_discoveryConfig.saveConfiguration(config);
    // m_discoveryConfig.reload();
    // Anticipate newSuspect events for all of the addresses
    EventAnticipator anticipator = m_eventIpcManager.getEventAnticipator();
    StreamSupport.stream(m_discoveryConfig.getConfiguredAddresses().spliterator(), false).forEach(addr -> {
        System.out.println("ANTICIPATING: " + str(addr.getAddress()));
        Event event = new Event();
        event.setUei(EventConstants.NEW_SUSPECT_INTERFACE_EVENT_UEI);
        event.setInterfaceAddress(addr.getAddress());
        anticipator.anticipateEvent(event);
    });
    // Don't re-init Discovery or it will reload the
    // DiscoveryConfigFactory and erase our changes to
    // the config
    // m_discovery.init();
    m_discovery.start();
    anticipator.waitForAnticipated(120000);
    anticipator.verifyAnticipated();
    anticipator.getAnticipatedEventsReceived().stream().forEach(eachEvent -> {
        Assert.assertNotNull(eachEvent.getParm("location"));
        Assert.assertEquals(CUSTOM_LOCATION, eachEvent.getParm("location").getValue().getContent());
    });
    m_discovery.stop();
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) Event(org.opennms.netmgt.xml.event.Event) EventAnticipator(org.opennms.netmgt.dao.mock.EventAnticipator) Test(org.junit.Test)

Example 5 with IncludeRange

use of org.opennms.netmgt.config.discovery.IncludeRange in project opennms by OpenNMS.

the class DiscoveryScanServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    LOG.info("Loading Discovery configuration.");
    HttpSession sess = request.getSession(true);
    DiscoveryConfiguration config = (DiscoveryConfiguration) sess.getAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION);
    if (config == null) {
        config = new DiscoveryConfiguration();
    }
    // load current general settings
    config = GeneralSettingsLoader.load(request, config);
    String action = request.getParameter("action");
    LOG.debug("action: {}", action);
    // add a Specific
    if (action.equals(addSpecificAction)) {
        LOG.debug("Adding Specific");
        String ipAddr = request.getParameter("specificipaddress");
        String timeout = request.getParameter("specifictimeout");
        String retries = request.getParameter("specificretries");
        String foreignSource = request.getParameter("specificforeignsource");
        String location = request.getParameter("specificlocation");
        Specific newSpecific = new Specific();
        newSpecific.setAddress(ipAddr);
        if (timeout != null && !"".equals(timeout.trim()) && !timeout.equals(String.valueOf(config.getTimeout().orElse(null)))) {
            newSpecific.setTimeout(WebSecurityUtils.safeParseLong(timeout));
        }
        if (retries != null && !"".equals(retries.trim()) && !retries.equals(String.valueOf(config.getRetries().orElse(null)))) {
            newSpecific.setRetries(WebSecurityUtils.safeParseInt(retries));
        }
        if (foreignSource != null && !"".equals(foreignSource.trim()) && !foreignSource.equals(config.getForeignSource().orElse(null))) {
            newSpecific.setForeignSource(foreignSource);
        }
        if (location != null && !"".equals(location.trim()) && !location.equals(config.getLocation().orElse(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID))) {
            newSpecific.setLocation(location);
        }
        config.addSpecific(newSpecific);
    }
    // remove 'Specific' from configuration
    if (action.equals(removeSpecificAction)) {
        LOG.debug("Removing Specific");
        String specificIndex = request.getParameter("index");
        int index = WebSecurityUtils.safeParseInt(specificIndex);
        final int index1 = index;
        Specific spec = config.getSpecifics().get(index1);
        boolean result = config.removeSpecific(spec);
        LOG.debug("Removing Specific result = {}", result);
    }
    // add an 'Include Range'
    if (action.equals(addIncludeRangeAction)) {
        LOG.debug("Adding Include Range");
        String ipAddrBase = request.getParameter("irbase");
        String ipAddrEnd = request.getParameter("irend");
        String timeout = request.getParameter("irtimeout");
        String retries = request.getParameter("irretries");
        String foreignSource = request.getParameter("irforeignsource");
        String location = request.getParameter("irlocation");
        IncludeRange newIR = new IncludeRange();
        newIR.setBegin(ipAddrBase);
        newIR.setEnd(ipAddrEnd);
        if (timeout != null && !"".equals(timeout.trim()) && !timeout.equals(String.valueOf(config.getTimeout().orElse(null)))) {
            newIR.setTimeout(WebSecurityUtils.safeParseLong(timeout));
        }
        if (retries != null && !"".equals(retries.trim()) && !retries.equals(String.valueOf(config.getRetries().orElse(null)))) {
            newIR.setRetries(WebSecurityUtils.safeParseInt(retries));
        }
        if (foreignSource != null && !"".equals(foreignSource.trim()) && !foreignSource.equals(config.getForeignSource().orElse(null))) {
            newIR.setForeignSource(foreignSource);
        }
        if (location != null && !"".equals(location.trim()) && !location.equals(config.getLocation().orElse(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID))) {
            newIR.setLocation(location);
        }
        config.addIncludeRange(newIR);
    }
    // remove 'Include Range' from configuration
    if (action.equals(removeIncludeRangeAction)) {
        LOG.debug("Removing Include Range");
        String specificIndex = request.getParameter("index");
        int index = WebSecurityUtils.safeParseInt(specificIndex);
        final int index1 = index;
        IncludeRange ir = config.getIncludeRanges().get(index1);
        boolean result = config.removeIncludeRange(ir);
        LOG.debug("Removing Include Range result = {}", result);
    }
    // add an 'Include URL'
    if (action.equals(addIncludeUrlAction)) {
        LOG.debug("Adding Include URL");
        String url = request.getParameter("iuurl");
        String timeout = request.getParameter("iutimeout");
        String retries = request.getParameter("iuretries");
        String foreignSource = request.getParameter("iuforeignsource");
        String location = request.getParameter("iulocation");
        IncludeUrl iu = new IncludeUrl();
        iu.setUrl(url);
        if (timeout != null && !"".equals(timeout.trim()) && !timeout.equals(String.valueOf(config.getTimeout().orElse(null)))) {
            iu.setTimeout(WebSecurityUtils.safeParseLong(timeout));
        }
        if (retries != null && !"".equals(retries.trim()) && !retries.equals(String.valueOf(config.getRetries().orElse(null)))) {
            iu.setRetries(WebSecurityUtils.safeParseInt(retries));
        }
        if (foreignSource != null && !"".equals(foreignSource.trim()) && !foreignSource.equals(config.getForeignSource().orElse(null))) {
            iu.setForeignSource(foreignSource);
        }
        if (location != null && !"".equals(location.trim()) && !location.equals(config.getLocation().orElse(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID))) {
            iu.setLocation(location);
        }
        config.addIncludeUrl(iu);
    }
    // remove 'Include URL' from configuration
    if (action.equals(removeIncludeUrlAction)) {
        LOG.debug("Removing Include URL");
        String specificIndex = request.getParameter("index");
        int index = WebSecurityUtils.safeParseInt(specificIndex);
        final int index1 = index;
        IncludeUrl iu = config.getIncludeUrls().get(index1);
        boolean result = config.removeIncludeUrl(iu);
        LOG.debug("Removing Include URL result = {}", result);
    }
    // add an 'Exclude Range'
    if (action.equals(addExcludeRangeAction)) {
        LOG.debug("Adding Exclude Range");
        String ipAddrBegin = request.getParameter("erbegin");
        String ipAddrEnd = request.getParameter("erend");
        ExcludeRange newER = new ExcludeRange();
        newER.setBegin(ipAddrBegin);
        newER.setEnd(ipAddrEnd);
        config.addExcludeRange(newER);
    }
    // remove 'Exclude Range' from configuration
    if (action.equals(removeExcludeRangeAction)) {
        LOG.debug("Removing Exclude Range");
        String specificIndex = request.getParameter("index");
        int index = WebSecurityUtils.safeParseInt(specificIndex);
        final int index1 = index;
        ExcludeRange er = config.getExcludeRanges().get(index1);
        boolean result = config.removeExcludeRange(er);
        LOG.debug("Removing Exclude Range result = {}", result);
    }
    // Submit the discovery job
    if (action.equals(saveAndRestartAction)) {
        try {
            WebApplicationContext beanFactory = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
            // Fetch the ServiceRegistry
            ServiceRegistry registry = beanFactory.getBean(ServiceRegistry.class);
            // Use it to look up a DiscoveryTaskExecutor service
            DiscoveryTaskExecutor executor = registry.findProvider(DiscoveryTaskExecutor.class);
            // If the service exists...
            if (executor != null) {
                // Submit the job to the discovery queue
                executor.handleDiscoveryTask(config);
            } else {
                LOG.warn("No DiscoveryTaskExecutor service is available");
            }
        } catch (Throwable ex) {
            LOG.error("Error while submitting task", ex);
            throw new ServletException(ex);
        }
        // TODO: Send an event here when the scan is started? Or do it on the Camel side?
        /*
        	EventProxy proxy = null;
        	try {
    			proxy = Util.createEventProxy();
    		} catch (Throwable me) {
    			LOG.error(me.getMessage());
    		}

    		EventBuilder bldr = new EventBuilder(EventConstants.DISCOVERYCONFIG_CHANGED_EVENT_UEI, "ActionDiscoveryServlet");
    		bldr.setHost("host");

            try {
            	proxy.send(bldr.getEvent());
            } catch (Throwable me) {
    			LOG.error(me.getMessage());
    		}

            LOG.info("Restart Discovery requested!");
            */
        sess.removeAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION);
        response.sendRedirect(Util.calculateUrlBase(request, "admin/discovery/scan-done.jsp"));
        return;
    }
    sess.setAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION, config);
    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/admin/discovery/edit-scan.jsp");
    dispatcher.forward(request, response);
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) IncludeUrl(org.opennms.netmgt.config.discovery.IncludeUrl) HttpSession(javax.servlet.http.HttpSession) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) Specific(org.opennms.netmgt.config.discovery.Specific) RequestDispatcher(javax.servlet.RequestDispatcher) WebApplicationContext(org.springframework.web.context.WebApplicationContext) DiscoveryTaskExecutor(org.opennms.netmgt.discovery.DiscoveryTaskExecutor) ServletException(javax.servlet.ServletException) ServiceRegistry(org.opennms.core.soa.ServiceRegistry) ExcludeRange(org.opennms.netmgt.config.discovery.ExcludeRange)

Aggregations

IncludeRange (org.opennms.netmgt.config.discovery.IncludeRange)11 DiscoveryConfiguration (org.opennms.netmgt.config.discovery.DiscoveryConfiguration)9 Specific (org.opennms.netmgt.config.discovery.Specific)7 Test (org.junit.Test)6 ExcludeRange (org.opennms.netmgt.config.discovery.ExcludeRange)6 IncludeUrl (org.opennms.netmgt.config.discovery.IncludeUrl)6 RequestDispatcher (javax.servlet.RequestDispatcher)2 ServletException (javax.servlet.ServletException)2 HttpSession (javax.servlet.http.HttpSession)2 DiscoveryConfigFactory (org.opennms.netmgt.config.DiscoveryConfigFactory)2 EventAnticipator (org.opennms.netmgt.dao.mock.EventAnticipator)2 Event (org.opennms.netmgt.xml.event.Event)2 File (java.io.File)1 FileReader (java.io.FileReader)1 StringWriter (java.io.StringWriter)1 UnknownHostException (java.net.UnknownHostException)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 ServiceRegistry (org.opennms.core.soa.ServiceRegistry)1 DiscoveryTaskExecutor (org.opennms.netmgt.discovery.DiscoveryTaskExecutor)1