@Test @JUnitHttpServer(port=10342) public void testLocalhostConnection() throws UnknownHostException { if (m_runTests == false) return; Map<String, Object> m = new ConcurrentSkipListMap<String, Object>(); Parameter p = new Parameter(); ServiceMonitor monitor = new TcpMonitor(); MonitoredService svc = MonitorTestUtils.getMonitoredService(3, "localhost", "TCP"); p.setKey("port"); p.setValue("10342"); m.put(p.getKey(), p.getValue()); p.setKey("retry"); p.setValue("1"); m.put(p.getKey(), p.getValue()); p.setKey("timeout"); p.setValue("500"); m.put(p.getKey(), p.getValue()); PollStatus status = monitor.poll(svc, m); MockUtil.println("Reason: "+status.getReason()); assertEquals(PollStatus.SERVICE_AVAILABLE, status.getStatusCode()); assertNull(status.getReason()); }
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("TcpMonitor: required parameter 'port' is not present in supplied properties."); } String strBannerMatch = ParameterMap.getKeyedString(parameters, PARAMETER_BANNER, null); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); if (log().isDebugEnabled()) log().debug("poll: address = " + hostAddress + ", port = " + port + ", " + tracker); 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(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("TcpMonitor: connected to host: " + ipv4Addr + " on port: " + port); serviceStatus = PollStatus.unresponsive(); if (strBannerMatch == null || strBannerMatch.length() == 0 || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(tracker.elapsedTimeInMillis()); break; } BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream())); String response = rdr.readLine(); double responseTime = tracker.elapsedTimeInMillis(); if (response == null) continue; if (log().isDebugEnabled()) { log().debug("poll: banner = " + response); log().debug("poll: responseTime= " + responseTime + "ms"); } if (response.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else serviceStatus = PollStatus.unavailable("Banner: '"+response+"' does not contain match string '"+strBannerMatch+"'"); } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) socket.close(); } catch (IOException e) { e.fillInStackTrace(); if (log().isDebugEnabled()) log().debug("poll: Error closing socket.", e); } } } return serviceStatus; }
TcpMonitor extends AbstractServiceMonitor { public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("TcpMonitor: required parameter 'port' is not present in supplied properties."); } String strBannerMatch = ParameterMap.getKeyedString(parameters, PARAMETER_BANNER, null); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); if (log().isDebugEnabled()) log().debug("poll: address = " + hostAddress + ", port = " + port + ", " + tracker); 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(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("TcpMonitor: connected to host: " + ipv4Addr + " on port: " + port); serviceStatus = PollStatus.unresponsive(); if (strBannerMatch == null || strBannerMatch.length() == 0 || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(tracker.elapsedTimeInMillis()); break; } BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream())); String response = rdr.readLine(); double responseTime = tracker.elapsedTimeInMillis(); if (response == null) continue; if (log().isDebugEnabled()) { log().debug("poll: banner = " + response); log().debug("poll: responseTime= " + responseTime + "ms"); } if (response.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else serviceStatus = PollStatus.unavailable("Banner: '"+response+"' does not contain match string '"+strBannerMatch+"'"); } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) socket.close(); } catch (IOException e) { e.fillInStackTrace(); if (log().isDebugEnabled()) log().debug("poll: Error closing socket.", e); } } } return serviceStatus; } }
TcpMonitor extends AbstractServiceMonitor { public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("TcpMonitor: required parameter 'port' is not present in supplied properties."); } String strBannerMatch = ParameterMap.getKeyedString(parameters, PARAMETER_BANNER, null); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); if (log().isDebugEnabled()) log().debug("poll: address = " + hostAddress + ", port = " + port + ", " + tracker); 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(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("TcpMonitor: connected to host: " + ipv4Addr + " on port: " + port); serviceStatus = PollStatus.unresponsive(); if (strBannerMatch == null || strBannerMatch.length() == 0 || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(tracker.elapsedTimeInMillis()); break; } BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream())); String response = rdr.readLine(); double responseTime = tracker.elapsedTimeInMillis(); if (response == null) continue; if (log().isDebugEnabled()) { log().debug("poll: banner = " + response); log().debug("poll: responseTime= " + responseTime + "ms"); } if (response.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else serviceStatus = PollStatus.unavailable("Banner: '"+response+"' does not contain match string '"+strBannerMatch+"'"); } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) socket.close(); } catch (IOException e) { e.fillInStackTrace(); if (log().isDebugEnabled()) log().debug("poll: Error closing socket.", e); } } } return serviceStatus; }  }
TcpMonitor extends AbstractServiceMonitor { public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("TcpMonitor: required parameter 'port' is not present in supplied properties."); } String strBannerMatch = ParameterMap.getKeyedString(parameters, PARAMETER_BANNER, null); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); if (log().isDebugEnabled()) log().debug("poll: address = " + hostAddress + ", port = " + port + ", " + tracker); 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(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("TcpMonitor: connected to host: " + ipv4Addr + " on port: " + port); serviceStatus = PollStatus.unresponsive(); if (strBannerMatch == null || strBannerMatch.length() == 0 || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(tracker.elapsedTimeInMillis()); break; } BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream())); String response = rdr.readLine(); double responseTime = tracker.elapsedTimeInMillis(); if (response == null) continue; if (log().isDebugEnabled()) { log().debug("poll: banner = " + response); log().debug("poll: responseTime= " + responseTime + "ms"); } if (response.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else serviceStatus = PollStatus.unavailable("Banner: '"+response+"' does not contain match string '"+strBannerMatch+"'"); } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) socket.close(); } catch (IOException e) { e.fillInStackTrace(); if (log().isDebugEnabled()) log().debug("poll: Error closing socket.", e); } } } return serviceStatus; }  PollStatus poll(MonitoredService svc, Map<String, Object> parameters); }
TcpMonitor extends AbstractServiceMonitor { public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); if (iface.getType() != NetworkInterface.TYPE_INET) throw new NetworkInterfaceNotSupportedException("Unsupported interface type, only TYPE_INET currently supported"); TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); if (port == DEFAULT_PORT) { throw new RuntimeException("TcpMonitor: required parameter 'port' is not present in supplied properties."); } String strBannerMatch = ParameterMap.getKeyedString(parameters, PARAMETER_BANNER, null); InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); if (log().isDebugEnabled()) log().debug("poll: address = " + hostAddress + ", port = " + port + ", " + tracker); 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(ipv4Addr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); log().debug("TcpMonitor: connected to host: " + ipv4Addr + " on port: " + port); serviceStatus = PollStatus.unresponsive(); if (strBannerMatch == null || strBannerMatch.length() == 0 || strBannerMatch.equals("*")) { serviceStatus = PollStatus.available(tracker.elapsedTimeInMillis()); break; } BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream())); String response = rdr.readLine(); double responseTime = tracker.elapsedTimeInMillis(); if (response == null) continue; if (log().isDebugEnabled()) { log().debug("poll: banner = " + response); log().debug("poll: responseTime= " + responseTime + "ms"); } if (response.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else serviceStatus = PollStatus.unavailable("Banner: '"+response+"' does not contain match string '"+strBannerMatch+"'"); } catch (NoRouteToHostException e) { serviceStatus = logDown(Level.WARN, "No route to host exception for address " + hostAddress, e); break; } catch (InterruptedIOException e) { serviceStatus = logDown(Level.DEBUG, "did not connect to host with " + tracker); } catch (ConnectException e) { serviceStatus = logDown(Level.DEBUG, "Connection exception for address: " + ipv4Addr, e); } catch (IOException e) { serviceStatus = logDown(Level.DEBUG, "IOException while polling address: " + ipv4Addr, e); } finally { try { if (socket != null) socket.close(); } catch (IOException e) { e.fillInStackTrace(); if (log().isDebugEnabled()) log().debug("poll: Error closing socket.", e); } } } return serviceStatus; }  PollStatus poll(MonitoredService svc, Map<String, Object> parameters); static final String PARAMETER_BANNER; static final String PARAMETER_PORT; }