Init, monitor for waybar module

main
Mathias Rothenhaeusler 2023-10-03 17:08:01 +02:00
commit d26fdffbda
4 changed files with 422 additions and 0 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
/target

264
Cargo.lock generated 100644
View File

@ -0,0 +1,264 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "byte-unit"
version = "4.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da78b32057b8fdfc352504708feeba7216dcd65a2c9ab02978cbd288d1279b6c"
dependencies = [
"serde",
"utf8-width",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "core-foundation-sys"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
dependencies = [
"cfg-if",
]
[[package]]
name = "derive"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72e3ae26c830a573f2e231fc2475f71fce4705609097cb9523abfc4007caed0b"
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "libc"
version = "0.2.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b"
[[package]]
name = "memoffset"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
dependencies = [
"autocfg",
]
[[package]]
name = "monitor"
version = "0.1.0"
dependencies = [
"byte-unit",
"derive",
"serde",
"serde_json",
"sysinfo",
]
[[package]]
name = "ntapi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4"
dependencies = [
"winapi",
]
[[package]]
name = "once_cell"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "proc-macro2"
version = "1.0.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "ryu"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.188"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.188"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "2.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "sysinfo"
version = "0.29.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a18d114d420ada3a891e6bc8e96a2023402203296a47cdd65083377dad18ba5"
dependencies = [
"cfg-if",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8-width"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

13
Cargo.toml 100644
View File

@ -0,0 +1,13 @@
[package]
name = "monitor"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
byte-unit = "4.0.19"
derive = "1.0.0"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
sysinfo = "0.29.10"

144
src/main.rs 100644
View File

@ -0,0 +1,144 @@
use std::process::Command;
use byte_unit::Byte;
use serde::Serialize;
use sysinfo::{DiskExt, NetworkExt, System, SystemExt};
#[derive(Serialize)]
struct WaybarModule {
tooltip: String,
text: String,
alt: String,
}
fn main() {
// Please note that we use "new_all" to ensure that all list of
// components, network interfaces, disks and users are already
// filled!
let mut sys = System::new_all();
let mut tooltip = String::new();
// First we update all information of our `System` struct.
sys.refresh_all();
// We display all disks' information:
let mut disks_string = String::try_from("\n=> disks ").unwrap();
disks_string += &("-".repeat(120 - disks_string.len()) + "\n");
for disk in sys.disks() {
if !disk.mount_point().to_str().unwrap().contains("/efi") {
let used = disk.total_space() - disk.available_space();
disks_string += disk.name().to_str().unwrap();
disks_string += &format!(
"\nDisk Space: Used: {} Available: {} Total:{}\n",
Byte::from(used).get_appropriate_unit(false).format(1),
Byte::from(disk.available_space())
.get_appropriate_unit(false)
.format(1),
Byte::from(disk.total_space())
.get_appropriate_unit(false)
.format(1)
);
}
}
// Network interfaces name, data received and data transmitted:
let mut networks = String::try_from("\n=> networks ").unwrap();
networks += &("-".repeat(120 - networks.len()) + "\n");
for (interface_name, data) in sys.networks() {
if interface_name.contains("wlan") {
let inter = format!(
"{}: Received/Sent {}/{}\n",
interface_name,
Byte::from(data.received())
.get_appropriate_unit(false)
.format(1),
Byte::from(data.transmitted())
.get_appropriate_unit(false)
.format(1)
);
networks += &inter;
}
}
// Components temperature:
let mut comp = String::try_from("\n=> components ").unwrap();
comp += &("-".repeat(120 - comp.len()) + "\n");
for component in sys.components() {
comp += &format!("{:?}\n", component);
}
let mut system = String::try_from("\n=> system ").unwrap();
system += &("-".repeat(120 - system.len()) + "\n");
// RAM information:
system += &format!(
"total memory: {}\n",
Byte::from(sys.total_memory())
.get_appropriate_unit(false)
.format(1)
);
system += &format!(
"used memory: {}\n",
Byte::from(sys.used_memory())
.get_appropriate_unit(false)
.format(1)
);
// Display system information:
system += &format!("System name: {:?}\n", sys.name().unwrap());
system += &format!(
"System kernel version: {:?}\n",
sys.kernel_version().unwrap()
);
system += &format!("System OS version: {:?}\n", sys.os_version().unwrap());
system += &format!("System host name: {:?}\n", sys.host_name().unwrap());
system += &format!(
"Load Average: 1min {} 5min {} 15min {}\n",
sys.load_average().one,
sys.load_average().five,
sys.load_average().fifteen
);
let output = Command::new("iw")
.arg("dev")
.arg("wlan0") // Replace with your WLAN interface name
.arg("link")
.output()
.expect("Failed to run iw command");
// Convert the command output to a string
let output_str = String::from_utf8_lossy(&output.stdout);
// Search for the SSID in the command output
let ssid = output_str
.lines()
.find(|line| line.contains("SSID"))
.map(|line| line.trim_start_matches("SSID: ").to_string())
.unwrap_or_else(|| String::from("Not found"));
let ip = Command::new("curl")
.arg("icanhazip.com")
.output()
.expect("Failed to run curl command");
let ip_str = String::from_utf8_lossy(&ip.stdout).to_string();
system += &format!("{} IP: {}\n", ssid.trim(), ip_str);
tooltip.push_str(&system);
tooltip.push_str(&disks_string);
tooltip.push_str(&networks);
tooltip.push_str(&comp);
// Number of CPUs:
// println!("NB CPUs: {}", sys.cpus().len());
// Display processes ID, name na disk usage:
// for (pid, process) in sys.processes() {
// println!("[{}] {} {:?}", pid, process.name(), process.disk_usage());
// }
//
let data = WaybarModule {
tooltip,
alt: "System Monitor".to_string(),
text: "System Monitor".to_string(),
};
println!("{}", serde_json::to_string(&data).unwrap());
}