forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

alerts.rs176 lines, 6.4 KiBHistoryRawDownload
1//! Admin alerts: the kinds of trouble the forge tells its admins about, and the pure rules that
2//! decide when to. Delivery and state live in `store::admin_alerts` and forge-web's `alerts`.
3
4/// What an alert is about. Each kind has its own on/off setting.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6pub enum AlertKind {
7 /// A CI runner stopped checking in.
8 RunnerOffline,
9 /// A backup, its verification or an fsck failed, or no backup succeeded in time.
10 Backup,
11 /// The data directory is nearly full.
12 Disk,
13 /// A CI job was killed by SIGKILL or the OOM killer.
14 CiKilled,
15 /// A self-update deployed or failed, and the server restarted.
16 SelfUpdate,
17 /// The standby is failing or lagging.
18 Replication,
19}
20
21impl AlertKind {
22 pub const ALL: [AlertKind; 6] = [
23 AlertKind::RunnerOffline,
24 AlertKind::Backup,
25 AlertKind::Disk,
26 AlertKind::CiKilled,
27 AlertKind::SelfUpdate,
28 AlertKind::Replication,
29 ];
30
31 pub fn as_str(self) -> &'static str {
32 match self {
33 AlertKind::RunnerOffline => "runner_offline",
34 AlertKind::Backup => "backup",
35 AlertKind::Disk => "disk",
36 AlertKind::CiKilled => "ci_killed",
37 AlertKind::SelfUpdate => "self_update",
38 AlertKind::Replication => "replication",
39 }
40 }
41
42 pub fn parse(s: &str) -> Option<AlertKind> {
43 AlertKind::ALL.into_iter().find(|k| k.as_str() == s)
44 }
45}
46
47/// Whether a job's result looks like the process was killed: SIGKILL (`signal: 9`, `SIGKILL`,
48/// `Killed`), the shell's `128 + 9` exit code, or an out-of-memory message.
49pub fn is_kill_signature(exit_code: Option<i64>, message: Option<&str>) -> bool {
50 if matches!(exit_code, Some(137) | Some(-9)) {
51 return true;
52 }
53 let Some(message) = message else {
54 return false;
55 };
56 let text = message.to_ascii_lowercase();
57 if text.contains("sigkill")
58 || text.contains("out of memory")
59 || text.contains("oom-kill")
60 || text.contains("oom kill")
61 || text.contains("oom_kill")
62 || text.contains("oomkill")
63 || text.contains("exit code 137")
64 || text.contains("exit status 137")
65 {
66 return true;
67 }
68 // "signal: 9", "signal 9", "signal=9", "killed by signal 9". A longer number such as 90 is
69 // not a match.
70 let mut rest = text.as_str();
71 while let Some(at) = rest.find("signal") {
72 rest = &rest[at + "signal".len()..];
73 let digits: String = rest
74 .trim_start_matches([':', '=', ' '])
75 .chars()
76 .take_while(char::is_ascii_digit)
77 .collect();
78 if digits == "9" {
79 return true;
80 }
81 }
82 text.trim() == "killed"
83}
84
85/// Free space as a percentage of the total, rounded down. `None` for an empty filesystem.
86pub fn free_percent(total: u64, available: u64) -> Option<u64> {
87 (u128::from(available) * 100)
88 .checked_div(u128::from(total))
89 .map(|p| u64::try_from(p).unwrap_or(100))
90}
91
92/// Whether free space is below the configured floor: under `min_percent` of the total, or under
93/// `min_gib` GiB when that is non-zero.
94pub fn disk_is_low(total: u64, available: u64, min_percent: u64, min_gib: u64) -> bool {
95 let low_percent = free_percent(total, available).is_some_and(|p| p < min_percent);
96 let low_gib = min_gib > 0 && available < min_gib.saturating_mul(1 << 30);
97 low_percent || low_gib
98}
99
100/// Whether the newest successful backup is older than twice the schedule interval. A forge that
101/// never made one counts from `since_unix`, when the alerting began.
102pub fn backup_overdue(
103 now_unix: i64,
104 last_ok_unix: Option<i64>,
105 since_unix: i64,
106 interval_hours: u64,
107) -> bool {
108 let limit =
109 i64::try_from(interval_hours.saturating_mul(3600).saturating_mul(2)).unwrap_or(i64::MAX);
110 let basis = last_ok_unix.unwrap_or(since_unix);
111 now_unix.saturating_sub(basis) > limit
112}
113
114/// Whether a runner has been silent too long. `last_seen_unix` is `None` for one that never
115/// checked in, which is not offline: it was never online.
116pub fn runner_is_offline(now_unix: i64, last_seen_unix: Option<i64>, minutes: u64) -> bool {
117 let limit = i64::try_from(minutes.saturating_mul(60)).unwrap_or(i64::MAX);
118 last_seen_unix.is_some_and(|seen| now_unix.saturating_sub(seen) > limit)
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn kill_signatures_are_recognised_and_other_failures_are_not() {
127 // (exit code, log tail, expected kill)
128 let cases: [(Option<i64>, Option<&str>, bool); 15] = [
129 (Some(137), None, true),
130 (Some(-9), None, true),
131 (None, Some("process exited: signal: 9 (killed)"), true),
132 (None, Some("terminated by SIGKILL"), true),
133 (None, Some("fatal: out of memory"), true),
134 (None, Some("Killed"), true),
135 (None, Some("job died with signal 9"), true),
136 (None, Some("exit status 137"), true),
137 (Some(1), None, false),
138 (Some(0), Some("ok"), false),
139 (None, Some("job was terminated by a signal"), false),
140 (None, Some("signal: 90"), false),
141 (None, Some("signal 15 (terminated)"), false),
142 (None, Some("a broom closet"), false),
143 (None, None, false),
144 ];
145 for (code, log, want) in cases {
146 assert_eq!(is_kill_signature(code, log), want, "case {code:?} {log:?}");
147 }
148 }
149
150 #[test]
151 fn disk_thresholds() {
152 let gib = 1u64 << 30;
153 assert_eq!(free_percent(100 * gib, 5 * gib), Some(5));
154 assert_eq!(free_percent(0, 0), None);
155 assert!(disk_is_low(100 * gib, 5 * gib, 10, 0));
156 assert!(!disk_is_low(100 * gib, 10 * gib, 10, 0));
157 assert!(disk_is_low(1000 * gib, 50 * gib, 1, 60));
158 assert!(!disk_is_low(1000 * gib, 70 * gib, 1, 60));
159 }
160
161 #[test]
162 fn backup_overdue_after_twice_the_interval() {
163 let h = 3600;
164 assert!(!backup_overdue(47 * h, Some(0), 0, 24));
165 assert!(backup_overdue(49 * h, Some(0), 0, 24));
166 assert!(backup_overdue(49 * h, None, 0, 24));
167 assert!(!backup_overdue(49 * h, None, 10 * h, 24));
168 }
169
170 #[test]
171 fn runner_offline_uses_the_threshold() {
172 assert!(!runner_is_offline(1000, Some(500), 10));
173 assert!(runner_is_offline(1000, Some(300), 10));
174 assert!(!runner_is_offline(1000, None, 10));
175 }
176}