/
teterkin
/
vm_load_testing
Обзор
Документация
Войти
/
teterkin
/
vm_load_testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/system.rs
99 строк
3 KB
alexefan136
upload files
12 ноя 2025, 18:00
12 ноя 2025, 18:00
eb766a0
Код
Авторство
О чём код?
// src/system.rs use sysinfo::{System, SystemExt, CpuExt, DiskExt}; use std::ffi::OsString; use std::os::unix::ffi::OsStringExt; #[derive(Debug, Clone)] pub struct SystemInfo { pub os_name: String, pub os_version: String, pub kernel_version: String, pub hostname: String, pub architecture: String, pub total_ram_mib: u64, pub total_swap_mib: u64, pub disks: Vec<DiskInfo>, pub cpu_model: String, pub cpu_physical_cores: usize, pub cpu_logical_cores: usize, pub cpu_base_frequency_mhz: Option<u64>, pub virtualization: Option<String>, } #[derive(Debug, Clone)] pub struct DiskInfo { pub name: String, pub type_: String, // "SSD" or "HDD" pub total_gib: u64, pub available_gib: u64, pub file_system: String, } pub fn collect_system_info() -> SystemInfo { let mut sys = System::new_all(); sys.refresh_all(); let mut cpu_model = "unknown".to_string(); if let Some(cpu) = sys.cpus().first() { cpu_model = cpu.brand().trim().to_string(); } let disks: Vec<DiskInfo> = sys.disks() .iter() .map(|disk| { let total = disk.total_space() / (1024 * 1024 * 1024); let avail = disk.available_space() / (1024 * 1024 * 1024); DiskInfo { name: disk.name().to_string_lossy().into_owned(), type_: if disk.is_removable() { "removable".to_string() } else { "SSD/HDD".to_string() }, total_gib: total, available_gib: avail, file_system: OsString::from_vec(disk.file_system().to_vec()).to_string_lossy().into_owned(), } }) .collect(); // Virtualization detection let virt = if std::path::Path::new("/sys/hypervisor/type").exists() { std::fs::read_to_string("/sys/hypervisor/type").ok().map(|s| s.trim().to_string()) } else if std::process::Command::new("systemd-detect-virt").output().is_ok() { std::process::Command::new("systemd-detect-virt") .output() .ok() .and_then(|out| String::from_utf8(out.stdout).ok()) .map(|s| s.trim().to_string()) } else { None }; SystemInfo { os_name: sys.name().unwrap_or_else(|| "Linux".to_string()), os_version: sys.os_version().unwrap_or_else(|| "unknown".to_string()), kernel_version: sys.kernel_version().unwrap_or_else(|| "unknown".to_string()), hostname: sys.host_name().unwrap_or_else(|| "unknown".to_string()), architecture: std::env::consts::ARCH.to_string(), total_ram_mib: sys.total_memory() / 1024 / 1024, total_swap_mib: sys.total_swap() / 1024 / 1024, disks, cpu_model, cpu_physical_cores: num_cpus::get_physical(), cpu_logical_cores: num_cpus::get(), cpu_base_frequency_mhz: detect_cpu_freq(), virtualization: virt, } } fn detect_cpu_freq() -> Option<u64> { if let Ok(content) = std::fs::read_to_string("/proc/cpuinfo") { for line in content.lines() { if line.starts_with("cpu MHz") { if let Some(freq) = line.split(':').nth(1) { if let Ok(f) = freq.trim().parse::<f64>() { return Some(f as u64); } } } } } None }