.entity(this) .period(2000, TimeUnit.MILLISECONDS) .baseUri(uri) .credentialsIfNotNull(config().get(MesosCluster.MESOS_USERNAME), config().get(MesosCluster.MESOS_PASSWORD)) .header("Accept", "application/json") .poll(new HttpPollConfig<Boolean>(SERVICE_UP) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), .poll(new HttpPollConfig<Long>(TASK_STARTED_AT) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), .poll(new HttpPollConfig<Long>(TASK_STAGED_AT) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), .poll(new HttpPollConfig<String>(Attributes.HOSTNAME) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), .poll(new HttpPollConfig<String>(TASK_ID) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), .poll(new HttpPollConfig<String>(Attributes.ADDRESS) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"),
protected void initialize() { checkState(getConfig(URL) != null ^ getConfig(URL_SENSOR) != null, "Must set exactly one of url or urlSensor: url=%s; urlSensor=%s", getConfig(URL), getConfig(URL_SENSOR)); checkState(getConfig(URL_SENSOR) != null || getConfig(URL_POST_PROCESSING) == null, "Must not set urlPostProcessing without urlSensor"); Object configValue = getConfig(URL); if (configValue != null) { url.set(configValue.toString()); } httpFeed = HttpFeed.builder() .entity(entity) .period(getConfig(PERIOD)) .baseUri(Suppliers.compose(Urls.stringToUriFunction(), AtomicReferences.supplier(url))) .poll(new HttpPollConfig<Double>(REQUEST_LATENCY_IN_SECONDS_MOST_RECENT) .onResult(MathFunctions.divide(HttpValueFunctions.latency(), 1000.0d)) .setOnException(null)) .suspended() .build(); if (getUniqueTag()==null) uniqueTag = JavaClassNames.simpleClassName(getClass())+":"+ (getConfig(URL)!=null ? getConfig(URL) : getConfig(URL_SENSOR)); }
.entity(this) .period(1, TimeUnit.MINUTES) .baseUri(sensors().get(Attributes.MAIN_URI)) .credentialsIfNotNull(config().get(MESOS_USERNAME), config().get(MESOS_PASSWORD)) .poll(HttpPollConfig.forSensor(SERVICE_UP) .suburl("/master/health") .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onSuccess(HttpValueFunctions.jsonContents("mem_free_bytes", Long.class)) .onFailureOrException(Functions.constant(-1L))) .poll(HttpPollConfig.forSensor(MEMORY_TOTAL_BYTES) .suburl("/system/stats.json") .onSuccess(HttpValueFunctions.jsonContents("mem_total_bytes", Long.class)) .onFailureOrException(Functions.constant(-1L))); httpFeed = httpFeedBuilder.build(); .entity(this) .period(scanInterval) .baseUri(sensors().get(Attributes.MAIN_URI)) .credentialsIfNotNull(config().get(MESOS_USERNAME), config().get(MESOS_PASSWORD)) .poll(HttpPollConfig.forSensor(MESOS_SLAVE_LIST) .description("Scan Cluster Slaves") .suburl("/master/state.json") scanner = scanBuilder.build();
.entity(this) .period(30, TimeUnit.SECONDS) .baseUri(getMesosCluster().sensors().get(Attributes.MAIN_URI)) .credentialsIfNotNull(config().get(MesosCluster.MESOS_USERNAME), config().get(MesosCluster.MESOS_PASSWORD)) .poll(HttpPollConfig.forSensor(MEMORY_AVAILABLE) .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.castM(Long.class))) .onFailureOrException(Functions.constant(-1L))) .poll(HttpPollConfig.forSensor(CPU_AVAILABLE) .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.castM(Double.class))) .onFailureOrException(Functions.constant(-1d))) .poll(HttpPollConfig.forSensor(DISK_AVAILABLE) .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.castM(Long.class))) .onFailureOrException(Functions.constant(-1L))) .poll(HttpPollConfig.forSensor(MEMORY_USED) .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.castM(Long.class))) .onFailureOrException(Functions.constant(-1L))) .poll(HttpPollConfig.forSensor(CPU_USED)
.entity(this) .period(getConfig(POLL_PERIOD)) .baseUri(webConsoleUri) .credentialsIfNotNull(getConfig(MANAGEMENT_USER), getConfig(MANAGEMENT_PASSWORD)) .poll(new HttpPollConfig<Boolean>(WEB_CONSOLE_ACCESSIBLE) .suburl("/v1/server/healthy") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(Boolean.class))) .poll(new HttpPollConfig<ManagementNodeState>(MANAGEMENT_NODE_STATE) .suburl("/v1/server/ha/state") .onSuccess(Functionals.chain(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(String.class)), Enums.fromStringFunction(ManagementNodeState.class))) .setOnFailureOrException(null)) .build();
.entity(this) .period(200) .baseUri(managementUri) .credentials(getConfig(MANAGEMENT_USER), getConfig(MANAGEMENT_PASSWORD)) .poll(new HttpPollConfig<Integer>(MANAGEMENT_STATUS) .onSuccess(HttpValueFunctions.responseCode()) .suppressDuplicates(true)) .poll(new HttpPollConfig<Boolean>(MANAGEMENT_URL_UP) .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant(false)) .suppressDuplicates(true)) .poll(new HttpPollConfig<Integer>(REQUEST_COUNT) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("requestCount", Integer.class)) .onFailureOrException(EntityFunctions.attribute(this, REQUEST_COUNT)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Integer>(ERROR_COUNT) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("errorCount", Integer.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Integer>(TOTAL_PROCESSING_TIME) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("processingTime", Integer.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Integer>(MAX_PROCESSING_TIME) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("maxTime", Integer.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Long>(BYTES_RECEIVED)
.period(period); HttpFeed.Builder httpRequestBuilder = HttpFeed.builder().entity(entity) .baseUri(uri) .credentialsIfNotNull(username, password) .preemptiveBasicAuth(Boolean.TRUE.equals(preemptiveBasicAuth)) .poll(pollConfig); httpRequestBuilder.headers(headers); HttpFeed feed = httpRequestBuilder.build(); entity.addFeed(feed);
mirror = HttpFeed.builder().entity(this) .baseUri(mirroredEntityUrl) .credentialsIfNotNull(getConfig(BrooklynNode.MANAGEMENT_USER), getConfig(BrooklynNode.MANAGEMENT_PASSWORD)) .period(getConfig(POLL_PERIOD)) .poll(HttpPollConfig.forMultiple() .suburl("/sensors/current-state") .onSuccess(mirrorSensors) .poll(HttpPollConfig.forSensor(MIRROR_SUMMARY).onSuccess(new MirrorSummary())).build();
@Test(groups="Integration") // marked integration as it takes a wee while public void testSuspendResume() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); feed.suspend(); final int countWhenSuspended = server.getRequestCount(); Thread.sleep(500); if (server.getRequestCount() > countWhenSuspended+1) Assert.fail("Request count continued to increment while feed was suspended, from "+countWhenSuspended+" to "+server.getRequestCount()); feed.resume(); Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertTrue(server.getRequestCount() > countWhenSuspended + 1, "Request count failed to increment when feed was resumed, from " + countWhenSuspended + ", still at " + server.getRequestCount()); } }); }
.entity(this) .baseUri(url) .poll(new HttpPollConfig<String>(SERVER_NAME) .onSuccess(HttpValueFunctions.jsonContents("name", String.class))) .poll(new HttpPollConfig<Integer>(SERVER_STATUS) .onSuccess(HttpValueFunctions.jsonContents("status", Integer.class))) .poll(new HttpPollConfig<Boolean>(SERVER_OK) .onSuccess(HttpValueFunctions.jsonContents("ok", Boolean.class))) .poll(new HttpPollConfig<String>(SERVER_BUILD_TIMESTAMP) .onSuccess(HttpValueFunctions.jsonContents(new String[]{"version", "build_timestamp"}, String.class))) .poll(new HttpPollConfig<String>(SERVER_BUILD_HASH) .onSuccess(HttpValueFunctions.jsonContents(new String[]{"version", "build_hash"}, String.class))) .poll(new HttpPollConfig<Boolean>(SERVER_IS_BUILD_SNAPSHOT) .onSuccess(HttpValueFunctions.jsonContents(new String[] {"version", "build_snapshot"}, Boolean.class))) .poll(new HttpPollConfig<String>(SERVER_LUCENE_VERSION) .onSuccess(HttpValueFunctions.jsonContents(new String[] {"version", "lucene_version"}, String.class))) .poll(new HttpPollConfig<String>(SERVER_ES_VERSION) .onSuccess(HttpValueFunctions.jsonContents(new String[] {"version", "es_version"}, String.class))) .build();
@Test(groups = {"Integration"}) public void testPollWithInvalidCredentialsFails() throws Exception { httpService = new HttpService(PortRanges.fromString("9000+")) .basicAuthentication("brooklyn", "hunter2") .start(); feed = HttpFeed.builder() .entity(entity) .baseUri(httpService.getUrl()) .credentials("brooklyn", "9876543210") .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onFailure(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction()) .onException(Functions.constant("Failed!"))) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 401); Asserts.succeedsEventually(new Runnable() { @Override public void run() { String val = entity.getAttribute(SENSOR_STRING); assertTrue(val != null && val.equals("Failed!"), "val=" + val); } }); } }
@Override public void connectSensors() { super.connectSensors(); HttpFeed.Builder httpFeedBuilder = HttpFeed.builder() .entity(this) .period(2000, TimeUnit.MILLISECONDS) .baseUri(sensors().get(FRAMEWORK_URL)) .credentialsIfNotNull(config().get(MesosCluster.MESOS_USERNAME), config().get(MesosCluster.MESOS_PASSWORD)) .poll(HttpPollConfig.forSensor(MARATHON_APPLICATIONS) .suppressDuplicates(true) .suburl("/v2/apps/") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("apps"), JsonFunctions.forEach(JsonFunctions.<String>getPath("id")))) .onFailureOrException(Functions.constant(Arrays.asList(new String[0])))) .poll(HttpPollConfig.forSensor(MARATHON_VERSION) .suppressDuplicates(true) .suburl("/v2/info/") .onSuccess(HttpValueFunctions.jsonContents("version", String.class)) .onFailureOrException(Functions.constant(""))) .poll(HttpPollConfig.forSensor(SERVICE_UP) .suppressDuplicates(true) .suburl("/ping") .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant(Boolean.FALSE))); httpFeed = httpFeedBuilder.build(); }
.entity(entity) .baseUrl(server.getUrl("/")) .credentials(username, password) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .build();
@Test(groups = {"Integration"}) public void testPollsAndParsesHttpGetResponseWithSsl() throws Exception { httpService = new HttpService(PortRanges.fromString("9000+"), true).start(); URI baseUrl = new URI(httpService.getUrl()); assertEquals(baseUrl.getScheme(), "https", "baseUrl="+baseUrl); feed = HttpFeed.builder() .entity(entity) .baseUri(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 200); Asserts.succeedsEventually(new Runnable() { @Override public void run() { String val = entity.getAttribute(SENSOR_STRING); assertTrue(val != null && val.contains("Hello, World"), "val="+val); }}); }
protected void runPollsAndParsesHttpGetResponseWithBasicAuthentication(boolean preemptiveBasicAuth) throws Exception { final String username = "brooklyn"; final String password = "hunter2"; httpService = new HttpService(PortRanges.fromString("9000+")) .basicAuthentication(username, password) .start(); URI baseUrl = new URI(httpService.getUrl()); assertEquals(baseUrl.getScheme(), "http", "baseUrl="+baseUrl); feed = HttpFeed.builder() .entity(entity) .baseUri(baseUrl) .credentials(username, password) .preemptiveBasicAuth(preemptiveBasicAuth) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 200); Asserts.succeedsEventually(new Runnable() { @Override public void run() { String val = entity.getAttribute(SENSOR_STRING); assertTrue(val != null && val.contains("Hello, World"), "val="+val); }}); }
@Test public void testUsesFailureHandlerOn4xx() throws Exception { if (server != null) server.shutdown(); server = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 100; i++) { server.enqueue(new MockResponse() .setResponseCode(401) .setBody("Unauthorised")); } server.play(); feed = HttpFeed.builder() .entity(entity) .baseUrl(server.getUrl("/")) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onFailure(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction()) .onFailure(Functions.constant("Failed"))) .build(); assertSensorEventually(SENSOR_INT, 401, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "Failed", TIMEOUT_MS); server.shutdown(); }
private void connectAuthenticatedSensors() { hostsHttpFeed = HttpFeed.builder() .entity(this) .period(1000, TimeUnit.MILLISECONDS) .baseUri(String.format("%s/api/v1/hosts", ambariUri)) .credentials(usernamePasswordCredentials.getUserName(), usernamePasswordCredentials.getPassword()) .header(HttpHeaders.AUTHORIZATION, HttpTool.toBasicAuthorizationValue(usernamePasswordCredentials)) .poll(new HttpPollConfig<List<String>>(REGISTERED_HOSTS) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), getHosts())) .onFailureOrException(Functions.<List<String>>constant(ImmutableList.<String>of()))) .build(); clusterHttpFeed = HttpFeed.builder() .entity(this) .period(1000, TimeUnit.MILLISECONDS) .baseUri(String.format("%s/api/v1/clusters/%s/requests/%d", ambariUri, getConfig(AmbariCluster.CLUSTER_NAME), 1)) .credentials(usernamePasswordCredentials.getUserName(), usernamePasswordCredentials.getPassword()) .header(HttpHeaders.AUTHORIZATION, HttpTool.toBasicAuthorizationValue(usernamePasswordCredentials)) .poll(new HttpPollConfig<String>(CLUSTER_STATE) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), getRequestState())) .onFailureOrException(Functions.<String>constant(null))) .build(); }
private void newMultiFeed(URL baseUrl) { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(HttpPollConfig.forMultiple() .onSuccess(new Function<HttpToolResponse,Void>() { @Override public Void apply(HttpToolResponse response) { entity.sensors().set(SENSOR_INT, response.getResponseCode()); if (response.getResponseCode()==200) entity.sensors().set(SENSOR_STRING, response.getContentAsString()); return null; } }) .onFailureOrException(Functionals.function(EntityFunctions.settingSensorsConstant(entity, MutableMap.<AttributeSensor<?>,Object>of( SENSOR_INT, -1, SENSOR_STRING, PollConfig.REMOVE)))) .period(100)) .build(); }