Skip to content

Commit f089846

Browse files
committed
new collections & small enhancement on metrics
1 parent f9baac3 commit f089846

6 files changed

Lines changed: 178 additions & 55 deletions

File tree

rust-plugins/src/generic/mod.rs

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl Command {
234234
target: &str,
235235
version: &str,
236236
community: &str,
237-
check_format: bool
237+
check_format: bool,
238238
) -> Vec<SnmpResult> {
239239
let mut collect: Vec<SnmpResult> = Vec::new();
240240

@@ -307,7 +307,7 @@ impl Command {
307307
community: &str,
308308
filter_in: &Vec<String>,
309309
filter_out: &Vec<String>,
310-
check_format: bool
310+
check_format: bool,
311311
) -> Result<CmdResult> {
312312
let mut collect = self.execute_snmp_collect(target, version, community, check_format);
313313

@@ -333,21 +333,25 @@ impl Command {
333333
let value = &metric.value;
334334
let parser = Parser::new(&collect, check_format);
335335
let value = parser.eval(value).map_err(|e| error::Error::InvalidJSON {
336-
message: format!("Metric \"{}\", field \"value\": {}", metric.name, e)
336+
message: format!("Metric \"{}\", field \"value\": {}", metric.name, e),
337337
})?;
338338
let min = if let Some(min_expr) = metric.min_expr.as_ref() {
339-
parser.eval(&min_expr).map_err(|e| error::Error::InvalidJSON {
340-
message: format!("Metric \"{}\", field \"min_expr\": {}", metric.name, e)
341-
})?
339+
parser
340+
.eval(&min_expr)
341+
.map_err(|e| error::Error::InvalidJSON {
342+
message: format!("Metric \"{}\", field \"min_expr\": {}", metric.name, e),
343+
})?
342344
} else if let Some(min_value) = metric.min {
343345
ExprResult::Number(min_value)
344346
} else {
345347
ExprResult::Empty
346348
};
347349
let max = if let Some(max_expr) = metric.max_expr.as_ref() {
348-
parser.eval(&max_expr).map_err(|e| error::Error::InvalidJSON {
349-
message: format!("Metric \"{}\", field \"max_expr\": {}", metric.name, e)
350-
})?
350+
parser
351+
.eval(&max_expr)
352+
.map_err(|e| error::Error::InvalidJSON {
353+
message: format!("Metric \"{}\", field \"max_expr\": {}", metric.name, e),
354+
})?
351355
} else if let Some(max_value) = metric.max {
352356
ExprResult::Number(max_value)
353357
} else {
@@ -362,38 +366,44 @@ impl Command {
362366
match &value {
363367
ExprResult::Vector(v) => {
364368
let prefix_str = match &metric.prefix {
365-
Some(prefix) => parser.eval_str(prefix).map_err(|e| error::Error::InvalidJSON {
366-
message: format!("Metric \"{}\", field \"prefix\": {}", metric.name, e)
367-
})?,
369+
Some(prefix) => {
370+
parser
371+
.eval_str(prefix)
372+
.map_err(|e| error::Error::InvalidJSON {
373+
message: format!(
374+
"Metric \"{}\", field \"prefix\": {}",
375+
metric.name, e
376+
),
377+
})?
378+
}
368379
None => ExprResult::Empty,
369380
};
370381
for (i, item) in v.iter().enumerate() {
371-
let name = match &prefix_str {
372-
ExprResult::StrVector(v) => {
373-
format!("{:?}#{}", v[i], metric.name)
374-
}
375-
ExprResult::Str(s) => {
376-
format!("{}#{}", s, metric.name)
377-
}
382+
// first, compose the instance name
383+
let instance_name = match &prefix_str {
384+
ExprResult::StrVector(v) => v[i].to_string(),
385+
ExprResult::Str(s) => s.to_string(),
378386
ExprResult::Empty => {
379-
let res = format!("{}#{}", idx, metric.name);
387+
let res = idx.to_string();
380388
idx += 1;
381389
res
382390
}
383391
_ => {
384392
panic!("A label must be a string");
385393
}
386394
};
387-
if !re_in.is_empty() {
388-
if !re_in.iter().any(|re| re.is_match(&name)) {
389-
continue;
390-
}
395+
// then apply filters exclusion and inclusion filters
396+
if !re_out.is_empty() && re_out.iter().any(|re| re.is_match(&instance_name))
397+
{
398+
continue;
391399
}
392-
if !re_out.is_empty() {
393-
if re_out.iter().any(|re| re.is_match(&name)) {
394-
continue;
395-
}
400+
if (!re_in.is_empty()
401+
&& !re_in.iter().any(|re| re.is_match(&instance_name)))
402+
{
403+
continue;
396404
}
405+
// and now concatenate to form the full perfdata
406+
let name = format!("'{}#{}'", instance_name, metric.name);
397407
let current_status =
398408
compute_status(item, &metric.warning, &metric.critical)?;
399409
status = worst(status, current_status);
@@ -422,7 +432,7 @@ impl Command {
422432
ExprResult::Number(s) => {
423433
let name = match &metric.prefix {
424434
Some(prefix) => {
425-
format!("{:?}#{}", prefix, metric.name)
435+
format!("{}#{}", prefix, metric.name)
426436
}
427437
None => {
428438
let res = format!("{}#{}", idx, metric.name);
@@ -477,9 +487,14 @@ impl Command {
477487
let value = &metric.value;
478488
let parser = Parser::new(&collect, check_format);
479489
let max = if let Some(max_expr) = metric.max_expr.as_ref() {
480-
let res = parser.eval(&max_expr).map_err(|e| error::Error::InvalidJSON {
481-
message: format!("Aggregation \"{}\", field \"max_expr\": {}", metric.name, e)
482-
})?;
490+
let res = parser
491+
.eval(&max_expr)
492+
.map_err(|e| error::Error::InvalidJSON {
493+
message: format!(
494+
"Aggregation \"{}\", field \"max_expr\": {}",
495+
metric.name, e
496+
),
497+
})?;
483498
Some(match res {
484499
ExprResult::Number(v) => v,
485500
ExprResult::Vector(v) => {
@@ -494,9 +509,14 @@ impl Command {
494509
None
495510
};
496511
let min = if let Some(min_expr) = metric.min_expr.as_ref() {
497-
let res = parser.eval(&min_expr).map_err(|e| error::Error::InvalidJSON {
498-
message: format!("Aggregation \"{}\", field \"min_expr\": {}", metric.name, e)
499-
})?;
512+
let res = parser
513+
.eval(&min_expr)
514+
.map_err(|e| error::Error::InvalidJSON {
515+
message: format!(
516+
"Aggregation \"{}\", field \"min_expr\": {}",
517+
metric.name, e
518+
),
519+
})?;
500520
Some(match res {
501521
ExprResult::Number(v) => v,
502522
ExprResult::Vector(v) => {
@@ -511,7 +531,7 @@ impl Command {
511531
None
512532
};
513533
let value = parser.eval(value).map_err(|e| error::Error::InvalidJSON {
514-
message: format!("Aggregation \"{}\", field \"value\": {}", metric.name, e)
534+
message: format!("Aggregation \"{}\", field \"value\": {}", metric.name, e),
515535
})?;
516536
match &value {
517537
ExprResult::Vector(v) => {
File renamed without changes.

rust-plugins/examples/storage.json renamed to tests/os/linux/snmp/generic-snmp/storage.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,28 @@
2323
"value": "{storage.used} * {storage.allocation_units}",
2424
"uom": "B",
2525
"min": 0,
26+
"max_expr": "{storage.size} * {storage.allocation_units}",
2627
"threshold-suffix": "storage-bytes"
28+
},
29+
{
30+
"prefix": "{storage.description}",
31+
"name": "storage.usage.percent",
32+
"value": "100 * {storage.used} / {storage.size}",
33+
"uom": "%",
34+
"min": 0,
35+
"max": 100,
36+
"threshold-suffix": "storage-prct"
2737
}
38+
],
39+
"aggregations": [
2840
]
2941
},
3042
"output": {
3143
"ok": "All storages are OK",
3244
"detail_ok": false,
33-
"warning": "Storage WARNING: ",
45+
"warning": "WARNING: ",
3446
"detail_warning": true,
35-
"critical": "Storage CRITICAL: ",
47+
"critical": "CRITICAL: ",
3648
"detail_critical": true,
3749
"instance_separator": " - ",
3850
"metric_separator": ", "

tests/os/linux/snmp/inodes.robot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ cgs-inodes ${tc}
6969
... --
7070
... 1
7171
... ${EMPTY}
72-
... All inode partitions are OK | "/run"#inodes.usage.percent=0%;;;0;100 "/"#inodes.usage.percent=6%;;;0;100 "/dev/shm"#inodes.usage.percent=0%;;;0;100 "/run/lock"#inodes.usage.percent=0%;;;0;100 "/run/user/0"#inodes.usage.percent=0%;;;0;100
72+
... All inode partitions are OK | '/run#inodes.usage.percent'=0%;;;0;100 '/#inodes.usage.percent'=6%;;;0;100 '/dev/shm#inodes.usage.percent'=0%;;;0;100 '/run/lock#inodes.usage.percent'=0%;;;0;100 '/run/user/0#inodes.usage.percent'=0%;;;0;100
7373
... 2
7474
... --check-format
75-
... Check format of JSON file '/home/omercier/projets/centreon-plugins/tests/os/linux/snmp/generic-snmp/inodes.json' JSON is valid
75+
... Check format of JSON file '${CURDIR}/generic-snmp/inodes.json' JSON is valid
7676
... 3
7777
... --warning-inodes=1
78-
... WARNING: "/"#inodes.usage.percent is 6% | "/run"#inodes.usage.percent=0%;1;;0;100 "/"#inodes.usage.percent=6%;1;;0;100 "/dev/shm"#inodes.usage.percent=0%;1;;0;100 "/run/lock"#inodes.usage.percent=0%;1;;0;100 "/run/user/0"#inodes.usage.percent=0%;1;;0;100
78+
... WARNING: '/#inodes.usage.percent' is 6% | '/run#inodes.usage.percent'=0%;1;;0;100 '/#inodes.usage.percent'=6%;1;;0;100 '/dev/shm#inodes.usage.percent'=0%;1;;0;100 '/run/lock#inodes.usage.percent'=0%;1;;0;100 '/run/user/0#inodes.usage.percent'=0%;1;;0;100
7979
... 4
8080
... --critical-inodes=1
81-
... CRITICAL: "/"#inodes.usage.percent is 6% | "/run"#inodes.usage.percent=0%;;1;0;100 "/"#inodes.usage.percent=6%;;1;0;100 "/dev/shm"#inodes.usage.percent=0%;;1;0;100 "/run/lock"#inodes.usage.percent=0%;;1;0;100 "/run/user/0"#inodes.usage.percent=0%;;1;0;100
81+
... CRITICAL: '/#inodes.usage.percent' is 6% | '/run#inodes.usage.percent'=0%;;1;0;100 '/#inodes.usage.percent'=6%;;1;0;100 '/dev/shm#inodes.usage.percent'=0%;;1;0;100 '/run/lock#inodes.usage.percent'=0%;;1;0;100 '/run/user/0#inodes.usage.percent'=0%;;1;0;100

tests/os/linux/snmp/processcount.robot

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,49 @@ processcount ${tc}
2727

2828
Ctn Run Command And Check Result As Strings ${command} ${expected_result}
2929

30-
Examples: tc extra_options expected_result --
31-
... 1 --critical-cpu-total OK: Number of current processes running: 84 | 'nbproc'=84;;;0;
32-
... 2 --top OK: Number of current processes running: 84 | 'nbproc'=84;;;0; 'top_gorgone-proxy'=324349952B;;;0; 'top_Anonymized 068'=298323968B;;;0; 'top_Anonymized 148'=127754240B;;;0; 'top_Anonymized 054'=79663104B;;;0; 'top_gorgone-autodis'=72368128B;;;0;
33-
... 3 --top-num OK: Number of current processes running: 84 | 'nbproc'=84;;;0;
34-
... 4 --top-size OK: Number of current processes running: 84 | 'nbproc'=84;;;0;
35-
... 5 --process-status='running|runnable|unHandle' OK: Number of current processes running: 86 | 'nbproc'=86;;;0;
36-
... 6 --process-status='unHandled#-2' --process-name='Anonymized 228' --verbose OK: Number of current processes running: 1 | 'nbproc'=1;;;0; Process '3534' [name: Anonymized 228] [status: unHandled#-2]
30+
Examples:
31+
... tc
32+
... extra_options
33+
... expected_result
34+
... --
35+
... 1
36+
... --critical-cpu-total
37+
... OK: Number of current processes running: 84 | 'nbproc'=84;;;0;
38+
... 2
39+
... --top
40+
... OK: Number of current processes running: 84 | 'nbproc'=84;;;0; 'top_gorgone-proxy'=324349952B;;;0; 'top_Anonymized 068'=298323968B;;;0; 'top_Anonymized 148'=127754240B;;;0; 'top_Anonymized 054'=79663104B;;;0; 'top_gorgone-autodis'=72368128B;;;0;
41+
... 3
42+
... --top-num
43+
... OK: Number of current processes running: 84 | 'nbproc'=84;;;0;
44+
... 4
45+
... --top-size
46+
... OK: Number of current processes running: 84 | 'nbproc'=84;;;0;
47+
... 5
48+
... --process-status='running|runnable|unHandle'
49+
... OK: Number of current processes running: 86 | 'nbproc'=86;;;0;
50+
... 6
51+
... --process-status='unHandled#-2' --process-name='Anonymized 228' --verbose
52+
... OK: Number of current processes running: 1 | 'nbproc'=1;;;0; Process '3534' [name: Anonymized 228] [status: unHandled#-2]
53+
54+
cgs-processcount ${tc}
55+
[Tags] os linux centreon-generic-snmp
56+
${command} Catenate
57+
... ${CENTREON_GENERIC_SNMP}
58+
... -j ${CURDIR}/generic-snmp/processcount.json
59+
... --hostname=${HOSTNAME}
60+
... --port=${SNMPPORT}
61+
... --snmp-version=${SNMPVERSION}
62+
... --snmp-community=os/linux/snmp/linux
63+
... --filter-in=111
64+
... ${extra_options}
65+
66+
Ctn Run Command Without Connector And Check Result As Strings ${command} ${expected_result}
67+
68+
Examples:
69+
... tc
70+
... extra_options
71+
... expected_result
72+
... --
73+
... 1
74+
... ${EMPTY}
75+
... All processes are OK | 'Anonymized 111#process.memory.bytes'=0B;;;0; 'Anonymized 111#process.cpu.percent'=0%;;;0;100

0 commit comments

Comments
 (0)