📄 ftpmonitor.java
字号:
int rc = -1; try { rc = Integer.parseInt(t.nextToken()); } catch (NumberFormatException nfE) { nfE.fillInStackTrace(); log.warn("Banner page returned invalid result code", nfE); } // Verify that return code is in proper range. // if (rc >= 200 && rc <= 299) { // // Attempt to login if userid and password available // boolean bLoginOk = false; if (userid == null || userid.length() == 0 || password == null || password.length() == 0) { bLoginOk = true; } else { // send the use string // String cmd = "user " + userid + "\r\n"; socket.getOutputStream().write(cmd.getBytes()); // get the response code. // String response = null; do { response = lineRdr.readLine(); } while (response != null && MULTILINE.match(response)); if (response == null) continue; t = new StringTokenizer(response); rc = Integer.parseInt(t.nextToken()); // Verify that return code is in proper range. // if (rc >= 200 && rc <= 399) { // send the password // cmd = "pass " + password + "\r\n"; socket.getOutputStream().write(cmd.getBytes()); // get the response...check for multi-line response // response = lineRdr.readLine(); if (response == null) continue; if (MULTILINE.match(response)) { // 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(), 0, 3) + " "; // Create new regExp to look for last line // of this mutli line response try { ENDMULTILINE = new RE(multiLineRC); } catch (RESyntaxException ex) { throw new java.lang.reflect.UndeclaredThrowableException(ex); } // read until we hit the last line of the // multi-line // response do { response = lineRdr.readLine(); } while (response != null && !ENDMULTILINE.match(response)); if (response == null) continue; } // Verify that return code is in proper range. // if (log.isDebugEnabled()) log.debug("FtpMonitor.poll: tokenizing respone to check for return code: " + response); t = new StringTokenizer(response); rc = Integer.parseInt(t.nextToken()); if (rc >= 200 && rc <= 299) { if (log.isDebugEnabled()) log.debug("FtpMonitor.poll: Login successful, parsed return code: " + rc); bLoginOk = true; } else { if (log.isDebugEnabled()) log.debug("FtpMonitor.poll: Login failed, parsed return code: " + rc); bLoginOk = false; } } } if (bLoginOk) { // FTP should recognize the QUIT command // String cmd = "QUIT\r\n"; socket.getOutputStream().write(cmd.getBytes()); // get the returned string, tokenize, and // verify the correct output. // String response = lineRdr.readLine(); if (response == null) continue; if (MULTILINE.match(response)) { // 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(), 0, 3) + " "; // Create new regExp to look for last line // of this mutli line response try { ENDMULTILINE = new RE(multiLineRC); } catch (RESyntaxException ex) { throw new java.lang.reflect.UndeclaredThrowableException(ex); } // read until we hit the last line of the multi-line // response do { response = lineRdr.readLine(); } while (response != null && !ENDMULTILINE.match(response)); if (response == null) continue; } t = new StringTokenizer(response); rc = Integer.parseInt(t.nextToken()); // Verify that return code is in proper range. // if (rc >= 200 && rc <= 299) { serviceStatus = ServiceMonitor.SERVICE_AVAILABLE; // Store response time in RRD if (responseTime >= 0 && rrdPath != null) { try { this.updateRRD(rrdPath, ipv4Addr, dsName, responseTime, pkg); } catch (RuntimeException rex) { log.debug("There was a problem writing the RRD:" + rex); } } } // Special Case: Also want to accept the following ERROR // message // generated by some FTP servers following a QUIT // command without // a previously successful login: // // "530 QUIT : User not logged in. Please login with // USER and PASS // first." // else if (rc == 530 && response.indexOf(FTP_ERROR_530_TEXT) != -1) { serviceStatus = ServiceMonitor.SERVICE_AVAILABLE; // Store response time in RRD if (responseTime >= 0 && rrdPath != null) { try { this.updateRRD(rrdPath, ipv4Addr, dsName, responseTime, pkg); } catch (RuntimeException rex) { log.debug("There was a problem writing the RRD:" + rex); } } } // Special Case: 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." // else if (rc == 425 && response.indexOf(FTP_ERROR_425_TEXT) != -1) { serviceStatus = ServiceMonitor.SERVICE_AVAILABLE; // Store response time in RRD if (responseTime >= 0 && rrdPath != null) { try { this.updateRRD(rrdPath, ipv4Addr, dsName, responseTime, pkg); } catch (RuntimeException rex) { log.debug("There was a problem writing the RRD:" + rex); } } } } } // 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 != ServiceMonitor.SERVICE_AVAILABLE) { serviceStatus = ServiceMonitor.SERVICE_UNAVAILABLE; } } catch (NumberFormatException e) { // Ignore e.fillInStackTrace(); log.info("FtpMonitor.poll: NumberFormatException while polling address: " + ipv4Addr, e); } catch (NoRouteToHostException e) { e.fillInStackTrace(); log.warn("FtpMonitor.poll: No route to host exception for address: " + ipv4Addr, e); break; // Break out of for(;;) } catch (InterruptedIOException e) { // Ignore log.debug("FtpMonitor: did not connect to host within timeout: " + timeout + " attempt: " + attempts); } catch (ConnectException e) { // Connection refused. Continue to retry. e.fillInStackTrace(); log.debug("FtpMonitor.poll: Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { // Ignore e.fillInStackTrace(); log.debug("FtpMonitor.poll: IOException while polling address: " + ipv4Addr, e); } finally { try { // Close the socket if (socket != null) socket.close(); } catch (IOException e) { e.fillInStackTrace(); log.debug("FtpMonitor.poll: Error closing socket.", e); } } } // // return the status of the service // return serviceStatus; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -