summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorSebastian Harl <sh@teamix.net>2011-04-08 08:18:48 (GMT)
committerHolger Weiss <holger@zedat.fu-berlin.de>2012-11-14 19:45:44 (GMT)
commit2bac48c02742dba92fc09d60fc211f60f10e9223 (patch)
treefee4e56cf35cd5b15987111c303169314237596c /plugins
parent217a6a7643867ad949c25b5e8ca2d1839ec28133 (diff)
downloadmonitoring-plugins-2bac48c02742dba92fc09d60fc211f60f10e9223.tar.gz
check_procs: Use the range/threshold support functions from libnagiosplug.
This adds support for @<range> and makes stuff a bit simpler by removing code duplications. Note: Previously, the compatibility code for 'check_procs <warn> <max>' accepted something like 'check_procs -w 10:-1 -c 10:-1 20 50' as well (treating it as if '-w 10:20 -c 10:50' was specified). This is no longer the case ... additional arguments are only used as warn/crit thresholds in case -w/-c is not specified at all.
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_procs.c106
1 files changed, 17 insertions, 89 deletions
diff --git a/plugins/check_procs.c b/plugins/check_procs.c
index 2f2dcc5..23fdc27 100644
--- a/plugins/check_procs.c
+++ b/plugins/check_procs.c
@@ -45,15 +45,13 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
45 45
46int process_arguments (int, char **); 46int process_arguments (int, char **);
47int validate_arguments (void); 47int validate_arguments (void);
48int check_thresholds (int);
49int convert_to_seconds (char *); 48int convert_to_seconds (char *);
50void print_help (void); 49void print_help (void);
51void print_usage (void); 50void print_usage (void);
52 51
53int wmax = -1; 52char *warning_range = NULL;
54int cmax = -1; 53char *critical_range = NULL;
55int wmin = -1; 54thresholds *procs_thresholds = NULL;
56int cmin = -1;
57 55
58int options = 0; /* bitmask of filter criteria to test against */ 56int options = 0; /* bitmask of filter criteria to test against */
59#define ALL 1 57#define ALL 1
@@ -238,14 +236,14 @@ main (int argc, char **argv)
238 } 236 }
239 237
240 if (metric == METRIC_VSZ) 238 if (metric == METRIC_VSZ)
241 i = check_thresholds (procvsz); 239 i = get_status ((double)procvsz, procs_thresholds);
242 else if (metric == METRIC_RSS) 240 else if (metric == METRIC_RSS)
243 i = check_thresholds (procrss); 241 i = get_status ((double)procrss, procs_thresholds);
244 /* TODO? float thresholds for --metric=CPU */ 242 /* TODO? float thresholds for --metric=CPU */
245 else if (metric == METRIC_CPU) 243 else if (metric == METRIC_CPU)
246 i = check_thresholds ((int)procpcpu); 244 i = get_status (procpcpu, procs_thresholds);
247 else if (metric == METRIC_ELAPSED) 245 else if (metric == METRIC_ELAPSED)
248 i = check_thresholds (procseconds); 246 i = get_status ((double)procseconds, procs_thresholds);
249 247
250 if (metric != METRIC_PROCS) { 248 if (metric != METRIC_PROCS) {
251 if (i == STATE_WARNING) { 249 if (i == STATE_WARNING) {
@@ -276,7 +274,7 @@ main (int argc, char **argv)
276 274
277 /* Needed if procs found, but none match filter */ 275 /* Needed if procs found, but none match filter */
278 if ( metric == METRIC_PROCS ) { 276 if ( metric == METRIC_PROCS ) {
279 result = max_state (result, check_thresholds (procs) ); 277 result = max_state (result, get_status ((double)procs, procs_thresholds) );
280 } 278 }
281 279
282 if ( result == STATE_OK ) { 280 if ( result == STATE_OK ) {
@@ -368,28 +366,10 @@ process_arguments (int argc, char **argv)
368 timeout_interval = atoi (optarg); 366 timeout_interval = atoi (optarg);
369 break; 367 break;
370 case 'c': /* critical threshold */ 368 case 'c': /* critical threshold */
371 if (is_integer (optarg)) 369 critical_range = optarg;
372 cmax = atoi (optarg);
373 else if (sscanf (optarg, ":%d", &cmax) == 1)
374 break;
375 else if (sscanf (optarg, "%d:%d", &cmin, &cmax) == 2)
376 break;
377 else if (sscanf (optarg, "%d:", &cmin) == 1)
378 break;
379 else
380 usage4 (_("Critical Process Count must be an integer!"));
381 break; 370 break;
382 case 'w': /* warning threshold */ 371 case 'w': /* warning threshold */
383 if (is_integer (optarg)) 372 warning_range = optarg;
384 wmax = atoi (optarg);
385 else if (sscanf (optarg, ":%d", &wmax) == 1)
386 break;
387 else if (sscanf (optarg, "%d:%d", &wmin, &wmax) == 2)
388 break;
389 else if (sscanf (optarg, "%d:", &wmin) == 1)
390 break;
391 else
392 usage4 (_("Warning Process Count must be an integer!"));
393 break; 373 break;
394 case 'p': /* process id */ 374 case 'p': /* process id */
395 if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) { 375 if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) {
@@ -518,16 +498,19 @@ process_arguments (int argc, char **argv)
518 } 498 }
519 499
520 c = optind; 500 c = optind;
521 if (wmax == -1 && argv[c]) 501 if ((! warning_range) && argv[c])
522 wmax = atoi (argv[c++]); 502 warning_range = argv[c++];
523 if (cmax == -1 && argv[c]) 503 if ((! critical_range) && argv[c])
524 cmax = atoi (argv[c++]); 504 critical_range = argv[c++];
525 if (statopts == NULL && argv[c]) { 505 if (statopts == NULL && argv[c]) {
526 xasprintf (&statopts, "%s", argv[c++]); 506 xasprintf (&statopts, "%s", argv[c++]);
527 xasprintf (&fmt, _("%s%sSTATE = %s"), (fmt ? fmt : ""), (options ? ", " : ""), statopts); 507 xasprintf (&fmt, _("%s%sSTATE = %s"), (fmt ? fmt : ""), (options ? ", " : ""), statopts);
528 options |= STAT; 508 options |= STAT;
529 } 509 }
530 510
511 /* this will abort in case of invalid ranges */
512 set_thresholds (&procs_thresholds, warning_range, critical_range);
513
531 return validate_arguments (); 514 return validate_arguments ();
532} 515}
533 516
@@ -536,27 +519,6 @@ process_arguments (int argc, char **argv)
536int 519int
537validate_arguments () 520validate_arguments ()
538{ 521{
539
540 if (wmax >= 0 && wmin == -1)
541 wmin = 0;
542 if (cmax >= 0 && cmin == -1)
543 cmin = 0;
544 if (wmax >= wmin && cmax >= cmin) { /* standard ranges */
545 if (wmax > cmax && cmax != -1) {
546 printf (_("wmax (%d) cannot be greater than cmax (%d)\n"), wmax, cmax);
547 return ERROR;
548 }
549 if (cmin > wmin && wmin != -1) {
550 printf (_("wmin (%d) cannot be less than cmin (%d)\n"), wmin, cmin);
551 return ERROR;
552 }
553 }
554
555/* if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) { */
556/* printf ("At least one threshold must be set\n"); */
557/* return ERROR; */
558/* } */
559
560 if (options == 0) 522 if (options == 0)
561 options = ALL; 523 options = ALL;
562 524
@@ -579,40 +541,6 @@ validate_arguments ()
579} 541}
580 542
581 543
582
583/* Check thresholds against value */
584int
585check_thresholds (int value)
586{
587 if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) {
588 return OK;
589 }
590 else if (cmax >= 0 && cmin >= 0 && cmax < cmin) {
591 if (value > cmax && value < cmin)
592 return STATE_CRITICAL;
593 }
594 else if (cmax >= 0 && value > cmax) {
595 return STATE_CRITICAL;
596 }
597 else if (cmin >= 0 && value < cmin) {
598 return STATE_CRITICAL;
599 }
600
601 if (wmax >= 0 && wmin >= 0 && wmax < wmin) {
602 if (value > wmax && value < wmin) {
603 return STATE_WARNING;
604 }
605 }
606 else if (wmax >= 0 && value > wmax) {
607 return STATE_WARNING;
608 }
609 else if (wmin >= 0 && value < wmin) {
610 return STATE_WARNING;
611 }
612 return STATE_OK;
613}
614
615
616/* convert the elapsed time to seconds */ 544/* convert the elapsed time to seconds */
617int 545int
618convert_to_seconds(char *etime) { 546convert_to_seconds(char *etime) {