use of org.opennms.core.utils.TimeoutTracker in project opennms by OpenNMS.
the class CitrixMonitor method poll.
// read()
/**
* {@inheritDoc}
*
* Poll the specified address for Citrix service availability.
*
* During the poll an attempt is made to connect on the specified port (by
* default port 1494). If the connection request is successful, the banner
* line generated by the interface is parsed and if the extracted return
* code indicates that we are talking to an Citrix server ('ICA' appears in
* the response) we set the service status to SERVICE_AVAILABLE and return.
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
// Get the category logger
//
// get the parameters
//
TimeoutTracker timeoutTracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);
// Extract the address
//
InetAddress ipv4Addr = svc.getAddress();
String host = InetAddressUtils.str(ipv4Addr);
LOG.debug("CitrixMonitor.poll: Polling interface: {} {}", host, timeoutTracker);
PollStatus serviceStatus = PollStatus.unavailable();
for (timeoutTracker.reset(); timeoutTracker.shouldRetry() && !serviceStatus.isAvailable(); timeoutTracker.nextAttempt()) {
Socket socket = null;
try {
timeoutTracker.startAttempt();
socket = new Socket();
socket.connect(new InetSocketAddress(ipv4Addr, port), timeoutTracker.getConnectionTimeout());
socket.setSoTimeout(timeoutTracker.getSoTimeout());
LOG.debug("CitrixMonitor: connected to host: {} on port: {}", host, port);
// We're connected, so upgrade status to unresponsive
// Allocate a line reader
//
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final StringBuilder buffer = new StringBuilder();
//
while (!serviceStatus.isAvailable()) {
buffer.append((char) reader.read());
if (buffer.toString().indexOf("ICA") > -1) {
serviceStatus = PollStatus.available(timeoutTracker.elapsedTimeInMillis());
} else {
serviceStatus = PollStatus.unavailable("magic cookie 'ICA' missing from service greeting.");
}
}
} catch (ConnectException e) {
// Connection refused!! Continue to retry.
String reason = "Connection refused by host " + host;
LOG.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (NoRouteToHostException e) {
// No route to host!! Try retries anyway in case strict timeouts are enabled
String reason = "Unable to test host " + host + ", no route available";
LOG.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (InterruptedIOException e) {
String reason = "did not connect to host " + host + " within timeout: " + timeoutTracker;
LOG.debug(reason);
serviceStatus = PollStatus.unavailable(reason);
} catch (IOException e) {
String reason = "Error communicating with host " + host;
LOG.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (Throwable t) {
String reason = "Undeclared throwable exception caught contacting host " + host;
LOG.debug(reason, t);
serviceStatus = PollStatus.unavailable(reason);
} finally {
try {
if (socket != null) {
socket.close();
socket = null;
}
} catch (IOException e) {
}
}
}
//
return serviceStatus;
}
use of org.opennms.core.utils.TimeoutTracker in project opennms by OpenNMS.
the class DnsMonitor method poll.
/**
* {@inheritDoc}
*
* <P>
* Poll the specified address for DNS service availability.
* </P>
*
* <P>
* During the poll an DNS address request query packet is generated for
* hostname 'localhost'. The query is sent via UDP socket to the interface
* at the specified port (by default UDP port 53). If a response is
* received, it is parsed and validated. If the DNS lookup was successful
* the service status is set to SERVICE_AVAILABLE and the method returns.
* </P>
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
// get the parameters
//
TimeoutTracker timeoutTracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);
// Host to lookup?
//
String lookup = ParameterMap.getKeyedString(parameters, "lookup", null);
if (lookup == null || lookup.length() == 0) {
// Get hostname of local machine for future DNS lookups
lookup = InetAddressUtils.getLocalHostAddressAsString();
if (lookup == null) {
throw new UnsupportedOperationException("Unable to look up local host address.");
}
}
// What do we consider fatal?
//
final List<Integer> fatalCodes = new ArrayList<>();
for (final int code : ParameterMap.getKeyedIntegerArray(parameters, "fatal-response-codes", DEFAULT_FATAL_RESP_CODES)) {
fatalCodes.add(code);
}
int minAnswers = ParameterMap.getKeyedInteger(parameters, "min-answers", DEFAULT_MIN_ANSWERS);
int maxAnswers = ParameterMap.getKeyedInteger(parameters, "max-answers", DEFAULT_MAX_ANSWERS);
// get the address and DNS address request
//
final InetAddress addr = svc.getAddress();
PollStatus serviceStatus = null;
serviceStatus = pollDNS(timeoutTracker, port, addr, lookup, fatalCodes, minAnswers, maxAnswers);
if (serviceStatus == null) {
String reason = "Never received valid DNS response for address: " + addr;
LOG.debug(reason);
serviceStatus = PollStatus.unavailable(reason);
}
//
return serviceStatus;
}
use of org.opennms.core.utils.TimeoutTracker in project opennms by OpenNMS.
the class DominoIIOPMonitor method poll.
// read()
/**
* {@inheritDoc}
*
* Poll the specified address for service 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 set the service
* status to SERVICE_AVAILABLE and return.
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
//
// Process parameters
//
TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
int IORport = ParameterMap.getKeyedInteger(parameters, "ior-port", DEFAULT_IORPORT);
// Port
//
int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);
// Get the address instance.
//
InetAddress ipAddr = svc.getAddress();
final String hostAddress = InetAddressUtils.str(ipAddr);
LOG.debug("poll: address = {}, port = {}, {}", hostAddress, port, tracker);
//
try {
retrieveIORText(hostAddress, IORport);
} catch (Throwable e) {
String reason = "failed to get the corba IOR from " + ipAddr;
LOG.debug(reason, e);
return PollStatus.unavailable(reason);
}
PollStatus status = null;
for (tracker.reset(); tracker.shouldRetry() && !status.isAvailable(); tracker.nextAttempt()) {
Socket socket = null;
try {
//
// create a connected socket
//
tracker.startAttempt();
socket = new Socket();
socket.connect(new InetSocketAddress(ipAddr, port), tracker.getConnectionTimeout());
socket.setSoTimeout(tracker.getSoTimeout());
LOG.debug("DominoIIOPMonitor: connected to host: {} on port: {}", ipAddr, port);
return PollStatus.up(tracker.elapsedTimeInMillis());
} catch (NoRouteToHostException e) {
String reason = " No route to host exception for address " + hostAddress;
LOG.debug(reason, e);
status = PollStatus.unavailable(reason);
} catch (InterruptedIOException e) {
String reason = "did not connect to host with " + tracker;
LOG.debug(reason);
status = PollStatus.unavailable(reason);
} catch (ConnectException e) {
String reason = "Connection exception for address: " + ipAddr + " : " + e.getMessage();
LOG.debug(reason);
status = PollStatus.unavailable(reason);
} catch (IOException e) {
String reason = "IOException while polling address: " + ipAddr + " : " + e.getMessage();
LOG.debug(reason);
status = PollStatus.unavailable(reason);
} finally {
try {
// Close the socket
if (socket != null)
socket.close();
} catch (IOException e) {
e.fillInStackTrace();
LOG.debug("DominoIIOPMonitor: Error closing socket.", e);
}
}
}
//
return status;
}
use of org.opennms.core.utils.TimeoutTracker in project opennms by OpenNMS.
the class ImapMonitor method poll.
/**
* {@inheritDoc}
*
* <P>
* Poll the specified address for IMAP service availability.
* </P>
*
* <P>
* During the poll an attempt is made to connect on the specified port (by
* default TCP port 143). If the connection request is successful, the
* banner line generated by the interface is parsed and if it starts with a '*
* OK , it indicates that we are talking to an IMAP server and we continue.
* Next, a 'LOGOUT' command is sent to the interface. Again the response is
* parsed and the response is verified to see that we get a '* OK'. If the
* interface's response is valid we set the service status to
* SERVICE_AVAILABLE and return.
* </P>
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
// Process parameters
//
TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
// Retries
//
final int port = determinePort(parameters);
// Get interface address from NetworkInterface
//
InetAddress ipAddr = svc.getAddress();
LOG.debug("ImapMonitor.poll: address: {} port: {} {}", ipAddr, port, tracker);
PollStatus serviceStatus = PollStatus.unavailable();
for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
Socket socket = null;
try {
//
// create a connected socket
//
tracker.startAttempt();
socket = new Socket();
socket.connect(new InetSocketAddress(ipAddr, port), tracker.getConnectionTimeout());
socket.setSoTimeout(tracker.getSoTimeout());
// We're connected, so upgrade status to unresponsive
serviceStatus = PollStatus.unresponsive();
// For SSL support
socket = getSocketWrapper().wrapSocket(socket);
BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//
// Tokenize the Banner Line, and check the first
// line for a valid return.
//
String banner = rdr.readLine();
double responseTime = tracker.elapsedTimeInMillis();
LOG.debug("ImapMonitor.Poll(): banner: {}", banner);
if (banner != null && banner.startsWith(IMAP_START_RESPONSE_PREFIX)) {
//
// Send the LOGOUT
//
socket.getOutputStream().write(IMAP_LOGOUT_REQUEST.getBytes());
//
// get the returned string, tokenize, and
// verify the correct output.
//
String response = rdr.readLine();
if (response != null && response.startsWith(IMAP_BYE_RESPONSE_PREFIX)) {
response = rdr.readLine();
if (response != null && response.startsWith(IMAP_LOGOUT_RESPONSE_PREFIX)) {
serviceStatus = PollStatus.available(responseTime);
}
}
}
// the banner checking or logout process.
if (!serviceStatus.isAvailable()) {
serviceStatus = PollStatus.unavailable();
}
} catch (NoRouteToHostException e) {
String reason = "No route to host exception for address: " + ipAddr;
LOG.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (ConnectException e) {
// Connection refused. Continue to retry.
String reason = "Connection exception for address: " + ipAddr;
LOG.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (InterruptedIOException e) {
String reason = "did not connect to host with " + tracker;
LOG.debug(reason);
serviceStatus = PollStatus.unavailable(reason);
} catch (IOException e) {
String reason = "IOException while polling address: " + ipAddr;
LOG.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} finally {
try {
// Close the socket
if (socket != null)
socket.close();
} catch (IOException e) {
e.fillInStackTrace();
LOG.debug("ImapMonitor.poll: Error closing socket.", e);
}
}
}
//
return serviceStatus;
}
use of org.opennms.core.utils.TimeoutTracker in project opennms by OpenNMS.
the class MailTransportMonitor method sendTestMessage.
/**
* Sends message based on properties and fields configured for the service.
*
* @param mailParms
* @return a PollStatus
*/
private PollStatus sendTestMessage(final MailTransportParameters mailParms) {
PollStatus status = PollStatus.unavailable("Test not completed.");
final long interval = mailParms.getSendTestAttemptInterval();
final TimeoutTracker tracker = new TimeoutTracker(mailParms.getParameterMap(), mailParms.getRetries(), mailParms.getTimeout());
for (tracker.reset(); tracker.shouldRetry(); tracker.nextAttempt()) {
tracker.startAttempt();
LOG.debug("sendTestMessage: sending mail attempt: {}, elapsed time: {}ms", (tracker.getAttempt() + 1), String.format("%.2f", tracker.elapsedTimeInMillis()));
try {
final JavaMailer sendMailer = createMailer(mailParms);
overRideDefaultProperties(mailParms, sendMailer);
sendMailer.mailSend();
status = PollStatus.available();
break;
} catch (final JavaMailerException e) {
status = PollStatus.unavailable(e.getLocalizedMessage());
}
if (tracker.shouldRetry()) {
delayTest(status, interval);
}
}
return status;
}
Aggregations