Search in sources :

Example 21 with TimeoutTracker

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;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) PollStatus(org.opennms.netmgt.poller.PollStatus) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) NoRouteToHostException(java.net.NoRouteToHostException) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) BufferedReader(java.io.BufferedReader) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Example 22 with TimeoutTracker

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;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress)

Example 23 with TimeoutTracker

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;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) PollStatus(org.opennms.netmgt.poller.PollStatus) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) InetAddress(java.net.InetAddress) NoRouteToHostException(java.net.NoRouteToHostException) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Example 24 with TimeoutTracker

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;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) PollStatus(org.opennms.netmgt.poller.PollStatus) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) NoRouteToHostException(java.net.NoRouteToHostException) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) BufferedReader(java.io.BufferedReader) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Example 25 with TimeoutTracker

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;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) JavaMailerException(org.opennms.javamail.JavaMailerException) JavaMailer(org.opennms.javamail.JavaMailer)

Aggregations

TimeoutTracker (org.opennms.core.utils.TimeoutTracker)42 PollStatus (org.opennms.netmgt.poller.PollStatus)29 InetAddress (java.net.InetAddress)24 IOException (java.io.IOException)16 InterruptedIOException (java.io.InterruptedIOException)13 ConnectException (java.net.ConnectException)13 NoRouteToHostException (java.net.NoRouteToHostException)13 InetSocketAddress (java.net.InetSocketAddress)11 Socket (java.net.Socket)11 InputStreamReader (java.io.InputStreamReader)7 BufferedReader (java.io.BufferedReader)6 HashMap (java.util.HashMap)4 MalformedURLException (java.net.MalformedURLException)3 Pattern (java.util.regex.Pattern)3 HostRuntimeInfo (com.vmware.vim25.HostRuntimeInfo)2 HostSystemPowerState (com.vmware.vim25.HostSystemPowerState)2 HostSystem (com.vmware.vim25.mo.HostSystem)2 SocketTimeoutException (java.net.SocketTimeoutException)2 RemoteException (java.rmi.RemoteException)2 Properties (java.util.Properties)2