kumo_server_common/
disk_space.rs

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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use anyhow::Context;
use human_bytes::human_bytes;
use num_format::{Locale, ToFormattedString};
use prometheus::IntGaugeVec;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{LazyLock, Mutex, Once};
use std::time::Duration;

static OVER_LIMIT: AtomicBool = AtomicBool::new(false);
static PATHS: LazyLock<Mutex<Vec<MonitoredPath>>> = LazyLock::new(Default::default);
static MONITOR: Once = Once::new();
static FREE_INODES: LazyLock<IntGaugeVec> = LazyLock::new(|| {
    prometheus::register_int_gauge_vec!(
        "disk_free_inodes",
        "number of available inodes in a monitored location",
        &["name"]
    )
    .unwrap()
});
static FREE_INODES_PCT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
    prometheus::register_int_gauge_vec!(
        "disk_free_inodes_percent",
        "percentage of available inodes in a monitored location",
        &["name"]
    )
    .unwrap()
});
static FREE_SPACE: LazyLock<IntGaugeVec> = LazyLock::new(|| {
    prometheus::register_int_gauge_vec!(
        "disk_free_bytes",
        "number of available bytes in a monitored location",
        &["name"]
    )
    .unwrap()
});
static FREE_SPACE_PCT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
    prometheus::register_int_gauge_vec!(
        "disk_free_percent",
        "percentage of available bytes in a monitored location",
        &["name"]
    )
    .unwrap()
});

#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy, Deserialize)]
#[serde(try_from = "serde_json::Value")]
pub enum MinFree {
    Percent(u8),
    Value(u64),
}

impl TryFrom<Value> for MinFree {
    type Error = String;
    fn try_from(v: Value) -> Result<Self, String> {
        match v {
            Value::String(s) => {
                match s.strip_suffix("%") {
                    Some(n) => n
                        .parse::<u8>()
                        .map_err(|err| format!("invalid MinFree percentage specifier. {err:#}"))
                        .map(|n| MinFree::Percent(n)),
                    None => s
                        .parse::<u64>()
                        .map_err(|err| format!("invalid MinFree size specifier. {err:#}"))
                        .map(|n| MinFree::Value(n)),
                }
            }
            Value::Number(n) => {
                match n.as_u64() {
                    Some(n) => Ok(MinFree::Value(n)),
                    None => Err(format!("invalid MinFree size specifier. {n} could not be converted to u64"))
                }
            }
            v => Err(format!("invalid MinFree specifier {v:?}. Value must be either a percentage string like '10%' or the size expressed as an integer"))
        }
    }
}

impl TryFrom<String> for MinFree {
    type Error = String;
    fn try_from(s: String) -> Result<Self, String> {
        Self::try_from(s.as_str())
    }
}

impl TryFrom<&str> for MinFree {
    type Error = String;
    fn try_from(s: &str) -> Result<Self, String> {
        match s.strip_suffix("%") {
            Some(n) => n
                .parse::<u8>()
                .map_err(|err| format!("invalid MinFree percentage specifier. {err:#}"))
                .map(|n| MinFree::Percent(n)),
            None => s
                .parse::<u64>()
                .map_err(|err| format!("invalid MinFree size specifier. {err:#}"))
                .map(|n| MinFree::Value(n)),
        }
    }
}

impl Default for MinFree {
    fn default() -> Self {
        Self::Percent(10)
    }
}

#[derive(Hash, PartialEq, Eq, Debug, Clone)]
pub struct MonitoredPath {
    pub name: String,
    pub path: PathBuf,
    pub min_free_space: MinFree,
    pub min_free_inodes: MinFree,
}

pub struct AvailableSpace {
    pub space_avail: u64,
    pub space_avail_percent: u8,
    pub inodes_avail: u64,
    pub inodes_avail_percent: u8,
}

impl MonitoredPath {
    pub fn register(self) {
        PATHS.lock().unwrap().push(self);

        MONITOR.call_once(|| {
            std::thread::Builder::new()
                .name("disk-space-monitor".to_string())
                .spawn(monitor_thread)
                .expect("failed to spawn disk-space-monitor thread");
        });
    }

    pub fn get_usage(&self) -> anyhow::Result<AvailableSpace> {
        let info = nix::sys::statvfs::statvfs(&self.path)
            .with_context(|| format!("statvfs({}) failed", self.path.display()))?;

        let blocks_avail = info.blocks_available() as u64;
        let blocks_total = info.blocks() as u64;

        let space_avail_percent =
            ((blocks_avail as f64 / blocks_total as f64) * 100.0).floor() as u8;
        let space_avail = blocks_avail * info.block_size();
        FREE_SPACE
            .get_metric_with_label_values(&[&self.name])
            .unwrap()
            .set(space_avail as i64);
        FREE_SPACE_PCT
            .get_metric_with_label_values(&[&self.name])
            .unwrap()
            .set(space_avail_percent as i64);

        let inodes_avail = info.files_available() as u64;
        let inodes_total = info.files();
        let inodes_avail_percent =
            ((inodes_avail as f64 / inodes_total as f64) * 100.0).floor() as u8;
        FREE_INODES
            .get_metric_with_label_values(&[&self.name])
            .unwrap()
            .set(inodes_avail as i64);
        FREE_INODES_PCT
            .get_metric_with_label_values(&[&self.name])
            .unwrap()
            .set(inodes_avail_percent as i64);

        Ok(AvailableSpace {
            space_avail,
            space_avail_percent,
            inodes_avail,
            inodes_avail_percent,
        })
    }

    pub fn check_usage(&self, avail: &AvailableSpace) -> anyhow::Result<()> {
        let mut reason = vec![];

        match self.min_free_space {
            MinFree::Percent(p) if avail.space_avail_percent < p => {
                reason.push(format!(
                    "{}% space available but minimum is {p}%",
                    avail.space_avail_percent
                ));
            }
            MinFree::Value(n) if avail.space_avail < n => {
                reason.push(format!(
                    "{} ({}) space available but minimum is {} ({})",
                    avail.space_avail.to_formatted_string(&Locale::en),
                    human_bytes(avail.space_avail as f64),
                    n.to_formatted_string(&Locale::en),
                    human_bytes(n as f64),
                ));
            }
            _ => {}
        }
        match self.min_free_inodes {
            MinFree::Percent(p) if avail.inodes_avail_percent < p => {
                reason.push(format!(
                    "{}% inodes available but minimum is {p}%",
                    avail.inodes_avail_percent
                ));
            }
            MinFree::Value(n) if avail.inodes_avail < n => {
                reason.push(format!(
                    "{} inodes available but minimum is {}",
                    avail.space_avail.to_formatted_string(&Locale::en),
                    n.to_formatted_string(&Locale::en),
                ));
            }
            _ => {}
        }

        if reason.is_empty() {
            Ok(())
        } else {
            anyhow::bail!(
                "{} path {} has issue(s): {}",
                self.name,
                self.path.display(),
                reason.join(", ")
            );
        }
    }
}

pub fn is_over_limit() -> bool {
    OVER_LIMIT.load(Ordering::SeqCst)
}

fn copy_paths() -> Vec<MonitoredPath> {
    PATHS.lock().unwrap().clone()
}

fn monitor_thread() {
    let mut bad_monitors = HashSet::new();
    loop {
        let paths = copy_paths();

        for p in paths {
            match p.get_usage() {
                Ok(avail) => match p.check_usage(&avail) {
                    Ok(()) => {
                        if bad_monitors.remove(&p) {
                            tracing::error!("{} path {} has recovered", p.name, p.path.display());
                        }
                    }
                    Err(err) => {
                        if bad_monitors.insert(p.clone()) {
                            tracing::error!("{err:#}");
                        }
                    }
                },
                Err(err) => {
                    tracing::error!("{err:#}");
                }
            }
        }

        OVER_LIMIT.store(!bad_monitors.is_empty(), Ordering::SeqCst);
        std::thread::sleep(Duration::from_secs(5));
    }
}