-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
license_expiration.ts
151 lines (137 loc) · 5.05 KB
/
license_expiration.ts
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
/*
* 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 moment from 'moment-timezone';
import { Logger, ILegacyCustomClusterClient, UiSettingsServiceStart } from 'src/core/server';
import { i18n } from '@kbn/i18n';
import { ALERT_TYPE_LICENSE_EXPIRATION } from '../../common/constants';
import { AlertType } from '../../../alerts/server';
import { fetchLicenses } from '../lib/alerts/fetch_licenses';
import {
AlertCommonState,
AlertLicensePerClusterState,
AlertCommonExecutorOptions,
AlertCommonCluster,
AlertLicensePerClusterUiState,
} from './types';
import { executeActions, getUiMessage } from '../lib/alerts/license_expiration.lib';
import { getPreparedAlert } from '../lib/alerts/get_prepared_alert';
const EXPIRES_DAYS = [60, 30, 14, 7];
export const getLicenseExpiration = (
getUiSettingsService: () => Promise<UiSettingsServiceStart>,
monitoringCluster: ILegacyCustomClusterClient,
getLogger: (...scopes: string[]) => Logger,
ccsEnabled: boolean
): AlertType => {
const logger = getLogger(ALERT_TYPE_LICENSE_EXPIRATION);
return {
id: ALERT_TYPE_LICENSE_EXPIRATION,
name: 'Monitoring Alert - License Expiration',
actionGroups: [
{
id: 'default',
name: i18n.translate('xpack.monitoring.alerts.licenseExpiration.actionGroups.default', {
defaultMessage: 'Default',
}),
},
],
defaultActionGroupId: 'default',
producer: 'monitoring',
async executor({ services, params, state }: AlertCommonExecutorOptions): Promise<any> {
logger.debug(
`Firing alert with params: ${JSON.stringify(params)} and state: ${JSON.stringify(state)}`
);
const preparedAlert = await getPreparedAlert(
ALERT_TYPE_LICENSE_EXPIRATION,
getUiSettingsService,
monitoringCluster,
logger,
ccsEnabled,
services,
fetchLicenses
);
if (!preparedAlert) {
return state;
}
const { emailAddress, data: licenses, clusters, dateFormat } = preparedAlert;
const result: AlertCommonState = { ...state };
const defaultAlertState: AlertLicensePerClusterState = {
expiredCheckDateMS: 0,
ui: {
isFiring: false,
message: null,
severity: 0,
resolvedMS: 0,
lastCheckedMS: 0,
triggeredMS: 0,
},
};
for (const license of licenses) {
const alertState: AlertLicensePerClusterState =
(state[license.clusterUuid] as AlertLicensePerClusterState) || defaultAlertState;
const cluster = clusters.find(
(c: AlertCommonCluster) => c.clusterUuid === license.clusterUuid
);
if (!cluster) {
logger.warn(`Unable to find cluster for clusterUuid='${license.clusterUuid}'`);
continue;
}
const $expiry = moment.utc(license.expiryDateMS);
let isExpired = false;
let severity = 0;
if (license.status !== 'active') {
isExpired = true;
severity = 2001;
} else if (license.expiryDateMS) {
for (let i = EXPIRES_DAYS.length - 1; i >= 0; i--) {
if (license.type === 'trial' && i < 2) {
break;
}
const $fromNow = moment.utc().add(EXPIRES_DAYS[i], 'days');
if ($fromNow.isAfter($expiry)) {
isExpired = true;
severity = 1000 * i;
break;
}
}
}
const ui = alertState.ui;
let triggered = ui.triggeredMS;
let resolved = ui.resolvedMS;
let message = ui.message;
let expiredCheckDate = alertState.expiredCheckDateMS;
const instance = services.alertInstanceFactory(ALERT_TYPE_LICENSE_EXPIRATION);
if (isExpired) {
if (!alertState.expiredCheckDateMS) {
logger.debug(`License will expire soon, sending email`);
executeActions(instance, cluster, $expiry, dateFormat, emailAddress);
expiredCheckDate = triggered = moment().valueOf();
}
message = getUiMessage();
resolved = 0;
} else if (!isExpired && alertState.expiredCheckDateMS) {
logger.debug(`License expiration has been resolved, sending email`);
executeActions(instance, cluster, $expiry, dateFormat, emailAddress, true);
expiredCheckDate = 0;
message = getUiMessage(true);
resolved = moment().valueOf();
}
result[license.clusterUuid] = {
expiredCheckDateMS: expiredCheckDate,
ui: {
message,
expirationTime: license.expiryDateMS,
isFiring: expiredCheckDate > 0,
severity,
resolvedMS: resolved,
triggeredMS: triggered,
lastCheckedMS: moment().valueOf(),
} as AlertLicensePerClusterUiState,
} as AlertLicensePerClusterState;
}
return result;
},
};
};