fix(generic-snmp): all the cpu tests are ok

This commit is contained in:
David Boucher 2025-03-09 10:46:28 +01:00
parent 3bc29e46c3
commit 271d9e8b75
2 changed files with 82 additions and 63 deletions

View File

@ -12,6 +12,17 @@ pub enum Status {
Unknown = 3, Unknown = 3,
} }
impl Status {
fn as_str(&self) -> &str {
match *self {
Status::Ok => "OK",
Status::Warning => "WARNING",
Status::Critical => "CRITICAL",
Status::Unknown => "UNKNOWN",
}
}
}
struct Metric<'b> { struct Metric<'b> {
name: String, name: String,
value: f32, value: f32,
@ -250,80 +261,88 @@ impl Command {
ag: &Option<(&str, usize, f32)>, ag: &Option<(&str, usize, f32)>,
ext: &CommandExt, ext: &CommandExt,
) -> String { ) -> String {
let no_threshold = ext.warning_core.is_none()
&& ext.critical_core.is_none()
&& ext.warning_agregation.is_none()
&& ext.critical_agregation.is_none();
let write_details =
no_threshold || (ext.warning_core.is_some() || ext.critical_core.is_some());
let write_agregation_details =
no_threshold || (ext.warning_agregation.is_some() || ext.critical_agregation.is_some());
let mut output_text = "".to_string(); let mut output_text = "".to_string();
let mut begun = false; let mut begun = false;
if &self.leaf.output.header != "" { if &self.leaf.output.header != "" {
output_text = match status { output_text = self.leaf.output.header.replace("{status}", status.as_str());
Status::Ok => self.leaf.output.header.replace("{status}", "OK"),
Status::Warning => self.leaf.output.header.replace("{status}", "WARNING"),
Status::Critical => self.leaf.output.header.replace("{status}", "CRITICAL"),
Status::Unknown => self.leaf.output.header.replace("{status}", "UNKNOWN"),
};
begun = true;
} }
let mut idx = 0;
for line in &self.leaf.output.text { for line in &self.leaf.output.text {
if line.contains("idx") { if line.contains("idx") {
// We have to iterate on metrics if write_details {
let mut output_vec = (Vec::new(), Vec::new(), Vec::new()); // We have to iterate on metrics
for m in metrics.iter() { let mut output_vec = (Vec::new(), Vec::new(), Vec::new());
if !m.agregated { let mut idx = 0;
let text = line for m in metrics.iter() {
.replace("{idx}", idx.to_string().as_str()) if !m.agregated {
.replace("{name}", m.name.as_str()) let text = line
.replace("{value}", format!("{:.2}", m.value).as_str()); .replace("{idx}", idx.to_string().as_str())
match m.status { .replace("{name}", m.name.as_str())
Status::Ok => { .replace("{value}", format!("{:.2}", m.value).as_str())
output_vec.0.push(text); .replace("{count}", count.to_string().as_str());
match m.status {
Status::Ok => {
output_vec.0.push(text);
}
Status::Warning => {
output_vec.1.push(text);
}
Status::Critical => {
output_vec.2.push(text);
}
Status::Unknown => (),
} }
Status::Warning => { idx += 1;
output_vec.1.push(text);
}
Status::Critical => {
output_vec.2.push(text);
}
Status::Unknown => (),
} }
idx += 1;
} }
} if !output_vec.2.is_empty() {
if !output_vec.2.is_empty() { if begun {
if begun { output_text += " - ";
output_text += " - "; } else {
} else { begun = true;
begun = true; }
output_text += output_vec.2.join(" - ").as_str();
} }
output_text += output_vec.2.join(" - ").as_str(); if !output_vec.1.is_empty() {
} if begun {
if !output_vec.1.is_empty() { output_text += " - ";
if begun { } else {
output_text += " - "; begun = true;
} else { }
begun = true; output_text += output_vec.1.join(" - ").as_str();
} }
output_text += output_vec.1.join(" - ").as_str(); if !output_vec.0.is_empty() {
} if begun {
if !output_vec.0.is_empty() { output_text += " - ";
if begun { }
output_text += " - "; output_text += output_vec.0.join(" - ").as_str();
} }
output_text += output_vec.0.join(" - ").as_str();
} }
} else { } else {
match ag { if write_agregation_details {
Some(a) => { match ag {
output_text += line Some(a) => {
.replace( output_text += line
format!("{{{}}}", a.0).as_str(), .replace(
format!("{:.2}", a.2).as_str(), format!("{{{}}}", a.0).as_str(),
) format!("{:.2}", a.2).as_str(),
.as_str(); )
} .replace("{count}", count.to_string().as_str())
None => output_text += line, .as_str();
}; begun = true;
}
None => output_text += line,
};
}
} }
} }
output_text = output_text.replace("{count}", idx.to_string().as_str());
let mut perfdata = " |".to_string(); let mut perfdata = " |".to_string();
match &self.leaf.data { match &self.leaf.data {

View File

@ -52,8 +52,8 @@ gs-cpu ${tc}
Ctn Run Command And Check Result As Strings ${command} ${expected_result} Ctn Run Command And Check Result As Strings ${command} ${expected_result}
Examples: tc extra_options expected_result -- Examples: tc extra_options expected_result --
... 1 ${EMPTY} OK: 1 CPU(s) average usage is 2.00 % - CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;;;0;100 ... 1 ${EMPTY} OK: 1 CPU(s) average usage is 2.00 % - CPU '0' usage: 2.00 % | 'total_cpu_avg'=2%;;;0;100 'cpu_0'=2%;;;0;100
... 2 --warning-agregation=0 WARNING: 1 CPU(s) average usage is 2.00 % | 'total_cpu_avg'=2.00%;0:0;;0;100 'cpu'=2.00%;;;0;100 ... 2 --warning-agregation=0 WARNING: 1 CPU(s) average usage is 2.00 % | 'total_cpu_avg'=2%;0;;0;100 'cpu_0'=2%;;;0;100
... 3 --critical-agregation=0 CRITICAL: 1 CPU(s) average usage is 2.00 % | 'total_cpu_avg'=2.00%;;0:0;0;100 'cpu'=2.00%;;;0;100 ... 3 --critical-agregation=0 CRITICAL: 1 CPU(s) average usage is 2.00 % | 'total_cpu_avg'=2%;;0;0;100 'cpu_0'=2%;;;0;100
... 4 --warning-core=0 WARNING: CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;0:0;;0;100 ... 4 --warning-core=0 WARNING: CPU '0' usage: 2.00 % | 'total_cpu_avg'=2%;;;0;100 'cpu_0'=2%;0;;0;100
... 5 --critical-core=0 CRITICAL: CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;;0:0;0;100 ... 5 --critical-core=0 CRITICAL: CPU '0' usage: 2.00 % | 'total_cpu_avg'=2%;;;0;100 'cpu_0'=2%;;0;0;100