| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 6 | pub enum AlertKind { |
| 7 | |
| 8 | RunnerOffline, |
| 9 | |
| 10 | Backup, |
| 11 | |
| 12 | Disk, |
| 13 | |
| 14 | CiKilled, |
| 15 | |
| 16 | SelfUpdate, |
| 17 | |
| 18 | Replication, |
| 19 | } |
| 20 | |
| 21 | impl 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 | |
| 48 | |
| 49 | pub 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 | |
| 69 | |
| 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 | |
| 86 | pub 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 | |
| 93 | |
| 94 | pub 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 | |
| 101 | |
| 102 | pub 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 | |
| 115 | |
| 116 | pub 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)] |
| 122 | mod tests { |
| 123 | use super::*; |
| 124 | |
| 125 | #[test] |
| 126 | fn kill_signatures_are_recognised_and_other_failures_are_not() { |
| 127 | |
| 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 | } |