| 1 |
|
| 2 |
|
| 3 |
use std::io; |
| 4 |
|
| 5 |
use rustc_hash::FxHashMap as HashMap; |
| 6 |
use serde::Deserialize; |
| 7 |
|
| 8 |
use super::{DiskHarvest, IoHarvest, keep_disk_entry}; |
| 9 |
use crate::collection::{DataCollector, deserialize_xo, disks::IoData, error::CollectionResult}; |
| 10 |
|
| 11 |
#[derive(Deserialize, Debug, Default)] |
| 12 |
#[serde(rename_all = "kebab-case")] |
| 13 |
struct StorageSystemInformation { |
| 14 |
filesystem: Vec<FileSystem>, |
| 15 |
} |
| 16 |
|
| 17 |
#[derive(Deserialize, Debug)] |
| 18 |
#[serde(rename_all = "kebab-case")] |
| 19 |
struct FileSystem { |
| 20 |
name: String, |
| 21 |
total_blocks: u64, |
| 22 |
used_blocks: u64, |
| 23 |
available_blocks: u64, |
| 24 |
mounted_on: String, |
| 25 |
} |
| 26 |
|
| 27 |
pub fn get_io_usage(collector: &DataCollector) -> CollectionResult<IoHarvest> { |
| 28 |
#[cfg_attr(not(feature = "zfs"), expect(unused_mut))] |
| 29 |
let mut io_harvest: HashMap<String, Option<IoData>> = collector |
| 30 |
.sys |
| 31 |
.disks |
| 32 |
.iter() |
| 33 |
.map(|disk| { |
| 34 |
let usage = disk.usage(); |
| 35 |
( |
| 36 |
disk.mount_point().to_string_lossy().to_string(), |
| 37 |
Some(IoData { |
| 38 |
read_bytes: usage.read_bytes, |
| 39 |
write_bytes: usage.written_bytes, |
| 40 |
}), |
| 41 |
) |
| 42 |
}) |
| 43 |
.collect(); |
| 44 |
|
| 45 |
#[cfg(feature = "zfs")] |
| 46 |
{ |
| 47 |
use crate::collection::disks::zfs_io_counters; |
| 48 |
if let Ok(zfs_io) = zfs_io_counters::zfs_io_stats() { |
| 49 |
for io in zfs_io.into_iter() { |
| 50 |
let mount_point = io.device_name().to_string_lossy(); |
| 51 |
io_harvest.insert( |
| 52 |
mount_point.to_string(), |
| 53 |
Some(IoData { |
| 54 |
read_bytes: io.read_bytes(), |
| 55 |
write_bytes: io.write_bytes(), |
| 56 |
}), |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
Ok(io_harvest) |
| 62 |
} |
| 63 |
|
| 64 |
pub fn get_disk_usage(collector: &DataCollector) -> CollectionResult<Vec<DiskHarvest>> { |
| 65 |
let disk_filter = &collector.filters.disk_filter; |
| 66 |
let mount_filter = &collector.filters.mount_filter; |
| 67 |
let vec_disks: Vec<DiskHarvest> = get_disk_info().map(|storage_system_information| { |
| 68 |
storage_system_information |
| 69 |
.filesystem |
| 70 |
.into_iter() |
| 71 |
.filter_map(|disk| { |
| 72 |
if keep_disk_entry(&disk.name, &disk.mounted_on, disk_filter, mount_filter) { |
| 73 |
Some(DiskHarvest { |
| 74 |
free_space: Some(disk.available_blocks * 1024), |
| 75 |
used_space: Some(disk.used_blocks * 1024), |
| 76 |
total_space: Some(disk.total_blocks * 1024), |
| 77 |
mount_point: disk.mounted_on, |
| 78 |
name: disk.name, |
| 79 |
}) |
| 80 |
} else { |
| 81 |
None |
| 82 |
} |
| 83 |
}) |
| 84 |
.collect() |
| 85 |
})?; |
| 86 |
|
| 87 |
Ok(vec_disks) |
| 88 |
} |
| 89 |
|
| 90 |
fn get_disk_info() -> io::Result<StorageSystemInformation> { |
| 91 |
|
| 92 |
let output = std::process::Command::new("df") |
| 93 |
.args(["--libxo", "json", "-k", "-t", "ufs,msdosfs,zfs"]) |
| 94 |
.output()?; |
| 95 |
deserialize_xo("storage-system-information", &output.stdout) |
| 96 |
} |