enh(generic-snmp): output is read from the json file now

This commit is contained in:
David Boucher 2025-05-28 15:20:06 +02:00
parent 8a355dcfeb
commit ad6dc52dba
3 changed files with 86 additions and 16 deletions

View File

@ -22,7 +22,8 @@
"value": "100 * {disk.used} / {disk.size}",
"uom": "%",
"min": 0,
"max": 100
"max": 100,
"output": "{prefix}: {metrics.value}% used"
},
{
"prefix": "{disk.label}",
@ -42,6 +43,16 @@
"max": 100
}
]
},
"output": {
"ok": "Disk usage OK: {aggregations.avg.disk.usage.percent}% used",
"detail_ok": false,
"warning": "Disk usage WARNING:",
"detail_warning": true,
"critical": "Disk usage CRITICAL:",
"detail_critical": true,
"instance_separator": " - ",
"metric_separator": ", "
}
}

View File

@ -4,7 +4,7 @@
{
"name": "disk",
"oid": "1.3.6.1.2.1.25.2.3.1",
"query": "Walk", // par défaut walk
"query": "Walk",
"labels": {
".3": "label",
".4": "allocationUnits",
@ -17,30 +17,42 @@
"compute": {
"metrics": [
{
"prefix": "{disk.label}", // par défaut "{idx}"
"name": "disk.usage.percent", // le nom final est `prefix`#`name`
"prefix": "{disk.label}",
"name": "disk.usage.percent",
"value": "100 * {disk.used} / {disk.size}",
"uom": "%", // optional
"min": 0, // optional
"max": 100, // optional
"uom": "%",
"min": 0,
"max": 100,
"output": "{prefix}: {metrics.value}% used" // optional : displayed if output. Default value: "{metrics.prefix}: {value}{metrics.uom}"
},
{
"prefix": "{disk.label}",
"name": "disk.usage.bytes",
"value": "{disk.used} * {disk.allocationUnits}",
"uom": "B", // optional
"min": 0, // optional
"max_expr": "{disk.size} * {disk.allocationUnits}" // optional
"uom": "B",
"min": 0,
"max_expr": "{disk.size} * {disk.allocationUnits}"
}
],
"aggregations": [
{
"name": "avg.disk.usage.percent",
"value": "Average({metrics.disk.usage.percent})", // si agregation, pas de prefix
"uom": "%", // optional
"min": 0, // optional
"max": 100 // optional
"value": "Average({metrics.disk.usage.percent})",
"uom": "%",
"min": 0,
"max": 100
}
]
},
"output": {
"ok": "Disk usage OK: {aggregations.avg.disk.usage.percent}% used", // optional: default value: "Everything is OK"
"detail_ok": False, // optional: default value: False, if True, metrics are concatenated to the output
"warning": "Disk usage WARNING:", // optional: default value: "WARNING:"
"detail_warning": True, // optional: default value: True
"critical": "Disk usage CRITICAL:", // optional: default value: "CRITICAL:"
"detail_critical": True, // optional: default value: True
"instance_separator": " - ", // optional: default value: " - "
"metric_separator": ", ", // optional: default value: ", "
}
}

View File

@ -80,10 +80,55 @@ 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,
}
#[derive(Debug)]
@ -252,7 +297,8 @@ impl Command {
panic!("A label must be a string");
}
};
let current_status = compute_status(*item, &metric.warning, &metric.critical)?;
let current_status =
compute_status(*item, &metric.warning, &metric.critical)?;
status = worst(status, current_status);
let w = match metric.warning {
Some(ref w) => Some(w.as_str()),
@ -361,7 +407,8 @@ impl Command {
res
}
};
let current_status = compute_status(item, &metric.warning, &metric.critical)?;
let current_status =
compute_status(item, &metric.warning, &metric.critical)?;
status = worst(status, current_status);
let w = match metric.warning {
Some(ref w) => Some(w.as_str()),