From 9ba8f5ed66004c102bb626e47bb36dc9d0388632 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Thu, 22 Dec 2022 12:02:52 +0100 Subject: check_snmp: always apply format when applying multiplier diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index d407609..56bad88 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c @@ -1167,10 +1167,14 @@ multiply (char *str) { double val = strtod (str, NULL); val *= multiplier; + char *conv = "%f"; + if (fmtstr != "") { + conv = fmtstr; + } if (val == (int)val) { sprintf(str, "%.0f", val); } else { - sprintf(str, "%f", val); + sprintf(str, conv, val); } return str; } diff --git a/plugins/tests/check_snmp.t b/plugins/tests/check_snmp.t index bb5b8db..bc03ec6 100755 --- a/plugins/tests/check_snmp.t +++ b/plugins/tests/check_snmp.t @@ -279,7 +279,7 @@ is($res->output,'SNMP OK - 4.200000 | iso.3.6.1.4.1.8072.3.2.67.19=4.200000 ' , $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19 --multiplier=.1 -f '%.2f' "); is($res->return_code, 0, "Test multiply RC + format" ); -is($res->output, 'SNMP OK - 4.200000 | iso.3.6.1.4.1.8072.3.2.67.19=4.200000 ', "Test multiply .1 output + format" ); +is($res->output, 'SNMP OK - 4.20 | iso.3.6.1.4.1.8072.3.2.67.19=4.20 ', "Test multiply .1 output + format" ); $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19 --multiplier=.1 -f '%.2f' -w 1"); is($res->return_code, 1, "Test multiply RC + format + thresholds" ); -- cgit v0.10-9-g596f