[monitoring-plugins] check_snmp: always apply format when applying ...

Sven Nierlein git at monitoring-plugins.org
Thu Dec 22 13:00:15 CET 2022


    Module: monitoring-plugins
    Branch: master
    Commit: 9ba8f5ed66004c102bb626e47bb36dc9d0388632
    Author: Sven Nierlein <sven at nierlein.de>
 Committer: Sven Nierlein <sven at nierlein.org>
      Date: Thu Dec 22 12:02:52 2022 +0100
       URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=9ba8f5e

check_snmp: always apply format when applying multiplier

---

 plugins/check_snmp.c       | 6 +++++-
 plugins/tests/check_snmp.t | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

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" );



More information about the Commits mailing list