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<W: Write>(&self, handle: &mut BufWriter<W>) -> Result<()> {
        let load = (LoadAverage::new().unwrap().five * 100.0) as isize;
        writeln!(handle, "load.value {}", load);
        Ok(())
    }

    // 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 acquire function is not needed for a simple plugin that
    // only gathers data every 5 minutes (munin standard), but the
    // trait requires a stub to be there.
    fn acquire(&mut self, config: &Config) -> Result<()> {
        unimplemented!()
    }
}

// The actual program start point
fn main() -> Result<()> {
    // Get our Plugin
    let mut load = LoadPlugin;
    // And let it do the work.
    load.simple_start(String::from("load"))?;
    Ok(())
}

Logging

This crate uses the default log crate to output log messages of level trace or debug. No other levels will be used. If you want to see them, select a log framework you like and ensure its level will display trace/debug messages. See that frameworks documentation on how to setup/include it.

If you do not want/need log output, just do nothing.

Re-exports

pub use crate::config::Config;

Modules

Config data for a munin plugin

Traits

Defines a Munin Plugin and the needed functions