Search in sources :

Example 1 with PingSummary

use of org.opennms.netmgt.icmp.proxy.PingSummary in project opennms by OpenNMS.

the class PingCommand method execute.

@Override
public Object execute() throws Exception {
    LOG.debug("ping:ping {} {}", m_location != null ? "-l " + m_location : "", m_host);
    final InetAddress byName = InetAddress.getByName(m_host);
    final PingRequestBuilder.Callback callback = (newSequence, summary) -> {
        if (m_count > 1) {
            if (summary.getSequences().size() == 0) {
                System.out.println(PingStringUtils.renderHeader(summary));
            }
            if (newSequence != null) {
                System.out.println(PingStringUtils.renderSequence(summary.getRequest(), newSequence));
            }
            if (summary.isComplete()) {
                System.out.println(PingStringUtils.renderSummary(summary));
            }
        }
    };
    final CompletableFuture<PingSummary> future = locationAwarePingClient.ping(byName).withLocation(m_location).withSystemId(m_systemId).withNumberOfRequests(m_count).withProgressCallback(callback).execute();
    while (true) {
        try {
            PingSummary summary = future.get(1, TimeUnit.SECONDS);
            // In case of m_count > 1, the callback takes care of the "karaf.log" output.
            if (m_count == 1) {
                System.out.println(String.format("PING: %s %.3f ms", byName, summary.getSequence(0).getResponse().getRtt()));
            }
            break;
        } catch (TimeoutException e) {
        // pass
        }
        System.out.print(".");
    }
    return null;
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) PingStringUtils(org.opennms.netmgt.icmp.proxy.PingStringUtils) CompletableFuture(java.util.concurrent.CompletableFuture) Argument(org.apache.karaf.shell.api.action.Argument) Command(org.apache.karaf.shell.api.action.Command) PingSummary(org.opennms.netmgt.icmp.proxy.PingSummary) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) Action(org.apache.karaf.shell.api.action.Action) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) LocationAwarePingClient(org.opennms.netmgt.icmp.proxy.LocationAwarePingClient) Service(org.apache.karaf.shell.api.action.lifecycle.Service) PingRequestBuilder(org.opennms.netmgt.icmp.proxy.PingRequestBuilder) Option(org.apache.karaf.shell.api.action.Option) PingSummary(org.opennms.netmgt.icmp.proxy.PingSummary) InetAddress(java.net.InetAddress) PingRequestBuilder(org.opennms.netmgt.icmp.proxy.PingRequestBuilder) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with PingSummary

use of org.opennms.netmgt.icmp.proxy.PingSummary in project opennms by OpenNMS.

the class DefaultPollContext method testCriticalPath.

private boolean testCriticalPath(CriticalPath criticalPath) {
    if (!"ICMP".equalsIgnoreCase(criticalPath.getServiceName())) {
        LOG.warn("Critical paths using services other than ICMP are not currently supported." + " ICMP will be used for testing {}.", criticalPath);
    }
    final InetAddress ipAddress = criticalPath.getIpAddress();
    final int retries = OpennmsServerConfigFactory.getInstance().getDefaultCriticalPathRetries();
    final int timeout = OpennmsServerConfigFactory.getInstance().getDefaultCriticalPathTimeout();
    boolean available = false;
    try {
        final PingSummary pingSummary = m_locationAwarePingClient.ping(ipAddress).withLocation(criticalPath.getLocationName()).withTimeout(timeout, TimeUnit.MILLISECONDS).withRetries(retries).execute().get();
        // We consider the path to be available if any of the requests were successful
        available = pingSummary.getSequences().stream().filter(PingSequence::isSuccess).count() > 0;
    } catch (InterruptedException e) {
        LOG.warn("Interrupted while testing {}. Marking the path as available.", criticalPath);
        available = true;
    } catch (Throwable e) {
        final Throwable cause = e.getCause();
        if (cause != null && cause instanceof RequestTimedOutException) {
            LOG.warn("No response was received when remotely testing {}." + " Marking the path as available.", criticalPath);
            available = true;
        } else if (cause != null && cause instanceof RequestRejectedException) {
            LOG.warn("Request was rejected when attemtping to test the remote path {}." + " Marking the path as available.", criticalPath);
            available = true;
        }
        LOG.warn("An unknown error occured while testing the critical path: {}." + " Marking the path as unavailable.", criticalPath, e);
        available = false;
    }
    LOG.debug("testCriticalPath: checking {}@{}, available ? {}", criticalPath.getServiceName(), ipAddress, available);
    return available;
}
Also used : RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) PingSummary(org.opennms.netmgt.icmp.proxy.PingSummary) RequestRejectedException(org.opennms.core.rpc.api.RequestRejectedException) InetAddress(java.net.InetAddress)

Example 3 with PingSummary

use of org.opennms.netmgt.icmp.proxy.PingSummary in project opennms by OpenNMS.

the class SinglePingExecutionStrategy method execute.

@Override
public CompletableFuture<PingSummary> execute(PingRequestDTO requestDTO) {
    return client.execute(requestDTO).thenApply(responseDTO -> {
        final PingResponse pingResponse = new PingResponse();
        pingResponse.setRtt(responseDTO.getRtt());
        PingSummary summary = new PingSummary(requestDTO.toPingRequest(), 1);
        summary.addSequence(1, pingResponse);
        return summary;
    });
}
Also used : PingSummary(org.opennms.netmgt.icmp.proxy.PingSummary) PingResponse(org.opennms.netmgt.icmp.proxy.PingResponse)

Example 4 with PingSummary

use of org.opennms.netmgt.icmp.proxy.PingSummary in project opennms by OpenNMS.

the class PingCommand method doExecute.

@Override
protected Object doExecute() throws Exception {
    LOG.debug("ping:ping {} {}", m_location != null ? "-l " + m_location : "", m_host);
    final InetAddress byName = InetAddress.getByName(m_host);
    final PingRequestBuilder.Callback callback = (newSequence, summary) -> {
        if (m_count > 1) {
            if (summary.getSequences().size() == 0) {
                System.out.println(PingStringUtils.renderHeader(summary));
            }
            if (newSequence != null) {
                System.out.println(PingStringUtils.renderSequence(summary.getRequest(), newSequence));
            }
            if (summary.isComplete()) {
                System.out.println(PingStringUtils.renderSummary(summary));
            }
        }
    };
    final CompletableFuture<PingSummary> future = locationAwarePingClient.ping(byName).withLocation(m_location).withNumberOfRequests(m_count).withProgressCallback(callback).execute();
    while (true) {
        try {
            PingSummary summary = future.get(1, TimeUnit.SECONDS);
            // In case of m_count > 1, the callback takes care of the "karaf.log" output.
            if (m_count == 1) {
                System.out.println(String.format("PING: %s %.3f ms", byName, summary.getSequence(0).getResponse().getRtt()));
            }
            break;
        } catch (TimeoutException e) {
        // pass
        }
        System.out.print(".");
    }
    return null;
}
Also used : Argument(org.apache.felix.gogo.commands.Argument) Logger(org.slf4j.Logger) OsgiCommandSupport(org.apache.karaf.shell.console.OsgiCommandSupport) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) PingStringUtils(org.opennms.netmgt.icmp.proxy.PingStringUtils) CompletableFuture(java.util.concurrent.CompletableFuture) PingSummary(org.opennms.netmgt.icmp.proxy.PingSummary) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) LocationAwarePingClient(org.opennms.netmgt.icmp.proxy.LocationAwarePingClient) PingRequestBuilder(org.opennms.netmgt.icmp.proxy.PingRequestBuilder) Command(org.apache.felix.gogo.commands.Command) Option(org.apache.felix.gogo.commands.Option) PingSummary(org.opennms.netmgt.icmp.proxy.PingSummary) InetAddress(java.net.InetAddress) PingRequestBuilder(org.opennms.netmgt.icmp.proxy.PingRequestBuilder) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

PingSummary (org.opennms.netmgt.icmp.proxy.PingSummary)4 InetAddress (java.net.InetAddress)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 LocationAwarePingClient (org.opennms.netmgt.icmp.proxy.LocationAwarePingClient)2 PingRequestBuilder (org.opennms.netmgt.icmp.proxy.PingRequestBuilder)2 PingStringUtils (org.opennms.netmgt.icmp.proxy.PingStringUtils)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Argument (org.apache.felix.gogo.commands.Argument)1 Command (org.apache.felix.gogo.commands.Command)1 Option (org.apache.felix.gogo.commands.Option)1 Action (org.apache.karaf.shell.api.action.Action)1 Argument (org.apache.karaf.shell.api.action.Argument)1 Command (org.apache.karaf.shell.api.action.Command)1 Option (org.apache.karaf.shell.api.action.Option)1 Reference (org.apache.karaf.shell.api.action.lifecycle.Reference)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 OsgiCommandSupport (org.apache.karaf.shell.console.OsgiCommandSupport)1