Search in sources :

Example 11 with NoRouteToHostException

use of java.net.NoRouteToHostException in project opennms by OpenNMS.

the class SmtpMonitor method poll.

/**
     * {@inheritDoc}
     *
     * <P>
     * Poll the specified address for SMTP service availability.
     * </P>
     *
     * <P>
     * During the poll an attempt is made to connect on the specified port (by
     * default TCP port 25). 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 SMTP server we continue. Next,
     * an SMTP 'HELO' command is sent to the interface. Again the response is
     * parsed and a return code extracted and verified. Finally, an SMTP 'QUIT'
     * command is sent. Provided that 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) {
    TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
    int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);
    InetAddress ipAddr = svc.getAddress();
    final String hostAddress = InetAddressUtils.str(ipAddr);
    LOG.debug("poll: address = {}, port = {}, {}", hostAddress, 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());
            LOG.debug("SmtpMonitor: connected to host: {} on port: {}", ipAddr, port);
            // We're connected, so upgrade status to unresponsive
            serviceStatus = PollStatus.unresponsive();
            // Forcing to check for CRLF instead of any other line terminator as per RFC specification
            CRLFLineReader rdr = new CRLFLineReader(new InputStreamReader(socket.getInputStream(), "ASCII"));
            //
            // Tokenize the Banner Line, and check the first
            // line for a valid return.
            //
            String banner = sendMessage(socket, rdr, null);
            LOG.debug("poll: banner = {}", banner);
            StringTokenizer t = new StringTokenizer(banner);
            int rc = Integer.parseInt(t.nextToken());
            if (rc == 220) {
                //
                // Send the HELO command
                //
                String cmd = "HELO " + LOCALHOST_NAME + "\r\n";
                socket.getOutputStream().write(cmd.getBytes());
                //
                // get the returned string, tokenize, and
                // verify the correct output.
                //
                String response = rdr.readLine();
                double responseTime = tracker.elapsedTimeInMillis();
                if (response == null) {
                    continue;
                }
                if (MULTILINE.matcher(response).find()) {
                    // Ok we have a multi-line response...first three
                    // chars of the response line are the return code.
                    // The last line of the response will start with
                    // return code followed by a space.
                    String multiLineRC = new String(response.getBytes("ASCII"), 0, 3, "ASCII");
                    // Create new regExp to look for last line
                    // of this multi line response
                    Pattern endMultiline = null;
                    try {
                        endMultiline = Pattern.compile(multiLineRC);
                    } catch (PatternSyntaxException ex) {
                        throw new java.lang.reflect.UndeclaredThrowableException(ex);
                    }
                    // response
                    do {
                        response = rdr.readLine();
                    } while (response != null && !endMultiline.matcher(response).find());
                    if (response == null) {
                        continue;
                    }
                }
                t = new StringTokenizer(response);
                rc = Integer.parseInt(t.nextToken());
                if (rc == 250) {
                    response = sendMessage(socket, rdr, "QUIT\r\n");
                    t = new StringTokenizer(response);
                    rc = Integer.parseInt(t.nextToken());
                    if (rc == 221) {
                        serviceStatus = PollStatus.available(responseTime);
                    }
                }
            } else if (rc == 554) {
                String response = sendMessage(socket, rdr, "QUIT\r\n");
                serviceStatus = PollStatus.unavailable("Server rejecting transactions with 554");
            }
            // checking or HELO/QUIT comand process.
            if (!serviceStatus.isAvailable()) {
                serviceStatus = PollStatus.unavailable(serviceStatus.getReason());
            }
        } catch (NumberFormatException e) {
            String reason = "NumberFormatException while polling address " + hostAddress;
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
        } catch (NoRouteToHostException e) {
            String reason = "No route to host exception for address " + hostAddress;
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
            // Break out of for(;;)
            break;
        } catch (InterruptedIOException e) {
            String reason = "Did not receive expected response within timeout " + tracker;
            LOG.debug(reason);
            serviceStatus = PollStatus.unavailable(reason);
        } catch (ConnectException e) {
            String reason = "Unable to connect to address " + hostAddress;
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
        } catch (IOException e) {
            String reason = "IOException while polling address " + hostAddress;
            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("poll: Error closing socket.", e);
            }
        }
    }
    //
    return serviceStatus;
}
Also used : Pattern(java.util.regex.Pattern) InterruptedIOException(java.io.InterruptedIOException) PollStatus(org.opennms.netmgt.poller.PollStatus) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) CRLFLineReader(org.apache.commons.net.io.CRLFLineReader) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) NoRouteToHostException(java.net.NoRouteToHostException) StringTokenizer(java.util.StringTokenizer) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) InetAddress(java.net.InetAddress) Socket(java.net.Socket) PatternSyntaxException(java.util.regex.PatternSyntaxException) ConnectException(java.net.ConnectException)

Example 12 with NoRouteToHostException

use of java.net.NoRouteToHostException in project opennms by OpenNMS.

the class SSLCertMonitor method poll.

/**
     * {@inheritDoc}
     *
     * Poll the specified address for HTTP service availability.
     *
     * During the poll an attempt is made to connect on the specified port. If
     * the connection request is successful, check the X509Certificates provided
     * by our peer and check that our time is between the certificates start and
     * end time.
     * Provided that the interface's response is valid we set the service status to
     * SERVICE_AVAILABLE and return.
     */
@Override
public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) {
    TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
    // Port
    int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT);
    if (port == DEFAULT_PORT) {
        throw new RuntimeException("Required parameter 'port' is not present in supplied properties.");
    }
    // Remaining days
    int validityDays = ParameterMap.getKeyedInteger(parameters, PARAMETER_DAYS, DEFAULT_DAYS);
    if (validityDays <= 0) {
        throw new RuntimeException("Required parameter 'days' must be a positive value.");
    }
    // Server name (optional)
    final String serverName = PropertiesUtils.substitute(ParameterMap.getKeyedString(parameters, PARAMETER_SERVER_NAME, ""), getServiceProperties(svc));
    // Calculate validity range
    Calendar calValid = this.getCalendarInstance();
    Calendar calCurrent = this.getCalendarInstance();
    calValid.setTimeInMillis(calCurrent.getTimeInMillis());
    calValid.add(Calendar.DAY_OF_MONTH, validityDays);
    Calendar calBefore = this.getCalendarInstance();
    Calendar calAfter = this.getCalendarInstance();
    // Get the address instance
    InetAddress ipAddr = svc.getAddress();
    final String hostAddress = InetAddressUtils.str(ipAddr);
    LOG.debug("poll: address={}, port={}, serverName={}, {}", hostAddress, port, serverName, tracker);
    // Give it a whirl
    PollStatus serviceStatus = PollStatus.unavailable();
    for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
        Socket socket = null;
        try {
            tracker.startAttempt();
            socket = new Socket();
            socket.connect(new InetSocketAddress(ipAddr, port), tracker.getConnectionTimeout());
            socket.setSoTimeout(tracker.getSoTimeout());
            LOG.debug("Connected to host: {} on port: {}", ipAddr, port);
            SSLSocket sslSocket = SocketUtils.wrapSocketInSslContext(socket, null, null);
            // We're connected, so upgrade status to unresponsive
            serviceStatus = PollStatus.unresponsive();
            // Use the server name as as SNI host name if available
            if (!Strings.isNullOrEmpty(serverName)) {
                final SSLParameters sslParameters = sslSocket.getSSLParameters();
                sslParameters.setServerNames(ImmutableList.of(new SNIHostName(serverName)));
                sslSocket.setSSLParameters(sslParameters);
                // Check certificates host name
                if (!new StrictHostnameVerifier().verify(serverName, sslSocket.getSession())) {
                    serviceStatus = PollStatus.unavailable("Host name verification failed - certificate common name is invalid");
                    continue;
                }
            }
            Certificate[] certs = sslSocket.getSession().getPeerCertificates();
            for (int i = 0; i < certs.length && !serviceStatus.isAvailable(); i++) {
                if (certs[i] instanceof X509Certificate) {
                    X509Certificate certx = (X509Certificate) certs[i];
                    LOG.debug("Checking validity against dates: [current: {}, valid: {}], NotBefore: {}, NotAfter: {}", calCurrent.getTime(), calValid.getTime(), certx.getNotBefore(), certx.getNotAfter());
                    calBefore.setTime(certx.getNotBefore());
                    calAfter.setTime(certx.getNotAfter());
                    if (calCurrent.before(calBefore)) {
                        LOG.debug("Certificate is invalid, current time is before start time");
                        serviceStatus = PollStatus.unavailable("Certificate is invalid, current time is before start time");
                        break;
                    } else if (calCurrent.before(calAfter)) {
                        if (calValid.before(calAfter)) {
                            LOG.debug("Certificate is valid, and does not expire before validity check date");
                            serviceStatus = PollStatus.available(tracker.elapsedTimeInMillis());
                            break;
                        } else {
                            String reason = "Certificate is valid, but will expire in " + validityDays + " days.";
                            LOG.debug(reason);
                            serviceStatus = PollStatus.unavailable(reason);
                            break;
                        }
                    } else {
                        LOG.debug("Certificate has expired.");
                        serviceStatus = PollStatus.unavailable("Certificate has expired.");
                        break;
                    }
                }
            }
        } catch (NoRouteToHostException e) {
            String reason = "No route to host exception for address " + hostAddress;
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
            // Break out of for(;;)
            break;
        } catch (InterruptedIOException e) {
            String reason = "did not connect to host with " + tracker;
            LOG.debug(reason);
            serviceStatus = PollStatus.unavailable(reason);
        } catch (ConnectException e) {
            String reason = "Connection exception for address: " + ipAddr;
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
        } catch (IOException e) {
            String reason = "IOException while polling address: " + ipAddr;
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.fillInStackTrace();
                LOG.debug("poll: Error closing socket.", e);
            }
        }
    }
    return serviceStatus;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) PollStatus(org.opennms.netmgt.poller.PollStatus) InetSocketAddress(java.net.InetSocketAddress) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) SSLSocket(javax.net.ssl.SSLSocket) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) X509Certificate(java.security.cert.X509Certificate) SSLParameters(javax.net.ssl.SSLParameters) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) StrictHostnameVerifier(org.apache.http.conn.ssl.StrictHostnameVerifier) SNIHostName(javax.net.ssl.SNIHostName) InetAddress(java.net.InetAddress) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) ConnectException(java.net.ConnectException)

Example 13 with NoRouteToHostException

use of java.net.NoRouteToHostException in project opennms by OpenNMS.

the class HttpMonitor method poll.

/**
     * {@inheritDoc}
     *
     * Poll the specified address for HTTP service availability.
     *
     * During the poll an attempt is made to connect on the specified port(s) (by default TCP
     * ports 80, 8080, 8888). If the connection request is successful, an HTTP 'GET' command is
     * sent to the interface. The response is parsed and a return code extracted and verified.
     * Provided that the interface's response is valid we set the service status to
     * SERVICE_AVAILABLE and return.
     */
@Override
public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) {
    final InetAddress addr = svc.getAddress();
    final String nodeLabel = svc.getNodeLabel();
    // Cycle through the port list
    //
    int currentPort = -1;
    final HttpMonitorClient httpClient = new HttpMonitorClient(nodeLabel, addr, new TreeMap<String, Object>(parameters));
    for (int portIndex = 0; portIndex < determinePorts(httpClient.getParameters()).length && httpClient.getPollStatus() != PollStatus.SERVICE_AVAILABLE; portIndex++) {
        currentPort = determinePorts(httpClient.getParameters())[portIndex];
        httpClient.setTimeoutTracker(new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT));
        LOG.debug("Port = {}, Address = {}, {}", currentPort, addr, httpClient.getTimeoutTracker());
        httpClient.setCurrentPort(currentPort);
        String serviceInfo = new StringBuilder(addr.toString()).append(":").append(svc.getSvcName()).append(":").append(currentPort).toString();
        for (httpClient.getTimeoutTracker().reset(); httpClient.getTimeoutTracker().shouldRetry() && httpClient.getPollStatus() != PollStatus.SERVICE_AVAILABLE; httpClient.getTimeoutTracker().nextAttempt()) {
            try {
                httpClient.getTimeoutTracker().startAttempt();
                httpClient.connect();
                LOG.debug("HttpMonitor: connected to host: {} on port: {}", addr, currentPort);
                httpClient.sendHttpCommand();
                if (httpClient.isEndOfStream()) {
                    continue;
                }
                httpClient.setResponseTime(httpClient.getTimeoutTracker().elapsedTimeInMillis());
                logResponseTimes(httpClient.getResponseTime(), httpClient.getCurrentLine());
                if (httpClient.getPollStatus() == PollStatus.SERVICE_AVAILABLE && StringUtils.isNotBlank(httpClient.getResponseText())) {
                    httpClient.setPollStatus(PollStatus.SERVICE_UNAVAILABLE);
                    httpClient.readLinedMatching();
                    if (httpClient.isEndOfStream()) {
                        continue;
                    }
                    httpClient.read();
                    if (!httpClient.isResponseTextFound()) {
                        String message = "Matching text: [" + httpClient.getResponseText() + "] not found in body of HTTP response for " + serviceInfo;
                        LOG.debug(message);
                        httpClient.setReason("Matching text: [" + httpClient.getResponseText() + "] not found in body of HTTP response");
                    }
                }
            } catch (NoRouteToHostException e) {
                LOG.warn("checkStatus: No route to host exception while polling {}", serviceInfo, e);
                // Will cause outer for(;;) to terminate
                portIndex = determinePorts(httpClient.getParameters()).length;
                httpClient.setReason("No route to host exception");
            } catch (SocketTimeoutException e) {
                LOG.info("checkStatus: HTTP socket connection for service {} timed out with {}", serviceInfo, httpClient.getTimeoutTracker().toString());
                httpClient.setReason("HTTP connection timeout");
            } catch (InterruptedIOException e) {
                LOG.info(String.format("checkStatus: HTTP connection for service {} interrupted after {} bytes transferred with {}", serviceInfo, e.bytesTransferred, httpClient.getTimeoutTracker().toString()), e);
                httpClient.setReason(String.format("HTTP connection interrupted, %d bytes transferred", e.bytesTransferred));
            } catch (ConnectException e) {
                LOG.warn("Connection exception for {}", serviceInfo, e);
                httpClient.setReason("HTTP connection exception on port: " + determinePorts(httpClient.getParameters())[portIndex] + ": " + e.getMessage());
            } catch (IOException e) {
                String exceptionClass = e.getClass().getSimpleName();
                LOG.warn("{} while polling {}", exceptionClass, serviceInfo, e);
                httpClient.setReason("IOException while polling address: " + addr + ": " + e.getMessage());
            } catch (Throwable e) {
                String exceptionClass = e.getClass().getSimpleName();
                LOG.warn("Unexpected {} while polling {}", exceptionClass, serviceInfo, e);
                httpClient.setReason("Unexpected exception while polling address: " + addr + ": " + e.getMessage());
            } finally {
                httpClient.closeConnection();
            }
        }
    // end for (attempts)
    }
    // end for (ports)
    return httpClient.determinePollStatusResponse();
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) SocketTimeoutException(java.net.SocketTimeoutException) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) InetAddress(java.net.InetAddress) ConnectException(java.net.ConnectException)

Example 14 with NoRouteToHostException

use of java.net.NoRouteToHostException in project opennms by OpenNMS.

the class LdapMonitor method poll.

/**
     * {@inheritDoc}
     *
     * Poll the specified address for service availability.
     *
     * During the poll an attempt is made to connect the service.
     *
     * 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) {
    int serviceStatus = PollStatus.SERVICE_UNAVAILABLE;
    String reason = null;
    final TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
    // get the parameters
    //
    final int ldapVersion = ParameterMap.getKeyedInteger(parameters, "version", LDAPConnection.LDAP_V3);
    final int ldapPort = determinePort(parameters);
    final String searchBase = ParameterMap.getKeyedString(parameters, "searchbase", DEFAULT_BASE);
    final String searchFilter = ParameterMap.getKeyedString(parameters, "searchfilter", DEFAULT_FILTER);
    final String password = (String) parameters.get("password");
    final String ldapDn = (String) parameters.get("dn");
    String address = InetAddrUtils.str(svc.getAddress());
    // first just try a connection to the box via socket. Just in case there
    // is
    // a no way to route to the address, don't iterate through the retries,
    // as a
    // NoRouteToHost exception will only be thrown after about 5 minutes,
    // thus tying
    // up the thread
    Double responseTime = null;
    Socket socket = null;
    try {
        socket = new Socket();
        socket.connect(new InetSocketAddress(svc.getAddress(), ldapPort), tracker.getConnectionTimeout());
        socket.setSoTimeout(tracker.getSoTimeout());
        LOG.debug("LdapMonitor: connected to host: {} on port: {}", address, ldapPort);
        // We're connected, so upgrade status to unresponsive
        serviceStatus = PollStatus.SERVICE_UNRESPONSIVE;
        if (socket != null)
            socket.close();
        // lets detect the service
        LDAPConnection lc = new LDAPConnection(new TimeoutLDAPSocket(tracker.getSoTimeout()));
        for (tracker.reset(); tracker.shouldRetry() && !(serviceStatus == PollStatus.SERVICE_AVAILABLE); tracker.nextAttempt()) {
            LOG.debug("polling LDAP on {}, {}", address, tracker);
            // connect to the ldap server
            tracker.startAttempt();
            try {
                lc.connect(address, ldapPort);
                LOG.debug("connected to LDAP server {} on port {}", address, ldapPort);
            } catch (LDAPException e) {
                LOG.debug("could not connect to LDAP server {} on port {}", address, ldapPort);
                reason = "could not connect to LDAP server " + address + " on port " + ldapPort;
                continue;
            }
            // bind if possible
            if (ldapDn != null && password != null) {
                try {
                    lc.bind(ldapVersion, ldapDn, password.getBytes());
                    LOG.debug("bound to LDAP server version {} with distinguished name {}", ldapVersion, ldapDn);
                    LOG.debug("poll: responseTime= {}ms", tracker.elapsedTimeInMillis());
                } catch (LDAPException e) {
                    try {
                        lc.disconnect();
                    } catch (LDAPException ex) {
                        LOG.debug(ex.getMessage());
                    }
                    LOG.debug("could not bind to LDAP server version {} with distinguished name {}", ldapVersion, ldapDn);
                    reason = "could not bind to LDAP server version " + ldapVersion + " with distinguished name " + ldapDn;
                    continue;
                }
            }
            // do a quick search and see if any results come back
            boolean attributeOnly = true;
            String[] attrs = { LDAPConnection.NO_ATTRS };
            int searchScope = LDAPConnection.SCOPE_ONE;
            LOG.debug("running search {} from {}", searchFilter, searchBase);
            LDAPSearchResults results = null;
            int msLimit = (int) tracker.getTimeoutInMillis();
            int serverLimit = (int) tracker.getTimeoutInSeconds() + 1;
            LDAPSearchConstraints cons = new LDAPSearchConstraints(msLimit, serverLimit, // dereference: default = never
            LDAPSearchConstraints.DEREF_NEVER, // maxResults: default = 1000
            1000, // doReferrals: default = false
            false, // batchSize: default = 1
            1, // handler: default = null
            null, // hop_limit: default = 10
            10);
            try {
                results = lc.search(searchBase, searchScope, searchFilter, attrs, attributeOnly, cons);
                if (results != null && results.hasMore()) {
                    responseTime = tracker.elapsedTimeInMillis();
                    LOG.debug("search yielded {} result(s)", results.getCount());
                    serviceStatus = PollStatus.SERVICE_AVAILABLE;
                } else {
                    LOG.debug("no results found from search");
                    reason = "No results found from search";
                    serviceStatus = PollStatus.SERVICE_UNAVAILABLE;
                }
            } catch (LDAPException e) {
                try {
                    lc.disconnect();
                } catch (LDAPException ex) {
                    LOG.debug(ex.getMessage());
                }
                LOG.debug("could not perform search {} from {}", searchFilter, searchBase);
                reason = "could not perform search " + searchFilter + " from " + searchBase;
                continue;
            }
            try {
                lc.disconnect();
                LOG.debug("disconected from LDAP server {} on port {}", address, ldapPort);
            } catch (LDAPException e) {
                LOG.debug(e.getMessage());
            }
        }
    } catch (ConnectException e) {
        LOG.debug("connection refused to host {}", address, e);
        reason = "connection refused to host " + address;
    } catch (NoRouteToHostException e) {
        LOG.debug("No route to host {}", address, e);
        reason = "No route to host " + address;
    } catch (InterruptedIOException e) {
        LOG.debug("did not connect to host with {}", tracker);
        reason = "did not connect to host with " + tracker;
    } catch (Throwable t) {
        LOG.debug("An undeclared throwable exception caught contacting host {}", address, t);
        reason = "An undeclared throwable exception caught contacting host " + address;
    }
    return PollStatus.get(serviceStatus, reason, responseTime);
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InetSocketAddress(java.net.InetSocketAddress) LDAPSearchConstraints(com.novell.ldap.LDAPSearchConstraints) LDAPConnection(com.novell.ldap.LDAPConnection) NoRouteToHostException(java.net.NoRouteToHostException) LDAPSearchResults(com.novell.ldap.LDAPSearchResults) LDAPException(com.novell.ldap.LDAPException) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Example 15 with NoRouteToHostException

use of java.net.NoRouteToHostException in project opennms by OpenNMS.

the class FtpMonitor method poll.

// read()
/**
     * {@inheritDoc}
     *
     * Poll the specified address for FTP service availability.
     *
     * During the poll an attempt is made to connect on the specified port (by
     * default TCP port 21). 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 FTP server we continue. Next, an
     * FTP 'QUIT' command is sent. 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) {
    // Get the parameters
    TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
    int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);
    String userid = ParameterMap.getKeyedString(parameters, "userid", null);
    String password = ParameterMap.getKeyedString(parameters, "password", null);
    // Extract the address
    InetAddress ipAddr = svc.getAddress();
    PollStatus serviceStatus = PollStatus.unavailable();
    for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
        LOG.debug("FtpMonitor.poll: Polling interface: {} {}", InetAddressUtils.str(ipAddr), tracker);
        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("FtpMonitor: connected to host: {} on port: {}", ipAddr, port);
            // We're connected, so upgrade status to unresponsive
            serviceStatus = PollStatus.unresponsive();
            BufferedReader lineRdr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            FtpResponse bannerResponse = FtpResponse.readResponse(lineRdr);
            if (bannerResponse.isSuccess()) {
                // Attempt to login if userid and password available
                boolean loggedInSuccessfully = false;
                LOG.debug("FtpMonitor: Banner response successful.");
                if (userid == null || userid.length() == 0 || password == null || password.length() == 0) {
                    loggedInSuccessfully = true;
                } else {
                    FtpResponse.sendCommand(socket, "USER " + userid);
                    FtpResponse userResponse = FtpResponse.readResponse(lineRdr);
                    if (userResponse.isSuccess() || userResponse.isIntermediate()) {
                        LOG.debug("FtpMonitor: User response successful.");
                        FtpResponse.sendCommand(socket, "PASS " + password);
                        FtpResponse passResponse = FtpResponse.readResponse(lineRdr);
                        if (passResponse.isSuccess()) {
                            LOG.debug("FtpMonitor.poll: Login successful, parsed return code: {}", passResponse.getCode());
                            loggedInSuccessfully = true;
                        } else {
                            LOG.debug("FtpMonitor.poll: Login failed, parsed return code: {}, full response: {}", passResponse.getCode(), passResponse);
                            loggedInSuccessfully = false;
                        }
                    }
                }
                // Store the response time before we try to quit
                double responseTime = tracker.elapsedTimeInMillis();
                if (loggedInSuccessfully) {
                    FtpResponse.sendCommand(socket, "QUIT");
                    FtpResponse quitResponse = FtpResponse.readResponse(lineRdr);
                    /*
                         * Special Cases for success:
                         * 
                         * Also want to accept the following
                         * ERROR message generated by some FTP servers
                         * following a QUIT command without a previous
                         * successful login:
                         *
                         * "530 QUIT : User not logged in. Please login with
                         * USER and PASS first."
                         * 
                         * Also want to accept the following ERROR
                         * message generated by some FTP servers following a
                         * QUIT command without a previously successful login:
                         *
                         * "425 Session is disconnected."
                         */
                    if (quitResponse.isSuccess() || (quitResponse.getCode() == 530) || (quitResponse.getCode() == 425)) {
                        serviceStatus = PollStatus.available(responseTime);
                    }
                }
            }
            /*
                 * If we get this far and the status has not been set
                 * to available, then something didn't verify during
                 * the banner checking or login/QUIT command process.
                 */
            if (!serviceStatus.isAvailable()) {
                serviceStatus = PollStatus.unavailable();
            }
        } catch (NumberFormatException e) {
            String reason = "NumberFormatException while polling address: " + ipAddr;
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
        } catch (NoRouteToHostException e) {
            String reason = "No route to host 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 (ConnectException e) {
            String reason = "Connection exception for address: " + ipAddr;
            LOG.debug(reason, e);
            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) {
                LOG.debug("FtpMonitor.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) FtpResponse(org.opennms.netmgt.poller.monitors.support.FtpResponse) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) BufferedReader(java.io.BufferedReader) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Aggregations

NoRouteToHostException (java.net.NoRouteToHostException)31 IOException (java.io.IOException)26 InterruptedIOException (java.io.InterruptedIOException)19 ConnectException (java.net.ConnectException)19 InetSocketAddress (java.net.InetSocketAddress)14 Socket (java.net.Socket)14 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)13 InetAddress (java.net.InetAddress)12 PollStatus (org.opennms.netmgt.poller.PollStatus)11 InputStreamReader (java.io.InputStreamReader)7 BufferedInputStream (java.io.BufferedInputStream)6 BufferedReader (java.io.BufferedReader)6 URL (java.net.URL)6 ProtocolException (java.net.ProtocolException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Test (org.junit.Test)4 DatagramSocket (java.net.DatagramSocket)3 SocketTimeoutException (java.net.SocketTimeoutException)3 OutputStream (java.io.OutputStream)2