Skip to content

Commit

Permalink
Merge pull request edgehog-device-manager#343 from rgallor/fix-teleme…
Browse files Browse the repository at this point in the history
…try-storage

fix(storage): remove disks with a depth higher than /dev
  • Loading branch information
harlem88 authored Mar 20, 2024
2 parents 96e919b + 63c6f8e commit ae65dfb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ where
}
}

let disks = telemetry::storage_usage::get_storage_usage()?;
let disks = telemetry::storage_usage::get_storage_usage();
for (disk_name, storage) in disks {
device
.send_object(
Expand Down Expand Up @@ -551,7 +551,7 @@ mod tests {
)
.returning(|_: &str, _: &str, _: AstarteType| Ok(()));

let storage_usage = get_storage_usage().unwrap();
let storage_usage = get_storage_usage();
publisher
.expect_send_object()
.withf(
Expand Down Expand Up @@ -616,7 +616,7 @@ mod tests {
)
.returning(|_: &str, _: &str, _: SystemStatus| Ok(()));

let storage_usage = get_storage_usage().unwrap();
let storage_usage = get_storage_usage();
publisher
.expect_send_object()
.withf(
Expand Down Expand Up @@ -646,7 +646,7 @@ mod tests {
},
)
.await;
for (path, payload) in get_storage_usage().unwrap() {
for (path, payload) in get_storage_usage() {
DeviceManager::<_, MockSubscriber>::send_telemetry(
&publisher,
TelemetryMessage {
Expand Down
2 changes: 1 addition & 1 deletion src/telemetry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ async fn send_data(
.await;
}
"io.edgehog.devicemanager.StorageUsage" => {
let storage_usage = storage_usage::get_storage_usage()?;
let storage_usage = storage_usage::get_storage_usage();
for (path, payload) in storage_usage {
let _ = communication_channel
.send(TelemetryMessage {
Expand Down
54 changes: 36 additions & 18 deletions src/telemetry/storage_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,52 @@
* SPDX-License-Identifier: Apache-2.0
*/

use crate::error::DeviceManagerError;
use astarte_device_sdk::AstarteAggregate;
use astarte_device_sdk::{astarte_aggregate, AstarteAggregate};
use log::{error, warn};
use std::collections::HashMap;
use sysinfo::{DiskExt, System, SystemExt};

#[derive(Debug, AstarteAggregate)]
#[allow(non_snake_case)]
#[astarte_aggregate(rename_all = "camelCase")]
pub struct DiskUsage {
pub totalBytes: i64,
pub freeBytes: i64,
pub total_bytes: i64,
pub free_bytes: i64,
}

/// get structured data for `io.edgehog.devicemanager.StorageUsage` interface
/// /dev/ is excluded from the device names since it is common for all devices
pub fn get_storage_usage() -> Result<HashMap<String, DiskUsage>, DeviceManagerError> {
let mut ret: HashMap<String, DiskUsage> = HashMap::new();
pub fn get_storage_usage() -> HashMap<String, DiskUsage> {
let mut sys = System::new_all();
sys.refresh_disks();

for disk in sys.disks() {
let disk_name = disk.name().to_str().unwrap().replace("/dev/", "");
ret.insert(
disk_name.to_owned() as String,
DiskUsage {
totalBytes: disk.total_space() as i64,
freeBytes: disk.available_space() as i64,
},
);
}
Ok(ret)
sys.disks()
.iter()
.filter_map(|disk| {
let Some(name) = disk.name().to_str() else {
warn!("non-utf8 path {}, ignoring", disk.name().to_string_lossy());
return None;
};
let name = name.strip_prefix("/dev/").unwrap_or(name);
// remove disks with a higher depth
if name.contains('/') {
warn!("not simple disks device, ignoring");
return None;
}
let Ok(total_bytes) = disk.total_space().try_into() else {
error!("disk size too big, ignoring");
return None;
};
let Ok(free_bytes) = disk.available_space().try_into() else {
error!("available space too big, ignoring");
return None;
};
Some((
name.to_string(),
DiskUsage {
total_bytes,
free_bytes,
},
))
})
.collect()
}

0 comments on commit ae65dfb

Please sign in to comment.