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);
}
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());
}
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();
}
}
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);
}
});
});
}
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();
}
}
Aggregations