initial commit

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ib131388c1056b6708b730a35011811026a6a6964
This commit is contained in:
raf 2026-02-18 20:13:00 +03:00
commit 033e253259
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
33 changed files with 3126 additions and 0 deletions

View file

@ -0,0 +1,15 @@
[package]
name = "scanner-system"
version.workspace = true
edition.workspace = true
license.workspace = true
authors.workspace = true
[dependencies]
pscand-core.workspace = true
toml.workspace = true
[lib]
name = "scanner_system"
path = "src/lib.rs"
crate-type = ["cdylib"]

View file

@ -0,0 +1,88 @@
#![allow(improper_ctypes)]
use pscand_core::helpers::{ResourceHelper, SystemHelper};
use pscand_core::scanner::{MetricValue, Scanner};
use pscand_core::Result;
use std::collections::HashMap;
use std::time::Duration;
struct SystemScanner {
_prev_cpu: Option<HashMap<String, f64>>,
}
impl Default for SystemScanner {
fn default() -> Self {
Self { _prev_cpu: None }
}
}
#[unsafe(no_mangle)]
pub extern "C" fn pscand_scanner() -> Box<dyn Scanner> {
Box::new(SystemScanner::default())
}
impl Scanner for SystemScanner {
fn name(&self) -> &'static str {
"system"
}
fn interval(&self) -> Duration {
Duration::from_secs(1)
}
fn init(&mut self, _config: &toml::Value) -> Result<()> {
Ok(())
}
fn collect(&self) -> Result<HashMap<String, MetricValue>> {
let mut metrics = HashMap::new();
if let Ok(uptime) = SystemHelper::uptime() {
metrics.insert(
"uptime_secs".to_string(),
MetricValue::from_f64(uptime.as_secs_f64()),
);
}
if let Ok((load1, load5, load15)) = SystemHelper::load_average() {
metrics.insert("load_1m".to_string(), MetricValue::from_f64(load1));
metrics.insert("load_5m".to_string(), MetricValue::from_f64(load5));
metrics.insert("load_15m".to_string(), MetricValue::from_f64(load15));
}
if let Ok(cpu) = ResourceHelper::cpu_usage() {
if let Some(total) = cpu.get("total_usage_percent") {
metrics.insert("cpu_percent".to_string(), MetricValue::from_f64(*total));
}
}
if let Ok(mem) = ResourceHelper::memory_info() {
if let Some(total) = mem.get("MemTotal") {
metrics.insert(
"mem_total_bytes".to_string(),
MetricValue::Integer(*total as i64),
);
}
if let Some(available) = mem.get("MemAvailable") {
metrics.insert(
"mem_available_bytes".to_string(),
MetricValue::Integer(*available as i64),
);
}
if let Some(used) = mem.get("MemAvailable") {
if let Some(total) = mem.get("MemTotal") {
let used_mem = total.saturating_sub(*used);
metrics.insert(
"mem_used_bytes".to_string(),
MetricValue::Integer(used_mem as i64),
);
}
}
}
Ok(metrics)
}
fn cleanup(&mut self) -> Result<()> {
Ok(())
}
}