Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #75 from Jiri-Kremser/HWKINVENT-26
Browse files Browse the repository at this point in the history
Hwkinvent-24 - port pinger to new inventory bus + setup work
  • Loading branch information
mtho11 committed Apr 13, 2015
2 parents f1a7cbb + 2f798a5 commit ab71d39
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 114 deletions.
8 changes: 5 additions & 3 deletions kettle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@

<!-- The versions of the components that go into our integration distribution -->
<version.org.hawkular.nest>1.0.0-SNAPSHOT</version.org.hawkular.nest>
<version.org.hawkular.inventory>1.0.0-SNAPSHOT</version.org.hawkular.inventory>
<version.org.hawkular.inventory>0.0.1-SNAPSHOT</version.org.hawkular.inventory>
<version.org.hawkular.alerts>1.0.0-SNAPSHOT</version.org.hawkular.alerts>
<version.org.hawkular.metrics>0.3.2-SNAPSHOT</version.org.hawkular.metrics>
<version.org.hawkular.console>1.0.0-SNAPSHOT</version.org.hawkular.console>
<version.org.hawkular.accounts>1.0.0-SNAPSHOT</version.org.hawkular.accounts>
<version.org.hawkular.pinger>1.0.0-SNAPSHOT</version.org.hawkular.pinger>
<version.org.hawkular.availCreator>1.0.0-SNAPSHOT</version.org.hawkular.availCreator>
<version.org.keycloak>1.1.0.Final</version.org.keycloak>
</properties>

Expand All @@ -65,13 +67,13 @@
<dependency>
<groupId>org.hawkular</groupId>
<artifactId>hawkular-pinger</artifactId>
<version>${version.org.hawkular.inventory}</version>
<version>${version.org.hawkular.pinger}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>org.hawkular</groupId>
<artifactId>hawkular-avail-creator</artifactId>
<version>${version.org.hawkular.inventory}</version>
<version>${version.org.hawkular.availCreator}</version>
<type>war</type>
</dependency>
<dependency>
Expand Down
6 changes: 3 additions & 3 deletions kettle/src/main/scripts/standalone.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@
<admin-object use-java-context="true"
enabled="true"
class-name="org.apache.activemq.command.ActiveMQTopic"
jndi-name="java:/topic/HawkularNotifications"
pool-name="HawkularNotifications">
<config-property name="PhysicalName">HawkularNotifications</config-property>
jndi-name="java:/topic/HawkularInventoryChanges"
pool-name="HawkularInventoryChanges">
<config-property name="PhysicalName">HawkularInventoryChanges</config-property>
</admin-object>
<admin-object use-java-context="true"
enabled="true"
Expand Down
2 changes: 1 addition & 1 deletion modules/end-to-end-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>org.hawkular.inventory</groupId>
<artifactId>inventory-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,85 @@

package org.hawkular.integration.test

import org.hawkular.inventory.api.MetricDefinition
import org.hawkular.inventory.api.MetricUnit
import org.hawkular.inventory.api.Resource
import org.hawkular.inventory.api.ResourceType
import org.hawkular.inventory.api.model.Environment
import org.hawkular.inventory.api.model.Metric
import org.hawkular.inventory.api.model.MetricType
import org.hawkular.inventory.api.model.Resource
import org.hawkular.inventory.api.model.ResourceType
import org.hawkular.inventory.api.model.Tenant

import org.junit.Test

import static junit.framework.Assert.assertEquals

class Scenario1 extends AbstractTestBase {

def tenantId = "i-test"
def environmentId = "test"
def hawk_id = "hawkular_web"

@Test
public void testScenario() throws Exception {


// 1) Add the resource to be monitored

def hawk_web = new Resource();

hawk_web.id = hawk_id;
hawk_web.type = ResourceType.URL
hawk_web.addParameter("url","http://hawkular.github.io")

def response = client.post(path: "inventory/$tenantId/resources", body : hawk_web)

assertEquals(200, response.status)

// 2) Add the "ping" status + time metrics
def statusCode = new MetricDefinition("status.code");
statusCode.description = "Status code returned from ping"
def statusTime = new MetricDefinition("status.time",MetricUnit.MILLI_SECOND);
statusTime.description = "Time to ping the target in ms"

response = client.post(path: "inventory/$tenantId/resources/$hawk_id/metrics",
body: [ statusCode, statusTime])

def good = response.status == 200 || response.status == 304
assert good

// 3 inform pinger is internal

// 4 simulate ping + response - metrics for ~ the last 30 minutes
// 1) create tenant in inventory
def tenant = new Tenant(tenantId)
def response = client.post(path: "inventory/tenants", body : tenant)
assertResponseOk(response.status)

// 2) create environment in inventory
def environment = new Environment(environmentId)
response = client.post(path: "inventory/$tenantId/environments", body : environment)
assertResponseOk(response.status)

// 3) create resource type in inventory
def res_type = new ResourceType(tenantId, "URL", "1.0")
response = client.post(path: "inventory/$tenantId/resourceTypes", body : res_type)
assertResponseOk(response.status)

// 4) create resource in inventory
def hawk_web = new Resource(tenantId, environmentId, hawk_id, res_type)
hawk_web.getProperties().put("url", "http://hawkular.org")
response = client.post(path: "inventory/$tenantId/$environmentId/resources", body : hawk_web)
assertResponseOk(response.status)

// 5) create metric types ("ping" status + time metrics)
def status_code_type = new MetricType(tenantId, "status.code.type")
status_code_type.getProperties().put("description", "Status code returned from ping")
def status_time_type = new MetricType(tenantId, "status.time.type", MetricUnit.MILLI_SECOND)
status_time_type.getProperties().put("description", "Time to ping the target in ms")
response = client.post(path: "inventory/$tenantId/metricTypes", body: status_code_type)
assertResponseOk(response.status)
response = client.post(path: "inventory/$tenantId/metricTypes", body: status_time_type)
assertResponseOk(response.status)

// 6) create metrics
def status_code = new Metric(tenantId, environmentId, "status.code", status_code_type)
def status_time = new Metric(tenantId, environmentId, "status.time", status_time_type)
response = client.post(path: "inventory/$tenantId/$environmentId/metrics", body: status_code)
assertResponseOk(response.status)
response = client.post(path: "inventory/$tenantId/$environmentId/metrics", body: status_time)
assertResponseOk(response.status)

// 7) assign metrics to the resource
response = client.post(path: "inventory/$tenantId/$environmentId/resources/$hawk_id/metrics",
body: ["status.code", "status.time"]
)
assertResponseOk(response.status)

// 8 informing pinger is internal (bus msg)

// 9 simulate ping + response - metrics for ~ the last 30 minutes
for (int i = -30 ; i <-3 ; i++ ) {
postMetricValue(hawk_id, statusTime, 100 + i, i)
postMetricValue(hawk_id, statusCode, 200, i)
postMetricValue(hawk_id, status_time.id, 100 + i, i)
postMetricValue(hawk_id, status_code.id, 200, i)
}

postMetricValue(hawk_id, statusCode, 500, -2)
postMetricValue(hawk_id, statusCode, 404, -1)
postMetricValue(hawk_id, statusCode, 200, 0)
postMetricValue(hawk_id, statusTime, 42, 0)

postMetricValue(hawk_id, status_code.id, 500, -2)
postMetricValue(hawk_id, status_code.id, 404, -1)
postMetricValue(hawk_id, status_code.id, 200, 0)
postMetricValue(hawk_id, status_time.id, 42, 0)

// 5 was simulated in step 4 as well

// 6 Get values for a chart - last 4h data
// 10 Get values for a chart - last 4h data

def end = System.currentTimeMillis()
def start = end - 4 *3600 * 1000 // 4h earlier
Expand All @@ -83,21 +104,26 @@ class Scenario1 extends AbstractTestBase {

println(response.data)

// 7 define an alert
// 11 define an alert

// response = client.post(path: "alerts/triggers/")

}

private void postMetricValue(String resourceId, MetricDefinition metric, int value, int timeSkewMinutes = 0) {
private void assertResponseOk(int responseCode) {
assertTrue("Response code should be 2xx or 304", (responseCode.status >= 200 && responseCode.status < 300) ||
responseCode.status == 304)
}

private void postMetricValue(String resourceId, String metricName, int value, int timeSkewMinutes = 0) {
def response
def now = System.currentTimeMillis()
def tmp = "$resourceId.$metric.name"
def tmp = "$resourceId.$metricName"

long time = now + (timeSkewMinutes * 60 * 1000)

response = client.post(path: "/hawkular-metrics/$tenantId/metrics/numeric/$tmp/data",
body: [[timestamp: time, value: value]])
assertEquals(200, response.status)
assertResponseOk(response.status)
}
}
8 changes: 7 additions & 1 deletion modules/pinger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@
<dependency>
<groupId>org.hawkular.inventory</groupId>
<artifactId>inventory-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.hawkular.inventory</groupId>
<artifactId>inventory-bus-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.hawkular.component.pinger;

import org.jboss.logging.BasicLogger;
import org.jboss.logging.LogMessage;
import org.jboss.logging.Logger;
import org.jboss.logging.Message;
Expand All @@ -27,14 +28,14 @@
* @author Heiko W. Rupp
*/
@MessageLogger(projectCode = "HAWKULAR")
public interface Log {
public interface Log extends BasicLogger {

Log LOG = Logger.getMessageLogger(Log.class, "org.hawkular.component.pinger");


@LogMessage(level = Logger.Level.WARN)
@Message(id = 5000, value = "No code in message headers found")
void wNoCode();
@Message(id = 5000, value = "No action in message headers found")
void wNoAction();

@LogMessage
@Message(id = 5001, value = "Pinging the remote threw an exception: %s")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
*/
package org.hawkular.component.pinger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.hawkular.bus.common.SimpleBasicMessage;
import org.hawkular.bus.common.consumer.BasicMessageListener;
import org.hawkular.inventory.api.Resource;
import org.hawkular.inventory.api.ResourceType;
import org.hawkular.inventory.api.model.Resource;
import org.hawkular.inventory.api.observable.Action;
import org.hawkular.inventory.bus.api.ResourceEvent;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
Expand All @@ -30,51 +28,49 @@

/**
* Receiver that listens on JMS Topic and checks for updated resources.
* Listening goes on 'java:/topic/HawkularNotifications'
* Listening goes on 'java:/topic/HawkularInventoryChanges'
*
* @author Heiko W. Rupp
*/
@MessageDriven( activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "HawkularNotifications"),
@ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "code LIKE 'resource%'")
@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/topic/HawkularInventoryChanges"),
@ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "entityType = 'resource'")
})
@SuppressWarnings("unused")
public class NotificationReceiver extends BasicMessageListener<SimpleBasicMessage> {
public class NotificationReceiver extends BasicMessageListener<ResourceEvent> {


@EJB PingManager pingManager;

@Override
public void onBasicMessage(SimpleBasicMessage message) {
public void onBasicMessage(ResourceEvent message) {

try {

String payload = message.getMessage();
Log.LOG.debugf("Received message: %s", message);

Map<String, String> details = message.getHeaders();
String code;
if (details!=null) {
code = details.get("code");
String action;
if (details != null) {
action = details.get("action");
} else {
// should never happen as the selector should not let them pass
Log.LOG.wNoCode();
Log.LOG.wNoAction();
return;
}

Gson gson = new GsonBuilder().create();
Resource resource = message.getObject();

Resource resource = gson.fromJson(payload, Resource.class);
if (!resource.getType().equals(ResourceType.URL)) {
if (!"URL".equals(resource.getType().getId())) {
return;
}
String url = resource.getParameters().get("url");
String url = (String) resource.getProperties().get("url");
PingDestination destination = new PingDestination(resource.getId(), url);

if ("resource_added".equals(code)) {

if (message.getAction() == Action.Enumerated.CREATED) {
pingManager.addDestination(destination);
}
else if ("resource_deleted".equals(code)) {
} else if (message.getAction() == Action.Enumerated.DELETED) {
pingManager.removeDestination(destination);
}

Expand Down

0 comments on commit ab71d39

Please sign in to comment.