Skip to content

Commit 11f25c4

Browse files
committed
Add new endpoint /metadata to return elastalert's metadata from the writeback index
1 parent 281ebbc commit 11f25c4

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ This server exposes the following REST API's:
211211
}
212212
```
213213

214+
- **GET `/metadata/:type`**
215+
216+
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).
217+
214218
- **[WIP] GET `/config`**
215219

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"express": "^4.14.0",
2828
"fs-extra": "^5.0.0",
2929
"joi": "^13.1.2",
30+
"js-yaml": "^3.12.0",
3031
"lodash": "^4.15.0",
3132
"mkdirp": "^0.5.1",
3233
"object-resolve-path": "^1.1.1",

src/common/elasticsearch_client.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import yaml from 'js-yaml';
2+
import fs from 'fs';
3+
import process from 'process';
4+
import elasticsearch from 'elasticsearch';
5+
6+
export function getConfig() {
7+
try {
8+
var appRoot = process.cwd();
9+
var config = yaml.safeLoad(fs.readFileSync(appRoot + '/config/elastalert.yaml', 'utf8'));
10+
return config;
11+
} catch (e) {
12+
console.error(e);
13+
}
14+
}
15+
16+
export function getClient() {
17+
var config = getConfig();
18+
var client = new elasticsearch.Client({
19+
hosts: [ `http://${config.es_host}:${config.es_port}`]
20+
});
21+
return client;
22+
}

src/handlers/metadata/get.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { getConfig, getClient } from '../../common/elasticsearch_client';
2+
3+
var config = getConfig();
4+
var client = getClient();
5+
6+
export default function metadataHandler(request, response) {
7+
/**
8+
* @type {ElastalertServer}
9+
*/
10+
11+
client.search({
12+
index: config.writeback_index,
13+
type: request.params.type,
14+
body: {
15+
from : request.query.from || 0,
16+
size : request.query.size || 10,
17+
query: {
18+
match_all: {}
19+
},
20+
sort: [{ '@timestamp': { order: 'desc' } }]
21+
}
22+
}).then(function(resp) {
23+
resp.hits.hits = resp.hits.hits.map(h => h._source);
24+
response.send(resp.hits);
25+
}, function(err) {
26+
response.send({
27+
error: err
28+
});
29+
});
30+
31+
}

src/routes/routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import templateDeleteHandler from '../handlers/templates/id/delete';
1414
import testPostHandler from '../handlers/test/post';
1515
import configGetHandler from '../handlers/config/get';
1616
import configPostHandler from '../handlers/config/post';
17+
import metadataHandler from '../handlers/metadata/get';
1718

1819
/**
1920
* A server route.
@@ -74,6 +75,11 @@ let routes = [
7475
path: 'download',
7576
method: ['POST'],
7677
handler: [downloadRulesHandler]
78+
},
79+
{
80+
path: 'metadata/:type',
81+
method: ['GET'],
82+
handler: [metadataHandler]
7783
}
7884
];
7985

0 commit comments

Comments
 (0)