enh(generic-snmp): thresholds are ok

This commit is contained in:
David Boucher 2025-05-10 18:15:44 +02:00
parent 2e3c1716f0
commit b273710cb8
6 changed files with 80 additions and 36 deletions

View File

@ -24,7 +24,8 @@
"value": "Average({cpu})",
"uom": "%",
"min": 0,
"max": 100
"max": 100,
"threshold-suffix": "cpu"
}
]
}

View File

@ -15,14 +15,6 @@
},
"compute": {
"metrics": [
{
"name": "core.mem.usage.percent1",
"value": "100 * (1 - {free} / {total})",
"uom": "%",
"min": 0,
"max": 100,
"threshold-suffix": "mem"
},
{
"name": "core.mem.usage.percent",
"value": "100 * (1 - {free}/{total})",

View File

@ -22,6 +22,7 @@ pub struct Metric {
pub min: Option<f64>,
pub max_expr: Option<String>,
pub max: Option<f64>,
#[serde(rename = "threshold-suffix")]
pub threshold_suffix: Option<String>,
pub warning: Option<String>,
pub critical: Option<String>,

View File

@ -100,12 +100,12 @@ impl Threshold {
}
}
fn in_alert(&self, value: f64) -> bool {
pub fn in_alert(&self, value: f64) -> bool {
if value < self.start || value > self.end {
if self.negation {
return false;
} else {
return true;
return true;
}
}
if self.negation {
@ -234,8 +234,10 @@ mod Test {
panic!("The threshold '{}' should not be valid", expr);
}
Err(err) => {
assert_eq!(err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'");
assert_eq!(
err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'"
);
}
}
}
@ -249,8 +251,10 @@ mod Test {
panic!("The threshold '{}' should not be valid", expr);
}
Err(err) => {
assert_eq!(err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'");
assert_eq!(
err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'"
);
}
}
}
@ -264,8 +268,10 @@ mod Test {
panic!("The threshold '{}' should not be valid", expr);
}
Err(err) => {
assert_eq!(err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'");
assert_eq!(
err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'"
);
}
}
}
@ -279,8 +285,10 @@ mod Test {
panic!("The threshold '{}' should not be valid", expr);
}
Err(err) => {
assert_eq!(err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'");
assert_eq!(
err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'"
);
}
}
}
@ -327,8 +335,10 @@ mod Test {
panic!("We should not have a threshold here");
}
Err(err) => {
assert_eq!(err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'");
assert_eq!(
err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'"
);
}
}
}
@ -342,8 +352,10 @@ mod Test {
panic!("We should not have a threshold here");
}
Err(err) => {
assert_eq!(err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'");
assert_eq!(
err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'"
);
}
}
}
@ -357,8 +369,10 @@ mod Test {
panic!("We should not have a threshold here");
}
Err(err) => {
assert_eq!(err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'");
assert_eq!(
err.to_string(),
"Threshold: Threshold not of the form '[@]start:end'"
);
}
}
}

View File

@ -10,7 +10,11 @@ pub enum Error {
))]
NegativeSimpleThreshold { value: f64 },
#[snafu(display("Threshold: The start value {} must be less than the end value {}", start, end))]
#[snafu(display(
"Threshold: The start value {} must be less than the end value {}",
start,
end
))]
BadThresholdRange { start: f64, end: f64 },
#[snafu(display("Threshold: Unable to read configuration from {}", path.display()))]
@ -23,7 +27,7 @@ pub enum Error {
WriteResult { source: io::Error, path: PathBuf },
}
type Result<T, E = Error> = std::result::Result<T, E>;
pub type Result<T, E = Error> = std::result::Result<T, E>;
fn process_data() -> Result<()> {
let path = "config.toml";

View File

@ -3,6 +3,7 @@ extern crate serde_json;
pub mod error;
use self::error::Result;
use compute::{ast::ExprResult, threshold::Threshold, Compute, Parser};
use log::{debug, trace};
use serde::Deserialize;
@ -19,9 +20,10 @@ struct Perfdata<'p> {
max: Option<f64>,
warning: Option<&'p str>,
critical: Option<&'p str>,
status: Status,
}
#[derive(Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Status {
Ok = 0,
Warning = 1,
@ -97,20 +99,20 @@ pub struct CommandExt {
pub critical_agregation: Option<String>,
}
fn compute_status(value: f64, warn: &Option<String>, crit: &Option<String>) -> Status {
fn compute_status(value: f64, warn: &Option<String>, crit: &Option<String>) -> Result<Status> {
if let Some(c) = crit {
let crit = c.parse().unwrap();
if value > crit {
return Status::Critical;
let crit = Threshold::parse(c)?;
if crit.in_alert(value) {
return Ok(Status::Critical);
}
}
if let Some(w) = warn {
let warn = w.parse().unwrap();
if value > warn {
return Status::Warning;
let warn = Threshold::parse(w)?;
if warn.in_alert(value) {
return Ok(Status::Warning);
}
}
Status::Ok
Ok(Status::Ok)
}
impl Command {
@ -124,7 +126,20 @@ impl Command {
None => false,
})
{
debug!("Adding warning to metric {}", metric.name);
metric.warning = Some(value);
} else if let Some(aggregations) = self.compute.aggregations.as_mut() {
if let Some(metric) =
aggregations
.iter_mut()
.find(|metric| match &metric.threshold_suffix {
Some(suffix) => suffix == name,
None => false,
})
{
debug!("Adding warning to aggregation metric {}", metric.name);
metric.warning = Some(value);
}
}
}
@ -139,6 +154,19 @@ impl Command {
})
{
metric.critical = Some(value);
debug!("Adding critical to metric {}", metric.name);
} else if let Some(aggregations) = self.compute.aggregations.as_mut() {
if let Some(metric) =
aggregations
.iter_mut()
.find(|metric| match &metric.threshold_suffix {
Some(suffix) => suffix == name,
None => false,
})
{
debug!("Adding critical to aggregation metric {}", metric.name);
metric.critical = Some(value);
}
}
}
@ -239,6 +267,7 @@ impl Command {
max: compute_threshold(i, &max),
warning: w,
critical: c,
status: status.unwrap(),
};
trace!("New metric '{}' with value {:?}", m.name, m.value);
metrics.push(m);
@ -271,6 +300,7 @@ impl Command {
max: compute_threshold(0, &max),
warning: w,
critical: c,
status: status.unwrap(),
};
trace!("New metric '{}' with value {:?}", m.name, m.value);
metrics.push(m);
@ -346,6 +376,7 @@ impl Command {
max,
warning: w,
critical: c,
status: status.unwrap(),
};
trace!("New metric '{}' with value {:?}", m.name, m.value);
metrics.push(m);
@ -369,6 +400,7 @@ impl Command {
max,
warning: w,
critical: c,
status: status.unwrap(),
};
trace!("New metric '{}' with value {:?}", m.name, m.value);
metrics.push(m);