Skip to content

Instantly share code, notes, and snippets.

@ahgittin
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahgittin/3d40d1f6ab35d0b396c1 to your computer and use it in GitHub Desktop.
Save ahgittin/3d40d1f6ab35d0b396c1 to your computer and use it in GitHub Desktop.
SampleMonitorEntity
package io.cloudsoft.ibm.mms.amp.util;
import brooklyn.config.ConfigKey;
import brooklyn.enricher.Enrichers;
import brooklyn.entity.basic.AbstractEntity;
import brooklyn.entity.basic.Attributes;
import brooklyn.entity.basic.ConfigKeys;
import brooklyn.event.AttributeSensor;
import brooklyn.event.basic.Sensors;
import brooklyn.event.feed.http.HttpFeed;
import brooklyn.event.feed.http.HttpPollConfig;
import brooklyn.event.feed.http.HttpValueFunctions;
import brooklyn.util.flags.SetFromFlag;
import brooklyn.util.guava.Functionals;
import brooklyn.util.guava.IfFunctions;
import brooklyn.util.math.MathPredicates;
import brooklyn.util.time.Duration;
import com.google.api.client.repackaged.com.google.common.base.Preconditions;
import com.google.common.base.Functions;
/**
* Sample entity which represents an external resource, in this case a Github repository
* specified by its URL, reporting the number of repositories
* <p>
* Can deploy to Brooklyn with a JAR containing this placed in the lib/dropins/ directory.
* (Alternatively you can use the brooklyn maven archetype to create your own distributable,
* or build as an OSGi bundle and add to the Brooklyn catalog pointing to the OSGi bundle with a URL.
* <p>
* The following YAML will then deploy it:
<code>
services:
- type: io.cloudsoft.ibm.mms.amp.util.SampleMonitorEntity
name: Cloudsoft Repo
brooklyn.config:
sample.targetUrl: https://api.github.com/orgs/cloudsoft
sample.minReposRequired: 500
# but note, the github rate limit is very low, 60 reqs / hour
</code>
*/
public class SampleMonitorEntity extends AbstractEntity {
@SetFromFlag("target_url")
ConfigKey<String> TARGET_URL = ConfigKeys.newStringConfigKey("sample.targetUrl", "The target URL"
// optional default value
, "https://api.github.com/orgs/apache"
);
@SetFromFlag("min_repos_required")
ConfigKey<Integer> MIN_REPOS_REQUIRED = ConfigKeys.newIntegerConfigKey("sample.minReposRequired", "Number of repositories required for service to be happy",
2);
AttributeSensor<String> REPO_NAME = Sensors.newStringSensor("sample.repoName");
AttributeSensor<Integer> REPO_COUNT = Sensors.newIntegerSensor("sample.repoCount");
AttributeSensor<Boolean> SERVICE_IS_HAPPY = Sensors.newBooleanSensor("sample.happy");
@Override
public void init() {
super.init();
connectSensors();
}
private void connectSensors() {
String url = getConfig(TARGET_URL);
Preconditions.checkNotNull(url, "Must supply: "+TARGET_URL);
HttpFeed.builder().baseUri(url)
.entity(this)
.period(Duration.FIVE_MINUTES)
.poll(HttpPollConfig.forSensor(REPO_NAME)
.onSuccess(HttpValueFunctions.<String>jsonContentsFromPath("name"))
)
.poll(HttpPollConfig.forSensor(REPO_COUNT)
.onSuccess(HttpValueFunctions.<Integer>jsonContentsFromPath("public_repos"))
.onFailureOrException(Functions.<Integer>constant(null))
)
.poll(HttpPollConfig.forSensor(SERVICE_IS_HAPPY)
.onSuccess(Functionals.chain(HttpValueFunctions.<Integer>jsonContentsFromPath("public_repos"),
Functions.forPredicate(MathPredicates.greaterThan(6))))
.onFailureOrException(Functions.constant(false))
)
.build();
// update the "service_up" status, using the build in "not-up-indicators", based on service_is_happy
addEnricher(
Enrichers.builder().updatingMap(Attributes.SERVICE_NOT_UP_INDICATORS).from(SERVICE_IS_HAPPY)
.computing(IfFunctions.ifNotEquals(true).value("Not enough repos here!").defaultValue(null))
.build());
}
}
# simply drop this into the "add application" -> "yaml" tab of a running brooklyn instance
# (no setup needed - but use a recent snapshot version, >= Sept 2014)
services:
- type: brooklyn.entity.basic.BasicEntity
name: Sample REST Monitor
entity.initializers:
- type: brooklyn.entity.software.http.HttpRequestSensor
brooklyn.config:
name: sample.repoCount
period: 5m
uri: https://api.github.com/orgs/apache
jsonPath: public_repos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment