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

Commit

Permalink
Describe the scenario and implement a basic pinger.
Browse files Browse the repository at this point in the history
  • Loading branch information
pilhuhn authored and jmazzitelli committed Feb 17, 2015
1 parent 418e03a commit dab3639
Show file tree
Hide file tree
Showing 9 changed files with 470 additions and 0 deletions.
22 changes: 22 additions & 0 deletions modules/end-to-end-test/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
= End To End Tests

This module contains End-to-End test scenarios

== Scenario 1: Register a target URL and list metrics

A user on the UI will add a http-URL of a remote server to monitor.
Hawkular will "ping" the resource via Http (HEAD?) request and record ping time + also
record the size of the returned data

image::Scenario1.png[]

Steps:

* Client adds a new resource of type URL
* Client adds 2 metric names for ping time and data size (TODO implicit for type=URL ?)
* Inventory informs the "pinger" that starts working
* Pinger reports metrics to Metrics module
* Clients requests metrics data for the metrics names above
* Client defines an alert against one of the metrics of the resource


52 changes: 52 additions & 0 deletions modules/end-to-end-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.hawkular</groupId>
<artifactId>hawkular-end-to-end-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<description>A module for some End-To-End integration tests on Rest-API level</description>

<parent>
<groupId>org.hawkular</groupId>
<artifactId>hawkular-parent</artifactId>
<version>5</version>
</parent>


<dependencies>

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

<dependency>
<groupId>org.rhq.metrics</groupId>
<artifactId>rhq-metrics-api</artifactId>
<version>0.2.6</version>
</dependency>


<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy.modules.http-builder</groupId>
<artifactId>http-builder</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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.junit.Test

import static junit.framework.Assert.assertEquals

class Scenario1 extends org.hawkular.integration.test.AbstractTestBase {

def tenantId = "i-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.org")

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/resource/$hawk_id/metrics",
body: [ statusCode, statusTime])

assertEquals(200, response.status)

// 3 inform pinger is internal

// 4 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(statusCode, 500, i-2)
postMetricValue(statusCode, 404, i-1)


// 5 was simulated in step 4 as well

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

def end = System.currentTimeMillis()
def start = end - 4 *3600 * 1000 // 4h earlier
response = client.get(path: "/metrics/$tenantId/metrics/numeric/${hawk_id}.status.time/data", query:
[start: start, end: end])

// 7 define an alert

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

}

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

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

response = client.post(path: "/metrics/$tenantId/metrics/numeric/$tmp/data",
body: [[timestamp: time, value: value]])
assertEquals(200, response.status)
}
}
59 changes: 59 additions & 0 deletions modules/pinger/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.hawkular</groupId>
<artifactId>hawkular-parent</artifactId>
<version>5</version>
</parent>


<groupId>org.hawkular</groupId>
<artifactId>hawkular-pinger</artifactId>
<version>1.0-SNAPSHOT</version>


<description>Component that allows to ping remote web sites and report their status code + response time
</description>

<dependencies>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
</dependency>

</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.component.pinger;

import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import java.util.ArrayList;
import java.util.List;

/**
* A SLSB that coordinates the pinging of resources
*
* @author Heiko W. Rupp
*/
@Startup
@Singleton
public class PingManager {

@EJB
public
Pinger pinger;

String[] destinations = {
"http://jboss.org/rhq",
"http://www.redhat.com/",
"http://hawkular.github.io/"};




@Schedule(minute = "*")
public void scheduleWork() {

List<PingStatus> results = new ArrayList<>(destinations.length);

for (String url : destinations) {
PingStatus result = pinger.ping(url);
results.add(result);
}

reportResults(results);
}

private void reportResults(List<PingStatus> results) {
for (PingStatus ps : results) {
System.err.println(ps);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.component.pinger;

/**
* Outcome of the ping
*
* @author Heiko W. Rupp
*/
public class PingStatus {

int duration;
int code;
boolean timedOut = false;

public PingStatus(int code, int duration) {
this.code = code;
this.duration = duration;
}

public PingStatus(int code, int duration, boolean timedOut) {
this.code = code;
this.duration = duration;
this.timedOut = timedOut;
}

public int getCode() {
return code;
}

public int getDuration() {
return duration;
}

public boolean isTimedOut() {
return timedOut;
}

@Override
public String toString() {
return "PingStatus{" +
"code=" + code +
", duration=" + duration +
", timedOut=" + timedOut +
'}';
}
}

0 comments on commit dab3639

Please sign in to comment.