Search in sources :

Example 1 with BestMatchPingerFactory

use of org.opennms.netmgt.icmp.best.BestMatchPingerFactory 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 2 with BestMatchPingerFactory

use of org.opennms.netmgt.icmp.best.BestMatchPingerFactory 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 3 with BestMatchPingerFactory

use of org.opennms.netmgt.icmp.best.BestMatchPingerFactory in project opennms by OpenNMS.

the class Installer method pingLocalhost.

/**
 * <p>pingLocalhost</p>
 *
 * @throws java.io.IOException if any.
 */
public void pingLocalhost() throws Exception {
    String host = "127.0.0.1";
    java.net.InetAddress addr = null;
    try {
        addr = InetAddress.getByName(host);
    } catch (java.net.UnknownHostException e) {
        System.out.println("UnknownHostException when looking up " + host + ".");
        throw e;
    }
    Pinger pinger;
    try {
        pinger = new BestMatchPingerFactory().getInstance();
    } catch (UnsatisfiedLinkError e) {
        System.out.println("UnsatisfiedLinkError while creating an ICMP Pinger.  Most likely failed to load " + "libjicmp.so.  Try setting the property 'opennms.library.jicmp' to point at the " + "full path name of the libjicmp.so shared library or switch to using the JnaPinger " + "(e.g. 'java -Dopennms.library.jicmp=/some/path/libjicmp.so ...')\n" + "You can also set the 'opennms.library.jicmp6' property in the same manner to specify " + "the location of the JICMP6 library.");
        throw e;
    } catch (NoClassDefFoundError e) {
        System.out.println("NoClassDefFoundError while creating an IcmpSocket.  Most likely failed to load libjicmp.so" + "or libjicmp6.so.");
        throw e;
    } catch (Exception e) {
        System.out.println("Exception while creating Pinger.");
        throw e;
    }
    // using regular InetAddress toString here since is just printed for the users benefit
    System.out.print("Pinging " + host + " (" + addr + ")...");
    Number rtt = pinger.ping(addr);
    if (rtt == null) {
        System.out.println("failed.!");
    } else {
        System.out.printf("successful.. round trip time: %.3f ms%n", rtt.doubleValue() / 1000.0);
    }
}
Also used : InetAddress(java.net.InetAddress) Pinger(org.opennms.netmgt.icmp.Pinger) BestMatchPingerFactory(org.opennms.netmgt.icmp.best.BestMatchPingerFactory) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

Pinger (org.opennms.netmgt.icmp.Pinger)3 BestMatchPingerFactory (org.opennms.netmgt.icmp.best.BestMatchPingerFactory)3 InetAddress (java.net.InetAddress)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1 CmdLineParser (org.kohsuke.args4j.CmdLineParser)1 AbstractPingerFactory (org.opennms.netmgt.icmp.AbstractPingerFactory)1 NullPinger (org.opennms.netmgt.icmp.NullPinger)1 PingerFactory (org.opennms.netmgt.icmp.PingerFactory)1