summaryrefslogtreecommitdiffstats
path: root/src/data_collection/disks/zfs_io_counters.rs
blob: a577915a9d514355fbf40be5f701a278b23206b3 (plain)
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
use crate::data_collection::disks::IoCounters;

/// Returns zpool I/O stats. Pulls data from `sysctl kstat.zfs.{POOL}.dataset.{objset-*}`
#[cfg(target_os = "freebsd")]
pub fn zfs_io_stats() -> anyhow::Result<Vec<IoCounters>> {
    use sysctl::Sysctl;
    let zfs_ctls: Vec<_> = sysctl::Ctl::new("kstat.zfs.")?
        .into_iter()
        .filter_map(|e| {
            e.ok().and_then(|ctl| {
                let name = ctl.name();
                if let Ok(name) = name {
                    if name.contains("objset-")
                        && (name.contains("dataset_name")
                            || name.contains("nwritten")
                            || name.contains("nread"))
                    {
                        Some(ctl)
                    } else {
                        None
                    }
                } else {
                    None
                }
            })
        })
        .collect();

    use itertools::Itertools;
    let results: Vec<IoCounters> = zfs_ctls
        .iter()
        .chunks(3)
        .into_iter()
        .filter_map(|chunk| {
            let mut nread = 0;
            let mut nwrite = 0;
            let mut ds_name = String::new();
            for ctl in chunk {
                if let Ok(name) = ctl.name() {
                    if name.contains("dataset_name") {
                        ds_name = ctl.value_string().ok()?;
                    } else if name.contains("nread") {
                        if let Ok(sysctl::CtlValue::U64(val)) = ctl.value() {
                            nread = val;
                        }
                    } else if name.contains("nwritten") {
                        if let Ok(sysctl::CtlValue::U64(val)) = ctl.value() {
                            nwrite = val;
                        }
                    }
                }
            }
            Some(IoCounters::new(ds_name, nread, nwrite))
        })
        .collect();
    Ok(results)
}

/// Returns zpool I/O stats. Pulls data from `/proc/spl/kstat/zfs/*/objset-*`.
#[cfg(target_os = "linux")]
pub fn zfs_io_stats() -> anyhow::Result<Vec<IoCounters>> {
    if let Ok(zpools) = std::fs::read_dir("/proc/spl/kstat/zfs") {
        let zpools_vec: Vec<std::path::PathBuf> = zpools
            .filter_map(|e| {
                e.ok().and_then(|d| {
                    let p = d.path();
                    if p.is_dir() {
                        Some(p)
                    } else {
                        None
                    }
                })
            })
            .collect();
        let results = zpools_vec
            .iter()
            .filter_map(|zpool| {
                // go through each pool
                if let Ok(datasets) = std::fs::read_dir(zpool) {
                    let datasets_vec: Vec<std::path::PathBuf> =
                        datasets // go through dataset
                            .filter_map(|e| {
                                e.ok().and_then(|d| {
                                    let p = d.path();
                                    if p.is_file() && p.to_str()?.contains("objset-") {
                                        Some(p)
                                    } else {
                                        None
                                    }
                                })
                            })
                            .collect();
                    let io_counters: Vec<IoCounters> = datasets_vec
                        .iter()
                        .filter_map(|ds| {
                            // get io-counter from each dataset
                            if let Ok(contents) = std::fs::read_to_string(ds) {
                                let mut read = 0;
                                let mut write = 0;
                                let mut name = "";
                                contents.lines().for_each(|line| {
                                    if let Some((label, value)) = line.split_once(' ') {
                                        match label {
                                            "dataset_name" => {
                                                if let Some((_type, val)) =
                                                    value.trim_start().rsplit_once(' ')
                                                {
                                                    name = val;
                                                }
                                            }
                                            "nwritten" => {
                                                if let Some((_type, val)) =
                                                    value.trim_start().rsplit_once(' ')
                                                {
                                                    if let Ok(number) = val.parse::<u64>() {
                                                        write = number;
                                                    }
                                                }
                                            }
                                            "nread" => {
                                                if let Some((_type, val)) =
                                                    value.trim_start().rsplit_once(' ')
                                                {
                                                    if let Ok(number) = val.parse::<u64>() {
                                                        read = number;
                                                    }
                                                }
                                            }
                                            _ => {}
                                        }
                                    }
                                });

                                let counter = IoCounters::new(name.to_owned(), read, write);
                                Some(counter)
                            } else {
                                None
                            }
                        })
                        .collect();
                    Some(io_counters)
                } else {
                    None
                }
            })
            .flatten()
            .collect(); // combine io-counters
        Ok(results)
    } else {
        Err(anyhow::anyhow!("Unable to open zfs proc directory"))
    }
}