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-proc"
version.workspace = true
edition.workspace = true
license.workspace = true
authors.workspace = true
[dependencies]
pscand-core.workspace = true
toml.workspace = true
[lib]
name = "scanner_proc"
path = "src/lib.rs"
crate-type = ["cdylib"]

View file

@ -0,0 +1,99 @@
use pscand_core::helpers::ProcessHelper;
use pscand_core::scanner::{MetricValue, Scanner};
use std::collections::HashMap;
use std::time::Duration;
struct ProcScanner;
#[unsafe(no_mangle)]
pub extern "C" fn pscand_scanner() -> Box<dyn Scanner> {
Box::new(ProcScanner)
}
impl Default for ProcScanner {
fn default() -> Self {
Self
}
}
impl Scanner for ProcScanner {
fn name(&self) -> &'static str {
"proc"
}
fn interval(&self) -> Duration {
Duration::from_secs(5)
}
fn init(&mut self, _config: &toml::Value) -> pscand_core::Result<()> {
Ok(())
}
fn collect(&self) -> pscand_core::Result<HashMap<String, MetricValue>> {
let mut metrics = HashMap::new();
if let Ok(counts) = ProcessHelper::process_count() {
if let Some(total) = counts.get("total") {
metrics.insert(
"process_total".to_string(),
MetricValue::Integer(*total as i64),
);
}
if let Some(running) = counts.get("running") {
metrics.insert(
"process_running".to_string(),
MetricValue::Integer(*running as i64),
);
}
if let Some(sleeping) = counts.get("sleeping") {
metrics.insert(
"process_sleeping".to_string(),
MetricValue::Integer(*sleeping as i64),
);
}
if let Some(zombie) = counts.get("zombie") {
metrics.insert(
"process_zombie".to_string(),
MetricValue::Integer(*zombie as i64),
);
}
}
if let Ok(zombies) = ProcessHelper::zombie_processes() {
metrics.insert(
"zombie_count".to_string(),
MetricValue::Integer(zombies.len() as i64),
);
if !zombies.is_empty() {
let mut zombie_info = Vec::new();
for z in zombies.iter().take(5) {
zombie_info.push(format!("{}({})", z.name, z.pid));
}
metrics.insert(
"zombie_processes".to_string(),
MetricValue::from_str(zombie_info.join(",")),
);
}
}
if let Ok(top_mem) = ProcessHelper::top_memory_processes(3) {
for (i, proc) in top_mem.iter().enumerate() {
metrics.insert(
format!("top_mem_{}_name", i + 1),
MetricValue::from_str(&proc.name),
);
metrics.insert(
format!("top_mem_{}_mb", i + 1),
MetricValue::Integer((proc.memory_kb / 1024) as i64),
);
}
}
Ok(metrics)
}
fn cleanup(&mut self) -> pscand_core::Result<()> {
Ok(())
}
}