summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTon Voon <ton.voon@opsera.com>2010-06-22 12:09:46 (GMT)
committerTon Voon <ton.voon@opsera.com>2010-06-22 12:09:46 (GMT)
commit5e89c3cc73fc54602b268750cf6ef6870648048f (patch)
tree888bf7c11c52c8cd894a5944863cfae2d0efe6e6
parent4a0b843173640562bed1c7f65771eea47163c32a (diff)
downloadmonitoring-plugins-5e89c3cc73fc54602b268750cf6ef6870648048f.tar.gz
Added a rate multiplier. Added simple overflow checker. Added extra
help for --rate (from Thomas)
-rw-r--r--plugins/check_snmp.c39
1 files changed, 35 insertions, 4 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index 5a02503..a775f95 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -59,6 +59,10 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
59 59
60#define MAX_OIDS 8 60#define MAX_OIDS 8
61 61
62/* Longopts only arguments */
63#define L_CALCULATE_RATE CHAR_MAX+1
64#define L_RATE_MULTIPLIER CHAR_MAX+2
65
62/* Gobble to string - stop incrementing c when c[0] match one of the 66/* Gobble to string - stop incrementing c when c[0] match one of the
63 * characters in s */ 67 * characters in s */
64#define GOBBLE_TOS(c, s) while(c[0]!='\0' && strchr(s, c[0])==NULL) { c++; } 68#define GOBBLE_TOS(c, s) while(c[0]!='\0' && strchr(s, c[0])==NULL) { c++; }
@@ -132,6 +136,7 @@ char *output_delim;
132char *miblist = NULL; 136char *miblist = NULL;
133int needmibs = FALSE; 137int needmibs = FALSE;
134int calculate_rate = 0; 138int calculate_rate = 0;
139int rate_multiplier = 1;
135state_data *previous_state; 140state_data *previous_state;
136double previous_value[MAX_OIDS]; 141double previous_value[MAX_OIDS];
137 142
@@ -167,6 +172,7 @@ main (int argc, char **argv)
167 double temp_double; 172 double temp_double;
168 time_t duration; 173 time_t duration;
169 char *conv = "12345678"; 174 char *conv = "12345678";
175 int is_counter=0;
170 176
171 setlocale (LC_ALL, ""); 177 setlocale (LC_ALL, "");
172 bindtextdomain (PACKAGE, LOCALEDIR); 178 bindtextdomain (PACKAGE, LOCALEDIR);
@@ -306,7 +312,7 @@ main (int argc, char **argv)
306 312
307 for (line=0, i=0; line < chld_out.lines; line++, i++) { 313 for (line=0, i=0; line < chld_out.lines; line++, i++) {
308 if(calculate_rate) 314 if(calculate_rate)
309 conv = "%.1f"; 315 conv = "%.10g";
310 else 316 else
311 conv = "%.0f"; 317 conv = "%.0f";
312 318
@@ -323,6 +329,7 @@ main (int argc, char **argv)
323 /* Clean up type array - Sol10 does not necessarily zero it out */ 329 /* Clean up type array - Sol10 does not necessarily zero it out */
324 bzero(type, sizeof(type)); 330 bzero(type, sizeof(type));
325 331
332 is_counter=0;
326 /* We strip out the datatype indicator for PHBs */ 333 /* We strip out the datatype indicator for PHBs */
327 if (strstr (response, "Gauge: ")) { 334 if (strstr (response, "Gauge: ")) {
328 show = strstr (response, "Gauge: ") + 7; 335 show = strstr (response, "Gauge: ") + 7;
@@ -335,12 +342,14 @@ main (int argc, char **argv)
335 else if (strstr (response, "Counter32: ")) { 342 else if (strstr (response, "Counter32: ")) {
336 show = strstr (response, "Counter32: ") + 11; 343 show = strstr (response, "Counter32: ") + 11;
337 is_numeric++; 344 is_numeric++;
345 is_counter=1;
338 if(!calculate_rate) 346 if(!calculate_rate)
339 strcpy(type, "c"); 347 strcpy(type, "c");
340 } 348 }
341 else if (strstr (response, "Counter64: ")) { 349 else if (strstr (response, "Counter64: ")) {
342 show = strstr (response, "Counter64: ") + 11; 350 show = strstr (response, "Counter64: ") + 11;
343 is_numeric++; 351 is_numeric++;
352 is_counter=1;
344 if(!calculate_rate) 353 if(!calculate_rate)
345 strcpy(type, "c"); 354 strcpy(type, "c");
346 } 355 }
@@ -405,6 +414,13 @@ main (int argc, char **argv)
405 if(duration<=0) 414 if(duration<=0)
406 die(STATE_UNKNOWN,_("Time duration between plugin calls is invalid")); 415 die(STATE_UNKNOWN,_("Time duration between plugin calls is invalid"));
407 temp_double = (response_value[i]-previous_value[i])/duration; 416 temp_double = (response_value[i]-previous_value[i])/duration;
417 /* Simple overflow catcher (same as in rrdtool, rrd_update.c) */
418 if(is_counter) {
419 if(temp_double<(double)0.0)
420 temp_double+=(double)4294967296.0; /* 2^32 */
421 if(temp_double<(double)0.0)
422 temp_double+=(double)18446744069414584320.0; /* 2^64-2^32 */;
423 }
408 iresult = get_status(temp_double, thlds[i]); 424 iresult = get_status(temp_double, thlds[i]);
409 asprintf (&show, conv, temp_double); 425 asprintf (&show, conv, temp_double);
410 } 426 }
@@ -564,7 +580,8 @@ process_arguments (int argc, char **argv)
564 {"authpasswd", required_argument, 0, 'A'}, 580 {"authpasswd", required_argument, 0, 'A'},
565 {"privpasswd", required_argument, 0, 'X'}, 581 {"privpasswd", required_argument, 0, 'X'},
566 {"next", no_argument, 0, 'n'}, 582 {"next", no_argument, 0, 'n'},
567 {"calculate-rate", no_argument, 0, CHAR_MAX+1}, 583 {"rate", no_argument, 0, L_CALCULATE_RATE},
584 {"rate-multiplier", required_argument, 0, L_RATE_MULTIPLIER},
568 {0, 0, 0, 0} 585 {0, 0, 0, 0}
569 }; 586 };
570 587
@@ -768,11 +785,15 @@ process_arguments (int argc, char **argv)
768 unitv[nunits - 1] = ptr; 785 unitv[nunits - 1] = ptr;
769 } 786 }
770 break; 787 break;
771 case CHAR_MAX+1: 788 case L_CALCULATE_RATE:
772 if(calculate_rate==0) 789 if(calculate_rate==0)
773 np_enable_state(NULL, 1); 790 np_enable_state(NULL, 1);
774 calculate_rate = 1; 791 calculate_rate = 1;
775 break; 792 break;
793 case L_RATE_MULTIPLIER:
794 if(!is_integer(optarg)||(rate_multiplier=atoi(optarg)<=0))
795 usage2(_("Rate multiplier must be a positive integer"),optarg);
796 break;
776 } 797 }
777 } 798 }
778 799
@@ -1012,7 +1033,7 @@ print_help (void)
1012 printf (" %s\n", "-c, --critical=THRESHOLD(s)"); 1033 printf (" %s\n", "-c, --critical=THRESHOLD(s)");
1013 printf (" %s\n", _("Critical threshold range(s)")); 1034 printf (" %s\n", _("Critical threshold range(s)"));
1014 printf (" %s\n", "--calculate-rate"); 1035 printf (" %s\n", "--calculate-rate");
1015 printf (" %s\n", _("Values will be converted to rate per second")); 1036 printf (" %s\n", _("Enable rate calculation. See 'Rate Calculation' below"));
1016 1037
1017 /* Tests Against Strings */ 1038 /* Tests Against Strings */
1018 printf (" %s\n", "-s, --string=STRING"); 1039 printf (" %s\n", "-s, --string=STRING");
@@ -1053,6 +1074,16 @@ print_help (void)
1053 printf (" %s\n", _("- All evaluation methods other than PR, STR, and SUBSTR expect that the value")); 1074 printf (" %s\n", _("- All evaluation methods other than PR, STR, and SUBSTR expect that the value"));
1054 printf (" %s\n", _("returned from the SNMP query is an unsigned integer.")); 1075 printf (" %s\n", _("returned from the SNMP query is an unsigned integer."));
1055 1076
1077 printf("\n");
1078 printf("%s\n", _("Rate Calculation:"));
1079 printf(" %s\n", _("In many places, SNMP returns counters that are only meaningful when"));
1080 printf(" %s\n", _("calculating the counter difference since the last check. check_snmp"));
1081 printf(" %s\n", _("saves the last state information in a file so that the rate can be"));
1082 printf(" %s\n", _("calculated. Use the --rate option to save state information. On the"));
1083 printf(" %s\n", _("first run, there will be no prior state - this will return with OK."));
1084 printf(" %s\n", _("The state is uniquely determined by the arguments to the plugin, so"));
1085 printf(" %s\n", _("changing the arguments will create a new state file."));
1086
1056 printf (UT_SUPPORT); 1087 printf (UT_SUPPORT);
1057} 1088}
1058 1089