-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathalerts.js
140 lines (126 loc) · 3.72 KB
/
alerts.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { schema } from '@kbn/config-schema';
import { isFunction } from 'lodash';
import {
ALERT_TYPE_LICENSE_EXPIRATION,
ALERT_TYPE_CLUSTER_STATE,
MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS,
ALERT_TYPES,
} from '../../../../../common/constants';
import { handleError } from '../../../../lib/errors';
import { fetchStatus } from '../../../../lib/alerts/fetch_status';
async function createAlerts(req, alertsClient, { selectedEmailActionId }) {
const createdAlerts = [];
// Create alerts
const ALERT_TYPES = {
[ALERT_TYPE_LICENSE_EXPIRATION]: {
schedule: { interval: '1m' },
actions: [
{
group: 'default',
id: selectedEmailActionId,
params: {
subject: '{{context.subject}}',
message: `{{context.message}}`,
to: ['{{context.to}}'],
},
},
],
},
[ALERT_TYPE_CLUSTER_STATE]: {
schedule: { interval: '1m' },
actions: [
{
group: 'default',
id: selectedEmailActionId,
params: {
subject: '{{context.subject}}',
message: `{{context.message}}`,
to: ['{{context.to}}'],
},
},
],
},
};
for (const alertTypeId of Object.keys(ALERT_TYPES)) {
const existingAlert = await alertsClient.find({
options: {
search: alertTypeId,
},
});
if (existingAlert.total === 1) {
await alertsClient.delete({ id: existingAlert.data[0].id });
}
const result = await alertsClient.create({
data: {
enabled: true,
alertTypeId,
...ALERT_TYPES[alertTypeId],
},
});
createdAlerts.push(result);
}
return createdAlerts;
}
async function saveEmailAddress(emailAddress, uiSettingsService) {
await uiSettingsService.set(MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS, emailAddress);
}
export function createKibanaAlertsRoute(server) {
server.route({
method: 'POST',
path: '/api/monitoring/v1/alerts',
config: {
validate: {
payload: schema.object({
selectedEmailActionId: schema.string(),
emailAddress: schema.string(),
}),
},
},
async handler(req, headers) {
const { emailAddress, selectedEmailActionId } = req.payload;
const alertsClient = isFunction(req.getAlertsClient) ? req.getAlertsClient() : null;
if (!alertsClient) {
return headers.response().code(404);
}
const [alerts, emailResponse] = await Promise.all([
createAlerts(req, alertsClient, { ...req.params, selectedEmailActionId }),
saveEmailAddress(emailAddress, req.getUiSettingsService()),
]);
return { alerts, emailResponse };
},
});
server.route({
method: 'POST',
path: '/api/monitoring/v1/alert_status',
config: {
validate: {
payload: schema.object({
timeRange: schema.object({
min: schema.string(),
max: schema.string(),
}),
}),
},
},
async handler(req, headers) {
const alertsClient = isFunction(req.getAlertsClient) ? req.getAlertsClient() : null;
if (!alertsClient) {
return headers.response().code(404);
}
const start = req.payload.timeRange.min;
const end = req.payload.timeRange.max;
let alerts;
try {
alerts = await fetchStatus(alertsClient, ALERT_TYPES, start, end, req.logger);
} catch (err) {
throw handleError(err, req);
}
return { alerts };
},
});
}