Skip to content

Commit

Permalink
Reuse util functions
Browse files Browse the repository at this point in the history
Add more debug logging
Update README.md
  • Loading branch information
dormant-user committed Sep 26, 2024
1 parent be4b3fb commit 49cdbbd
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 89 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ jobs:
matrix:
platform:
- release_for: Linux-x86_64
os: linux-amd64
os: linux-x64
bin: ${{ needs.release.outputs.bin-name }}
name: ${{ needs.release.outputs.pkg-name }}-Linux-x86_64.tar.gz

- release_for: Windows-x86_64
os: windows-amd64
os: windows-x64
bin: ${{ needs.release.outputs.bin-name }}.exe
name: ${{ needs.release.outputs.pkg-name }}-Windows-x86_64.zip

- release_for: macOS-x86_64
os: darwin-amd64
os: darwin-x64
bin: ${{ needs.release.outputs.bin-name }}
name: ${{ needs.release.outputs.pkg-name }}-Darwin-x86_64.tar.gz

Expand Down Expand Up @@ -155,7 +155,6 @@ jobs:
printf '*%.0s' {1..60} && printf "\n"
echo "Existing rust version: $(rustc --version)"
printf '*%.0s' {1..60} && printf "\n\n"
rustup update && printf "\n"
rustup default 1.81.0 && printf "\n"
printf '*%.0s' {1..60} && printf "\n"
echo "Updated rust version: $(rustc --version)"
Expand Down Expand Up @@ -250,7 +249,6 @@ jobs:
printf '*%.0s' {1..60} && printf "\n"
echo "Existing rust version: $(rustc --version)"
printf '*%.0s' {1..60} && printf "\n\n"
rustup update && printf "\n"
rustup default 1.81.0 && printf "\n"
printf '*%.0s' {1..60} && printf "\n"
echo "Updated rust version: $(rustc --version)"
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ async fn main() {
<details>
<summary><strong>Download OS specific Executable</strong></summary>

###### macOS
###### macOS (x86_64)
```shell
curl -o SysMonk-Darwin-x86_64.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Darwin-x86_64.tar.gz"
```

###### Linux
###### macOS (arm64)
```shell
curl -o SysMonk-Linux-x86_64.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Linux-x86_64.tar.gz"
curl -o SysMonk-Darwin-arm64.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Darwin-arm64.tar.gz"
```

###### RaspberryPi
###### Linux (x86_64)
```shell
curl -o SysMonk-RaspberryPi.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-RaspberryPi.tar.gz"
curl -o SysMonk-Linux-x86_64.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Linux-x86_64.tar.gz"
```

###### Windows
###### Windows (x86_64)
```shell
curl -o SysMonk-Windows-x86_64.zip -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Windows-x86_64.zip"
```
Expand Down
6 changes: 3 additions & 3 deletions src/legacy/cpu_brand.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs::File;
use std::io::{self, BufRead};

use crate::legacy::helper::run_command;
use crate::squire;

/// Function to get processor information.
///
Expand All @@ -13,7 +13,7 @@ use crate::legacy::helper::run_command;
///
/// A `Option` containing the processor information if successful, otherwise `None`.
fn get_processor_info_darwin(lib_path: &str) -> Result<String, &'static str> {
let result = run_command(lib_path, &["-n", "machdep.cpu.brand_string"]);
let result = squire::util::run_command(lib_path, &["-n", "machdep.cpu.brand_string"]);
if result.is_err() {
return Err("Failed to get processor info");
}
Expand Down Expand Up @@ -60,7 +60,7 @@ fn get_processor_info_linux(lib_path: &str) -> Result<String, &'static str> {
///
/// A `Option` containing the processor information if successful, otherwise `None`.
fn get_processor_info_windows(lib_path: &str) -> Result<String, &'static str> {
let result = run_command(lib_path, &["cpu", "get", "name"]);
let result = squire::util::run_command(lib_path, &["cpu", "get", "name"]);
let output = match result {
Ok(output) => output,
Err(_) => return Err("Failed to get processor info"),
Expand Down
16 changes: 8 additions & 8 deletions src/legacy/disks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde_json::Value;
use std::collections::HashMap;
use std::str;

use crate::legacy::helper::{run_command, size_converter};
use crate::squire;

/// Function to parse size string for Linux.
///
Expand All @@ -27,7 +27,7 @@ fn parse_size(size_str: &str) -> String {
("P", 2_f64.powi(50)),
]);
let multiplier = unit_multipliers.get(unit).unwrap_or(&1.0);
return size_converter((value * multiplier) as u64);
return squire::util::size_converter((value * multiplier) as u64);
}
size_str.replace("K", " KB")
.replace("M", " MB")
Expand All @@ -47,7 +47,7 @@ fn parse_size(size_str: &str) -> String {
///
/// A `bool` indicating if the disk is physical.
fn is_physical_disk(lib_path: &str, device_id: &str) -> bool {
let result = run_command(lib_path, &["info", device_id]);
let result = squire::util::run_command(lib_path, &["info", device_id]);
let output = match result {
Ok(output) => output,
Err(_) => {
Expand All @@ -73,7 +73,7 @@ fn is_physical_disk(lib_path: &str, device_id: &str) -> bool {
///
/// A `Vec` of `HashMap` containing the disk information.
fn linux_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
let result = run_command(lib_path, &["-o", "NAME,SIZE,TYPE,MODEL", "-d"]);
let result = squire::util::run_command(lib_path, &["-o", "NAME,SIZE,TYPE,MODEL", "-d"]);
let output = match result {
Ok(output) => output,
Err(_) => {
Expand Down Expand Up @@ -112,7 +112,7 @@ fn linux_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
///
/// A `Vec` of `HashMap` containing the disk information.
fn darwin_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
let result = run_command(lib_path, &["list"]);
let result = squire::util::run_command(lib_path, &["list"]);
let output = match result {
Ok(output) => output,
Err(_) => {
Expand All @@ -128,7 +128,7 @@ fn darwin_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
if !is_physical_disk(lib_path, device_id) {
continue;
}
let result = run_command(lib_path, &["info", device_id]);
let result = squire::util::run_command(lib_path, &["info", device_id]);
let disk_info_output = match result {
Ok(output) => output,
Err(_) => {
Expand Down Expand Up @@ -166,7 +166,7 @@ fn reformat_windows(data: &mut HashMap<String, Value>) -> HashMap<String, String
let size = data.get("Size").unwrap().as_f64().unwrap();
let model = data.get("Model").unwrap().as_str().unwrap().to_string();
let mut reformatted_data = HashMap::new();
reformatted_data.insert("Size".to_string(), size_converter(size as u64));
reformatted_data.insert("Size".to_string(), squire::util::size_converter(size as u64));
reformatted_data.insert("Name".to_string(), model);
reformatted_data.insert("DeviceID".to_string(), data.get("DeviceID").unwrap().as_str().unwrap().to_string());
reformatted_data
Expand All @@ -183,7 +183,7 @@ fn reformat_windows(data: &mut HashMap<String, Value>) -> HashMap<String, String
/// A `Vec` of `HashMap` containing the disk information.
fn windows_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
let ps_command = "Get-CimInstance Win32_DiskDrive | Select-Object Caption, DeviceID, Model, Partitions, Size | ConvertTo-Json";
let result = run_command(lib_path, &["-Command", ps_command]);
let result = squire::util::run_command(lib_path, &["-Command", ps_command]);
let output = match result {
Ok(output) => output,
Err(_) => {
Expand Down
58 changes: 0 additions & 58 deletions src/legacy/helper.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/legacy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
pub mod cpu_brand;
/// This module contains disk related functions.
pub mod disks;
/// Helper for legacy functions.
pub mod helper;
3 changes: 2 additions & 1 deletion src/resources/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ fn get_cpu_brand(sys: &System) -> String {
cpu_brands.insert(cpu.brand().to_string());
}
if cpu_brands.is_empty() {
log::warn!("Unable to get brand information for all {} CPUs", cpus.len());
let legacy_cpu_brand_name = legacy::cpu_brand::get_name();
return if let Some(cpu_brand) = legacy_cpu_brand_name {
log::debug!("Using legacy methods for CPU brand!!");
cpu_brand
} else {
log::error!("Unable to get brand information for all {} CPUs", cpus.len());
"Unknown".to_string()
};
}
Expand Down
1 change: 0 additions & 1 deletion src/resources/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fn get_docker_stats() -> Result<Vec<serde_json::Value>, Box<dyn std::error::Erro
}
}
Ok(_) => {
log::debug!("No running containers");
return Ok(vec![]);
}
Err(err) => {
Expand Down
12 changes: 9 additions & 3 deletions src/routes/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ pub async fn monitor(request: HttpRequest,
let sys_info_map = resources::info::get_sys_info(&disks);
let legacy_disk_info = legacy::disks::get_all_disks();

let sys_info_disks = if legacy_disk_info.is_empty() {
resources::info::get_disks(&disks)
} else {
// legacy functions have a mechanism to check for physical devices, so it takes precedence
let has_name_and_size = !legacy_disk_info.is_empty() &&
legacy_disk_info.iter().all(|disk| {
disk.contains_key("Name") && disk.contains_key("Size")
});
let sys_info_disks = if has_name_and_size {
log::debug!("Using legacy methods for disks!");
legacy_disk_info
} else {
resources::info::get_disks(&disks)
};

let sys_info_network = resources::network::get_network_info().await;
Expand Down
1 change: 0 additions & 1 deletion src/squire/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ pub fn run_command(command: &str, args: &[&str]) -> Result<String, String> {
{
Ok(output) => {
if output.status.success() {
log::debug!("Command [{}] executed successfully", &command);
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
} else {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
Expand Down
2 changes: 1 addition & 1 deletion src/templates/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub fn get_content() -> String {
<h1>SysMonk - System Monitor</h1>
<div class="center-container">
<details>
<summary><strong>System Information<strong></summary>
<summary><strong>System Information</strong></summary>
<br>
{% for key, value in sys_info_basic|items() %}
<strong>{{ key }}: </strong>{{ value }}<br>
Expand Down

0 comments on commit 49cdbbd

Please sign in to comment.