Search in sources :

Example 1 with IPPollRange

use of org.opennms.netmgt.model.discovery.IPPollRange in project opennms by OpenNMS.

the class PingSweepRpcModule method execute.

@Override
public CompletableFuture<PingSweepResponseDTO> execute(PingSweepRequestDTO request) {
    final Pinger pinger = pingerFactory.getInstance();
    final PingSweepResultTracker tracker = new PingSweepResultTracker();
    String location = request.getLocation();
    int packetSize = request.getPacketSize();
    List<IPPollRange> ranges = new ArrayList<>();
    for (IPRangeDTO dto : request.getIpRanges()) {
        IPPollRange pollRange = new IPPollRange(null, location, dto.getBegin(), dto.getEnd(), dto.getTimeout(), dto.getRetries());
        ranges.add(pollRange);
    }
    // Use a RateLimiter to limit the ping packets per second that we send
    RateLimiter limiter = RateLimiter.create(request.getPacketsPerSecond());
    List<IPPollAddress> addresses = StreamSupport.stream(getAddresses(ranges).spliterator(), false).filter(j -> j.getAddress() != null).collect(Collectors.toList());
    return CompletableFuture.supplyAsync(() -> {
        addresses.stream().forEach(pollAddress -> {
            try {
                tracker.expectCallbackFor(pollAddress.getAddress());
                limiter.acquire();
                pinger.ping(pollAddress.getAddress(), pollAddress.getTimeout(), pollAddress.getRetries(), packetSize, 1, tracker);
            } catch (Exception e) {
                tracker.handleError(pollAddress.getAddress(), null, e);
                tracker.completeExceptionally(e);
            }
        });
        try {
            tracker.getLatch().await();
        } catch (InterruptedException e) {
            throw Throwables.propagate(e);
        }
        tracker.complete();
        return tracker.getResponse();
    }, executor);
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) PingerFactory(org.opennms.netmgt.icmp.PingerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) RateLimiter(com.google.common.util.concurrent.RateLimiter) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) EchoPacket(org.opennms.netmgt.icmp.EchoPacket) PingResponseCallback(org.opennms.netmgt.icmp.PingResponseCallback) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange) StreamSupport(java.util.stream.StreamSupport) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) IteratorUtils(org.opennms.core.utils.IteratorUtils) Iterator(java.util.Iterator) Set(java.util.Set) Throwables(com.google.common.base.Throwables) Pinger(org.opennms.netmgt.icmp.Pinger) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Sets(com.google.common.collect.Sets) IPPollAddress(org.opennms.netmgt.model.discovery.IPPollAddress) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractXmlRpcModule(org.opennms.core.rpc.xml.AbstractXmlRpcModule) List(java.util.List) Component(org.springframework.stereotype.Component) Pinger(org.opennms.netmgt.icmp.Pinger) ArrayList(java.util.ArrayList) RateLimiter(com.google.common.util.concurrent.RateLimiter) IPPollAddress(org.opennms.netmgt.model.discovery.IPPollAddress) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange)

Example 2 with IPPollRange

use of org.opennms.netmgt.model.discovery.IPPollRange in project opennms by OpenNMS.

the class RangeChunkerTest method testCoalesceConsecutiveSpecifics.

@Test
public void testCoalesceConsecutiveSpecifics() {
    DiscoveryConfiguration config = new DiscoveryConfiguration();
    for (int i = 0; i < 5; i++) {
        Specific specific = new Specific();
        specific.setAddress("10.0.0." + i);
        specific.setForeignSource("ABC");
        specific.setLocation("123");
        specific.setRetries(1);
        specific.setTimeout(1000l);
        config.addSpecific(specific);
    }
    Map<String, List<DiscoveryJob>> jobs = new RangeChunker(ipAddressFilter).chunk(config);
    printJobs(jobs);
    // The specifics have been combined into one range
    assertEquals(1, jobs.size());
    assertEquals(1, jobs.get("123").get(0).getRanges().size());
    IPPollRange range = jobs.get("123").get(0).getRanges().iterator().next();
    assertEquals("10.0.0.0", new IPAddress(range.getAddressRange().getBegin()).toString());
    assertEquals("10.0.0.4", new IPAddress(range.getAddressRange().getEnd()).toString());
}
Also used : DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) List(java.util.List) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange) Specific(org.opennms.netmgt.config.discovery.Specific) IPAddress(org.opennms.core.network.IPAddress) Test(org.junit.Test)

Example 3 with IPPollRange

use of org.opennms.netmgt.model.discovery.IPPollRange in project opennms by OpenNMS.

the class DiscoveryConfigFactory method getConfiguredAddresses.

/**
 * <p>getConfiguredAddresses</p>
 *
 * TODO: This function is inefficient. It has O(n^2) complexity based on the
 * product of the include ranges and exclude ranges. This might cause problems
 * if users are using a large number of excluded ranges.
 *
 * @return a {@link java.lang.Iterable} object.
 */
@Override
public Iterable<IPPollAddress> getConfiguredAddresses() {
    getReadLock().lock();
    try {
        final List<IPPollAddress> specifics = getSpecifics();
        final List<IPPollRange> ranges = getRanges();
        specifics.addAll(getURLSpecifics());
        final List<Iterator<IPPollAddress>> iters = new ArrayList<Iterator<IPPollAddress>>();
        iters.add(specifics.iterator());
        for (final IPPollRange range : ranges) {
            iters.add(getExcludingInterator(range.iterator()));
        }
        return IteratorUtils.concatIterators(iters);
    } finally {
        getReadLock().unlock();
    }
}
Also used : IPPollAddress(org.opennms.netmgt.model.discovery.IPPollAddress) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange)

Example 4 with IPPollRange

use of org.opennms.netmgt.model.discovery.IPPollRange in project opennms by OpenNMS.

the class DiscoveryTaskExecutorImpl method triggerNextJobAsync.

private void triggerNextJobAsync(String location, Queue<DiscoveryJob> jobs, AtomicInteger jobIndexTracker, int totalNumberOfJobs, int taskId, CompletableFuture<Void> future) {
    final DiscoveryJob job = jobs.poll();
    if (job == null) {
        future.complete(null);
        return;
    }
    // Build the request
    final PingSweepRequestBuilder builder = locationAwarePingClient.sweep().withLocation(job.getLocation()).withPacketsPerSecond(job.getPacketsPerSecond());
    for (IPPollRange range : job.getRanges()) {
        try {
            InetAddress begin = InetAddress.getByAddress(range.getAddressRange().getBegin());
            InetAddress end = InetAddress.getByAddress(range.getAddressRange().getEnd());
            builder.withRange(begin, end, range.getRetries(), range.getTimeout(), TimeUnit.MILLISECONDS);
        } catch (UnknownHostException e) {
            LOG.error("Failed to retrieve addresses from range: {}. The range will be skipped.", e);
        }
    }
    final int jobIndex = jobIndexTracker.incrementAndGet();
    LOG.debug("Starting job {} of {} at location {} (on task #{}).", jobIndex, totalNumberOfJobs, location, taskId);
    builder.execute().whenComplete((summary, ex) -> {
        // When finished, used the calling thread to generate the newSuspect events
        Logging.withPrefix(Discovery.getLoggingCategory(), new Runnable() {

            @Override
            public void run() {
                if (summary != null) {
                    LOG.debug("Job {} of {} at location {} (on task #{}) completed succesfully.", jobIndex, totalNumberOfJobs, location, taskId);
                    // Generate an event log containing a newSuspect event for every host
                    // that responded to our pings
                    final Log eventLog = toNewSuspectEvents(job, summary);
                    // Avoid forwarding an empty log
                    if (eventLog.getEvents() != null && eventLog.getEvents().getEventCount() >= 1) {
                        eventForwarder.sendNow(toNewSuspectEvents(job, summary));
                    }
                } else {
                    LOG.error("An error occurred while processing job {} of {} at location {} (on task #{})." + " No newSuspect events will be generated.", jobIndex, totalNumberOfJobs, location, taskId, ex);
                }
                // Recurse until the queue is empty
                triggerNextJobAsync(location, jobs, jobIndexTracker, totalNumberOfJobs, taskId, future);
            }
        });
    });
}
Also used : PingSweepRequestBuilder(org.opennms.netmgt.icmp.proxy.PingSweepRequestBuilder) UnknownHostException(java.net.UnknownHostException) Log(org.opennms.netmgt.xml.event.Log) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange) InetAddress(java.net.InetAddress)

Example 5 with IPPollRange

use of org.opennms.netmgt.model.discovery.IPPollRange in project opennms by OpenNMS.

the class DiscoveryConfigFactory method getRanges.

/**
 * <p>getRanges</p>
 *
 * @return a {@link java.util.List} object.
 */
@Override
public List<IPPollRange> getRanges() {
    final List<IPPollRange> includes = new LinkedList<IPPollRange>();
    getReadLock().lock();
    try {
        Long defaultTimeout = getConfiguration().getTimeout().orElse(DEFAULT_TIMEOUT);
        Integer defaultRetries = getConfiguration().getRetries().orElse(DEFAULT_RETRIES);
        for (final IncludeRange ir : getConfiguration().getIncludeRanges()) {
            // Validate IP range; if invalid, then log and discard this range
            try {
                InetAddressUtils.toIpAddrBytes(ir.getBegin());
            } catch (Throwable e) {
                LOG.warn("Begin address of discovery range is invalid, discarding: {}", ir.getBegin());
                continue;
            }
            try {
                InetAddressUtils.toIpAddrBytes(ir.getEnd());
            } catch (Throwable e) {
                LOG.warn("End address of discovery range is invalid, discarding: {}", ir.getEnd());
                continue;
            }
            long timeout = ir.getTimeout().orElse(defaultTimeout);
            int retries = ir.getRetries().orElse(defaultRetries);
            try {
                includes.add(new IPPollRange(ir.getForeignSource().orElse(null), ir.getLocation().orElse(null), ir.getBegin(), ir.getEnd(), timeout, retries));
            } catch (final UnknownHostException uhE) {
                LOG.warn("Failed to convert address range ({}, {})", ir.getBegin(), ir.getEnd(), uhE);
            }
        }
        return includes;
    } finally {
        getReadLock().unlock();
    }
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) UnknownHostException(java.net.UnknownHostException) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange) LinkedList(java.util.LinkedList)

Aggregations

IPPollRange (org.opennms.netmgt.model.discovery.IPPollRange)6 List (java.util.List)3 InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 Collectors (java.util.stream.Collectors)2 StreamSupport (java.util.stream.StreamSupport)2 IPAddress (org.opennms.core.network.IPAddress)2 DiscoveryConfiguration (org.opennms.netmgt.config.discovery.DiscoveryConfiguration)2 IPPollAddress (org.opennms.netmgt.model.discovery.IPPollAddress)2 Preconditions (com.google.common.base.Preconditions)1 Throwables (com.google.common.base.Throwables)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 BigInteger (java.math.BigInteger)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1