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