-
Notifications
You must be signed in to change notification settings - Fork 38
/
alarms.ts
85 lines (77 loc) · 3.56 KB
/
alarms.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
/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
import {
Alarm, AlarmWidget, ComparisonOperator, Dashboard, MathExpression, Metric, TreatMissingData,
} from 'aws-cdk-lib/aws-cloudwatch';
import { InfraStack } from '../infra/infra-stack';
export class InfraStackMonitoring {
public readonly alarmMetrics: {
memUsed: Metric | MathExpression,
diskUsed: Metric| MathExpression,
openSearchProcessNotFound: Metric | MathExpression,
openSearchDashboardsProcessNotFound?: Metric | MathExpression,
}
public readonly alarms: Alarm[] = []
constructor(infraStack: InfraStack, dashboardsUrl: string) {
this.alarmMetrics = {
memUsed: new Metric({
metricName: 'mem_used_percent',
namespace: `${infraStack.stackName}/InfraStack`,
}),
diskUsed: new MathExpression({
expression: `SELECT AVG(disk_used_percent) FROM "${infraStack.stackName}/InfraStack" WHERE "fstype" = 'xfs'`,
}),
openSearchProcessNotFound: new MathExpression({
expression: `SELECT AVG(procstat_lookup_pid_count) FROM "${infraStack.stackName}/InfraStack" WHERE "pattern" = '-Dopensearch'`,
}),
openSearchDashboardsProcessNotFound: new MathExpression({
expression: `SELECT AVG(procstat_lookup_pid_count) FROM "${infraStack.stackName}/InfraStack" WHERE "pattern" = 'opensearch-dashboards'`,
}),
};
const alarmDashboard = new Dashboard(infraStack, 'AlarmDashboard');
this.alarms.push(new Alarm(infraStack, 'OpenSearchProcessNotFound', {
alarmDescription: 'OpenSearch Process not found',
metric: this.alarmMetrics.openSearchProcessNotFound.with({ statistic: 'avg' }),
evaluationPeriods: 3,
threshold: 1,
datapointsToAlarm: 3,
comparisonOperator: ComparisonOperator.LESS_THAN_THRESHOLD,
treatMissingData: TreatMissingData.IGNORE,
}));
if (dashboardsUrl !== 'undefined' && this.alarmMetrics.openSearchDashboardsProcessNotFound !== undefined) {
this.alarms.push(new Alarm(infraStack, 'OpenSearchDashboardsProcessNotFound', {
alarmDescription: 'OpenSearch Dashboards Process not found',
metric: this.alarmMetrics.openSearchDashboardsProcessNotFound.with({ statistic: 'avg' }),
evaluationPeriods: 3,
threshold: 1,
datapointsToAlarm: 3,
comparisonOperator: ComparisonOperator.LESS_THAN_THRESHOLD,
treatMissingData: TreatMissingData.IGNORE,
}));
}
this.alarms.push(new Alarm(infraStack, 'HighMemoryUtilization', {
alarmDescription: 'The process is using more memory than expected',
metric: this.alarmMetrics.memUsed.with({ statistic: 'avg' }),
evaluationPeriods: 5,
threshold: 65,
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
treatMissingData: TreatMissingData.IGNORE,
}));
this.alarms.push(new Alarm(infraStack, 'HighDiskUtilization', {
alarmDescription: 'High disk utilization found',
metric: this.alarmMetrics.diskUsed.with({ statistic: 'avg' }),
evaluationPeriods: 5,
threshold: 70,
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
treatMissingData: TreatMissingData.IGNORE,
}));
this.alarms
.map((alarm) => new AlarmWidget({ alarm }))
.forEach((widget) => alarmDashboard.addWidgets(widget));
}
}