forked from sentinl/sentinl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
executable file
·200 lines (178 loc) · 6.57 KB
/
init.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright 2016, Lorenzo Mangani (lorenzo.mangani@gmail.com)
* Copyright 2015, Rao Chenlin (rao.chenlin@gmail.com)
*
* This file is part of Sentinl (http://github.com/sirensolutions/sentinl)
*
* 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.
*/
/*global later:false*/
import 'later/later';
import { get, once, has, forEach, includes } from 'lodash';
import url from 'url';
import path from 'path';
import getScheduler from './server/lib/scheduler';
import initIndices from './server/lib/init_indices';
import getConfiguration from './server/lib/get_configuration';
import { existsSync } from 'fs';
import Log from './server/lib/log';
import getChromePath from './server/lib/actions/report/get_chrome_path';
import installPhantomjs from './server/lib/actions/report/install_phantomjs';
import { isKibi } from './server/lib/helpers';
import sentinlRoutes from './server/routes/routes';
import watcherRoutes from './server/routes/watcher';
import alarmRoutes from './server/routes/alarm';
import userRoutes from './server/routes/user';
import kableRoutes from './server/routes/kable';
import timelionRoutes from './server/routes/timelion';
import sqlRoutes from './server/routes/sql';
const mappings = {
alarm: require('./server/mappings/alarm_index'),
watcher: require('./server/mappings/sentinl')
};
const siren = {
schema: {
watch: require('./server/lib/siren/saved_objects/watch'),
script: require('./server/lib/siren/saved_objects/script'),
user: require('./server/lib/siren/saved_objects/user'),
},
};
async function prepareIndices(server, log, config, mappings) {
try {
let resp;
resp = await initIndices.createIndex({
server,
config,
index: config.es.default_index,
mappings: mappings.watcher
});
log.debug(`create index ${config.es.default_index}: ${JSON.stringify(resp)}`);
resp = await initIndices.createIndex({
server,
config,
index: config.es.alarm_index,
mappings: mappings.alarm,
alarmIndex: true
});
log.debug(`create index ${config.es.alarm_index}: ${JSON.stringify(resp)}`);
} catch (err) {
throw new Error('init indices: ' + err.toString());
}
}
/**
* Initializes Sentinl app.
*
* @param {object} server - Kibana server.
*/
const init = once(function (server) {
const config = getConfiguration(server);
const scheduler = getScheduler(server);
const log = new Log(config.app_name, server, 'init');
if (isKibi(server)) {
const Migration1 = require('./lib/migrations/migrations_5/migration_1');
const migrations = [
Migration1
];
server.expose('getMigrations', () => migrations);
}
if (existsSync('/etc/sentinl.json')) {
server.plugins.sentinl.status.red('Setting configuration values in /etc/sentinl.json is not supported anymore, please copy ' +
'your Sentinl configuration values to config/kibi.yml or config/kibana.yml, ' +
'remove /etc/sentinl.json and restart.');
return;
}
log.info('initializing ...');
try {
if (config.settings.report.puppeteer.browser_path) {
server.expose('chrome_path', config.settings.report.puppeteer.browser_path);
} else {
server.expose('chrome_path', getChromePath());
}
log.info('Chrome bin found at: ' + server.plugins.sentinl.chrome_path);
} catch (err) {
log.error('setting puppeteer report engine: ' + err.toString());
}
if (config.settings.report.horseman.browser_path) {
server.expose('phantomjs_path', config.settings.report.horseman.browser_path);
log.info('PhantomJS bin found at: ' + server.plugins.sentinl.phantomjs_path);
} else {
installPhantomjs().then((pkg) => {
server.expose('phantomjs_path', pkg.binary);
log.info('PhantomJS bin found at: ' + server.plugins.sentinl.phantomjs_path);
}).catch((err) => {
log.error('setting horseman report engines: ' + err.toString());
});
}
// Object to hold different runtime values.
server.sentinlStore = {
schedule: {}
};
// Load Sentinl routes.
sentinlRoutes(server);
watcherRoutes(server);
alarmRoutes(server);
userRoutes(server);
kableRoutes(server);
timelionRoutes(server);
sqlRoutes(server);
// auto detect elasticsearch host, protocol and port
const esUrl = url.parse(server.config().get('elasticsearch.url'));
config.es.host = esUrl.hostname;
config.es.port = +esUrl.port;
config.es.protocol = esUrl.protocol.substring(0, esUrl.protocol.length - 1);
if (config.settings.authentication.enabled && config.es.protocol === 'https') {
config.settings.authentication.https = true;
}
config.es.default_index = config.es.default_index || server.config().get('kibana.index');
config.settings.authentication.user_index = server.config().get('kibana.index');
if (server.plugins.saved_objects_api) { // Siren: savedObjectsAPI.
forEach(siren.schema, (schema) => {
server.plugins.saved_objects_api.registerType(schema);
});
}
(async () => {
try {
await prepareIndices(server, log, config, mappings);
// Start cluster
let node;
if (config.settings.cluster.enabled) {
const GunMaster = require('gun-master');
node = new GunMaster(config.settings.cluster);
node.run().catch(function (err) {
log.error(`fail to run cluster node, ${err}`);
});
}
if (isKibi(server)) {
require('./server/lib/load_watcher_templates')(server, config)
.then(() => log.info('Loaded sample scripts'))
.catch(error => log.error(error));
}
// Schedule watchers execution.
const sched = later.parse.recur().on(25, 55).second();
const handleWatchers = later.setInterval(() => scheduler.doalert(server, node), sched);
} catch (err) {
log.error('start: ' + err.toString());
}
})();
});
export default function (server, options) {
if (server.plugins.elasticsearch.status.state === 'green') {
init(server);
} else {
server.plugins.elasticsearch.status.on('change', () => {
if (server.plugins.elasticsearch.status.state === 'green') {
init(server);
}
});
}
};