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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use std::path::PathBuf;
use derive_variants::EnumVariants;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
use typeshare::typeshare;
use crate::entities::{MongoId, I64};
use super::{
_Serror, deployment::DeploymentState, stack::StackState,
ResourceTarget, Version,
};
/// Representation of an alert in the system.
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[cfg_attr(
feature = "mongo",
derive(mongo_indexed::derive::MongoIndexed)
)]
#[cfg_attr(feature = "mongo", doc_index({ "data.type": 1 }))]
#[cfg_attr(feature = "mongo", doc_index({ "target.type": 1 }))]
#[cfg_attr(feature = "mongo", doc_index({ "target.id": 1 }))]
pub struct Alert {
/// The Mongo ID of the alert.
/// This field is de/serialized from/to JSON as
/// `{ "_id": { "$oid": "..." }, ...(rest of serialized Alert) }`
#[serde(
default,
rename = "_id",
skip_serializing_if = "String::is_empty",
with = "bson::serde_helpers::hex_string_as_object_id"
)]
pub id: MongoId,
/// Unix timestamp in milliseconds the alert was opened
#[cfg_attr(feature = "mongo", index)]
pub ts: I64,
/// Whether the alert is already resolved
#[cfg_attr(feature = "mongo", index)]
pub resolved: bool,
/// The severity of the alert
#[cfg_attr(feature = "mongo", index)]
pub level: SeverityLevel,
/// The target of the alert
pub target: ResourceTarget,
/// The data attached to the alert
pub data: AlertData,
/// The timestamp of alert resolution
pub resolved_ts: Option<I64>,
}
/// The variants of data related to the alert.
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, EnumVariants)]
#[variant_derive(
Serialize,
Deserialize,
Debug,
Clone,
Copy,
PartialEq,
Eq,
Hash
)]
#[serde(tag = "type", content = "data")]
pub enum AlertData {
/// A null alert
None {},
/// A server could not be reached.
ServerUnreachable {
/// The id of the server
id: String,
/// The name of the server
name: String,
/// The region of the server
region: Option<String>,
/// The error data
err: Option<_Serror>,
},
/// A server has high CPU usage.
ServerCpu {
/// The id of the server
id: String,
/// The name of the server
name: String,
/// The region of the server
region: Option<String>,
/// The cpu usage percentage
percentage: f64,
},
/// A server has high memory usage.
ServerMem {
/// The id of the server
id: String,
/// The name of the server
name: String,
/// The region of the server
region: Option<String>,
/// The used memory
used_gb: f64,
/// The total memory
total_gb: f64,
},
/// A server has high disk usage.
ServerDisk {
/// The id of the server
id: String,
/// The name of the server
name: String,
/// The region of the server
region: Option<String>,
/// The mount path of the disk
path: PathBuf,
/// The used portion of the disk in GB
used_gb: f64,
/// The total size of the disk in GB
total_gb: f64,
},
/// A container's state has changed unexpectedly.
ContainerStateChange {
/// The id of the deployment
id: String,
/// The name of the deployment
name: String,
/// The server id of server that the deployment is on
server_id: String,
/// The server name
server_name: String,
/// The previous container state
from: DeploymentState,
/// The current container state
to: DeploymentState,
},
/// A stack's state has changed unexpectedly.
StackStateChange {
/// The id of the stack
id: String,
/// The name of the stack
name: String,
/// The server id of server that the stack is on
server_id: String,
/// The server name
server_name: String,
/// The previous stack state
from: StackState,
/// The current stack state
to: StackState,
},
/// An AWS builder failed to terminate.
AwsBuilderTerminationFailed {
/// The id of the aws instance which failed to terminate
instance_id: String,
/// A reason for the failure
message: String,
},
/// A resource sync has pending updates
ResourceSyncPendingUpdates {
/// The id of the resource sync
id: String,
/// The name of the resource sync
name: String,
},
/// A build has failed
BuildFailed {
/// The id of the build
id: String,
/// The name of the build
name: String,
/// The version that failed to build
version: Version,
},
/// A repo has failed
RepoBuildFailed {
/// The id of the repo
id: String,
/// The name of the repo
name: String,
},
}
impl Default for AlertData {
fn default() -> Self {
AlertData::None {}
}
}
#[allow(clippy::derivable_impls)]
impl Default for AlertDataVariant {
fn default() -> Self {
AlertDataVariant::None
}
}
/// Severity level of problem.
#[typeshare]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Default,
Display,
EnumString,
)]
#[serde(rename_all = "UPPERCASE")]
#[strum(serialize_all = "UPPERCASE")]
pub enum SeverityLevel {
/// No problem.
#[default]
Ok,
/// Problem is imminent.
Warning,
/// Problem fully realized.
Critical,
}