Search in sources :

Example 1 with Ssh

use of org.opennms.netmgt.poller.monitors.support.Ssh in project opennms by OpenNMS.

the class SshIT method setUp.

@Override
public void setUp() throws Exception {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("retries", "0");
    parameters.put("port", "22");
    parameters.put("timeout", Integer.toString(TIMEOUT));
    tt = new TimeoutTracker(parameters, 0, TIMEOUT);
    ssh = new Ssh();
    ssh.setPort(PORT);
    ssh.setTimeout(TIMEOUT);
    good = InetAddressUtils.addr(GOOD_HOST);
}
Also used : HashMap(java.util.HashMap) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) Ssh(org.opennms.netmgt.poller.monitors.support.Ssh)

Example 2 with Ssh

use of org.opennms.netmgt.poller.monitors.support.Ssh in project opennms by OpenNMS.

the class SshMonitor method poll.

/**
     * {@inheritDoc}
     *
     * Poll an {@link InetAddress} for SSH availability.
     *
     * During the poll an attempt is made to connect on the specified port. If
     * the connection request is successful, the banner line generated by the
     * interface is parsed and if the banner text indicates that we are
     * talking
     * to Provided that the interface's response is valid we mark the poll
     * status
     * as available and return.
     * <p>
     * @param address
     * @param parameters
     * @return
     */
public PollStatus poll(final InetAddress address, final Map<String, Object> parameters) {
    TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
    int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);
    String banner = ParameterMap.getKeyedString(parameters, "banner", null);
    String match = ParameterMap.getKeyedString(parameters, "match", null);
    String clientBanner = ParameterMap.getKeyedString(parameters, "client-banner", Ssh.DEFAULT_CLIENT_BANNER);
    PollStatus ps = PollStatus.unavailable();
    Ssh ssh = new Ssh(address, port, tracker.getConnectionTimeout());
    ssh.setClientBanner(clientBanner);
    Pattern regex = null;
    try {
        if (match == null && (banner == null || banner.equals("*"))) {
            regex = null;
        } else if (match != null) {
            regex = Pattern.compile(match);
            LOG.debug("match: /{}/", match);
        } else if (banner != null) {
            regex = Pattern.compile(banner);
            LOG.debug("banner: /{}/", banner);
        }
    } catch (final PatternSyntaxException e) {
        final String matchString = match == null ? banner : match;
        LOG.info("Invalid regular expression for SSH banner match /{}/: {}", matchString, e.getMessage());
        return ps;
    }
    for (tracker.reset(); tracker.shouldRetry() && !ps.isAvailable(); tracker.nextAttempt()) {
        try {
            ps = ssh.poll(tracker);
        } catch (final InsufficientParametersException e) {
            LOG.error("An error occurred polling host '{}'", address, e);
            break;
        }
        if (!ps.isAvailable()) {
            // not able to connect, retry
            continue;
        }
        // only need to test connectivity and we've got that!
        if (regex == null) {
            return ps;
        } else {
            String response = ssh.getServerBanner();
            if (response == null) {
                return PollStatus.unavailable("server closed connection before banner was received.");
            }
            if (regex.matcher(response).find()) {
                LOG.debug("isServer: matching response={}", response);
                return ps;
            } else {
                // Got a response but it didn't match... no need to attempt
                // retries
                LOG.debug("isServer: NON-matching response={}", response);
                return PollStatus.unavailable("server responded, but banner did not match '" + banner + "'");
            }
        }
    }
    return ps;
}
Also used : Pattern(java.util.regex.Pattern) PollStatus(org.opennms.netmgt.poller.PollStatus) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) Ssh(org.opennms.netmgt.poller.monitors.support.Ssh) InsufficientParametersException(org.opennms.netmgt.poller.InsufficientParametersException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

TimeoutTracker (org.opennms.core.utils.TimeoutTracker)2 Ssh (org.opennms.netmgt.poller.monitors.support.Ssh)2 HashMap (java.util.HashMap)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 InsufficientParametersException (org.opennms.netmgt.poller.InsufficientParametersException)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1