From 3e23f6af87ad7196b02009f225aaeb21085e5880 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Tue, 3 Jun 2025 09:23:33 +0200 Subject: [PATCH] enh(generic-snmp): work on the output --- experimental/src/compute/mod.rs | 2 +- experimental/src/generic/mod.rs | 83 ++++++--------------------------- experimental/src/main.rs | 9 ++-- 3 files changed, 19 insertions(+), 75 deletions(-) diff --git a/experimental/src/compute/mod.rs b/experimental/src/compute/mod.rs index f3f50ff0a..b3717da75 100644 --- a/experimental/src/compute/mod.rs +++ b/experimental/src/compute/mod.rs @@ -5,7 +5,7 @@ pub mod threshold; use self::ast::ExprResult; use self::lexer::{LexicalError, Tok}; use lalrpop_util::{lalrpop_mod, ParseError}; -use log::{debug, trace}; +use log::debug; use regex::Regex; use serde::Deserialize; use snmp::SnmpResult; diff --git a/experimental/src/generic/mod.rs b/experimental/src/generic/mod.rs index 98fde6455..ac7e56dce 100644 --- a/experimental/src/generic/mod.rs +++ b/experimental/src/generic/mod.rs @@ -6,6 +6,7 @@ pub mod error; use self::error::Result; use compute::{ast::ExprResult, threshold::Threshold, Compute, Parser}; use log::{debug, trace}; +use output::{Output, OutputFormatter}; use serde::Deserialize; use snmp::{snmp_bulk_get, snmp_bulk_walk, snmp_bulk_walk_with_labels}; use std::collections::HashMap; @@ -13,13 +14,13 @@ use std::collections::HashMap; use crate::snmp::SnmpResult; #[derive(Debug)] -struct Perfdata<'p> { - name: String, - value: f64, - min: Option, - max: Option, - warning: Option<&'p str>, - critical: Option<&'p str>, +pub struct Perfdata<'p> { + pub name: String, + pub value: f64, + pub min: Option, + pub max: Option, + pub warning: Option<&'p str>, + pub critical: Option<&'p str>, } #[derive(Debug, Copy, Clone, PartialEq)] @@ -80,55 +81,11 @@ pub struct Collect { snmp: Vec, } -#[derive(Deserialize, Debug)] -pub struct Output { - #[serde(default = "default_ok")] - ok: String, - #[serde(default = "default_detail_ok")] - detail_ok: bool, - #[serde(default = "default_warning")] - warning: String, - #[serde(default = "default_detail_warning")] - detail_warning: bool, - #[serde(default = "default_critical")] - critical: String, - #[serde(default = "default_detail_critical")] - detail_critical: bool, - #[serde(default = "default_instance_separator")] - instance_separator: String, - #[serde(default = "default_metric_separator")] - metric_separator: String, -} -fn default_ok() -> String { - "Everything is OK".to_string() -} -fn default_detail_ok() -> bool { - false -} -fn default_warning() -> String { - "WARNING: ".to_string() -} -fn default_detail_warning() -> bool { - true -} -fn default_critical() -> String { - "CRITICAL: ".to_string() -} -fn default_detail_critical() -> bool { - true -} -fn default_instance_separator() -> String { - " - ".to_string() -} -fn default_metric_separator() -> String { - ", ".to_string() -} - #[derive(Deserialize, Debug)] pub struct Command { collect: Collect, compute: Compute, - output: Output, + pub output: Output, } #[derive(Debug)] @@ -137,13 +94,6 @@ pub struct CmdResult { pub output: String, } -pub struct CommandExt { - pub warning_core: Option, - pub critical_core: Option, - pub warning_agregation: Option, - pub critical_agregation: Option, -} - fn compute_status(value: f64, warn: &Option, crit: &Option) -> Result { if let Some(c) = crit { let crit = Threshold::parse(c)?; @@ -215,13 +165,7 @@ impl Command { } } - pub fn execute( - &self, - target: &str, - version: &str, - community: &str, - //ext: &CommandExt, - ) -> Result { + pub fn execute(&self, target: &str, version: &str, community: &str) -> Result { let mut to_get = Vec::new(); let mut get_name = Vec::new(); let mut collect = Vec::new(); @@ -460,9 +404,8 @@ impl Command { trace!("collect: {:#?}", collect); println!("metrics: {:#?}", metrics); - Ok(CmdResult { - status, - output: "No result".to_string(), - }) + let output_formatter = OutputFormatter::new(status, &metrics, &self.output); + let output = output_formatter.to_string(); + Ok(CmdResult { status, output }) } } diff --git a/experimental/src/main.rs b/experimental/src/main.rs index 6543b537a..9433d80d9 100644 --- a/experimental/src/main.rs +++ b/experimental/src/main.rs @@ -12,13 +12,14 @@ extern crate snafu; mod compute; mod generic; +mod output; mod snmp; use generic::error::*; use generic::Command; use lalrpop_util::lalrpop_mod; use lexopt::Arg; -use log::{debug, trace}; +use log::trace; use snafu::ResultExt; use std::fs; @@ -83,7 +84,7 @@ fn main() -> Result<(), Error> { let value = parser.value().unwrap().into_string().unwrap(); match cmd.as_mut() { Some(ref mut cmd) => { - if (!value.is_empty()) { + if !value.is_empty() { cmd.add_warning(&wmetric, value); } else { trace!("Warning metric '{}' is empty", wmetric); @@ -99,7 +100,7 @@ fn main() -> Result<(), Error> { let value = parser.value().unwrap().into_string().unwrap(); match cmd.as_mut() { Some(ref mut cmd) => { - if (!value.is_empty()) { + if !value.is_empty() { cmd.add_critical(&cmetric, value); } else { trace!("Critical metric '{}' is empty", cmetric); @@ -134,6 +135,6 @@ fn main() -> Result<(), Error> { } }; - println!("{:?}", result); + println!("{}", result.output); std::process::exit(result.status as i32); }