Search in sources :

Example 1 with Pinger

use of org.opennms.netmgt.icmp.Pinger 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 Pinger

use of org.opennms.netmgt.icmp.Pinger in project opennms by OpenNMS.

the class Manager method testPinger.

private void testPinger() {
    final PingerFactoryImpl pingerFactory = new PingerFactoryImpl();
    final Pinger pinger = pingerFactory.getInstance();
    boolean hasV4 = pinger.isV4Available();
    boolean hasV6 = pinger.isV6Available();
    LOG.info("Using ICMP implementation: {}", pinger.getClass().getName());
    LOG.info("IPv4 ICMP available? {}", hasV4);
    LOG.info("IPv6 ICMP available? {}", hasV6);
    if (!hasV4) {
        try {
            pinger.initialize4();
            hasV4 = true;
        } catch (final Exception e) {
            LOG.warn("Failed to initialize IPv4 stack.", e);
        }
    }
    if (!hasV6) {
        try {
            pinger.initialize6();
            hasV6 = true;
        } catch (final Exception e) {
            LOG.warn("Failed to initialize IPv6 stack.", e);
        }
    }
    if (!hasV4 && !hasV6) {
        throwPingError("Neither IPv4 nor IPv6 are avaialable.  Bailing.");
    }
    final String requireV4String = System.getProperty("org.opennms.netmgt.icmp.requireV4");
    final String requireV6String = System.getProperty("org.opennms.netmgt.icmp.requireV6");
    if ("true".equalsIgnoreCase(requireV4String) && !hasV4) {
        throwPingError("org.opennms.netmgt.icmp.requireV4 is true, but IPv4 ICMP could not be initialized.");
    }
    if ("true".equalsIgnoreCase(requireV6String) && !hasV6) {
        throwPingError("org.opennms.netmgt.icmp.requireV6 is true, but IPv6 ICMP could not be initialized.");
    }
    // at least one is initialized, and we haven't said otherwise, so barrel ahead
    // but first, reset the pinger factory so we can let auto-detection happen
    pingerFactory.reset();
}
Also used : Pinger(org.opennms.netmgt.icmp.Pinger) PingerFactoryImpl(org.opennms.netmgt.icmp.PingerFactoryImpl) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) UnknownHostException(java.net.UnknownHostException)

Example 3 with Pinger

use of org.opennms.netmgt.icmp.Pinger in project opennms by OpenNMS.

the class CLIPinger method doMain.

public void doMain(String[] args) throws CmdLineException {
    setPropertiesIfPossible();
    CmdLineParser parser = new CmdLineParser(this);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        parser.printUsage(System.err);
        System.exit(1);
    }
    InetAddress host;
    double rttMs;
    if (s_arguments.isEmpty()) {
        parser.printUsage(System.err);
        System.exit(1);
    }
    try {
        host = InetAddress.getByName(s_arguments.get(0));
        final PingerFactory pf = new BestMatchPingerFactory();
        final Pinger p = pf.getInstance(Integer.decode(s_dscp), s_allowFragmentation);
        for (int i = 0; i < s_count; i++) {
            Number rtt = p.ping(host, s_timeout, s_retries);
            if (rtt == null) {
                System.out.println("request timed out");
            } else {
                rttMs = rtt.doubleValue() / 1000.0;
                System.out.println("Reply from " + host.getHostName() + " (" + host.getHostAddress() + "): time=" + rttMs + " ms");
            }
            if (i < s_count - 1) {
                Thread.sleep(s_interval);
            }
        }
    } catch (UnknownHostException ex) {
        System.out.println("Unknown host " + args[0]);
        System.exit(1);
    } catch (Throwable ex) {
        System.out.println("Unexpected exception while pinging " + args[0] + ": " + ex.getMessage());
        ex.printStackTrace();
        System.exit(1);
    } finally {
        System.exit(0);
    }
}
Also used : Pinger(org.opennms.netmgt.icmp.Pinger) PingerFactory(org.opennms.netmgt.icmp.PingerFactory) BestMatchPingerFactory(org.opennms.netmgt.icmp.best.BestMatchPingerFactory) CmdLineParser(org.kohsuke.args4j.CmdLineParser) BestMatchPingerFactory(org.opennms.netmgt.icmp.best.BestMatchPingerFactory) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 4 with Pinger

use of org.opennms.netmgt.icmp.Pinger in project opennms by OpenNMS.

the class Main method initializePinger.

public void initializePinger() {
    final AbstractPingerFactory pingerFactory = new BestMatchPingerFactory();
    if (m_disableIcmp) {
        LOG.info("Disabling ICMP by user request.");
        System.setProperty("org.opennms.netmgt.icmp.pingerClass", "org.opennms.netmgt.icmp.NullPinger");
        pingerFactory.setInstance(0, true, new NullPinger());
        return;
    }
    final String pingerClass = System.getProperty("org.opennms.netmgt.icmp.pingerClass");
    if (pingerClass == null) {
        LOG.info("System property org.opennms.netmgt.icmp.pingerClass is not set; using JnaPinger by default");
        System.setProperty("org.opennms.netmgt.icmp.pingerClass", "org.opennms.netmgt.icmp.jna.JnaPinger");
    }
    LOG.info("Pinger class: {}", System.getProperty("org.opennms.netmgt.icmp.pingerClass"));
    try {
        final Pinger pinger = pingerFactory.getInstance();
        pinger.ping(InetAddress.getLoopbackAddress());
    } catch (final Throwable t) {
        LOG.warn("Unable to get pinger instance.  Setting pingerClass to NullPinger.  For details, see: http://www.opennms.org/wiki/ICMP_could_not_be_initialized");
        System.setProperty("org.opennms.netmgt.icmp.pingerClass", "org.opennms.netmgt.icmp.NullPinger");
        pingerFactory.setInstance(0, true, new NullPinger());
        if (m_gui) {
            final String message = "ICMP (ping) could not be initialized: " + t.getMessage() + "\nDisabling ICMP and using the NullPinger instead." + "\nFor details, see: http://www.opennms.org/wiki/ICMP_could_not_be_initialized";
            JOptionPane.showMessageDialog(null, message, "ICMP Not Available", JOptionPane.WARNING_MESSAGE);
        }
    }
}
Also used : NullPinger(org.opennms.netmgt.icmp.NullPinger) Pinger(org.opennms.netmgt.icmp.Pinger) NullPinger(org.opennms.netmgt.icmp.NullPinger) AbstractPingerFactory(org.opennms.netmgt.icmp.AbstractPingerFactory) BestMatchPingerFactory(org.opennms.netmgt.icmp.best.BestMatchPingerFactory)

Example 5 with Pinger

use of org.opennms.netmgt.icmp.Pinger in project opennms by OpenNMS.

the class BestMatchPingerFactory method tryPinger.

private static PingerMatch tryPinger(final Class<? extends Pinger> pingerClass) {
    boolean v4 = false;
    boolean v6 = false;
    final Pinger pinger;
    try {
        pinger = pingerClass.newInstance();
    } catch (final Throwable t) {
        LOG.info("Failed to get instance of {}: {}", pingerClass, t.getMessage());
        LOG.trace("Failed to get instance of {}.", pingerClass, t);
        return PingerMatch.NONE;
    }
    try {
        if (pinger.isV4Available()) {
            pinger.initialize4();
            v4 = true;
        }
    } catch (final Throwable t) {
        LOG.info("Failed to initialize {} for IPv4: ", pingerClass, t.getMessage());
        LOG.trace("Failed to initialize {} for IPv4.", pingerClass, t);
    }
    try {
        if (pinger.isV6Available()) {
            pinger.initialize6();
            v6 = true;
        }
    } catch (final Throwable t) {
        LOG.info("Failed to initialize {} for IPv4: {}", pingerClass, t.getMessage());
        LOG.trace("Failed to initialize {} for IPv4.", pingerClass, t);
    }
    try {
        final long timeout = Long.valueOf(System.getProperty("org.opennms.netmgt.icmp.best.timeout", "500"), 10);
        final Number result = pinger.ping(InetAddressUtils.getLocalHostAddress(), timeout, 0);
        if (result == null) {
            throw new IllegalStateException("No result pinging localhost.");
        }
    } catch (final Throwable t) {
        LOG.info("Found pinger {}, but it was unable to ping localhost: {}", pingerClass, t.getMessage());
        LOG.trace("Found pinger {}, but it was unable to ping localhost.", pingerClass, t);
        return PingerMatch.NONE;
    }
    if (v4 && v6) {
        return PingerMatch.IPv46;
    } else if (v6) {
        return PingerMatch.IPv6;
    } else if (v4) {
        return PingerMatch.IPv4;
    } else {
        return PingerMatch.NONE;
    }
}
Also used : JniPinger(org.opennms.netmgt.icmp.jni.JniPinger) Pinger(org.opennms.netmgt.icmp.Pinger) JnaPinger(org.opennms.netmgt.icmp.jna.JnaPinger) NullPinger(org.opennms.netmgt.icmp.NullPinger) Jni6Pinger(org.opennms.netmgt.icmp.jni6.Jni6Pinger)

Aggregations

Pinger (org.opennms.netmgt.icmp.Pinger)6 InetAddress (java.net.InetAddress)3 BestMatchPingerFactory (org.opennms.netmgt.icmp.best.BestMatchPingerFactory)3 UnknownHostException (java.net.UnknownHostException)2 NullPinger (org.opennms.netmgt.icmp.NullPinger)2 PingerFactory (org.opennms.netmgt.icmp.PingerFactory)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 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1