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

View file

@ -0,0 +1,80 @@
#![allow(improper_ctypes)]
use pscand_core::helpers::SensorHelper;
use pscand_core::scanner::{MetricValue, Scanner};
use pscand_core::Result;
use std::collections::HashMap;
use std::time::Duration;
struct SensorScanner;
#[unsafe(no_mangle)]
pub extern "C" fn pscand_scanner() -> Box<dyn Scanner> {
Box::new(SensorScanner)
}
impl Default for SensorScanner {
fn default() -> Self {
Self
}
}
impl Scanner for SensorScanner {
fn name(&self) -> &'static str {
"sensor"
}
fn interval(&self) -> Duration {
Duration::from_secs(2)
}
fn init(&mut self, _config: &toml::Value) -> Result<()> {
Ok(())
}
fn collect(&self) -> Result<HashMap<String, MetricValue>> {
let mut metrics = HashMap::new();
if let Ok(sensors) = SensorHelper::all_sensors() {
let mut temp_count = 0;
let mut fan_count = 0;
for (hwmon, values) in sensors {
for (key, value) in values {
if key.starts_with("temp_") && key.ends_with("_celsius") {
if let Ok(v) = value.parse::<f64>() {
metrics.insert(format!("{}_{}", hwmon, key), MetricValue::from_f64(v));
temp_count += 1;
if temp_count <= 3 {
metrics.insert(
format!("temp_{}", temp_count),
MetricValue::from_f64(v),
);
}
}
}
if key.starts_with("fan_") && key.ends_with("_rpm") {
if let Ok(v) = value.parse::<f64>() {
metrics.insert(format!("{}_{}", hwmon, key), MetricValue::from_f64(v));
fan_count += 1;
if fan_count <= 2 {
metrics
.insert(format!("fan_{}", fan_count), MetricValue::from_f64(v));
}
}
}
if key.starts_with("voltage_") {
if let Ok(v) = value.parse::<f64>() {
metrics.insert(format!("{}_{}", hwmon, key), MetricValue::from_f64(v));
}
}
}
}
}
Ok(metrics)
}
fn cleanup(&mut self) -> Result<()> {
Ok(())
}
}