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::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;

View File

@ -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<f64>,
max: Option<f64>,
warning: Option<&'p str>,
critical: Option<&'p str>,
pub struct Perfdata<'p> {
pub name: String,
pub value: f64,
pub min: Option<f64>,
pub max: Option<f64>,
pub warning: Option<&'p str>,
pub critical: Option<&'p str>,
}
#[derive(Debug, Copy, Clone, PartialEq)]
@ -80,55 +81,11 @@ pub struct Collect {
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)]
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<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> {
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<CmdResult> {
pub fn execute(&self, target: &str, version: &str, community: &str) -> Result<CmdResult> {
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 })
}
}

View File

@ -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);
}