@Test @JUnitHttpServer(port=10342) public void testNMS2702() throws UnknownHostException { HttpMonitor monitor = new HttpMonitor(); Map<String, Object> parameters = new ConcurrentSkipListMap<String, Object>(); parameters.put("port", "10342"); parameters.put("url", "/test-NMS2702.html"); parameters.put("retry", "1"); parameters.put("timeout", "500"); parameters.put("verbose", "true"); parameters.put("response-text", "~.*OK.*"); MonitoredService svc = MonitorTestUtils.getMonitoredService(3, "localhost", "HTTP", false); PollStatus status = monitor.poll(svc, parameters); assertTrue(status.isAvailable()); parameters.put("response-text", "~.*Jetty.*"); svc = MonitorTestUtils.getMonitoredService(3, "localhost", "HTTP", false); status = monitor.poll(svc, parameters); assertTrue(status.isAvailable()); }
public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); final String nodeLabel = svc.getNodeLabel(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } int currentPort = -1; final HttpMonitorClient httpClient = new HttpMonitorClient(nodeLabel, iface, 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 = " + currentPort + ", Address = " + (iface.getAddress()) + ", " + httpClient.getTimeoutTracker()); httpClient.setCurrentPort(currentPort); String serviceInfo = new StringBuilder(iface.getAddress().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: " + (iface.getAddress()) + " on port: " + 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); portIndex = determinePorts(httpClient.getParameters()).length; httpClient.setReason("No route to host exception"); } catch (SocketTimeoutException e) { log().info("checkStatus: HTTP socket connection for service " + serviceInfo + " timed out with " + httpClient.getTimeoutTracker().toString()); httpClient.setReason("HTTP connection timeout"); } catch (InterruptedIOException e) { log().info(String.format("checkStatus: HTTP connection for service " + serviceInfo + " interrupted after %d bytes transferred with %s", 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(exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("IOException while polling address: "+(iface.getAddress())+": "+e.getMessage()); } catch (Throwable e) { String exceptionClass = e.getClass().getSimpleName(); log().warn("Unexpected " + exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("Unexpected exception while polling address: "+(iface.getAddress())+": "+e.getMessage()); } finally { httpClient.closeConnection(); } } } return httpClient.determinePollStatusResponse(); }
HttpMonitor extends AbstractServiceMonitor { public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); final String nodeLabel = svc.getNodeLabel(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } int currentPort = -1; final HttpMonitorClient httpClient = new HttpMonitorClient(nodeLabel, iface, 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 = " + currentPort + ", Address = " + (iface.getAddress()) + ", " + httpClient.getTimeoutTracker()); httpClient.setCurrentPort(currentPort); String serviceInfo = new StringBuilder(iface.getAddress().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: " + (iface.getAddress()) + " on port: " + 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); portIndex = determinePorts(httpClient.getParameters()).length; httpClient.setReason("No route to host exception"); } catch (SocketTimeoutException e) { log().info("checkStatus: HTTP socket connection for service " + serviceInfo + " timed out with " + httpClient.getTimeoutTracker().toString()); httpClient.setReason("HTTP connection timeout"); } catch (InterruptedIOException e) { log().info(String.format("checkStatus: HTTP connection for service " + serviceInfo + " interrupted after %d bytes transferred with %s", 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(exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("IOException while polling address: "+(iface.getAddress())+": "+e.getMessage()); } catch (Throwable e) { String exceptionClass = e.getClass().getSimpleName(); log().warn("Unexpected " + exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("Unexpected exception while polling address: "+(iface.getAddress())+": "+e.getMessage()); } finally { httpClient.closeConnection(); } } } return httpClient.determinePollStatusResponse(); } }
HttpMonitor extends AbstractServiceMonitor { public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); final String nodeLabel = svc.getNodeLabel(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } int currentPort = -1; final HttpMonitorClient httpClient = new HttpMonitorClient(nodeLabel, iface, 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 = " + currentPort + ", Address = " + (iface.getAddress()) + ", " + httpClient.getTimeoutTracker()); httpClient.setCurrentPort(currentPort); String serviceInfo = new StringBuilder(iface.getAddress().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: " + (iface.getAddress()) + " on port: " + 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); portIndex = determinePorts(httpClient.getParameters()).length; httpClient.setReason("No route to host exception"); } catch (SocketTimeoutException e) { log().info("checkStatus: HTTP socket connection for service " + serviceInfo + " timed out with " + httpClient.getTimeoutTracker().toString()); httpClient.setReason("HTTP connection timeout"); } catch (InterruptedIOException e) { log().info(String.format("checkStatus: HTTP connection for service " + serviceInfo + " interrupted after %d bytes transferred with %s", 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(exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("IOException while polling address: "+(iface.getAddress())+": "+e.getMessage()); } catch (Throwable e) { String exceptionClass = e.getClass().getSimpleName(); log().warn("Unexpected " + exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("Unexpected exception while polling address: "+(iface.getAddress())+": "+e.getMessage()); } finally { httpClient.closeConnection(); } } } return httpClient.determinePollStatusResponse(); }  }
HttpMonitor extends AbstractServiceMonitor { public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); final String nodeLabel = svc.getNodeLabel(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } int currentPort = -1; final HttpMonitorClient httpClient = new HttpMonitorClient(nodeLabel, iface, 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 = " + currentPort + ", Address = " + (iface.getAddress()) + ", " + httpClient.getTimeoutTracker()); httpClient.setCurrentPort(currentPort); String serviceInfo = new StringBuilder(iface.getAddress().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: " + (iface.getAddress()) + " on port: " + 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); portIndex = determinePorts(httpClient.getParameters()).length; httpClient.setReason("No route to host exception"); } catch (SocketTimeoutException e) { log().info("checkStatus: HTTP socket connection for service " + serviceInfo + " timed out with " + httpClient.getTimeoutTracker().toString()); httpClient.setReason("HTTP connection timeout"); } catch (InterruptedIOException e) { log().info(String.format("checkStatus: HTTP connection for service " + serviceInfo + " interrupted after %d bytes transferred with %s", 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(exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("IOException while polling address: "+(iface.getAddress())+": "+e.getMessage()); } catch (Throwable e) { String exceptionClass = e.getClass().getSimpleName(); log().warn("Unexpected " + exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("Unexpected exception while polling address: "+(iface.getAddress())+": "+e.getMessage()); } finally { httpClient.closeConnection(); } } } return httpClient.determinePollStatusResponse(); }  PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters); }
HttpMonitor extends AbstractServiceMonitor { public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) { final NetworkInterface<InetAddress> iface = svc.getNetInterface(); final String nodeLabel = svc.getNodeLabel(); if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); } int currentPort = -1; final HttpMonitorClient httpClient = new HttpMonitorClient(nodeLabel, iface, 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 = " + currentPort + ", Address = " + (iface.getAddress()) + ", " + httpClient.getTimeoutTracker()); httpClient.setCurrentPort(currentPort); String serviceInfo = new StringBuilder(iface.getAddress().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: " + (iface.getAddress()) + " on port: " + 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); portIndex = determinePorts(httpClient.getParameters()).length; httpClient.setReason("No route to host exception"); } catch (SocketTimeoutException e) { log().info("checkStatus: HTTP socket connection for service " + serviceInfo + " timed out with " + httpClient.getTimeoutTracker().toString()); httpClient.setReason("HTTP connection timeout"); } catch (InterruptedIOException e) { log().info(String.format("checkStatus: HTTP connection for service " + serviceInfo + " interrupted after %d bytes transferred with %s", 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(exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("IOException while polling address: "+(iface.getAddress())+": "+e.getMessage()); } catch (Throwable e) { String exceptionClass = e.getClass().getSimpleName(); log().warn("Unexpected " + exceptionClass + " while polling " + serviceInfo, e); httpClient.setReason("Unexpected exception while polling address: "+(iface.getAddress())+": "+e.getMessage()); } finally { httpClient.closeConnection(); } } } return httpClient.determinePollStatusResponse(); }  PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters); static final String PARAMETER_VERBOSE; static final String PARAMETER_USER_AGENT; static final String PARAMETER_BASIC_AUTHENTICATION; static final String PARAMETER_USER; static final String PARAMETER_PASSWORD; static final String PARAMETER_RESOLVE_IP; static final String PARAMETER_NODE_LABEL_HOST_NAME; static final String PARAMETER_HOST_NAME; static final String PARAMETER_RESPONSE_TEXT; static final String PARAMETER_RESPONSE; static final String PARAMETER_URL; static final String PARAMETER_PORT; }