use of org.opennms.netmgt.poller.InsufficientParametersException in project opennms by OpenNMS.
the class Ssh method tryConnect.
/**
* Attempt to connect, based on the parameters which have been set in
* the object.
*
* @return true if it is able to connect
* @throws org.opennms.netmgt.protocols.InsufficientParametersException if any.
*/
protected boolean tryConnect() throws InsufficientParametersException {
if (getAddress() == null) {
throw new InsufficientParametersException("you must specify an address");
}
try {
m_socket = new Socket();
m_socket.setTcpNoDelay(true);
m_socket.connect(new InetSocketAddress(getAddress(), getPort()), getTimeout());
m_socket.setSoTimeout(getTimeout());
m_reader = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
m_writer = m_socket.getOutputStream();
// read the banner
m_serverBanner = m_reader.readLine();
// write our own
m_writer.write((getClientBanner() + "\r\n").getBytes());
// then, disconnect
disconnect();
return true;
} catch (final NumberFormatException e) {
LOG.debug("unable to parse server version", e);
setError(e);
disconnect();
} catch (final ConnectException e) {
LOG.debug("connection failed: {}", e.getMessage());
setError(e);
disconnect();
} catch (final Throwable e) {
LOG.debug("connection failed", e);
setError(e);
disconnect();
}
return false;
}
use of org.opennms.netmgt.poller.InsufficientParametersException 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;
}
Aggregations