Search in sources :

Example 1 with PingSweepRequestBuilder

use of org.opennms.netmgt.icmp.proxy.PingSweepRequestBuilder 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)

Aggregations

InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 PingSweepRequestBuilder (org.opennms.netmgt.icmp.proxy.PingSweepRequestBuilder)1 IPPollRange (org.opennms.netmgt.model.discovery.IPPollRange)1 Log (org.opennms.netmgt.xml.event.Log)1