Search in sources :

Example 1 with DiscoveryConfigFactory

use of org.opennms.netmgt.config.DiscoveryConfigFactory in project opennms by OpenNMS.

the class RangeChunker method chunk.

public Map<String, List<DiscoveryJob>> chunk(final DiscoveryConfiguration config) {
    final int chunkSize = config.getChunkSize().orElse(DiscoveryConfigFactory.DEFAULT_CHUNK_SIZE);
    final double packetsPerSecond = config.getPacketsPerSecond().orElse(DiscoveryConfigFactory.DEFAULT_PACKETS_PER_SECOND);
    // If the foreign source for the discovery config is not set than use
    // a value of null so that non-requisitioned nodes are created.
    // 
    // TODO: Use the "default" foreign source instead so that we can move
    // away from using non-requisitioned nodes.
    // 
    final String foreignSourceFromConfig = config.getForeignSource().isPresent() ? config.getForeignSource().get().trim() : null;
    // If the monitoring location for the discovery config is not set than use
    // the default localhost location
    final String locationFromConfig = config.getLocation().map(l -> {
        final String trimmed = l.trim();
        if ("".equals(trimmed)) {
            return null;
        }
        return trimmed;
    }).orElse(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID);
    final DiscoveryConfigFactory configFactory = new DiscoveryConfigFactory(config);
    final AtomicReference<IPPollRange> previousRange = new AtomicReference<>();
    return StreamSupport.stream(configFactory.getConfiguredAddresses().spliterator(), false).filter(address -> {
        // If there is no IP address filter set or the filter matches
        return ipAddressFilter.matches(address.getLocation(), address.getAddress());
    }).map(address -> {
        // Create a singleton IPPollRange
        return new IPPollRange(// Make sure that foreignSource is not null so that we can partition on the value
        address.getForeignSource() == null ? foreignSourceFromConfig : address.getForeignSource(), // Make sure that location is not null so that we can partition on the value
        address.getLocation() == null ? locationFromConfig : address.getLocation(), address.getAddress(), address.getAddress(), address.getTimeout(), address.getRetries());
    }).collect(Collectors.groupingBy(range -> {
        // Create a Map<ForeignSourceLocationKey,List<IPPollRange>>
        return new ForeignSourceLocationKey(// Make sure that foreignSource is not null so that we can partition on the value
        range.getForeignSource() == null ? foreignSourceFromConfig : range.getForeignSource(), // Make sure that location is not null so that we can partition on the value
        range.getLocation() == null ? locationFromConfig : range.getLocation());
    }, LinkedHashMap::new, Collectors.toList())).entrySet().stream().flatMap(entry -> {
        // Partition the list of address values
        return Lists.partition(entry.getValue(), chunkSize).stream().map(ranges -> {
            DiscoveryJob retval = new DiscoveryJob(ranges.stream().map(address -> {
                // then just extend the range to cover this address too
                if (isConsecutive(previousRange.get(), address)) {
                    previousRange.get().getAddressRange().incrementEnd();
                    return null;
                }
                previousRange.set(address);
                return address;
            }).filter(Objects::nonNull).collect(Collectors.toList()), entry.getKey().getForeignSource(), entry.getKey().getLocation(), packetsPerSecond);
            // Reset the previousRange value
            previousRange.set(null);
            return retval;
        }).collect(Collectors.toList()).stream();
    }).collect(Collectors.groupingBy(DiscoveryJob::getLocation, LinkedHashMap::new, Collectors.toList()));
}
Also used : DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) IPAddress(org.opennms.core.network.IPAddress) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) List(java.util.List) Lists(com.google.common.collect.Lists) Map(java.util.Map) DiscoveryConfigFactory(org.opennms.netmgt.config.DiscoveryConfigFactory) MonitoringLocationDao(org.opennms.netmgt.dao.api.MonitoringLocationDao) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange) BigInteger(java.math.BigInteger) StreamSupport(java.util.stream.StreamSupport) Preconditions(com.google.common.base.Preconditions) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange) AtomicReference(java.util.concurrent.atomic.AtomicReference) DiscoveryConfigFactory(org.opennms.netmgt.config.DiscoveryConfigFactory)

Example 2 with DiscoveryConfigFactory

use of org.opennms.netmgt.config.DiscoveryConfigFactory in project opennms by OpenNMS.

the class DiscoveryIntegrationIT method testDiscoveryTaskExecutor.

@Test
public void testDiscoveryTaskExecutor() 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.setBegin("192.168.99.1");
    range.setEnd("192.168.99.100");
    range.setTimeout(5000l);
    range.setRetries(0);
    DiscoveryConfiguration config = new DiscoveryConfiguration();
    config.setInitialSleepTime(0l);
    // 100 addresses at 10 per second should take at least 10 seconds
    config.setPacketsPerSecond(10d);
    config.clearIncludeRanges();
    config.addIncludeRange(range);
    // Anticipate newSuspect events for all of the addresses
    EventAnticipator anticipator = m_eventIpcManager.getEventAnticipator();
    StreamSupport.stream(new DiscoveryConfigFactory(config).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);
    });
    Date beforeTime = new Date();
    // Invoke a one-time scan via the DiscoveryTaskExecutor service
    m_taskExecutor.handleDiscoveryTask(config);
    Date afterTime = new Date();
    // Make sure that this call returns quickly as an async. call
    long timespan = (afterTime.getTime() - beforeTime.getTime());
    System.out.println("Task executor invocation took " + timespan + "ms");
    assertTrue("Timespan was not less than 8 seconds: " + timespan, timespan < 8000L);
    anticipator.waitForAnticipated(60000);
    anticipator.verifyAnticipated();
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) Event(org.opennms.netmgt.xml.event.Event) DiscoveryConfigFactory(org.opennms.netmgt.config.DiscoveryConfigFactory) Date(java.util.Date) EventAnticipator(org.opennms.netmgt.dao.mock.EventAnticipator) Test(org.junit.Test)

Example 3 with DiscoveryConfigFactory

use of org.opennms.netmgt.config.DiscoveryConfigFactory in project opennms by OpenNMS.

the class ActionDiscoveryServlet method getDiscoveryConfig.

/**
 * <p>getDiscoveryConfig</p>
 *
 * @return a {@link org.opennms.netmgt.config.discovery.DiscoveryConfiguration} object.
 * @throws ServletException
 */
public static DiscoveryConfiguration getDiscoveryConfig() throws ServletException {
    DiscoveryConfiguration config = null;
    try {
        DiscoveryConfigFactory factory = DiscoveryConfigFactory.getInstance();
        factory.reload();
        config = factory.getConfiguration();
    } catch (final Exception e) {
        throw new ServletException("Could not load configuration: " + e.getMessage(), e);
    }
    return config;
}
Also used : ServletException(javax.servlet.ServletException) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) DiscoveryConfigFactory(org.opennms.netmgt.config.DiscoveryConfigFactory) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 4 with DiscoveryConfigFactory

use of org.opennms.netmgt.config.DiscoveryConfigFactory in project opennms by OpenNMS.

the class ActionDiscoveryServlet 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 = getDiscoveryConfig();
        sess.setAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION, config);
    }
    // Update general settings from the incoming request parameters
    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);
    }
    // save configuration and restart discovery service
    if (action.equals(saveAndRestartAction)) {
        DiscoveryConfigFactory dcf = null;
        try {
            StringWriter configString = new StringWriter();
            JaxbUtils.marshal(config, configString);
            LOG.debug(configString.toString().trim());
            dcf = DiscoveryConfigFactory.getInstance();
            dcf.saveConfiguration(config);
        } catch (Throwable ex) {
            LOG.error("Error while saving configuration. {}", ex);
            throw new ServletException(ex);
        }
        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/config-done.jsp"));
        return;
    }
    sess.setAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION, config);
    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/admin/discovery/edit-config.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) ServletException(javax.servlet.ServletException) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) StringWriter(java.io.StringWriter) DiscoveryConfigFactory(org.opennms.netmgt.config.DiscoveryConfigFactory) ExcludeRange(org.opennms.netmgt.config.discovery.ExcludeRange) EventProxy(org.opennms.netmgt.events.api.EventProxy)

Aggregations

DiscoveryConfigFactory (org.opennms.netmgt.config.DiscoveryConfigFactory)4 DiscoveryConfiguration (org.opennms.netmgt.config.discovery.DiscoveryConfiguration)4 ServletException (javax.servlet.ServletException)2 IncludeRange (org.opennms.netmgt.config.discovery.IncludeRange)2 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 BigInteger (java.math.BigInteger)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 HttpSession (javax.servlet.http.HttpSession)1 Test (org.junit.Test)1