pscand/docs/SCANNERS.md
NotAShelf 16672b2ff1
docs: update scanner development guide
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I1968c93cf2d6c8b4f250bd7f88cc45846a6a6964
2026-02-19 01:42:42 +03:00

1.6 KiB

Creating a Custom Scanner

pscand comes with four scanners built-in, but you may easily create your own scanners for future extensibility. The process is simple.

  1. Create your own crate
  2. Implement the Scanner trait
  3. Build, and place it into a scanner directory

See below:

Creating your scanner crate

# scanners/scanner-custom/Cargo.toml
[package]
name = "scanner-custom"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
pscand-core = { workspace = true }

Implementing the Scanner trait

use std::time::Duration;
use pscand_core::scanner::{Scanner, ScannerCollectionResult, MetricValue, Result};
use pscand_macros::scanner;
use std::collections::HashMap;

pub struct CustomScanner;

impl Scanner for CustomScanner {
    fn name(&self) -> &'static str {
        "custom"
    }

    fn interval(&self) -> Duration {
        Duration::from_secs(5)
    }

    fn init(&mut self, _config: &toml::Value) -> Result<()> {
        // Initialize your scanner (e.g., open hardware interfaces)
        Ok(())
    }

    fn collect(&self) -> ScannerCollectionResult {
        let mut metrics = HashMap::new();
        metrics.insert("value".to_string(), MetricValue::from_i64(42));
        Ok(metrics)
    }

    fn cleanup(&mut self) -> Result<()> {
        // Clean up resources
        Ok(())
    }
}

#[scanner]
static SCANNER: CustomScanner = CustomScanner;

Building and installing

cargo build --release
# Install to a directory in PSCAND_SCANNER_DIRS (e.g., ~/.local/share/pscand/scanners)
install -Dm755 target/release/libscanner_custom.so \
    ~/.local/share/pscand/scanners/scanner-custom.so