initial commit
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ib131388c1056b6708b730a35011811026a6a6964
This commit is contained in:
commit
033e253259
33 changed files with 3126 additions and 0 deletions
15
scanners/scanner-power/Cargo.toml
Normal file
15
scanners/scanner-power/Cargo.toml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
[package]
|
||||
name = "scanner-power"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
pscand-core.workspace = true
|
||||
toml.workspace = true
|
||||
|
||||
[lib]
|
||||
name = "scanner_power"
|
||||
path = "src/lib.rs"
|
||||
crate-type = ["cdylib"]
|
||||
94
scanners/scanner-power/src/lib.rs
Normal file
94
scanners/scanner-power/src/lib.rs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
#![allow(improper_ctypes)]
|
||||
use pscand_core::helpers::PowerHelper;
|
||||
use pscand_core::scanner::{MetricValue, Scanner};
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
|
||||
struct PowerScanner;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn pscand_scanner() -> Box<dyn Scanner> {
|
||||
Box::new(PowerScanner)
|
||||
}
|
||||
|
||||
impl Default for PowerScanner {
|
||||
fn default() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl Scanner for PowerScanner {
|
||||
fn name(&self) -> &'static str {
|
||||
"power"
|
||||
}
|
||||
|
||||
fn interval(&self) -> Duration {
|
||||
Duration::from_secs(2)
|
||||
}
|
||||
|
||||
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(Some(battery)) = PowerHelper::battery_info() {
|
||||
metrics.insert(
|
||||
"battery_present".to_string(),
|
||||
MetricValue::from_bool(battery.present),
|
||||
);
|
||||
metrics.insert(
|
||||
"battery_charge_percent".to_string(),
|
||||
MetricValue::Integer(battery.charge_percent as i64),
|
||||
);
|
||||
metrics.insert(
|
||||
"battery_voltage_v".to_string(),
|
||||
MetricValue::from_f64(battery.voltage),
|
||||
);
|
||||
metrics.insert(
|
||||
"battery_power_now_mw".to_string(),
|
||||
MetricValue::Integer(battery.power_now),
|
||||
);
|
||||
metrics.insert(
|
||||
"battery_status".to_string(),
|
||||
MetricValue::from_str(&battery.status),
|
||||
);
|
||||
}
|
||||
|
||||
if let Ok(supplies) = PowerHelper::power_supplies() {
|
||||
for (name, info) in supplies {
|
||||
if let Some(status) = info.get("status") {
|
||||
metrics.insert(
|
||||
format!("supply_{}_status", name),
|
||||
MetricValue::from_str(status),
|
||||
);
|
||||
}
|
||||
if let Some(online) = info.get("online") {
|
||||
metrics.insert(
|
||||
format!("supply_{}_online", name),
|
||||
MetricValue::from_bool(online == "1"),
|
||||
);
|
||||
}
|
||||
if let Some(capacity) = info.get("capacity") {
|
||||
if let Ok(cap) = capacity.parse::<u32>() {
|
||||
metrics.insert(
|
||||
format!("supply_{}_capacity", name),
|
||||
MetricValue::Integer(cap as i64),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(state) = PowerHelper::suspend_state() {
|
||||
metrics.insert("suspend_state".to_string(), MetricValue::from_str(&state));
|
||||
}
|
||||
|
||||
Ok(metrics)
|
||||
}
|
||||
|
||||
fn cleanup(&mut self) -> pscand_core::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
15
scanners/scanner-proc/Cargo.toml
Normal file
15
scanners/scanner-proc/Cargo.toml
Normal 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"]
|
||||
99
scanners/scanner-proc/src/lib.rs
Normal file
99
scanners/scanner-proc/src/lib.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
15
scanners/scanner-sensor/Cargo.toml
Normal file
15
scanners/scanner-sensor/Cargo.toml
Normal 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"]
|
||||
80
scanners/scanner-sensor/src/lib.rs
Normal file
80
scanners/scanner-sensor/src/lib.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
15
scanners/scanner-system/Cargo.toml
Normal file
15
scanners/scanner-system/Cargo.toml
Normal 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"]
|
||||
88
scanners/scanner-system/src/lib.rs
Normal file
88
scanners/scanner-system/src/lib.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue