-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (125 loc) · 4.75 KB
/
index.js
File metadata and controls
150 lines (125 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { spawn, spawnSync } from 'child_process';
import config from '../../common/config';
import Logger from '../../common/logger';
import { Status } from '../../common/status';
let logger = new Logger('ProcessController');
export default class ProcessController {
constructor() {
this._elastalertPath = config.get('elastalertPath');
this._onExitCallbacks = [];
this._status = Status.IDLE;
/**
* @type {ChildProcess}
* @private
*/
this._process = null;
}
onExit(onExitCallback) {
this._onExitCallbacks.push(onExitCallback);
}
get status() {
return this._status;
}
/**
* Start ElastAlert if it isn't already running.
*/
start() {
// Do not do anything if ElastAlert is already running
if (this._process !== null) {
logger.warn('ElastAlert is already running!');
return;
}
// Start ElastAlert from the directory specified in the config
logger.info('Starting ElastAlert');
this._status = Status.STARTING;
// Create ElastAlert index if it doesn't exist yet
logger.info('Creating index');
var indexCreate = spawnSync('python3', ['-m', 'elastalert.create_index', '--index', config.get('writeback_index'), '--old-index', ''], {
cwd: this._elastalertPath
});
// Redirect stdin/stderr to logger
if (indexCreate.stdout && indexCreate.stdout.toString() !== '') {
logger.info(indexCreate.stdout.toString());
}
if (indexCreate.stderr && indexCreate.stderr.toString() !== '') {
logger.error(indexCreate.stderr.toString());
}
// Set listeners for index create exit
if (indexCreate.status === 0) {
logger.info(`Index create exited with code ${indexCreate.status}`);
} else {
logger.error(`Index create exited with code ${indexCreate.status}`);
logger.warn('ElastAlert will start but might not be able to save its data!');
}
let startArguments = [];
if (config.get('start') !== undefined && config.get('start') !== '') {
logger.info('Setting ElastAlert query beginning to time ' + config.get('start'));
startArguments.push('--start', config.get('start'));
}
if (config.get('end') !== undefined && config.get('end') !== '') {
logger.info('Setting ElastAlert query ending to time ' + config.get('end'));
startArguments.push('--end', config.get('end'));
}
if (config.get('debug') === true) {
logger.info('Setting ElastAlert debug mode. This will increase the logging verboseness, change all alerts to DebugAlerter, which prints alerts and suppresses their normal action, and skips writing search and alert metadata back to Elasticsearch.');
startArguments.push('--debug');
}
if (config.get('verbose') === true) {
logger.info('Setting ElastAlert verbose mode. This will increase the logging verboseness, which allows you to see information about the state of queries.');
startArguments.push('--verbose');
}
if (config.get('es_debug') === true) {
logger.info('Setting ElastAlert es_debug mode. This will enable logging for all queries made to Elasticsearch.');
startArguments.push('--es_debug');
}
logger.info('Starting elastalert with arguments ' + (startArguments.join(' ') || '[none]'));
this._process = spawn('python3', ['-m', 'elastalert.elastalert'].concat(startArguments), {
cwd: this._elastalertPath
});
logger.info(`Started Elastalert (PID: ${this._process.pid})`);
this._status = Status.READY;
// Redirect stdin/stderr to logger
this._process.stdout.on('data', (data) => {
logger.info(data.toString());
});
this._process.stderr.on('data', (data) => {
logger.error(data.toString());
});
// Set listeners for ElastAlert exit
this._process.on('exit', (code) => {
if (code === 0) {
logger.info(`ElastAlert exited with code ${code}`);
this._status = Status.IDLE;
} else {
logger.error(`ElastAlert exited with code ${code}`);
this._status = Status.ERROR;
}
this._process = null;
this._onExitCallbacks.map(function(onExitCallback) {
if (onExitCallback !== null) {
onExitCallback();
}
});
});
// Set listener for ElastAlert error
this._process.on('error', (err) => {
logger.error(`ElastAlert error: ${err.toString()}`);
this._status = Status.ERROR;
this._process = null;
});
}
/**
* Stop ElastAlert if it is running.
*/
stop() {
if (this._process !== null) {
// Stop ElastAlert
logger.info(`Stopping ElastAlert (PID: ${this._process.pid})`);
this._status = Status.CLOSING;
this._process.kill('SIGINT');
} else {
// Do not do anything if ElastAlert is not running
logger.info('ElastAlert is not running');
}
}
}