enh(generic-snmp): work on the output

This commit is contained in:
David Boucher 2025-06-03 09:23:33 +02:00
parent d24222caca
commit 3e23f6af87
3 changed files with 19 additions and 75 deletions

View File

@ -5,7 +5,7 @@ pub mod threshold;
use self::ast::ExprResult; use self::ast::ExprResult;
use self::lexer::{LexicalError, Tok}; use self::lexer::{LexicalError, Tok};
use lalrpop_util::{lalrpop_mod, ParseError}; use lalrpop_util::{lalrpop_mod, ParseError};
use log::{debug, trace}; use log::debug;
use regex::Regex; use regex::Regex;
use serde::Deserialize; use serde::Deserialize;
use snmp::SnmpResult; use snmp::SnmpResult;

View File

@ -6,6 +6,7 @@ pub mod error;
use self::error::Result; use self::error::Result;
use compute::{ast::ExprResult, threshold::Threshold, Compute, Parser}; use compute::{ast::ExprResult, threshold::Threshold, Compute, Parser};
use log::{debug, trace}; use log::{debug, trace};
use output::{Output, OutputFormatter};
use serde::Deserialize; use serde::Deserialize;
use snmp::{snmp_bulk_get, snmp_bulk_walk, snmp_bulk_walk_with_labels}; use snmp::{snmp_bulk_get, snmp_bulk_walk, snmp_bulk_walk_with_labels};
use std::collections::HashMap; use std::collections::HashMap;
@ -13,13 +14,13 @@ use std::collections::HashMap;
use crate::snmp::SnmpResult; use crate::snmp::SnmpResult;
#[derive(Debug)] #[derive(Debug)]
struct Perfdata<'p> { pub struct Perfdata<'p> {
name: String, pub name: String,
value: f64, pub value: f64,
min: Option<f64>, pub min: Option<f64>,
max: Option<f64>, pub max: Option<f64>,
warning: Option<&'p str>, pub warning: Option<&'p str>,
critical: Option<&'p str>, pub critical: Option<&'p str>,
} }
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
@ -80,55 +81,11 @@ pub struct Collect {
snmp: Vec<Snmp>, snmp: Vec<Snmp>,
} }
#[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)] #[derive(Deserialize, Debug)]
pub struct Command { pub struct Command {
collect: Collect, collect: Collect,
compute: Compute, compute: Compute,
output: Output, pub output: Output,
} }
#[derive(Debug)] #[derive(Debug)]
@ -137,13 +94,6 @@ pub struct CmdResult {
pub output: String, pub output: String,
} }
pub struct CommandExt {
pub warning_core: Option<String>,
pub critical_core: Option<String>,
pub warning_agregation: Option<String>,
pub critical_agregation: Option<String>,
}
fn compute_status(value: f64, warn: &Option<String>, crit: &Option<String>) -> Result<Status> { fn compute_status(value: f64, warn: &Option<String>, crit: &Option<String>) -> Result<Status> {
if let Some(c) = crit { if let Some(c) = crit {
let crit = Threshold::parse(c)?; let crit = Threshold::parse(c)?;
@ -215,13 +165,7 @@ impl Command {
} }
} }
pub fn execute( pub fn execute(&self, target: &str, version: &str, community: &str) -> Result<CmdResult> {
&self,
target: &str,
version: &str,
community: &str,
//ext: &CommandExt,
) -> Result<CmdResult> {
let mut to_get = Vec::new(); let mut to_get = Vec::new();
let mut get_name = Vec::new(); let mut get_name = Vec::new();
let mut collect = Vec::new(); let mut collect = Vec::new();
@ -460,9 +404,8 @@ impl Command {
trace!("collect: {:#?}", collect); trace!("collect: {:#?}", collect);
println!("metrics: {:#?}", metrics); println!("metrics: {:#?}", metrics);
Ok(CmdResult { let output_formatter = OutputFormatter::new(status, &metrics, &self.output);
status, let output = output_formatter.to_string();
output: "No result".to_string(), Ok(CmdResult { status, output })
})
} }
} }

View File

@ -12,13 +12,14 @@ extern crate snafu;
mod compute; mod compute;
mod generic; mod generic;
mod output;
mod snmp; mod snmp;
use generic::error::*; use generic::error::*;
use generic::Command; use generic::Command;
use lalrpop_util::lalrpop_mod; use lalrpop_util::lalrpop_mod;
use lexopt::Arg; use lexopt::Arg;
use log::{debug, trace}; use log::trace;
use snafu::ResultExt; use snafu::ResultExt;
use std::fs; use std::fs;
@ -83,7 +84,7 @@ fn main() -> Result<(), Error> {
let value = parser.value().unwrap().into_string().unwrap(); let value = parser.value().unwrap().into_string().unwrap();
match cmd.as_mut() { match cmd.as_mut() {
Some(ref mut cmd) => { Some(ref mut cmd) => {
if (!value.is_empty()) { if !value.is_empty() {
cmd.add_warning(&wmetric, value); cmd.add_warning(&wmetric, value);
} else { } else {
trace!("Warning metric '{}' is empty", wmetric); trace!("Warning metric '{}' is empty", wmetric);
@ -99,7 +100,7 @@ fn main() -> Result<(), Error> {
let value = parser.value().unwrap().into_string().unwrap(); let value = parser.value().unwrap().into_string().unwrap();
match cmd.as_mut() { match cmd.as_mut() {
Some(ref mut cmd) => { Some(ref mut cmd) => {
if (!value.is_empty()) { if !value.is_empty() {
cmd.add_critical(&cmetric, value); cmd.add_critical(&cmetric, value);
} else { } else {
trace!("Critical metric '{}' is empty", cmetric); 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); std::process::exit(result.status as i32);
} }