mirror of
https://github.com/centreon/centreon-plugins.git
synced 2025-07-27 15:44:21 +02:00
enh(generic): the output is almost good for this first example and the return value is also good
This commit is contained in:
parent
513ef9c8ff
commit
66abd1c93c
@ -1,9 +1,9 @@
|
|||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
|
use lib::{r_snmp_walk, SnmpResult};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
use lib::{r_snmp_walk, SnmpResult};
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone, Copy)]
|
#[derive(Deserialize, Debug, Clone, Copy)]
|
||||||
enum Operation {
|
enum Operation {
|
||||||
@ -44,11 +44,12 @@ struct Leaf {
|
|||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct Command {
|
pub struct Command {
|
||||||
leaf: Leaf,
|
leaf: Leaf,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CmdResult {
|
pub struct CmdResult {
|
||||||
|
pub status: i32,
|
||||||
|
pub output: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Command {
|
impl Command {
|
||||||
@ -58,16 +59,13 @@ impl Command {
|
|||||||
for entry in &self.leaf.entries {
|
for entry in &self.leaf.entries {
|
||||||
match entry {
|
match entry {
|
||||||
Entry::Agregation(op) => {
|
Entry::Agregation(op) => {
|
||||||
println!("Agregation: {:?}", op);
|
|
||||||
agregation = (&op.name, op.op);
|
agregation = (&op.name, op.op);
|
||||||
},
|
|
||||||
Entry::Query(query) => {
|
|
||||||
match query.query {
|
|
||||||
QueryType::Walk => {
|
|
||||||
res = Some((&query.name, r_snmp_walk(target, &query.oid)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Entry::Query(query) => match query.query {
|
||||||
|
QueryType::Walk => {
|
||||||
|
res = Some((&query.name, r_snmp_walk(target, &query.oid)));
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match res {
|
match res {
|
||||||
@ -86,18 +84,66 @@ impl Command {
|
|||||||
Operation::Average => {
|
Operation::Average => {
|
||||||
let sum: f32 = values.iter().sum();
|
let sum: f32 = values.iter().sum();
|
||||||
Some((agregation.0, sum / values.len() as f32))
|
Some((agregation.0, sum / values.len() as f32))
|
||||||
},
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
let metrics = self.build_metrics(&labels, &values, ag);
|
let metrics = self.build_metrics(&labels, &values, &ag);
|
||||||
println!("Metrics: {:?}", metrics);
|
let status = self.build_status();
|
||||||
},
|
let output = self.build_output(&ag, count, status, &metrics);
|
||||||
None => println!("No result"),
|
return CmdResult { status, output };
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return CmdResult {
|
||||||
|
status: 3,
|
||||||
|
output: "No result".to_string(),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
CmdResult {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_metrics(&self, labels: &Vec<String>, values: &Vec<f32>, ag: Option<(&str, f32)>) -> BTreeMap<String, f32> {
|
fn build_status(&self) -> i32 {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_output(
|
||||||
|
&self,
|
||||||
|
ag: &Option<(&str, f32)>,
|
||||||
|
count: usize,
|
||||||
|
status: i32,
|
||||||
|
metrics: &BTreeMap<String, f32>,
|
||||||
|
) -> String {
|
||||||
|
let mut retval = self
|
||||||
|
.leaf
|
||||||
|
.output
|
||||||
|
.replace("{count}", count.to_string().as_str())
|
||||||
|
.replace(
|
||||||
|
"{status}",
|
||||||
|
match status {
|
||||||
|
0 => "OK",
|
||||||
|
1 => "WARNING",
|
||||||
|
2 => "CRITICAL",
|
||||||
|
_ => "UNKNOWN",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
match ag {
|
||||||
|
Some(a) => {
|
||||||
|
retval = retval.replace(format!("{{{}}}", a.0).as_str(), a.1.to_string().as_str());
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
};
|
||||||
|
retval += " |";
|
||||||
|
metrics.iter().for_each(|(k, v)| {
|
||||||
|
retval += format!(" {}={}", k, v).as_str();
|
||||||
|
});
|
||||||
|
retval
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_metrics(
|
||||||
|
&self,
|
||||||
|
labels: &Vec<String>,
|
||||||
|
values: &Vec<f32>,
|
||||||
|
ag: &Option<(&str, f32)>,
|
||||||
|
) -> BTreeMap<String, f32> {
|
||||||
let mut metrics: BTreeMap<String, f32> = BTreeMap::new();
|
let mut metrics: BTreeMap<String, f32> = BTreeMap::new();
|
||||||
values.iter().enumerate().for_each(|(i, v)| {
|
values.iter().enumerate().for_each(|(i, v)| {
|
||||||
metrics.insert(labels[i].clone(), *v);
|
metrics.insert(labels[i].clone(), *v);
|
||||||
@ -105,7 +151,7 @@ impl Command {
|
|||||||
match ag {
|
match ag {
|
||||||
Some(a) => {
|
Some(a) => {
|
||||||
metrics.insert(a.0.to_string(), a.1);
|
metrics.insert(a.0.to_string(), a.1);
|
||||||
},
|
}
|
||||||
None => (),
|
None => (),
|
||||||
}
|
}
|
||||||
metrics
|
metrics
|
||||||
|
@ -52,9 +52,9 @@ fn json_to_command(file_name: &str) -> Result<Command> {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
let url = format!("{}:{}", cli.hostname, cli.port);
|
let url = format!("{}:{}", cli.hostname, cli.port);
|
||||||
// let result = r_snmp_get(&url, "1.3.6.1.2.1.1.1.0", &cli.community);
|
|
||||||
// println!("Hello, {:?}!", &result);
|
|
||||||
let cmd = json_to_command(&cli.json_conf);
|
let cmd = json_to_command(&cli.json_conf);
|
||||||
let cmd = cmd.unwrap();
|
let cmd = cmd.unwrap();
|
||||||
let result = cmd.execute(&url);
|
let result = cmd.execute(&url);
|
||||||
|
println!("{}", result.output);
|
||||||
|
std::process::exit(result.status);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user