[monitoring-plugins] add 'multiplier' to modify current value

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


    Module: monitoring-plugins
    Branch: master
    Commit: c35bf8966a593d7926470121269b08ec00883593
    Author: Wolfgang Nieder <wnd at gmx.net>
 Committer: Sven Nierlein <sven at nierlein.org>
      Date: Sat Jul  7 09:12:44 2018 +0200
       URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=c35bf89

add 'multiplier' to modify current value

---

 plugins/check_snmp.c       | 84 +++++++++++++++++++++++++++++++++-------------
 plugins/tests/check_snmp.t | 12 +++++--
 2 files changed, 70 insertions(+), 26 deletions(-)

diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index 2601ccd..d407609 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -1,31 +1,31 @@
 /*****************************************************************************
-* 
+*
 * Monitoring check_snmp plugin
-* 
+*
 * License: GPL
 * Copyright (c) 1999-2007 Monitoring Plugins Development Team
-* 
+*
 * Description:
-* 
+*
 * This file contains the check_snmp plugin
-* 
+*
 * Check status of remote machines and obtain system information via SNMP
-* 
-* 
+*
+*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
-* 
+*
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
-* 
+*
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
-* 
-* 
+*
+*
 *****************************************************************************/
 
 const char *progname = "check_snmp";
@@ -90,6 +90,7 @@ char *thisarg (char *str);
 char *nextarg (char *str);
 void print_usage (void);
 void print_help (void);
+char *multiply (char *str);
 
 #include "regex.h"
 char regex_expect[MAX_INPUT_BUFFER] = "";
@@ -154,6 +155,8 @@ double *previous_value;
 size_t previous_size = OID_COUNT_STEP;
 int perf_labels = 1;
 char* ip_version = "";
+double multiplier = 1.0;
+char *fmtstr = "";
 
 static char *fix_snmp_range(char *th)
 {
@@ -316,7 +319,7 @@ main (int argc, char **argv)
 	for (i = 0; i < numcontext; i++) {
 		command_line[10 + i] = contextargs[i];
 	}
-	
+
 	for (i = 0; i < numauthpriv; i++) {
 		command_line[10 + numcontext + i] = authpriv[i];
 	}
@@ -330,7 +333,7 @@ main (int argc, char **argv)
 
 	for (i = 0; i < numoids; i++) {
 		command_line[10 + numcontext + numauthpriv + 1 + i] = oids[i];
-		xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]);	
+		xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]);
 	}
 
 	command_line[10 + numcontext + numauthpriv + 1 + numoids] = NULL;
@@ -398,15 +401,15 @@ main (int argc, char **argv)
 		is_counter=0;
 		/* We strip out the datatype indicator for PHBs */
 		if (strstr (response, "Gauge: ")) {
-			show = strstr (response, "Gauge: ") + 7;
-		} 
+			show = multiply (strstr (response, "Gauge: ") + 7);
+		}
 		else if (strstr (response, "Gauge32: ")) {
-			show = strstr (response, "Gauge32: ") + 9;
-		} 
+			show = multiply (strstr (response, "Gauge32: ") + 9);
+		}
 		else if (strstr (response, "Counter32: ")) {
 			show = strstr (response, "Counter32: ") + 11;
 			is_counter=1;
-			if(!calculate_rate) 
+			if(!calculate_rate)
 				strcpy(type, "c");
 		}
 		else if (strstr (response, "Counter64: ")) {
@@ -416,7 +419,10 @@ main (int argc, char **argv)
 				strcpy(type, "c");
 		}
 		else if (strstr (response, "INTEGER: ")) {
-			show = strstr (response, "INTEGER: ") + 9;
+			show = multiply (strstr (response, "INTEGER: ") + 9);
+			if (fmtstr != "") {
+				conv = fmtstr;
+			}
 		}
 		else if (strstr (response, "OID: ")) {
 			show = strstr (response, "OID: ") + 5;
@@ -616,7 +622,7 @@ main (int argc, char **argv)
 		state_string=malloc(string_length);
 		if(state_string==NULL)
 			die(STATE_UNKNOWN, _("Cannot malloc"));
-		
+
 		current_length=0;
 		for(i=0; i<total_oids; i++) {
 			xasprintf(&temp_string,"%.0f",response_value[i]);
@@ -638,7 +644,7 @@ main (int argc, char **argv)
 		state_string[--current_length]='\0';
 		if (verbose > 2)
 			printf("State string=%s\n",state_string);
-		
+
 		/* This is not strictly the same as time now, but any subtle variations will cancel out */
 		np_state_write_string(current_time, state_string );
 		if(previous_state==NULL) {
@@ -698,6 +704,8 @@ process_arguments (int argc, char **argv)
 		{"perf-oids", no_argument, 0, 'O'},
 		{"ipv4", no_argument, 0, '4'},
 		{"ipv6", no_argument, 0, '6'},
+		{"multiplier", required_argument, 0, 'M'},
+		{"fmtstr", required_argument, 0, 'f'},
 		{0, 0, 0, 0}
 	};
 
@@ -715,7 +723,7 @@ process_arguments (int argc, char **argv)
 	}
 
 	while (1) {
-		c = getopt_long (argc, argv, "nhvVO46t:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:N:L:U:a:x:A:X:z:",
+		c = getopt_long (argc, argv, "nhvVO46t:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:N:L:U:a:x:A:X:M:f:z:",
 									 longopts, &option);
 
 		if (c == -1 || c == EOF)
@@ -953,6 +961,16 @@ process_arguments (int argc, char **argv)
 			if(verbose>2)
 				printf("IPv6 detected! Will pass \"udp6:\" to snmpget.\n");
 			break;
+		case 'M':
+			if ( strspn( optarg, "0123456789.," ) == strlen( optarg ) ) {
+				multiplier=strtod(optarg,NULL);
+			}
+			break;
+		case 'f':
+			if (multiplier != 1.0) {
+				fmtstr=optarg;
+			}
+			break;
 		}
 	}
 
@@ -1022,7 +1040,7 @@ validate_arguments ()
 			contextargs[0] = strdup ("-n");
 			contextargs[1] = strdup (context);
 		}
-		
+
 		if (seclevel == NULL)
 			xasprintf(&seclevel, "noAuthNoPriv");
 
@@ -1143,6 +1161,21 @@ nextarg (char *str)
 
 
 
+/* multiply result (values 0 < n < 1 work as divider) */
+char *
+multiply (char *str)
+{
+	double val = strtod (str, NULL);
+	val *= multiplier;
+	if (val == (int)val) {
+		sprintf(str, "%.0f", val);
+	} else {
+		sprintf(str, "%f", val);
+	}
+	return str;
+}
+
+
 void
 print_help (void)
 {
@@ -1235,6 +1268,10 @@ print_help (void)
 	printf ("    %s\n", _("Units label(s) for output data (e.g., 'sec.')."));
 	printf (" %s\n", "-D, --output-delimiter=STRING");
 	printf ("    %s\n", _("Separates output on multiple OID requests"));
+	printf (" %s\n", "-M, --multiplier=FLOAT");
+	printf ("    %s\n", _("Multiplies current value, 0 < n < 1 works as divider, defaults to 1"));
+	printf (" %s\n", "-f, --fmtstr=STRING");
+	printf ("    %s\n", _("C-style format string for float values (see option -M)"));
 
 	printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
 	printf ("    %s\n", _("NOTE the final timeout value is calculated using this formula: timeout_interval * retries + 5"));
@@ -1287,4 +1324,5 @@ print_usage (void)
 	printf ("[-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n");
 	printf ("[-m miblist] [-P snmp version] [-N context] [-L seclevel] [-U secname]\n");
 	printf ("[-a authproto] [-A authpasswd] [-x privproto] [-X privpasswd] [-4|6]\n");
+	printf ("[-M multiplier [-f format]]\n");
 }
diff --git a/plugins/tests/check_snmp.t b/plugins/tests/check_snmp.t
index 0a77fa8..e9cc021 100755
--- a/plugins/tests/check_snmp.t
+++ b/plugins/tests/check_snmp.t
@@ -9,7 +9,7 @@ use NPTest;
 use FindBin qw($Bin);
 use POSIX qw/strftime/;
 
-my $tests = 73;
+my $tests = 75;
 # Check that all dependent modules are available
 eval {
 	require NetSNMP::OID;
@@ -57,9 +57,9 @@ if ($pid) {
 	exec("snmpd -c tests/conf/snmpd.conf -C -f -r udp:$port_snmp");
 }
 
-END { 
+END {
 	foreach my $pid (@pids) {
-		if ($pid) { print "Killing $pid\n"; kill "INT", $pid } 
+		if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
 	}
 };
 
@@ -268,3 +268,9 @@ like($res->output, '/SNMP WARNING - \d+ \*-4\* | iso.3.6.1.4.1.8072.3.2.67.10=\d
 $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.10,.1.3.6.1.4.1.8072.3.2.67.17 -w 1,2 -c 1" );
 is($res->return_code, 2, "Multiple OIDs with some thresholds" );
 like($res->output, '/SNMP CRITICAL - \*\d+\* \*-4\* | iso.3.6.1.4.1.8072.3.2.67.10=\d+c;1;2 iso.3.6.1.4.1.8072.3.2.67.17=-4;;/', "Multiple OIDs with thresholds output" );
+
+$res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.2.1.25.2.2.0 -M .125 ");
+is($res->return_code, 0, "Multiply OK" );
+
+$res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.2.1.25.2.2.0 --multiplier=.0009765625 -f '%.3f' ");
+is($res->return_code, 0, "Multiply format OK" );



More information about the Commits mailing list