Struct nagiosplugin::Runner

source ·
pub struct Runner<E> { /* private fields */ }
Expand description

The runner is a helper to run a function that returns a Result with a Resource and maps the error case to a ServiceState and a message. This is to avoid boilerplate in every plugin.

Example

use std::error::Error;

use nagiosplugin::{Metric, Resource, Runner, TriggerIfValue};

fn main() {
    Runner::new().safe_run(do_check).print_and_exit()
}

fn do_check() -> Result<Resource, Box<dyn Error>> {
   // The first metric will not issue an alarm, the second one will.
   let resource = Resource::new("foo")
        .with_description("This is a simple test plugin")
        .with_result(Metric::new("test", 15).with_thresholds(20, 50, TriggerIfValue::Greater))
        .with_result(Metric::new("alerting", 42).with_thresholds(40, 50, TriggerIfValue::Greater));

    Ok(resource)
}

Implementations§

source§

impl<E: Debug> Runner<E>

source

pub fn new() -> Self

source

pub fn on_error(self, f: impl FnOnce(E) -> (ServiceState, E) + 'static) -> Self

This will set a custom error handler. The is mostly useful to provide better plugin output.

source

pub fn safe_run( self, f: impl FnOnce() -> Result<Resource, E> ) -> RunnerResult<E>

This will run either the default on_error handler or the one specified by calling Self::on_error. It will use the given (ServiceState, message) tuple and exit with these.

Trait Implementations§

source§

impl<E: Debug> Default for Runner<E>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<E> !RefUnwindSafe for Runner<E>

§

impl<E> !Send for Runner<E>

§

impl<E> !Sync for Runner<E>

§

impl<E> Unpin for Runner<E>

§

impl<E> !UnwindSafe for Runner<E>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.