-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::process::{Command, exit}; | ||
use std::io; | ||
|
||
pub fn get_disk() -> io::Result<String> { | ||
// Run the df command and capture its output | ||
let output = Command::new("df").output(); | ||
|
||
// Check if the command was successful | ||
match output { | ||
Ok(output) => { | ||
// Convert the output to a string | ||
let output_str = String::from_utf8_lossy(&output.stdout); | ||
|
||
// Extract the filesystem and usage information | ||
if let Some(line) = output_str.lines().nth(1) { | ||
// Split the line into columns | ||
let columns: Vec<&str> = line.split_whitespace().collect(); | ||
|
||
// Check if the columns have the expected format | ||
if columns.len() >= 5 { | ||
let size = columns[1]; | ||
let used = columns[2]; | ||
let usage_percentage = columns[4]; | ||
|
||
// Return disk usage information as a formatted string | ||
return Ok(format!( | ||
"{} of {} ({})", | ||
size, used, usage_percentage | ||
)); | ||
} | ||
} | ||
|
||
Err(io::Error::new( | ||
io::ErrorKind::NotFound, | ||
"Disk usage information not found", | ||
)) | ||
} | ||
Err(error) => { | ||
// Print the error and exit the program | ||
eprintln!("Failed to execute df command: {}", error); | ||
exit(1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,30 @@ | ||
use std::fs::read_to_string; | ||
use std::process::{Command, exit}; | ||
use std::io; | ||
|
||
pub fn get_uptime() -> io::Result<String> { | ||
// Read the content of /proc/uptime into a string | ||
let uptime_str = read_to_string("/proc/uptime")?; | ||
// Run the uptime -p command and capture its output | ||
let output = Command::new("uptime").arg("-p").output(); | ||
|
||
// Parse the uptime value from the string | ||
let uptime_secs: f64 = uptime_str.trim().parse().map_err(|e| { | ||
io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
format!("Failed to parse uptime: {}", e), | ||
) | ||
})?; | ||
// Check if the command was successful | ||
match output { | ||
Ok(output) => { | ||
// Convert the output to a string | ||
let output_str = String::from_utf8_lossy(&output.stdout); | ||
|
||
// Calculate days, hours, and minutes | ||
let days = (uptime_secs / (24.0 * 3600.0)).floor() as u64; | ||
let hours = ((uptime_secs % (24.0 * 3600.0)) / 3600.0).floor() as u64; | ||
let minutes = ((uptime_secs % 3600.0) / 60.0).floor() as u64; | ||
// Extract the remaining text after 'up' | ||
if let Some(remaining_text) = output_str.strip_prefix("up") { | ||
return Ok(remaining_text.trim().to_string()); | ||
} | ||
|
||
// Format the result as "days, hours, minutes" | ||
Ok(format!("{}, {}, {}", days, hours, minutes)) | ||
} | ||
Err(io::Error::new( | ||
io::ErrorKind::NotFound, | ||
"Invalid 'uptime -p' output format", | ||
)) | ||
} | ||
Err(error) => { | ||
// Print the error and exit the program | ||
eprintln!("Failed to execute uptime command: {}", error); | ||
exit(1); | ||
} | ||
} | ||
} |