Search in sources :

Example 1 with IPPollAddress

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

the class DiscoveryConfigFactoryTest method testAddToSpecificsFromURLViaStream.

@Test
public void testAddToSpecificsFromURLViaStream() throws Exception {
    final List<IPPollAddress> specifics = new ArrayList<IPPollAddress>();
    final InputStream in = this.getClass().getResourceAsStream("validDiscoveryIncludeFile.txt");
    final long timeout = 100;
    final int retries = 1;
    DiscoveryConfigFactory.addToSpecificsFromURL(specifics, in, null, null, timeout, retries);
    assertEquals(8, specifics.size());
    assertEquals("127.0.0.1", InetAddressUtils.str(specifics.get(0).getAddress()));
    assertEquals("10.1.1.1", InetAddressUtils.str(specifics.get(1).getAddress()));
    assertEquals("10.2.1.1", InetAddressUtils.str(specifics.get(2).getAddress()));
    assertEquals("8.8.8.8", InetAddressUtils.str(specifics.get(3).getAddress()));
    assertEquals("fe80:0000:0000:0000:ffff:eeee:dddd:cccc", InetAddressUtils.str(specifics.get(4).getAddress()));
    assertEquals("0000:0000:0000:0000:0000:0000:0000:0001", InetAddressUtils.str(specifics.get(5).getAddress()));
    assertEquals("fe80:0000:0000:0000:ffff:eeee:dddd:cccd", InetAddressUtils.str(specifics.get(6).getAddress()));
    assertEquals("fe80:0000:0000:0000:ffff:eeee:dddd:cccc", InetAddressUtils.str(specifics.get(7).getAddress()));
}
Also used : IPPollAddress(org.opennms.netmgt.model.discovery.IPPollAddress) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with IPPollAddress

use of org.opennms.netmgt.model.discovery.IPPollAddress 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) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange)

Example 3 with IPPollAddress

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

the class DiscoveryConfigFactory method addToSpecificsFromURL.

/**
     * <p>addToSpecificsFromURL</p>
     *
     * @param specifics a {@link java.util.List} object.
     * @param is a {@link java.io.InputStream} object.
     * @param timeout a long.
     * @param retries a int.
     * @return a boolean.
     * @throws java.io.IOException if any.
     */
public static boolean addToSpecificsFromURL(final List<IPPollAddress> specifics, final InputStream is, final String foreignSource, final String location, final long timeout, final int retries) throws IOException {
    boolean bRet = true;
    try {
        final BufferedReader buffer = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
        String ipLine = null;
        String specIP = null;
        // get each line of the file and turn it into a specific range
        while ((ipLine = buffer.readLine()) != null) {
            ipLine = ipLine.trim();
            if (ipLine.length() == 0 || ipLine.charAt(0) == DiscoveryConfigFactory.COMMENT_CHAR) {
                // blank line or skip comment
                continue;
            }
            // check for comments after IP
            final int comIndex = ipLine.indexOf(DiscoveryConfigFactory.COMMENT_STR);
            if (comIndex == -1) {
                specIP = ipLine;
            } else {
                specIP = ipLine.substring(0, comIndex);
                specIP = specIP.trim();
            }
            try {
                specifics.add(new IPPollAddress(foreignSource, location, InetAddressUtils.addr(specIP), timeout, retries));
            } catch (final IllegalArgumentException e) {
                LOG.warn("Unknown host \'{}\' inside discovery include file: address ignored", specIP);
            }
            specIP = null;
        }
    } catch (final UnsupportedEncodingException e) {
        LOG.error("Your JVM doesn't support UTF-8");
        return false;
    }
    return bRet;
}
Also used : IPPollAddress(org.opennms.netmgt.model.discovery.IPPollAddress) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with IPPollAddress

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

the class DiscoveryConfigFactory method getURLSpecifics.

/**
     * <p>getURLSpecifics</p>
     *
     * @return a {@link java.util.List} object.
     */
@Override
public List<IPPollAddress> getURLSpecifics() {
    final List<IPPollAddress> specifics = new LinkedList<IPPollAddress>();
    getReadLock().lock();
    try {
        Long defaultTimeout = getConfiguration().getTimeout().orElse(DEFAULT_TIMEOUT);
        Integer defaultRetries = getConfiguration().getRetries().orElse(DEFAULT_RETRIES);
        for (final IncludeUrl url : getConfiguration().getIncludeUrls()) {
            long timeout = url.getTimeout().orElse(defaultTimeout);
            int retries = url.getRetries().orElse(defaultRetries);
            addToSpecificsFromURL(specifics, url.getUrl().orElse(null), url.getForeignSource().orElse(null), url.getLocation().orElse(null), timeout, retries);
        }
        return specifics;
    } finally {
        getReadLock().unlock();
    }
}
Also used : IPPollAddress(org.opennms.netmgt.model.discovery.IPPollAddress) IncludeUrl(org.opennms.netmgt.config.discovery.IncludeUrl) LinkedList(java.util.LinkedList)

Example 5 with IPPollAddress

use of org.opennms.netmgt.model.discovery.IPPollAddress 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)

Aggregations

IPPollAddress (org.opennms.netmgt.model.discovery.IPPollAddress)7 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 Test (org.junit.Test)2 IPPollRange (org.opennms.netmgt.model.discovery.IPPollRange)2 Throwables (com.google.common.base.Throwables)1 Sets (com.google.common.collect.Sets)1 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetAddress (java.net.InetAddress)1 URL (java.net.URL)1 List (java.util.List)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1