Expand description

munin-plugin - Simple writing of plugins for munin in Rust

SPDX-License-Identifier: LGPL-3.0-only

Copyright (C) 2022 Joerg Jaspert joerg@debian.org

About

Simple way to write munin plugins.

Repository / plugin using this code

Usage

To implement a standard munin plugin, which munin runs every 5 minutes when fetching data, you load this library, create an empty struct named for your plugin and then implement MuninPlugin for your struct. You need to write out the functions config and fetch, the rest can have the magic unimplemented!(), and you call start() on your Plugin.

Example

The following implements the load plugin from munin, graphing the load average of the system, using the 5-minute value. As implemented, it expects to be run by munin every 5 minutes, usually munin will first run it with the config parameter, followed by no parameter to fetch data. If munin-node supports it and the capability dirtyconfig is set, config will also print out data.

It is a shortened version of the plugin linked above (Simple munin plugin to graph load), with things like logging dropped.

use anyhow::Result;
use munin_plugin::{config::Config, MuninPlugin};
use procfs::LoadAverage;
use std::io::{self, BufWriter, Write};

// Our plugin struct
#[derive(Debug)]
struct LoadPlugin;

// Implement the needed functions
impl MuninPlugin for LoadPlugin {
    // Write out munin config. handle is setup as a bufwriter to stdout.
    fn config<W: Write>(&self, handle: &mut BufWriter<W>) -> Result<()> {
       writeln!(handle, "graph_title Load average")?;
       writeln!(handle, "graph_args --base 1000 -l 0")?;
       writeln!(handle, "graph_vlabel load")?;
       writeln!(handle, "graph_scale no")?;
       writeln!(handle, "graph_category system")?;
       writeln!(handle, "load.label load")?;
       writeln!(handle, "load.warning 10")?;
       writeln!(handle, "load.critical 120")?;
       writeln!(handle, "graph_info The load average of the machine describes how many processes are in the run-queue (scheduled to run immediately.")?;
       writeln!(handle, "load.info Average load for the five minutes.")?;
       Ok(())
    }

    // Fetch and display data
    fn fetch(&self) {
        let load = (LoadAverage::new().unwrap().five * 100.0) as isize;
        println!("load.value {}", load);
    }

    // This plugin does not need any setup and will just work, so
    // just auto-configure it, if asked for.
    fn check_autoconf(&self) -> bool {
        true
    }

    // The other functions are not needed for a simple plugin that
    // only gathers data every 5 minutes (munin standard), but the
    // trait requires stubs to be there.
    fn run(&self) {
        unimplemented!()
    }
    fn daemonize(&self) {
        unimplemented!()
    }
    fn acquire(&self) {
        unimplemented!()
    }
}

// The actual program start point
fn main() -> Result<()> {
    // Setup our config, needs our name, rest the defaults will work.
    let config = Config::new("load".to_string());
    // Get our Plugin
    let load = LoadPlugin;
    // And let it do the work.
    load.start(config)?;
    Ok(())
}

Re-exports

pub use crate::config::Config;

Modules

Config data for a munin plugin

Traits

Defines a Munin Plugin and the needed functions