Skip to content

Commit 808030b

Browse files
Merge branch 'develop' into 'master'
Develop See merge request bitsensor/back-end/elastalert!35
2 parents 4b6a372 + 671cac6 commit 808030b

File tree

15 files changed

+186
-15
lines changed

15 files changed

+186
-15
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,5 @@ lib/
7474
*.pyc
7575
config/config.json
7676
package-lock.json
77+
78+
.vscode

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM alpine:latest as py-ea
2-
ARG ELASTALERT_VERSION=v0.1.38
2+
ARG ELASTALERT_VERSION=v0.1.39
33
ENV ELASTALERT_VERSION=${ELASTALERT_VERSION}
44
# URL from which to download Elastalert.
55
ARG ELASTALERT_URL=https://github.com/Yelp/elastalert/archive/$ELASTALERT_VERSION.zip

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
v ?= v0.1.38
1+
v ?= v0.1.39
22

33
all: build
44

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The most convenient way to run the ElastAlert server is by using our Docker cont
1414
To run the Docker image you will want to mount the volumes for configuration and rule files to keep them after container updates. In order to do that conveniently, please do: `git clone https://github.com/bitsensor/elastalert.git; cd elastalert`
1515

1616
```bash
17-
docker run -d -p 3030:3030 \
17+
docker run -d -p 3030:3030 -p 3333:3333 \
1818
-v `pwd`/config/elastalert.yaml:/opt/elastalert/config.yaml \
1919
-v `pwd`/config/elastalert-test.yaml:/opt/elastalert/config-test.yaml \
2020
-v `pwd`/config/config.json:/opt/elastalert-server/config/config.json \
@@ -61,6 +61,7 @@ You can use the following config options:
6161
{
6262
"appName": "elastalert-server", // The name used by the logging framework.
6363
"port": 3030, // The port to bind to
64+
"wsport": 3333, // The port to bind to for websockets
6465
"elastalertPath": "/opt/elastalert", // The path to the root ElastAlert folder. It's the folder that contains the `setup.py` script.
6566
"start": "2014-01-01T00:00:00", // Optional date to start querying from
6667
"end": "2016-01-01T00:00:00", // Optional date to stop querying at
@@ -211,7 +212,11 @@ This server exposes the following REST API's:
211212
}
212213
}
213214
```
215+
216+
- **WEBSOCKET `/test`**
214217

218+
This allows you to test a rule and receive progress over a websocket. Send a message as JSON object (stringified) with two keys: `rule` (yaml string) and `options` (JSON object). You will receive progress messages over the socket as the test runs.
219+
215220
- **GET `/metadata/:type`**
216221

217222
Returns metadata from elasticsearch related to elasalert's state. `:type` should be one of: elastalert_status, elastalert, elastalert_error, or silence. See [docs about the elastalert metadata index](https://elastalert.readthedocs.io/en/latest/elastalert_status.html).
@@ -220,6 +225,10 @@ This server exposes the following REST API's:
220225

221226
Returns field mapping from elasticsearch for a given index.
222227

228+
- **GET `/search/:index`**
229+
230+
Performs elasticsearch query on behalf of the API. JSON body to this endpoint will become body of an ES search.
231+
223232
- **[WIP] GET `/config`**
224233

225234
Gets the ElastAlert configuration from `config.yaml` in `elastalertPath` (from the config).

config/config-hisoric-data-example.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"appName": "elastalert-server",
33
"port": 3030,
4+
"wsport": 3333,
45
"elastalertPath": "/opt/elastalert",
56
"start": "2014-01-01T00:00:00",
67
"end": "2016-01-01T00:00:00",
@@ -14,5 +15,9 @@
1415
"templatesPath": {
1516
"relative": true,
1617
"path": "/rule_templates"
17-
}
18+
},
19+
"es_host": "elasticsearch",
20+
"es_port": 9200,
21+
"writeback_index": "elastalert_status"
22+
1823
}

config/config-local-elastalert-installation.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"appName": "elastalert-server",
33
"port": 3030,
4+
"wsport": 3333,
45
"elastalertPath": "/opt/elastalert",
56
"verbose": false,
67
"es_debug": false,
@@ -12,5 +13,9 @@
1213
"templatesPath": {
1314
"relative": false,
1415
"path": "/opt/elastalert/rule_templates"
15-
}
16+
},
17+
"es_host": "elasticsearch",
18+
"es_port": 9200,
19+
"writeback_index": "elastalert_status"
20+
1621
}

config/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"appName": "elastalert-server",
33
"port": 3030,
4+
"wsport": 3333,
45
"elastalertPath": "/opt/elastalert",
56
"verbose": false,
67
"es_debug": false,
@@ -13,7 +14,7 @@
1314
"relative": true,
1415
"path": "/rule_templates"
1516
},
16-
"es_host": "localhost",
17+
"es_host": "elasticsearch",
1718
"es_port": 9200,
1819
"writeback_index": "elastalert_status"
1920
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bitsensor/elastalert",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "A server that runs ElastAlert and exposes REST API's for manipulating rules and alerts.",
55
"license": "MIT",
66
"main": "index.js",
@@ -35,7 +35,8 @@
3535
"raven": "^2.6.1",
3636
"request": "^2.85.0",
3737
"request-promise-native": "^1.0.5",
38-
"tar": "^4.4.1"
38+
"tar": "^4.4.1",
39+
"ws": "^6.0.0"
3940
},
4041
"devDependencies": {
4142
"eslint": "^4.17.0",

src/common/websocket.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import WebSocket from 'ws';
2+
3+
export var wss = null;
4+
5+
export function listen(port) {
6+
wss = new WebSocket.Server({ port, path: '/test' });
7+
8+
wss.on('connection', ws => {
9+
ws.isAlive = true;
10+
ws.on('pong', () => {
11+
ws.isAlive = true;
12+
});
13+
});
14+
15+
return wss;
16+
}
17+
18+
// Keepalive in case clients lose connection during a long rule test.
19+
// If client doesn't respond in 10s this will close the socket and
20+
// therefore stop the elastalert test from continuing to run detached.
21+
setInterval(() => {
22+
wss.clients.forEach(ws => {
23+
if (ws.isAlive === false) return ws.terminate();
24+
ws.isAlive = false;
25+
ws.ping(() => {});
26+
});
27+
}, 10000);

src/controllers/process/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default class ProcessController {
99

1010
constructor() {
1111
this._elastalertPath = config.get('elastalertPath');
12+
this._onExitCallbacks = [];
1213
this._status = Status.IDLE;
1314

1415
/**
@@ -18,6 +19,10 @@ export default class ProcessController {
1819
this._process = null;
1920
}
2021

22+
onExit(onExitCallback) {
23+
this._onExitCallbacks.push(onExitCallback);
24+
}
25+
2126
get status() {
2227
return this._status;
2328
}
@@ -38,7 +43,7 @@ export default class ProcessController {
3843

3944
// Create ElastAlert index if it doesn't exist yet
4045
logger.info('Creating index');
41-
var indexCreate = spawnSync('python', ['-m', 'elastalert.create_index', '--index', 'elastalert_status', '--old-index', ''], {
46+
var indexCreate = spawnSync('python', ['-m', 'elastalert.create_index', '--index', config.get('writeback_index'), '--old-index', ''], {
4247
cwd: this._elastalertPath
4348
});
4449

@@ -112,6 +117,12 @@ export default class ProcessController {
112117
this._status = Status.ERROR;
113118
}
114119
this._process = null;
120+
121+
this._onExitCallbacks.map(function(onExitCallback) {
122+
if (onExitCallback !== null) {
123+
onExitCallback();
124+
}
125+
});
115126
});
116127

117128
// Set listener for ElastAlert error

0 commit comments

Comments
 (0)