httpsmonitor.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 370 行 · 第 1/2 页

JAVA
370
字号
                log.debug("Port = " + currentPort + ", Address = " + ipv4Addr + ", Timeout = " + timeout + ", Retry = " + retry);            }            for (int attempts = 0; attempts <= retry && serviceStatus != ServiceMonitor.SERVICE_AVAILABLE; attempts++) {                Socket socket = null;                Socket sslSocket = null;                try {                    // set up the certificate validation. USING THIS SCHEME WILL                    // ACCEPT ALL                    // CERTIFICATES                    SSLSocketFactory sslSF = null;                    TrustManager[] tm = { new RelaxedX509TrustManager() };                    SSLContext sslContext = SSLContext.getInstance("SSL");                    sslContext.init(null, tm, new java.security.SecureRandom());                    sslSF = sslContext.getSocketFactory();                    // connect and communicate                    long sentTime = System.currentTimeMillis();                    socket = new Socket();                    socket.connect(new InetSocketAddress(ipv4Addr, currentPort), timeout);                    socket.setSoTimeout(timeout);                    log.debug(getClass().getName() + ": connect successful!!");                    // We're connected, so upgrade status to unresponsive                    serviceStatus = SERVICE_UNRESPONSIVE;                    sslSocket = sslSF.createSocket(socket, ipv4Addr.getHostAddress(), currentPort, true);                    sslSocket.getOutputStream().write(cmd.getBytes());                    //                    // Get a buffered input stream that will read a line                    // at a time                    //                    BufferedReader lineRdr = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));                    String line = lineRdr.readLine();                    responseTime = System.currentTimeMillis() - sentTime;                    if (line == null)                        continue;                    if (log.isDebugEnabled())                        log.debug("HttpPlugin.poll: Response = " + line);                    if (line.startsWith("HTTP/")) {                        StringTokenizer t = new StringTokenizer(line);                        t.nextToken();                        int rVal = -1;                        try {                            rVal = Integer.parseInt(t.nextToken());                        } catch (NumberFormatException nfE) {                            log.info("Error converting response code from host = " + ipv4Addr + ", response = " + line);                        }                        if (bStrictResponse && rVal == response) {                            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);                                }                            }                        } else if (!bStrictResponse && rVal > 99 && rVal < 500 && (url.equals(DEFAULT_URL))) {                            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);                                }                            }                        } else if (!bStrictResponse && rVal > 99 && rVal < 400) {                            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);                                }                            }                        } else {                            serviceStatus = ServiceMonitor.SERVICE_UNAVAILABLE;                        }                    }                    if (serviceStatus == ServiceMonitor.SERVICE_AVAILABLE && responseText != null && responseText.length() > 0) {                        // This loop will rip through the rest of the Response                        // Header                        //                        do {                            line = lineRdr.readLine();                        } while (line != null && line.length() != 0);                        if (line == null)                            continue;                        // Now lets rip through the Entity-Body (i.e., content)                        // looking                        // for the required text.                        //                        boolean bResponseTextFound = false;                        do {                            line = lineRdr.readLine();                            if (line != null) {                                int responseIndex = line.indexOf(responseText);                                if (responseIndex != -1)                                    bResponseTextFound = true;                            }                        } while (line != null && !bResponseTextFound);                        // Set the status back to failed                        //                        if (!bResponseTextFound)                            serviceStatus = ServiceMonitor.SERVICE_UNAVAILABLE;                    }                } catch (NoRouteToHostException e) {                    e.fillInStackTrace();                    log.warn("No route to host exception for address " + ipv4Addr, e);                    portIndex = ports.length; // Will cause outer for(;;) to                                                // terminate                    break; // Break out of inner for(;;)                } catch (ConnectException e) {                    // Connection Refused!! Continue to retry.                    //                    e.fillInStackTrace();                    log.debug("Connection exception for " + ipv4Addr + ":" + ports[portIndex]);                } catch (InterruptedIOException e) {                    log.debug(getClass().getName() + ": failed to connect within specified timeout (attempt #" + attempts + ")");                } catch (IOException e) {                    // Ignore                    //                    e.fillInStackTrace();                    log.debug("IOException while polling address " + ipv4Addr, e);                } catch (Throwable t) {                    log.warn(getClass().getName() + ": An undeclared throwable exception caught contacting host " + ipv4Addr, t);                    break;                } finally {                    try {                        // Close the socket                        if (socket != null)                            socket.close();                    } catch (IOException e) {                        e.fillInStackTrace();                        log.debug("Error closing socket connection", e);                    }                }            } // end for (attempts)        } // end for (ports)        // Add the 'qualifier' parm to the parameter map. This parm will        // contain the port on which the service was found if AVAILABLE or        // will contain a comma delimited list of the port(s) which were        // tried if the service is UNAVAILABLE        //        if (serviceStatus == ServiceMonitor.SERVICE_UNAVAILABLE) {            //            // Build port string            //            StringBuffer testedPorts = new StringBuffer();            for (int i = 0; i < ports.length; i++) {                if (i == 0)                    testedPorts.append(ports[0]);                else                    testedPorts.append(',').append(ports[i]);            }            // Add to parameter map            parameters.put("qualifier", testedPorts.toString());        } else {            parameters.put("qualifier", Integer.toString(currentPort));        }        //        // return the status of the service        //        return serviceStatus;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?