diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-15 13:18:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-09-15 13:18:17 +0200 |
commit | 8ef825d85fb4d09c32ca44c545d6eb8d995ddea4 (patch) | |
tree | 5ab7b18797dfd5849dec7827c87ca3bb5fcb0993 /plugins/check_procs.c | |
parent | a3cf9041af810770daf5d9b83f1906fa9bb0dd11 (diff) | |
parent | 204cf956f0b3db90d079321ee957b3860da7e33f (diff) | |
download | monitoring-plugins-8ef825d85fb4d09c32ca44c545d6eb8d995ddea4.tar.gz |
Merge pull request #2149 from RincewindsHat/clang-format
Clang format
Diffstat (limited to 'plugins/check_procs.c')
-rw-r--r-- | plugins/check_procs.c | 93 |
1 files changed, 61 insertions, 32 deletions
diff --git a/plugins/check_procs.c b/plugins/check_procs.c index 83e6864e..ae6e9c23 100644 --- a/plugins/check_procs.c +++ b/plugins/check_procs.c | |||
@@ -77,8 +77,8 @@ void print_usage(void); | |||
77 | #define EREG_ARGS 1024 | 77 | #define EREG_ARGS 1024 |
78 | #define EXCLUDE_PROGS 2048 | 78 | #define EXCLUDE_PROGS 2048 |
79 | 79 | ||
80 | #define KTHREAD_PARENT \ | 80 | #define KTHREAD_PARENT \ |
81 | "kthreadd" /* the parent process of kernel threads: \ | 81 | "kthreadd" /* the parent process of kernel threads: \ |
82 | ppid of procs are compared to pid of this proc*/ | 82 | ppid of procs are compared to pid of this proc*/ |
83 | 83 | ||
84 | static int verbose = 0; | 84 | static int verbose = 0; |
@@ -197,14 +197,17 @@ int main(int argc, char **argv) { | |||
197 | procseconds = convert_to_seconds(procetime, config.metric); | 197 | procseconds = convert_to_seconds(procetime, config.metric); |
198 | 198 | ||
199 | if (verbose >= 3) { | 199 | if (verbose >= 3) { |
200 | printf("proc#=%d uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", procs, procuid, procvsz, | 200 | printf("proc#=%d uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s " |
201 | procrss, procpid, procppid, procpcpu, procstat, procetime, procprog, procargs); | 201 | "prog=%s args=%s\n", |
202 | procs, procuid, procvsz, procrss, procpid, procppid, procpcpu, procstat, | ||
203 | procetime, procprog, procargs); | ||
202 | } | 204 | } |
203 | 205 | ||
204 | /* Ignore self */ | 206 | /* Ignore self */ |
205 | int ret = 0; | 207 | int ret = 0; |
206 | if ((config.usepid && mypid == procpid) || | 208 | if ((config.usepid && mypid == procpid) || |
207 | (((!config.usepid) && ((ret = stat_exe(procpid, &statbuf) != -1) && statbuf.st_dev == mydev && statbuf.st_ino == myino)) || | 209 | (((!config.usepid) && ((ret = stat_exe(procpid, &statbuf) != -1) && |
210 | statbuf.st_dev == mydev && statbuf.st_ino == myino)) || | ||
208 | (ret == -1 && errno == ENOENT))) { | 211 | (ret == -1 && errno == ENOENT))) { |
209 | if (verbose >= 3) { | 212 | if (verbose >= 3) { |
210 | printf("not considering - is myself or gone\n"); | 213 | printf("not considering - is myself or gone\n"); |
@@ -255,7 +258,8 @@ int main(int argc, char **argv) { | |||
255 | 258 | ||
256 | if (kthread_ppid == procppid) { | 259 | if (kthread_ppid == procppid) { |
257 | if (verbose >= 2) { | 260 | if (verbose >= 2) { |
258 | printf("Ignore kernel thread: pid=%d ppid=%d prog=%s args=%s\n", procpid, procppid, procprog, procargs); | 261 | printf("Ignore kernel thread: pid=%d ppid=%d prog=%s args=%s\n", procpid, |
262 | procppid, procprog, procargs); | ||
259 | } | 263 | } |
260 | continue; | 264 | continue; |
261 | } | 265 | } |
@@ -267,7 +271,8 @@ int main(int argc, char **argv) { | |||
267 | if ((config.options & ARGS) && procargs && (strstr(procargs, config.args) != NULL)) { | 271 | if ((config.options & ARGS) && procargs && (strstr(procargs, config.args) != NULL)) { |
268 | resultsum |= ARGS; | 272 | resultsum |= ARGS; |
269 | } | 273 | } |
270 | if ((config.options & EREG_ARGS) && procargs && (regexec(&config.re_args, procargs, (size_t)0, NULL, 0) == 0)) { | 274 | if ((config.options & EREG_ARGS) && procargs && |
275 | (regexec(&config.re_args, procargs, (size_t)0, NULL, 0) == 0)) { | ||
271 | resultsum |= EREG_ARGS; | 276 | resultsum |= EREG_ARGS; |
272 | } | 277 | } |
273 | if ((config.options & PROG) && procprog && (strcmp(config.prog, procprog) == 0)) { | 278 | if ((config.options & PROG) && procprog && (strcmp(config.prog, procprog) == 0)) { |
@@ -298,8 +303,10 @@ int main(int argc, char **argv) { | |||
298 | 303 | ||
299 | procs++; | 304 | procs++; |
300 | if (verbose >= 2) { | 305 | if (verbose >= 2) { |
301 | printf("Matched: uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s prog=%s args=%s\n", procuid, procvsz, | 306 | printf("Matched: uid=%d vsz=%d rss=%d pid=%d ppid=%d pcpu=%.2f stat=%s etime=%s " |
302 | procrss, procpid, procppid, procpcpu, procstat, procetime, procprog, procargs); | 307 | "prog=%s args=%s\n", |
308 | procuid, procvsz, procrss, procpid, procppid, procpcpu, procstat, procetime, | ||
309 | procprog, procargs); | ||
303 | } | 310 | } |
304 | 311 | ||
305 | mp_state_enum temporary_result = STATE_OK; | 312 | mp_state_enum temporary_result = STATE_OK; |
@@ -318,12 +325,14 @@ int main(int argc, char **argv) { | |||
318 | if (config.metric != METRIC_PROCS) { | 325 | if (config.metric != METRIC_PROCS) { |
319 | if (temporary_result == STATE_WARNING) { | 326 | if (temporary_result == STATE_WARNING) { |
320 | warn++; | 327 | warn++; |
321 | xasprintf(&config.fails, "%s%s%s", config.fails, (strcmp(config.fails, "") ? ", " : ""), procprog); | 328 | xasprintf(&config.fails, "%s%s%s", config.fails, |
329 | (strcmp(config.fails, "") ? ", " : ""), procprog); | ||
322 | result = max_state(result, temporary_result); | 330 | result = max_state(result, temporary_result); |
323 | } | 331 | } |
324 | if (temporary_result == STATE_CRITICAL) { | 332 | if (temporary_result == STATE_CRITICAL) { |
325 | crit++; | 333 | crit++; |
326 | xasprintf(&config.fails, "%s%s%s", config.fails, (strcmp(config.fails, "") ? ", " : ""), procprog); | 334 | xasprintf(&config.fails, "%s%s%s", config.fails, |
335 | (strcmp(config.fails, "") ? ", " : ""), procprog); | ||
327 | result = max_state(result, temporary_result); | 336 | result = max_state(result, temporary_result); |
328 | } | 337 | } |
329 | } | 338 | } |
@@ -420,7 +429,8 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
420 | 429 | ||
421 | while (true) { | 430 | while (true) { |
422 | int option = 0; | 431 | int option = 0; |
423 | int option_index = getopt_long(argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:T:X:", longopts, &option); | 432 | int option_index = |
433 | getopt_long(argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:T:X:", longopts, &option); | ||
424 | 434 | ||
425 | if (option_index == -1 || option_index == EOF) { | 435 | if (option_index == -1 || option_index == EOF) { |
426 | break; | 436 | break; |
@@ -451,7 +461,8 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
451 | case 'p': { /* process id */ | 461 | case 'p': { /* process id */ |
452 | static char tmp[MAX_INPUT_BUFFER]; | 462 | static char tmp[MAX_INPUT_BUFFER]; |
453 | if (sscanf(optarg, "%d%[^0-9]", &result.config.ppid, tmp) == 1) { | 463 | if (sscanf(optarg, "%d%[^0-9]", &result.config.ppid, tmp) == 1) { |
454 | xasprintf(&result.config.fmt, "%s%sPPID = %d", (result.config.fmt ? result.config.fmt : ""), | 464 | xasprintf(&result.config.fmt, "%s%sPPID = %d", |
465 | (result.config.fmt ? result.config.fmt : ""), | ||
455 | (result.config.options ? ", " : ""), result.config.ppid); | 466 | (result.config.options ? ", " : ""), result.config.ppid); |
456 | result.config.options |= PPID; | 467 | result.config.options |= PPID; |
457 | break; | 468 | break; |
@@ -464,7 +475,8 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
464 | } else { | 475 | } else { |
465 | result.config.statopts = optarg; | 476 | result.config.statopts = optarg; |
466 | } | 477 | } |
467 | xasprintf(&result.config.fmt, _("%s%sSTATE = %s"), (result.config.fmt ? result.config.fmt : ""), | 478 | xasprintf(&result.config.fmt, _("%s%sSTATE = %s"), |
479 | (result.config.fmt ? result.config.fmt : ""), | ||
468 | (result.config.options ? ", " : ""), result.config.statopts); | 480 | (result.config.options ? ", " : ""), result.config.statopts); |
469 | result.config.options |= STAT; | 481 | result.config.options |= STAT; |
470 | break; | 482 | break; |
@@ -488,7 +500,8 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
488 | } | 500 | } |
489 | 501 | ||
490 | char *user = pw->pw_name; | 502 | char *user = pw->pw_name; |
491 | xasprintf(&result.config.fmt, "%s%sUID = %d (%s)", (result.config.fmt ? result.config.fmt : ""), | 503 | xasprintf(&result.config.fmt, "%s%sUID = %d (%s)", |
504 | (result.config.fmt ? result.config.fmt : ""), | ||
492 | (result.config.options ? ", " : ""), result.config.uid, user); | 505 | (result.config.options ? ", " : ""), result.config.uid, user); |
493 | result.config.options |= USER; | 506 | result.config.options |= USER; |
494 | } break; | 507 | } break; |
@@ -499,7 +512,8 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
499 | } else { | 512 | } else { |
500 | result.config.prog = optarg; | 513 | result.config.prog = optarg; |
501 | } | 514 | } |
502 | xasprintf(&result.config.fmt, _("%s%scommand name '%s'"), (result.config.fmt ? result.config.fmt : ""), | 515 | xasprintf(&result.config.fmt, _("%s%scommand name '%s'"), |
516 | (result.config.fmt ? result.config.fmt : ""), | ||
503 | (result.config.options ? ", " : ""), result.config.prog); | 517 | (result.config.options ? ", " : ""), result.config.prog); |
504 | result.config.options |= PROG; | 518 | result.config.options |= PROG; |
505 | break; | 519 | break; |
@@ -509,14 +523,17 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
509 | } else { | 523 | } else { |
510 | result.config.exclude_progs = optarg; | 524 | result.config.exclude_progs = optarg; |
511 | } | 525 | } |
512 | xasprintf(&result.config.fmt, _("%s%sexclude progs '%s'"), (result.config.fmt ? result.config.fmt : ""), | 526 | xasprintf(&result.config.fmt, _("%s%sexclude progs '%s'"), |
527 | (result.config.fmt ? result.config.fmt : ""), | ||
513 | (result.config.options ? ", " : ""), result.config.exclude_progs); | 528 | (result.config.options ? ", " : ""), result.config.exclude_progs); |
514 | char *tmp_pointer = strtok(result.config.exclude_progs, ","); | 529 | char *tmp_pointer = strtok(result.config.exclude_progs, ","); |
515 | 530 | ||
516 | while (tmp_pointer) { | 531 | while (tmp_pointer) { |
517 | result.config.exclude_progs_arr = | 532 | result.config.exclude_progs_arr = |
518 | realloc(result.config.exclude_progs_arr, sizeof(char *) * ++result.config.exclude_progs_counter); | 533 | realloc(result.config.exclude_progs_arr, |
519 | result.config.exclude_progs_arr[result.config.exclude_progs_counter - 1] = tmp_pointer; | 534 | sizeof(char *) * ++result.config.exclude_progs_counter); |
535 | result.config.exclude_progs_arr[result.config.exclude_progs_counter - 1] = | ||
536 | tmp_pointer; | ||
520 | tmp_pointer = strtok(NULL, ","); | 537 | tmp_pointer = strtok(NULL, ","); |
521 | } | 538 | } |
522 | 539 | ||
@@ -529,7 +546,8 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
529 | } else { | 546 | } else { |
530 | result.config.args = optarg; | 547 | result.config.args = optarg; |
531 | } | 548 | } |
532 | xasprintf(&result.config.fmt, "%s%sargs '%s'", (result.config.fmt ? result.config.fmt : ""), | 549 | xasprintf(&result.config.fmt, "%s%sargs '%s'", |
550 | (result.config.fmt ? result.config.fmt : ""), | ||
533 | (result.config.options ? ", " : ""), result.config.args); | 551 | (result.config.options ? ", " : ""), result.config.args); |
534 | result.config.options |= ARGS; | 552 | result.config.options |= ARGS; |
535 | break; | 553 | break; |
@@ -539,7 +557,8 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
539 | if (err != 0) { | 557 | if (err != 0) { |
540 | char errbuf[MAX_INPUT_BUFFER]; | 558 | char errbuf[MAX_INPUT_BUFFER]; |
541 | regerror(err, &result.config.re_args, errbuf, MAX_INPUT_BUFFER); | 559 | regerror(err, &result.config.re_args, errbuf, MAX_INPUT_BUFFER); |
542 | die(STATE_UNKNOWN, "PROCS %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); | 560 | die(STATE_UNKNOWN, "PROCS %s: %s - %s\n", _("UNKNOWN"), |
561 | _("Could not compile regular expression"), errbuf); | ||
543 | } | 562 | } |
544 | /* Strip off any | within the regex optarg */ | 563 | /* Strip off any | within the regex optarg */ |
545 | char *temp_string = strdup(optarg); | 564 | char *temp_string = strdup(optarg); |
@@ -550,14 +569,16 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
550 | } | 569 | } |
551 | index++; | 570 | index++; |
552 | } | 571 | } |
553 | xasprintf(&result.config.fmt, "%s%sregex args '%s'", (result.config.fmt ? result.config.fmt : ""), | 572 | xasprintf(&result.config.fmt, "%s%sregex args '%s'", |
573 | (result.config.fmt ? result.config.fmt : ""), | ||
554 | (result.config.options ? ", " : ""), temp_string); | 574 | (result.config.options ? ", " : ""), temp_string); |
555 | result.config.options |= EREG_ARGS; | 575 | result.config.options |= EREG_ARGS; |
556 | } break; | 576 | } break; |
557 | case 'r': { /* RSS */ | 577 | case 'r': { /* RSS */ |
558 | static char tmp[MAX_INPUT_BUFFER]; | 578 | static char tmp[MAX_INPUT_BUFFER]; |
559 | if (sscanf(optarg, "%d%[^0-9]", &result.config.rss, tmp) == 1) { | 579 | if (sscanf(optarg, "%d%[^0-9]", &result.config.rss, tmp) == 1) { |
560 | xasprintf(&result.config.fmt, "%s%sRSS >= %d", (result.config.fmt ? result.config.fmt : ""), | 580 | xasprintf(&result.config.fmt, "%s%sRSS >= %d", |
581 | (result.config.fmt ? result.config.fmt : ""), | ||
561 | (result.config.options ? ", " : ""), result.config.rss); | 582 | (result.config.options ? ", " : ""), result.config.rss); |
562 | result.config.options |= RSS; | 583 | result.config.options |= RSS; |
563 | break; | 584 | break; |
@@ -567,7 +588,8 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
567 | case 'z': { /* VSZ */ | 588 | case 'z': { /* VSZ */ |
568 | static char tmp[MAX_INPUT_BUFFER]; | 589 | static char tmp[MAX_INPUT_BUFFER]; |
569 | if (sscanf(optarg, "%d%[^0-9]", &result.config.vsz, tmp) == 1) { | 590 | if (sscanf(optarg, "%d%[^0-9]", &result.config.vsz, tmp) == 1) { |
570 | xasprintf(&result.config.fmt, "%s%sVSZ >= %d", (result.config.fmt ? result.config.fmt : ""), | 591 | xasprintf(&result.config.fmt, "%s%sVSZ >= %d", |
592 | (result.config.fmt ? result.config.fmt : ""), | ||
571 | (result.config.options ? ", " : ""), result.config.vsz); | 593 | (result.config.options ? ", " : ""), result.config.vsz); |
572 | result.config.options |= VSZ; | 594 | result.config.options |= VSZ; |
573 | break; | 595 | break; |
@@ -578,7 +600,8 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
578 | /* TODO: -P 1.5.5 is accepted */ | 600 | /* TODO: -P 1.5.5 is accepted */ |
579 | static char tmp[MAX_INPUT_BUFFER]; | 601 | static char tmp[MAX_INPUT_BUFFER]; |
580 | if (sscanf(optarg, "%f%[^0-9.]", &result.config.pcpu, tmp) == 1) { | 602 | if (sscanf(optarg, "%f%[^0-9.]", &result.config.pcpu, tmp) == 1) { |
581 | xasprintf(&result.config.fmt, "%s%sPCPU >= %.2f", (result.config.fmt ? result.config.fmt : ""), | 603 | xasprintf(&result.config.fmt, "%s%sPCPU >= %.2f", |
604 | (result.config.fmt ? result.config.fmt : ""), | ||
582 | (result.config.options ? ", " : ""), result.config.pcpu); | 605 | (result.config.options ? ", " : ""), result.config.pcpu); |
583 | result.config.options |= PCPU; | 606 | result.config.options |= PCPU; |
584 | break; | 607 | break; |
@@ -633,13 +656,15 @@ check_procs_config_wrapper process_arguments(int argc, char **argv) { | |||
633 | } | 656 | } |
634 | if (result.config.statopts == NULL && argv[index]) { | 657 | if (result.config.statopts == NULL && argv[index]) { |
635 | xasprintf(&result.config.statopts, "%s", argv[index++]); | 658 | xasprintf(&result.config.statopts, "%s", argv[index++]); |
636 | xasprintf(&result.config.fmt, _("%s%sSTATE = %s"), (result.config.fmt ? result.config.fmt : ""), | 659 | xasprintf(&result.config.fmt, _("%s%sSTATE = %s"), |
637 | (result.config.options ? ", " : ""), result.config.statopts); | 660 | (result.config.fmt ? result.config.fmt : ""), (result.config.options ? ", " : ""), |
661 | result.config.statopts); | ||
638 | result.config.options |= STAT; | 662 | result.config.options |= STAT; |
639 | } | 663 | } |
640 | 664 | ||
641 | /* this will abort in case of invalid ranges */ | 665 | /* this will abort in case of invalid ranges */ |
642 | set_thresholds(&result.config.procs_thresholds, result.config.warning_range, result.config.critical_range); | 666 | set_thresholds(&result.config.procs_thresholds, result.config.warning_range, |
667 | result.config.critical_range); | ||
643 | 668 | ||
644 | return validate_arguments(result); | 669 | return validate_arguments(result); |
645 | } | 670 | } |
@@ -722,13 +747,17 @@ void print_help(void) { | |||
722 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | 747 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); |
723 | printf(COPYRIGHT, copyright, email); | 748 | printf(COPYRIGHT, copyright, email); |
724 | 749 | ||
725 | printf("%s\n", _("Checks all processes and generates WARNING or CRITICAL states if the specified")); | 750 | printf("%s\n", |
726 | printf("%s\n", _("metric is outside the required threshold ranges. The metric defaults to number")); | 751 | _("Checks all processes and generates WARNING or CRITICAL states if the specified")); |
727 | printf("%s\n", _("of processes. Search filters can be applied to limit the processes to check.")); | 752 | printf("%s\n", |
753 | _("metric is outside the required threshold ranges. The metric defaults to number")); | ||
754 | printf("%s\n", | ||
755 | _("of processes. Search filters can be applied to limit the processes to check.")); | ||
728 | 756 | ||
729 | printf("\n\n"); | 757 | printf("\n\n"); |
730 | 758 | ||
731 | printf("%s\n", _("The parent process, check_procs itself and any child process of check_procs (ps)")); | 759 | printf("%s\n", |
760 | _("The parent process, check_procs itself and any child process of check_procs (ps)")); | ||
732 | printf("%s\n", _("are excluded from any checks to prevent false positives.")); | 761 | printf("%s\n", _("are excluded from any checks to prevent false positives.")); |
733 | 762 | ||
734 | printf("\n\n"); | 763 | printf("\n\n"); |