This commit is contained in:
csehviktor
2025-06-29 02:44:39 +02:00
commit c83e5c86e0
11 changed files with 474 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

270
Cargo.lock generated Normal file
View File

@@ -0,0 +1,270 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "agent"
version = "0.1.0"
dependencies = [
"common",
"sysinfo",
]
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "common"
version = "0.1.0"
dependencies = [
"serde",
]
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "memchr"
version = "2.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
[[package]]
name = "ntapi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4"
dependencies = [
"winapi",
]
[[package]]
name = "objc2-core-foundation"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c10c2894a6fed806ade6027bcd50662746363a9589d3ec9d9bef30a4e4bc166"
dependencies = [
"bitflags",
]
[[package]]
name = "objc2-io-kit"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71c1c64d6120e51cd86033f67176b1cb66780c2efe34dec55176f77befd93c0a"
dependencies = [
"libc",
"objc2-core-foundation",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "server"
version = "0.1.0"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "sysinfo"
version = "0.35.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c3ffa3e4ff2b324a57f7aeb3c349656c7b127c3c189520251a648102a92496e"
dependencies = [
"libc",
"memchr",
"ntapi",
"objc2-core-foundation",
"objc2-io-kit",
"windows",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[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"
[[package]]
name = "windows"
version = "0.61.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893"
dependencies = [
"windows-collections",
"windows-core",
"windows-future",
"windows-link",
"windows-numerics",
]
[[package]]
name = "windows-collections"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8"
dependencies = [
"windows-core",
]
[[package]]
name = "windows-core"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-future"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e"
dependencies = [
"windows-core",
"windows-link",
"windows-threading",
]
[[package]]
name = "windows-implement"
version = "0.60.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a"
[[package]]
name = "windows-numerics"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1"
dependencies = [
"windows-core",
"windows-link",
]
[[package]]
name = "windows-result"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-threading"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6"
dependencies = [
"windows-link",
]

3
Cargo.toml Normal file
View File

@@ -0,0 +1,3 @@
[workspace]
resolver = "3"
members = ["agent", "common", "server"]

8
agent/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "agent"
version = "0.1.0"
edition = "2024"
[dependencies]
common = { path = "../common" }
sysinfo = "0.35.2"

109
agent/src/collector.rs Normal file
View File

@@ -0,0 +1,109 @@
use common::metrics::{CPU, Disk, Memory, Metrics, Network, SystemInfo};
use sysinfo::{Disks, Networks, System};
pub struct Collector {
sys: System,
disks: Disks,
networks: Networks,
}
impl Collector {
pub fn new() -> Self {
Collector {
sys: System::new_all(),
disks: Disks::new_with_refreshed_list(),
networks: Networks::new_with_refreshed_list(),
}
}
pub fn collect_all(&mut self) -> Metrics {
let system_info = self.collect_sysinfo();
self.sys.refresh_cpu_usage();
self.sys.refresh_memory();
self.networks.refresh(true);
let cpu = self.collect_cpu();
let disk = self.collect_disk();
let memory = self.collect_memory();
let network = self.collect_network();
let metrics = Metrics {
system_info,
cpu,
disk,
memory,
network,
};
metrics
}
fn collect_sysinfo(&self) -> SystemInfo {
let system_info = SystemInfo {
uptime: System::uptime(),
host: System::host_name(),
name: System::name(),
kernel: System::kernel_version(),
os_version: System::os_version(),
};
system_info
}
fn collect_cpu(&self) -> CPU {
let cpus = self.sys.cpus();
let threads = cpus.len();
let usage = self.sys.global_cpu_usage();
CPU {
brand: String::from(cpus[0].brand()),
usage,
threads,
}
}
fn collect_disk(&self) -> Disk {
let disk = self
.disks
.iter()
.max_by_key(|disk| disk.total_space())
.unwrap();
let total = disk.total_space();
let free = disk.available_space();
Disk {
used: total - free,
free,
total
}
}
fn collect_memory(&self) -> Memory {
let memory_metrics = Memory {
used: self.sys.used_memory(),
total: self.sys.total_memory(),
swap_used: self.sys.used_swap(),
swap_total: self.sys.total_swap(),
};
memory_metrics
}
fn collect_network(&self) -> Network {
let (down, up): (u64, u64) = self.networks
.values()
.map(|data| (data.received(), data.transmitted()))
.fold((0, 0), |(acc_down, acc_up), (down, up)| {
(acc_down + down, acc_up + up)
});
Network {
down,
up,
}
}
}

19
agent/src/main.rs Normal file
View File

@@ -0,0 +1,19 @@
use collector::Collector;
use std::thread;
use std::time;
pub mod collector;
const REFRESH_INTERVAL: u64 = 3000;
fn main() {
let mut collector = Collector::new();
loop {
let metrics = collector.collect_all();
println!("Metrics: {:?}", metrics);
thread::sleep(time::Duration::from_millis(REFRESH_INTERVAL));
}
}

7
common/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "common"
version = "0.1.0"
edition = "2024"
[dependencies]
serde = { version = "1.0.219", features = ["derive"] }

1
common/src/lib.rs Normal file
View File

@@ -0,0 +1 @@
pub mod metrics;

47
common/src/metrics.rs Normal file
View File

@@ -0,0 +1,47 @@
use serde::Serialize;
#[derive(Debug, Serialize)]
pub struct Metrics {
pub system_info: SystemInfo,
pub cpu: CPU,
pub disk: Disk,
pub memory: Memory,
pub network: Network,
}
#[derive(Debug, Serialize)]
pub struct SystemInfo {
pub uptime: u64,
pub host: Option<String>,
pub name: Option<String>,
pub kernel: Option<String>,
pub os_version: Option<String>,
}
#[derive(Debug, Serialize)]
pub struct CPU {
pub brand: String,
pub usage: f32,
pub threads: usize,
}
#[derive(Debug, Serialize)]
pub struct Disk {
pub used: u64,
pub free: u64,
pub total: u64,
}
#[derive(Debug, Serialize)]
pub struct Memory {
pub used: u64,
pub total: u64,
pub swap_used: u64,
pub swap_total: u64,
}
#[derive(Debug, Serialize)]
pub struct Network {
pub down: u64,
pub up: u64,
}

6
server/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "server"
version = "0.1.0"
edition = "2024"
[dependencies]

3
server/src/main.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
println!("hello server");
}