Skip to content

Commit

Permalink
Adding a simple webhook plugin
Browse files Browse the repository at this point in the history
Moving previous webhook plugin as global
  • Loading branch information
lucasponce committed Mar 13, 2016
1 parent 5bf1289 commit 63571e7
Show file tree
Hide file tree
Showing 25 changed files with 971 additions and 121 deletions.
47 changes: 47 additions & 0 deletions examples/webhook/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
= WebHook

This example shows how to use Hawkular Alerts REST API in Hawkular Alerts.

The scripts are written in groovy to make them work from maven in any platorm but those are pretty simple and can be
translated to bash or a different environment easily.

== Running the example

Build a Hawkular Alerts standalone distribution

[source,shell,subs="+attributes"]
----
cd hawkular-alerts
mvn clean install -Pstandalone
----

Start the standalone server

[source,shell,subs="+attributes"]
----
cd hawkular-alerts/hawkular-alerts-rest-tests/target/wildfly-10.0.0.Final/
bin/standalone.sh
----

Open a new shell to run the webhook example

[source,shell,subs="+attributes"]
----
cd hawkular-alerts/examples/webhook
mvn validate
----

TIP: To run the example against the Hawkular distribution, use the command `mvn validate -Dhawkular`.

== create_definitions.groovy

Create a hello world trigger to two conditions to fire an alert everytime that:

. numeric data with id "data-x" is less than 5 and
. numeric data with id "data-y" is greater than 5

Create an action definition to notify by a webhook url.

== send_data.groovy

Send random data for "data-x" and "data-y" over the REST API.
92 changes: 92 additions & 0 deletions examples/webhook/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015-2016 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>

<groupId>org.hawkular.alerts</groupId>
<artifactId>webhook</artifactId>
<version>0.9.0.Final-SNAPSHOT</version>

<name>Hawkular Alerts Examples: Webhook</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.net.sf.json-lib>2.3</version.net.sf.json-lib>
<version.org.codehaus.groovy>2.4.5</version.org.codehaus.groovy>
<version.org.codehaus.groovy.maven>1.0</version.org.codehaus.groovy.maven>
<version.org.codehaus.groovy.modules.http-builder>0.7</version.org.codehaus.groovy.modules.http-builder>
</properties>

<dependencies>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${version.org.codehaus.groovy}</version>
</dependency>

<dependency>
<groupId>org.codehaus.groovy.modules.http-builder</groupId>
<artifactId>http-builder</artifactId>
<version>${version.org.codehaus.groovy.modules.http-builder}</version>
</dependency>

<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>${version.net.sf.json-lib}</version>
<classifier>jdk15</classifier>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>${version.org.codehaus.groovy.maven}</version>
<executions>
<execution>
<id>create-definitions-in-hawkular-alerts</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>${basedir}/src/test/groovy/create_definitions.groovy</source>
</configuration>
</execution>
<execution>
<id>send-demo-data-to-hawkular-alerts</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>${basedir}/src/test/groovy/send_data.groovy</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
51 changes: 51 additions & 0 deletions examples/webhook/src/test/groovy/create_definitions.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2015-2016 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.
*/
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import net.sf.json.groovy.JsonSlurper

def url = "http://localhost:8080/hawkular/alerts/"
def tenant = "my-organization"

def definitions = new File("src/test/resources/webhook-definitions.json").text

RESTClient client = new RESTClient(url, ContentType.JSON)

client.handler.failure = { resp ->
def mapper = new JsonSlurper()
def error = mapper.parseText(resp.entity.content.text)
println "Status error: ${resp.status} \nerrorMsg: [${error.errorMsg}]"
return resp
}

if (System.getProperties().containsKey("hawkular")) {
/*
User: jdoe
Password: password
String encodedCredentials = Base64.getMimeEncoder()
.encodeToString("$testUser:$testPasword".getBytes("utf-8"))
*/
client.defaultRequestHeaders.Authorization = "Basic amRvZTpwYXNzd29yZA=="
} else {
client.defaultRequestHeaders.put("Hawkular-Tenant", tenant)
}

def resp = client.post(path: "import/all", body: definitions)

if (resp.status == 200) {
println "Imported definitions from json: \n${definitions}"
}
68 changes: 68 additions & 0 deletions examples/webhook/src/test/groovy/send_data.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2015-2016 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.
*/
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import net.sf.json.groovy.JsonSlurper

def url = "http://localhost:8080/hawkular/alerts/"
def tenant = "my-organization"

RESTClient client = new RESTClient(url, ContentType.JSON)

client.handler.failure = { resp ->
def mapper = new JsonSlurper()
def error = mapper.parseText(resp.entity.content.text)
println "Status error: ${resp.status} \nerrorMsg: [${error.errorMsg}]"
return resp
}

if (System.getProperties().containsKey("hawkular")) {
/*
User: jdoe
Password: password
String encodedCredentials = Base64.getMimeEncoder()
.encodeToString("$testUser:$testPasword".getBytes("utf-8"))
*/
client.defaultRequestHeaders.Authorization = "Basic amRvZTpwYXNzd29yZA=="
} else {
client.defaultRequestHeaders.put("Hawkular-Tenant", tenant)
}

while (true) {

def x = [:]
x.id = "data-x"
x.timestamp = System.currentTimeMillis()
x.value = new Random().nextInt(10)

def y = [:]
y.id = "data-y"
y.timestamp = System.currentTimeMillis()
y.value = new Random().nextInt(10)

def data = [x, y]

println "Sending data ${data}"

def resp = client.post(path: "data", body: data)

if (resp.status != 200) {
break
}

sleep(500)
}
45 changes: 45 additions & 0 deletions examples/webhook/src/test/resources/webhook-definitions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"triggers":[
{
"trigger":{
"id": "hello-world-trigger",
"name": "Hello World Trigger",
"description": "A mandatory Hello World Trigger",
"severity": "HIGH",
"enabled": true,
"actions":[
{
"actionPlugin": "webhook",
"actionId": "notify-to-admins"
}
]
},
"conditions":[
{
"triggerMode": "FIRING",
"type": "threshold",
"dataId": "data-x",
"operator": "LT",
"threshold": 5
},
{
"triggerMode": "FIRING",
"type": "threshold",
"dataId": "data-y",
"operator": "GT",
"threshold": 5
}
]
}
],
"actions":[
{
"actionPlugin": "webhook",
"actionId": "notify-to-admins",
"properties": {
"url": "http://localhost:8080/hawkular/actions/webhook/ping",
"method": "POST"
}
}
]
}

0 comments on commit 63571e7

Please sign in to comment.