From f2900e0ccf18f321857e4072681ad9c10a0cb67f Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 13 Mar 2025 15:36:36 +0100 Subject: check_load: clang-format --- plugins/check_load.c | 330 +++++++++++++++++++++++++-------------------------- 1 file changed, 162 insertions(+), 168 deletions(-) (limited to 'plugins/check_load.c') diff --git a/plugins/check_load.c b/plugins/check_load.c index 1431d130..e3a45f58 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -1,32 +1,32 @@ /***************************************************************************** -* -* Monitoring check_load plugin -* -* License: GPL -* Copyright (c) 1999-2007 Monitoring Plugins Development Team -* -* Description: -* -* This file contains the check_load plugin -* -* This plugin tests the current system load average. -* -* -* 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 . -* -* -*****************************************************************************/ + * + * Monitoring check_load plugin + * + * License: GPL + * Copyright (c) 1999-2007 Monitoring Plugins Development Team + * + * Description: + * + * This file contains the check_load plugin + * + * This plugin tests the current system load average. + * + * + * 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 . + * + * + *****************************************************************************/ const char *progname = "check_load"; const char *copyright = "1999-2022"; @@ -40,127 +40,129 @@ const char *email = "devel@monitoring-plugins.org"; #include #ifdef HAVE_SYS_LOADAVG_H -#include +# include #endif /* needed for compilation under NetBSD, as suggested by Andy Doran */ #ifndef LOADAVG_1MIN -#define LOADAVG_1MIN 0 -#define LOADAVG_5MIN 1 -#define LOADAVG_15MIN 2 +# define LOADAVG_1MIN 0 +# define LOADAVG_5MIN 1 +# define LOADAVG_15MIN 2 #endif /* !defined LOADAVG_1MIN */ - -static int process_arguments (int argc, char **argv); -static int validate_arguments (void); -void print_help (void); -void print_usage (void); +static int process_arguments(int argc, char **argv); +static int validate_arguments(void); +void print_help(void); +void print_usage(void); static int print_top_consuming_processes(); static int n_procs_to_show = 0; /* strictly for pretty-print usage in loops */ -static const int nums[3] = { 1, 5, 15 }; +static const int nums[3] = {1, 5, 15}; /* provide some fairly sane defaults */ -double wload[3] = { 0.0, 0.0, 0.0 }; -double cload[3] = { 0.0, 0.0, 0.0 }; -#define la1 la[0] -#define la5 la[1] +double wload[3] = {0.0, 0.0, 0.0}; +double cload[3] = {0.0, 0.0, 0.0}; +#define la1 la[0] +#define la5 la[1] #define la15 la[2] char *status_line; bool take_into_account_cpus = false; -static void -get_threshold(char *arg, double *th) -{ +static void get_threshold(char *arg, double *th) { size_t i, n; int valid = 0; char *str = arg, *p; n = strlen(arg); - for(i = 0; i < 3; i++) { + for (i = 0; i < 3; i++) { th[i] = strtod(str, &p); - if(p == str) break; + if (p == str) { + break; + } valid = 1; str = p + 1; - if(n <= (size_t)(str - arg)) break; + if (n <= (size_t)(str - arg)) { + break; + } } /* empty argument or non-floatish, so warn about it and die */ - if(!i && !valid) usage (_("Warning threshold must be float or float triplet!\n")); + if (!i && !valid) { + usage(_("Warning threshold must be float or float triplet!\n")); + } - if(i != 2) { + if (i != 2) { /* one or more numbers were given, so fill array with last * we got (most likely to NOT produce the least expected result) */ - for(n = i; n < 3; n++) th[n] = th[i]; + for (n = i; n < 3; n++) { + th[n] = th[i]; + } } } - -int -main (int argc, char **argv) -{ +int main(int argc, char **argv) { int result = -1; int i; long numcpus; - double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about uninitialized arrays */ + double la[3] = {0.0, 0.0, 0.0}; /* NetBSD complains about uninitialized arrays */ #ifndef HAVE_GETLOADAVG char input_buffer[MAX_INPUT_BUFFER]; #endif - setlocale (LC_ALL, ""); - bindtextdomain (PACKAGE, LOCALEDIR); - textdomain (PACKAGE); + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); setlocale(LC_NUMERIC, "POSIX"); /* Parse extra opts if any */ - argv = np_extra_opts (&argc, argv, progname); + argv = np_extra_opts(&argc, argv, progname); - if (process_arguments (argc, argv) == ERROR) - usage4 (_("Could not parse arguments")); + if (process_arguments(argc, argv) == ERROR) { + usage4(_("Could not parse arguments")); + } #ifdef HAVE_GETLOADAVG - result = getloadavg (la, 3); - if (result != 3) + result = getloadavg(la, 3); + if (result != 3) { return STATE_UNKNOWN; + } #else - child_process = spopen (PATH_TO_UPTIME); + child_process = spopen(PATH_TO_UPTIME); if (child_process == NULL) { - printf (_("Error opening %s\n"), PATH_TO_UPTIME); + printf(_("Error opening %s\n"), PATH_TO_UPTIME); return STATE_UNKNOWN; } - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); + child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); if (child_stderr == NULL) { - printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME); + printf(_("Could not open stderr for %s\n"), PATH_TO_UPTIME); } - fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); - if(strstr(input_buffer, "load average:")) { - sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15); - } - else if(strstr(input_buffer, "load averages:")) { - sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15); - } - else { - printf (_("could not parse load from uptime %s: %d\n"), PATH_TO_UPTIME, result); + fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); + if (strstr(input_buffer, "load average:")) { + sscanf(input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15); + } else if (strstr(input_buffer, "load averages:")) { + sscanf(input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15); + } else { + printf(_("could not parse load from uptime %s: %d\n"), PATH_TO_UPTIME, result); return STATE_UNKNOWN; - } + } - result = spclose (child_process); + result = spclose(child_process); if (result) { - printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME); + printf(_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME); return STATE_UNKNOWN; } #endif if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) { #ifdef HAVE_GETLOADAVG - printf (_("Error in getloadavg()\n")); + printf(_("Error in getloadavg()\n")); #else - printf (_("Error processing %s\n"), PATH_TO_UPTIME); + printf(_("Error processing %s\n"), PATH_TO_UPTIME); #endif return STATE_UNKNOWN; } @@ -171,8 +173,7 @@ main (int argc, char **argv) xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15); xasprintf(&status_line, ("total %s"), status_line); - - double scaled_la[3] = { 0.0, 0.0, 0.0 }; + double scaled_la[3] = {0.0, 0.0, 0.0}; bool is_using_scaled_load_values = false; if (take_into_account_cpus == true && (numcpus = GET_NUMBER_OF_CPUS()) > 0) { @@ -187,24 +188,26 @@ main (int argc, char **argv) xasprintf(&status_line, "scaled %s - %s", tmp, status_line); } - for(i = 0; i < 3; i++) { + for (i = 0; i < 3; i++) { if (is_using_scaled_load_values) { - if(scaled_la[i] > cload[i]) { + if (scaled_la[i] > cload[i]) { result = STATE_CRITICAL; break; + } else if (scaled_la[i] > wload[i]) { + result = STATE_WARNING; } - else if(scaled_la[i] > wload[i]) result = STATE_WARNING; } else { - if(la[i] > cload[i]) { + if (la[i] > cload[i]) { result = STATE_CRITICAL; break; + } else if (la[i] > wload[i]) { + result = STATE_WARNING; } - else if(la[i] > wload[i]) result = STATE_WARNING; } } printf("LOAD %s - %s|", state_text(result), status_line); - for(i = 0; i < 3; i++) { + for (i = 0; i < 3; i++) { if (is_using_scaled_load_values) { printf("load%d=%.3f;;;0; ", nums[i], la[i]); printf("scaled_load%d=%.3f;%.3f;%.3f;0; ", nums[i], scaled_la[i], wload[i], cload[i]); @@ -220,32 +223,29 @@ main (int argc, char **argv) return result; } - /* process command-line arguments */ -static int -process_arguments (int argc, char **argv) -{ +static int process_arguments(int argc, char **argv) { int c = 0; int option = 0; - static struct option longopts[] = { - {"warning", required_argument, 0, 'w'}, - {"critical", required_argument, 0, 'c'}, - {"percpu", no_argument, 0, 'r'}, - {"version", no_argument, 0, 'V'}, - {"help", no_argument, 0, 'h'}, - {"procs-to-show", required_argument, 0, 'n'}, - {0, 0, 0, 0} - }; - - if (argc < 2) + static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, + {"critical", required_argument, 0, 'c'}, + {"percpu", no_argument, 0, 'r'}, + {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, + {"procs-to-show", required_argument, 0, 'n'}, + {0, 0, 0, 0}}; + + if (argc < 2) { return ERROR; + } while (1) { - c = getopt_long (argc, argv, "Vhrc:w:n:", longopts, &option); + c = getopt_long(argc, argv, "Vhrc:w:n:", longopts, &option); - if (c == -1 || c == EOF) + if (c == -1 || c == EOF) { break; + } switch (c) { case 'w': /* warning time threshold */ @@ -257,94 +257,89 @@ process_arguments (int argc, char **argv) case 'r': /* Divide load average by number of CPUs */ take_into_account_cpus = true; break; - case 'V': /* version */ - print_revision (progname, NP_VERSION); - exit (STATE_UNKNOWN); - case 'h': /* help */ - print_help (); - exit (STATE_UNKNOWN); + case 'V': /* version */ + print_revision(progname, NP_VERSION); + exit(STATE_UNKNOWN); + case 'h': /* help */ + print_help(); + exit(STATE_UNKNOWN); case 'n': n_procs_to_show = atoi(optarg); break; - case '?': /* help */ - usage5 (); + case '?': /* help */ + usage5(); } } c = optind; - if (c == argc) - return validate_arguments (); + if (c == argc) { + return validate_arguments(); + } /* handle the case if both arguments are missing, * but not if only one is given without -c or -w flag */ - if(c - argc == 2) { + if (c - argc == 2) { get_threshold(argv[c++], wload); get_threshold(argv[c++], cload); - } - else if(c - argc == 1) { + } else if (c - argc == 1) { get_threshold(argv[c++], cload); } - return validate_arguments (); + return validate_arguments(); } - -static int -validate_arguments (void) -{ +static int validate_arguments(void) { int i = 0; /* match cload first, as it will give the most friendly error message * if user hasn't given the -c switch properly */ - for(i = 0; i < 3; i++) { - if(cload[i] < 0) - die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]); - if(wload[i] < 0) - die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]); - if(wload[i] > cload[i]) - die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]); + for (i = 0; i < 3; i++) { + if (cload[i] < 0) { + die(STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]); + } + if (wload[i] < 0) { + die(STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]); + } + if (wload[i] > cload[i]) { + die(STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]); + } } return OK; } +void print_help(void) { + print_revision(progname, NP_VERSION); -void -print_help (void) -{ - print_revision (progname, NP_VERSION); - - printf ("Copyright (c) 1999 Felipe Gustavo de Almeida \n"); - printf (COPYRIGHT, copyright, email); + printf("Copyright (c) 1999 Felipe Gustavo de Almeida \n"); + printf(COPYRIGHT, copyright, email); - printf (_("This plugin tests the current system load average.")); + printf(_("This plugin tests the current system load average.")); - printf ("\n\n"); + printf("\n\n"); - print_usage (); + print_usage(); - printf (UT_HELP_VRSN); - printf (UT_EXTRA_OPTS); + printf(UT_HELP_VRSN); + printf(UT_EXTRA_OPTS); - printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15"); - printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn")); - printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15"); - printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn")); - printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); - printf (" %s\n", "-r, --percpu"); - printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)")); - printf (" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS"); - printf (" %s\n", _("Number of processes to show when printing the top consuming processes.")); - printf (" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0")); + printf(" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15"); + printf(" %s\n", _("Exit with WARNING status if load average exceeds WLOADn")); + printf(" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15"); + printf(" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn")); + printf(" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); + printf(" %s\n", "-r, --percpu"); + printf(" %s\n", _("Divide the load averages by the number of CPUs (when possible)")); + printf(" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS"); + printf(" %s\n", _("Number of processes to show when printing the top consuming processes.")); + printf(" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0")); - printf (UT_SUPPORT); + printf(UT_SUPPORT); } -void -print_usage (void) -{ - printf ("%s\n", _("Usage:")); - printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname); +void print_usage(void) { + printf("%s\n", _("Usage:")); + printf("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname); } #ifdef PS_USES_PROCPCPU @@ -356,14 +351,14 @@ int cmpstringp(const void *p1, const void *p2) { int procrss = 0; float procpcpu = 0; char procstat[8]; -#ifdef PS_USES_PROCETIME +# ifdef PS_USES_PROCETIME char procetime[MAX_INPUT_BUFFER]; -#endif /* PS_USES_PROCETIME */ +# endif /* PS_USES_PROCETIME */ char procprog[MAX_INPUT_BUFFER]; int pos; - sscanf (* (char * const *) p1, PS_FORMAT, PS_VARLIST); + sscanf(*(char *const *)p1, PS_FORMAT, PS_VARLIST); float procpcpu1 = procpcpu; - sscanf (* (char * const *) p2, PS_FORMAT, PS_VARLIST); + sscanf(*(char *const *)p2, PS_FORMAT, PS_VARLIST); return procpcpu1 < procpcpu; } #endif /* PS_USES_PROCPCPU */ @@ -371,7 +366,7 @@ int cmpstringp(const void *p1, const void *p2) { static int print_top_consuming_processes() { int i = 0; struct output chld_out, chld_err; - if(np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0){ + if (np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0) { fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND); return STATE_UNKNOWN; } @@ -380,10 +375,9 @@ static int print_top_consuming_processes() { return STATE_UNKNOWN; } #ifdef PS_USES_PROCPCPU - qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char*), cmpstringp); + qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char *), cmpstringp); #endif /* PS_USES_PROCPCPU */ - int lines_to_show = chld_out.lines < (size_t)(n_procs_to_show + 1) - ? (int)chld_out.lines : n_procs_to_show + 1; + int lines_to_show = chld_out.lines < (size_t)(n_procs_to_show + 1) ? (int)chld_out.lines : n_procs_to_show + 1; for (i = 0; i < lines_to_show; i += 1) { printf("%s\n", chld_out.line[i]); } -- cgit v1.2.3-74-g34f1 From 08a475a14fff2d0eee2e49f4765cb286cac8d2e2 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 13 Mar 2025 23:41:12 +0100 Subject: Refactor check_load + new ouput --- plugins/Makefile.am | 1 + plugins/check_load.c | 420 ++++++++++++++++++++++++------------------ plugins/check_load.d/config.h | 30 +++ 3 files changed, 274 insertions(+), 177 deletions(-) create mode 100644 plugins/check_load.d/config.h (limited to 'plugins/check_load.c') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 30ca63d1..f395e594 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -56,6 +56,7 @@ EXTRA_DIST = t \ check_game.d \ check_radius.d \ check_time.d \ + check_load.d \ check_nagios.d \ check_dbi.d \ check_tcp.d \ diff --git a/plugins/check_load.c b/plugins/check_load.c index e3a45f58..57be8c69 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -28,6 +28,9 @@ * *****************************************************************************/ +#include "output.h" +#include "perfdata.h" +#include "thresholds.h" const char *progname = "check_load"; const char *copyright = "1999-2022"; const char *email = "devel@monitoring-plugins.org"; @@ -36,6 +39,10 @@ const char *email = "devel@monitoring-plugins.org"; #include "./runcmd.h" #include "./utils.h" #include "./popen.h" +#include "states.h" +#include "check_load.d/config.h" + +#include "../gl/stdlib.h" #include @@ -50,70 +57,68 @@ const char *email = "devel@monitoring-plugins.org"; # define LOADAVG_15MIN 2 #endif /* !defined LOADAVG_1MIN */ -static int process_arguments(int argc, char **argv); -static int validate_arguments(void); +typedef struct { + int errorcode; + check_load_config config; +} check_load_config_wrapper; +static check_load_config_wrapper process_arguments(int argc, char **argv); +static check_load_config_wrapper validate_arguments(check_load_config_wrapper /*config_wrapper*/); + void print_help(void); void print_usage(void); -static int print_top_consuming_processes(); - -static int n_procs_to_show = 0; - -/* strictly for pretty-print usage in loops */ -static const int nums[3] = {1, 5, 15}; - -/* provide some fairly sane defaults */ -double wload[3] = {0.0, 0.0, 0.0}; -double cload[3] = {0.0, 0.0, 0.0}; -#define la1 la[0] -#define la5 la[1] -#define la15 la[2] - -char *status_line; -bool take_into_account_cpus = false; - -static void get_threshold(char *arg, double *th) { - size_t i, n; - int valid = 0; - char *str = arg, *p; - - n = strlen(arg); - for (i = 0; i < 3; i++) { - th[i] = strtod(str, &p); - if (p == str) { +typedef struct { + int errorcode; + char **top_processes; +} top_processes_result; +static top_processes_result print_top_consuming_processes(int /*n_procs_to_show*/); + +typedef struct { + mp_range load[3]; +} parsed_thresholds; +static parsed_thresholds get_threshold(char *arg) { + size_t index; + char *str = arg; + char *tmp_pointer; + bool valid = false; + + parsed_thresholds result = { + .load[0] = mp_range_init(), + .load[1] = mp_range_init(), + .load[2] = mp_range_init(), + }; + + size_t arg_length = strlen(arg); + for (index = 0; index < 3; index++) { + double tmp = strtod(str, &tmp_pointer); + if (tmp_pointer == str) { break; } - valid = 1; - str = p + 1; - if (n <= (size_t)(str - arg)) { + result.load[index] = mp_range_set_end(result.load[index], mp_create_pd_value(tmp)); + + valid = true; + str = tmp_pointer + 1; + if (arg_length <= (size_t)(str - arg)) { break; } } /* empty argument or non-floatish, so warn about it and die */ - if (!i && !valid) { + if (!index && !valid) { usage(_("Warning threshold must be float or float triplet!\n")); } - if (i != 2) { + if (index != 2) { /* one or more numbers were given, so fill array with last * we got (most likely to NOT produce the least expected result) */ - for (n = i; n < 3; n++) { - th[n] = th[i]; + for (size_t tmp_index = index; tmp_index < 3; tmp_index++) { + result.load[tmp_index] = result.load[index]; } } + return result; } int main(int argc, char **argv) { - int result = -1; - int i; - long numcpus; - - double la[3] = {0.0, 0.0, 0.0}; /* NetBSD complains about uninitialized arrays */ -#ifndef HAVE_GETLOADAVG - char input_buffer[MAX_INPUT_BUFFER]; -#endif - setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); @@ -122,112 +127,138 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) { + check_load_config_wrapper tmp_config = process_arguments(argc, argv); + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } -#ifdef HAVE_GETLOADAVG - result = getloadavg(la, 3); - if (result != 3) { - return STATE_UNKNOWN; - } -#else - child_process = spopen(PATH_TO_UPTIME); - if (child_process == NULL) { - printf(_("Error opening %s\n"), PATH_TO_UPTIME); - return STATE_UNKNOWN; - } - child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); - if (child_stderr == NULL) { - printf(_("Could not open stderr for %s\n"), PATH_TO_UPTIME); - } - fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); - if (strstr(input_buffer, "load average:")) { - sscanf(input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15); - } else if (strstr(input_buffer, "load averages:")) { - sscanf(input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15); - } else { - printf(_("could not parse load from uptime %s: %d\n"), PATH_TO_UPTIME, result); - return STATE_UNKNOWN; - } + const check_load_config config = tmp_config.config; - result = spclose(child_process); - if (result) { - printf(_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME); - return STATE_UNKNOWN; - } -#endif + double load_values[3] = {0, 0, 0}; - if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) { -#ifdef HAVE_GETLOADAVG - printf(_("Error in getloadavg()\n")); -#else - printf(_("Error processing %s\n"), PATH_TO_UPTIME); -#endif - return STATE_UNKNOWN; + int error = getloadavg(load_values, 3); + if (error != 3) { + die(STATE_UNKNOWN, _("Failed to retrieve load values")); } - /* we got this far, so assume OK until we've measured */ - result = STATE_OK; - - xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15); - xasprintf(&status_line, ("total %s"), status_line); + mp_check overall = mp_check_init(); + if (config.output_format_set) { + mp_set_format(config.output_format); + } - double scaled_la[3] = {0.0, 0.0, 0.0}; bool is_using_scaled_load_values = false; - - if (take_into_account_cpus == true && (numcpus = GET_NUMBER_OF_CPUS()) > 0) { + long numcpus; + if (config.take_into_account_cpus && ((numcpus = GET_NUMBER_OF_CPUS()) > 0)) { is_using_scaled_load_values = true; - scaled_la[0] = la[0] / numcpus; - scaled_la[1] = la[1] / numcpus; - scaled_la[2] = la[2] / numcpus; + double scaled_la[3] = {0.0, 0.0, 0.0}; + scaled_la[0] = load_values[0] / numcpus; + scaled_la[1] = load_values[1] / numcpus; + scaled_la[2] = load_values[2] / numcpus; + + mp_subcheck scaled_load_sc = mp_subcheck_init(); + scaled_load_sc = mp_set_subcheck_default_state(scaled_load_sc, STATE_OK); + scaled_load_sc.output = "Scaled Load (divided by number of CPUs"; + + mp_perfdata pd_scaled_load1 = perfdata_init(); + pd_scaled_load1.label = "scaled_load1"; + pd_scaled_load1 = mp_set_pd_value(pd_scaled_load1, scaled_la[0]); + pd_scaled_load1 = mp_pd_set_thresholds(pd_scaled_load1, config.th_load[0]); + + mp_subcheck scaled_load_sc1 = mp_subcheck_init(); + scaled_load_sc1 = mp_set_subcheck_state(scaled_load_sc1, mp_get_pd_status(pd_scaled_load1)); + mp_add_perfdata_to_subcheck(&scaled_load_sc1, pd_scaled_load1); + xasprintf(&scaled_load_sc1.output, "1 Minute: %s", pd_value_to_string(pd_scaled_load1.value)); + mp_add_subcheck_to_subcheck(&scaled_load_sc, scaled_load_sc1); + + mp_perfdata pd_scaled_load5 = perfdata_init(); + pd_scaled_load5.label = "scaled_load5"; + pd_scaled_load5 = mp_set_pd_value(pd_scaled_load5, scaled_la[1]); + pd_scaled_load5 = mp_pd_set_thresholds(pd_scaled_load5, config.th_load[1]); + + mp_subcheck scaled_load_sc5 = mp_subcheck_init(); + scaled_load_sc5 = mp_set_subcheck_state(scaled_load_sc5, mp_get_pd_status(pd_scaled_load5)); + mp_add_perfdata_to_subcheck(&scaled_load_sc5, pd_scaled_load5); + xasprintf(&scaled_load_sc5.output, "5 Minutes: %s", pd_value_to_string(pd_scaled_load5.value)); + mp_add_subcheck_to_subcheck(&scaled_load_sc, scaled_load_sc5); + + mp_perfdata pd_scaled_load15 = perfdata_init(); + pd_scaled_load15.label = "scaled_load15"; + pd_scaled_load15 = mp_set_pd_value(pd_scaled_load15, scaled_la[2]); + pd_scaled_load15 = mp_pd_set_thresholds(pd_scaled_load15, config.th_load[2]); + + mp_subcheck scaled_load_sc15 = mp_subcheck_init(); + scaled_load_sc15 = mp_set_subcheck_state(scaled_load_sc15, mp_get_pd_status(pd_scaled_load15)); + mp_add_perfdata_to_subcheck(&scaled_load_sc15, pd_scaled_load15); + xasprintf(&scaled_load_sc15.output, "15 Minutes: %s", pd_value_to_string(pd_scaled_load15.value)); + mp_add_subcheck_to_subcheck(&scaled_load_sc, scaled_load_sc15); + + mp_add_subcheck_to_check(&overall, scaled_load_sc); + } + + mp_subcheck load_sc = mp_subcheck_init(); + load_sc = mp_set_subcheck_default_state(load_sc, STATE_OK); + load_sc.output = "Total Load"; - char *tmp = NULL; - xasprintf(&tmp, _("load average: %.2f, %.2f, %.2f"), scaled_la[0], scaled_la[1], scaled_la[2]); - xasprintf(&status_line, "scaled %s - %s", tmp, status_line); + mp_perfdata pd_load1 = perfdata_init(); + pd_load1.label = "load1"; + pd_load1 = mp_set_pd_value(pd_load1, load_values[0]); + if (!is_using_scaled_load_values) { + pd_load1 = mp_pd_set_thresholds(pd_load1, config.th_load[0]); } - for (i = 0; i < 3; i++) { - if (is_using_scaled_load_values) { - if (scaled_la[i] > cload[i]) { - result = STATE_CRITICAL; - break; - } else if (scaled_la[i] > wload[i]) { - result = STATE_WARNING; - } - } else { - if (la[i] > cload[i]) { - result = STATE_CRITICAL; - break; - } else if (la[i] > wload[i]) { - result = STATE_WARNING; - } - } + mp_subcheck load_sc1 = mp_subcheck_init(); + load_sc1 = mp_set_subcheck_state(load_sc1, mp_get_pd_status(pd_load1)); + mp_add_perfdata_to_subcheck(&load_sc1, pd_load1); + xasprintf(&load_sc1.output, "1 Minute: %s", pd_value_to_string(pd_load1.value)); + mp_add_subcheck_to_subcheck(&load_sc, load_sc1); + + mp_perfdata pd_load5 = perfdata_init(); + pd_load5.label = "load5"; + pd_load5 = mp_set_pd_value(pd_load5, load_values[1]); + if (!is_using_scaled_load_values) { + pd_load5 = mp_pd_set_thresholds(pd_load5, config.th_load[1]); } - printf("LOAD %s - %s|", state_text(result), status_line); - for (i = 0; i < 3; i++) { - if (is_using_scaled_load_values) { - printf("load%d=%.3f;;;0; ", nums[i], la[i]); - printf("scaled_load%d=%.3f;%.3f;%.3f;0; ", nums[i], scaled_la[i], wload[i], cload[i]); - } else { - printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); - } + mp_subcheck load_sc5 = mp_subcheck_init(); + load_sc5 = mp_set_subcheck_state(load_sc5, mp_get_pd_status(pd_load5)); + mp_add_perfdata_to_subcheck(&load_sc5, pd_load5); + xasprintf(&load_sc5.output, "5 Minutes: %s", pd_value_to_string(pd_load5.value)); + mp_add_subcheck_to_subcheck(&load_sc, load_sc5); + + mp_perfdata pd_load15 = perfdata_init(); + pd_load15.label = "load15"; + pd_load15 = mp_set_pd_value(pd_load15, load_values[2]); + if (!is_using_scaled_load_values) { + pd_load15 = mp_pd_set_thresholds(pd_load15, config.th_load[2]); } - putchar('\n'); - if (n_procs_to_show > 0) { - print_top_consuming_processes(); + mp_subcheck load_sc15 = mp_subcheck_init(); + load_sc15 = mp_set_subcheck_state(load_sc15, mp_get_pd_status(pd_load15)); + mp_add_perfdata_to_subcheck(&load_sc15, pd_load15); + xasprintf(&load_sc15.output, "15 Minutes: %s", pd_value_to_string(pd_load15.value)); + mp_add_subcheck_to_subcheck(&load_sc, load_sc15); + + mp_add_subcheck_to_check(&overall, load_sc); + + if (config.n_procs_to_show > 0) { + mp_subcheck top_proc_sc = mp_subcheck_init(); + top_proc_sc = mp_set_subcheck_state(top_proc_sc, STATE_OK); + top_processes_result top_proc = print_top_consuming_processes(config.n_procs_to_show); + top_proc_sc.output = ""; + + if (top_proc.errorcode == OK) { + for (int i = 0; i < config.n_procs_to_show; i++) { + xasprintf(&top_proc_sc.output, "%s\n%s", top_proc_sc.output, top_proc.top_processes[i]); + } + } } - return result; + + mp_exit(overall); } /* process command-line arguments */ -static int process_arguments(int argc, char **argv) { - int c = 0; - - int option = 0; +static check_load_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"percpu", no_argument, 0, 'r'}, @@ -236,26 +267,45 @@ static int process_arguments(int argc, char **argv) { {"procs-to-show", required_argument, 0, 'n'}, {0, 0, 0, 0}}; + check_load_config_wrapper result = { + .errorcode = OK, + .config = check_load_config_init(), + }; + if (argc < 2) { - return ERROR; + result.errorcode = ERROR; + return result; } - while (1) { - c = getopt_long(argc, argv, "Vhrc:w:n:", longopts, &option); + while (true) { + int option = 0; + int option_index = getopt_long(argc, argv, "Vhrc:w:n:", longopts, &option); - if (c == -1 || c == EOF) { + if (option_index == -1 || option_index == EOF) { break; } - switch (c) { - case 'w': /* warning time threshold */ - get_threshold(optarg, wload); - break; - case 'c': /* critical time threshold */ - get_threshold(optarg, cload); - break; + switch (option_index) { + case 'w': /* warning time threshold */ { + parsed_thresholds warning_range = get_threshold(optarg); + result.config.th_load[0].warning = warning_range.load[0]; + result.config.th_load[0].warning_is_set = true; + result.config.th_load[1].warning = warning_range.load[1]; + result.config.th_load[1].warning_is_set = true; + result.config.th_load[2].warning = warning_range.load[2]; + result.config.th_load[2].warning_is_set = true; + } break; + case 'c': /* critical time threshold */ { + parsed_thresholds critical_range = get_threshold(optarg); + result.config.th_load[0].critical = critical_range.load[0]; + result.config.th_load[0].critical_is_set = true; + result.config.th_load[1].critical = critical_range.load[1]; + result.config.th_load[1].critical_is_set = true; + result.config.th_load[2].critical = critical_range.load[2]; + result.config.th_load[2].critical_is_set = true; + } break; case 'r': /* Divide load average by number of CPUs */ - take_into_account_cpus = true; + result.config.take_into_account_cpus = true; break; case 'V': /* version */ print_revision(progname, NP_VERSION); @@ -264,49 +314,49 @@ static int process_arguments(int argc, char **argv) { print_help(); exit(STATE_UNKNOWN); case 'n': - n_procs_to_show = atoi(optarg); + result.config.n_procs_to_show = atoi(optarg); break; case '?': /* help */ usage5(); } } - c = optind; - if (c == argc) { - return validate_arguments(); + int index = optind; + if (index == argc) { + return validate_arguments(result); } /* handle the case if both arguments are missing, * but not if only one is given without -c or -w flag */ - if (c - argc == 2) { - get_threshold(argv[c++], wload); - get_threshold(argv[c++], cload); - } else if (c - argc == 1) { - get_threshold(argv[c++], cload); + if (index - argc == 2) { + parsed_thresholds warning_range = get_threshold(argv[index++]); + result.config.th_load[0].warning = warning_range.load[0]; + result.config.th_load[0].warning_is_set = true; + result.config.th_load[1].warning = warning_range.load[1]; + result.config.th_load[1].warning_is_set = true; + result.config.th_load[2].warning = warning_range.load[2]; + result.config.th_load[2].warning_is_set = true; + parsed_thresholds critical_range = get_threshold(argv[index++]); + result.config.th_load[0].critical = critical_range.load[0]; + result.config.th_load[0].critical_is_set = true; + result.config.th_load[1].critical = critical_range.load[1]; + result.config.th_load[1].critical_is_set = true; + result.config.th_load[2].critical = critical_range.load[2]; + result.config.th_load[2].critical_is_set = true; + } else if (index - argc == 1) { + parsed_thresholds critical_range = get_threshold(argv[index++]); + result.config.th_load[0].critical = critical_range.load[0]; + result.config.th_load[0].critical_is_set = true; + result.config.th_load[1].critical = critical_range.load[1]; + result.config.th_load[1].critical_is_set = true; + result.config.th_load[2].critical = critical_range.load[2]; + result.config.th_load[2].critical_is_set = true; } - return validate_arguments(); + return validate_arguments(result); } -static int validate_arguments(void) { - int i = 0; - - /* match cload first, as it will give the most friendly error message - * if user hasn't given the -c switch properly */ - for (i = 0; i < 3; i++) { - if (cload[i] < 0) { - die(STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]); - } - if (wload[i] < 0) { - die(STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]); - } - if (wload[i] > cload[i]) { - die(STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]); - } - } - - return OK; -} +static check_load_config_wrapper validate_arguments(check_load_config_wrapper config_wrapper) { return config_wrapper; } void print_help(void) { print_revision(progname, NP_VERSION); @@ -363,23 +413,39 @@ int cmpstringp(const void *p1, const void *p2) { } #endif /* PS_USES_PROCPCPU */ -static int print_top_consuming_processes() { - int i = 0; - struct output chld_out, chld_err; +static top_processes_result print_top_consuming_processes(int n_procs_to_show) { + top_processes_result result = { + .errorcode = OK, + }; + struct output chld_out; + struct output chld_err; if (np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0) { fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND); - return STATE_UNKNOWN; + result.errorcode = ERROR; + return result; } + if (chld_out.lines < 2) { fprintf(stderr, _("some error occurred getting procs list.\n")); - return STATE_UNKNOWN; + result.errorcode = ERROR; + return result; } + #ifdef PS_USES_PROCPCPU qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char *), cmpstringp); #endif /* PS_USES_PROCPCPU */ int lines_to_show = chld_out.lines < (size_t)(n_procs_to_show + 1) ? (int)chld_out.lines : n_procs_to_show + 1; - for (i = 0; i < lines_to_show; i += 1) { - printf("%s\n", chld_out.line[i]); + + result.top_processes = calloc(lines_to_show, sizeof(char *)); + if (result.top_processes == NULL) { + // Failed allocation + result.errorcode = ERROR; + return result; } - return OK; + + for (int i = 0; i < lines_to_show; i += 1) { + xasprintf(&result.top_processes[i], "%s", chld_out.line[i]); + } + + return result; } diff --git a/plugins/check_load.d/config.h b/plugins/check_load.d/config.h new file mode 100644 index 00000000..d399c19c --- /dev/null +++ b/plugins/check_load.d/config.h @@ -0,0 +1,30 @@ +#pragma once + +#include "output.h" +#include "thresholds.h" +typedef struct { + mp_thresholds th_load[3]; + + bool take_into_account_cpus; + int n_procs_to_show; + + mp_output_format output_format; + bool output_format_set; +} check_load_config; + +check_load_config check_load_config_init() { + check_load_config tmp = { + .th_load = + { + mp_thresholds_init(), + mp_thresholds_init(), + mp_thresholds_init(), + }, + + .take_into_account_cpus = false, + .n_procs_to_show = 0, + + .output_format_set = false, + }; + return tmp; +} -- cgit v1.2.3-74-g34f1 From fedff97c96ec7f7866fa15bd38b9a349cf260cad Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 13 Mar 2025 23:51:51 +0100 Subject: improved includes and some comments --- plugins/check_load.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'plugins/check_load.c') diff --git a/plugins/check_load.c b/plugins/check_load.c index 57be8c69..5d3e6237 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -28,28 +28,24 @@ * *****************************************************************************/ -#include "output.h" -#include "perfdata.h" -#include "thresholds.h" const char *progname = "check_load"; const char *copyright = "1999-2022"; const char *email = "devel@monitoring-plugins.org"; #include "./common.h" +#include #include "./runcmd.h" #include "./utils.h" #include "./popen.h" -#include "states.h" +#include "../lib/states.h" +#include "../lib/output.h" +#include "../lib/perfdata.h" +#include "../lib/thresholds.h" #include "check_load.d/config.h" +// getloadavg comes from gnulib #include "../gl/stdlib.h" -#include - -#ifdef HAVE_SYS_LOADAVG_H -# include -#endif - /* needed for compilation under NetBSD, as suggested by Andy Doran */ #ifndef LOADAVG_1MIN # define LOADAVG_1MIN 0 @@ -136,6 +132,7 @@ int main(int argc, char **argv) { double load_values[3] = {0, 0, 0}; + // this should be getloadavg from gnulib, should work everywhere™ int error = getloadavg(load_values, 3); if (error != 3) { die(STATE_UNKNOWN, _("Failed to retrieve load values")); -- cgit v1.2.3-74-g34f1 From 5eaeadbb3a50e254fb465bf22c6022de9ecbab2d Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 14 Mar 2025 00:04:02 +0100 Subject: check_load: Actually allow output format configuring --- plugins/check_load.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'plugins/check_load.c') diff --git a/plugins/check_load.c b/plugins/check_load.c index 5d3e6237..bf3c89d8 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -256,12 +256,18 @@ int main(int argc, char **argv) { /* process command-line arguments */ static check_load_config_wrapper process_arguments(int argc, char **argv) { + + enum { + output_format_index = CHAR_MAX + 1, + }; + static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"percpu", no_argument, 0, 'r'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"procs-to-show", required_argument, 0, 'n'}, + {"output-format", required_argument, 0, output_format_index}, {0, 0, 0, 0}}; check_load_config_wrapper result = { @@ -283,6 +289,18 @@ static check_load_config_wrapper process_arguments(int argc, char **argv) { } switch (option_index) { + case output_format_index: { + parsed_output_format parser = mp_parse_output_format(optarg); + if (!parser.parsing_success) { + // TODO List all available formats here, maybe add anothoer usage function + printf("Invalid output format: %s\n", optarg); + exit(STATE_UNKNOWN); + } + + result.config.output_format_set = true; + result.config.output_format = parser.output_format; + break; + } case 'w': /* warning time threshold */ { parsed_thresholds warning_range = get_threshold(optarg); result.config.th_load[0].warning = warning_range.load[0]; @@ -381,6 +399,7 @@ void print_help(void) { printf(" %s\n", _("Number of processes to show when printing the top consuming processes.")); printf(" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0")); + printf(UT_OUTPUT_FORMAT); printf(UT_SUPPORT); } -- cgit v1.2.3-74-g34f1 From 4ef8af0d9a2acb2e199b613af2d85af86fed24b2 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 6 Jul 2025 21:46:54 +0200 Subject: check_load: remove useless code and do some formatting --- plugins/check_load.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'plugins/check_load.c') diff --git a/plugins/check_load.c b/plugins/check_load.c index bf3c89d8..f7e80fdb 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -58,7 +58,6 @@ typedef struct { check_load_config config; } check_load_config_wrapper; static check_load_config_wrapper process_arguments(int argc, char **argv); -static check_load_config_wrapper validate_arguments(check_load_config_wrapper /*config_wrapper*/); void print_help(void); void print_usage(void); @@ -78,9 +77,12 @@ static parsed_thresholds get_threshold(char *arg) { bool valid = false; parsed_thresholds result = { - .load[0] = mp_range_init(), - .load[1] = mp_range_init(), - .load[2] = mp_range_init(), + .load = + { + mp_range_init(), + mp_range_init(), + mp_range_init(), + }, }; size_t arg_length = strlen(arg); @@ -148,10 +150,11 @@ int main(int argc, char **argv) { if (config.take_into_account_cpus && ((numcpus = GET_NUMBER_OF_CPUS()) > 0)) { is_using_scaled_load_values = true; - double scaled_la[3] = {0.0, 0.0, 0.0}; - scaled_la[0] = load_values[0] / numcpus; - scaled_la[1] = load_values[1] / numcpus; - scaled_la[2] = load_values[2] / numcpus; + double scaled_la[3] = { + load_values[0] / numcpus, + load_values[1] / numcpus, + load_values[2] / numcpus, + }; mp_subcheck scaled_load_sc = mp_subcheck_init(); scaled_load_sc = mp_set_subcheck_default_state(scaled_load_sc, STATE_OK); @@ -292,7 +295,6 @@ static check_load_config_wrapper process_arguments(int argc, char **argv) { case output_format_index: { parsed_output_format parser = mp_parse_output_format(optarg); if (!parser.parsing_success) { - // TODO List all available formats here, maybe add anothoer usage function printf("Invalid output format: %s\n", optarg); exit(STATE_UNKNOWN); } @@ -305,8 +307,10 @@ static check_load_config_wrapper process_arguments(int argc, char **argv) { parsed_thresholds warning_range = get_threshold(optarg); result.config.th_load[0].warning = warning_range.load[0]; result.config.th_load[0].warning_is_set = true; + result.config.th_load[1].warning = warning_range.load[1]; result.config.th_load[1].warning_is_set = true; + result.config.th_load[2].warning = warning_range.load[2]; result.config.th_load[2].warning_is_set = true; } break; @@ -314,8 +318,10 @@ static check_load_config_wrapper process_arguments(int argc, char **argv) { parsed_thresholds critical_range = get_threshold(optarg); result.config.th_load[0].critical = critical_range.load[0]; result.config.th_load[0].critical_is_set = true; + result.config.th_load[1].critical = critical_range.load[1]; result.config.th_load[1].critical_is_set = true; + result.config.th_load[2].critical = critical_range.load[2]; result.config.th_load[2].critical_is_set = true; } break; @@ -338,7 +344,7 @@ static check_load_config_wrapper process_arguments(int argc, char **argv) { int index = optind; if (index == argc) { - return validate_arguments(result); + return result; } /* handle the case if both arguments are missing, @@ -347,32 +353,36 @@ static check_load_config_wrapper process_arguments(int argc, char **argv) { parsed_thresholds warning_range = get_threshold(argv[index++]); result.config.th_load[0].warning = warning_range.load[0]; result.config.th_load[0].warning_is_set = true; + result.config.th_load[1].warning = warning_range.load[1]; result.config.th_load[1].warning_is_set = true; + result.config.th_load[2].warning = warning_range.load[2]; result.config.th_load[2].warning_is_set = true; parsed_thresholds critical_range = get_threshold(argv[index++]); result.config.th_load[0].critical = critical_range.load[0]; result.config.th_load[0].critical_is_set = true; + result.config.th_load[1].critical = critical_range.load[1]; result.config.th_load[1].critical_is_set = true; + result.config.th_load[2].critical = critical_range.load[2]; result.config.th_load[2].critical_is_set = true; } else if (index - argc == 1) { parsed_thresholds critical_range = get_threshold(argv[index++]); result.config.th_load[0].critical = critical_range.load[0]; result.config.th_load[0].critical_is_set = true; + result.config.th_load[1].critical = critical_range.load[1]; result.config.th_load[1].critical_is_set = true; + result.config.th_load[2].critical = critical_range.load[2]; result.config.th_load[2].critical_is_set = true; } - return validate_arguments(result); + return result; } -static check_load_config_wrapper validate_arguments(check_load_config_wrapper config_wrapper) { return config_wrapper; } - void print_help(void) { print_revision(progname, NP_VERSION); -- cgit v1.2.3-74-g34f1 From b002a6870b6ef9333d3234cec58b8c2fc1acfeec Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 6 Jul 2025 21:58:55 +0200 Subject: check_load: Add top x functionality to output --- plugins/check_load.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'plugins/check_load.c') diff --git a/plugins/check_load.c b/plugins/check_load.c index f7e80fdb..78daa945 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -245,13 +245,15 @@ int main(int argc, char **argv) { mp_subcheck top_proc_sc = mp_subcheck_init(); top_proc_sc = mp_set_subcheck_state(top_proc_sc, STATE_OK); top_processes_result top_proc = print_top_consuming_processes(config.n_procs_to_show); - top_proc_sc.output = ""; + xasprintf(&top_proc_sc.output, "Top %lu CPU time consuming processes", config.n_procs_to_show); if (top_proc.errorcode == OK) { for (int i = 0; i < config.n_procs_to_show; i++) { xasprintf(&top_proc_sc.output, "%s\n%s", top_proc_sc.output, top_proc.top_processes[i]); } } + + mp_add_subcheck_to_check(&overall, top_proc_sc); } mp_exit(overall); -- cgit v1.2.3-74-g34f1 From 351b104894fd52e634d60d79f573be682a2da8d4 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 6 Jul 2025 21:59:12 +0200 Subject: check_load some number type fixes --- plugins/check_load.c | 12 ++++++------ plugins/check_load.d/config.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'plugins/check_load.c') diff --git a/plugins/check_load.c b/plugins/check_load.c index 78daa945..2925bff3 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -65,7 +65,7 @@ typedef struct { int errorcode; char **top_processes; } top_processes_result; -static top_processes_result print_top_consuming_processes(int /*n_procs_to_show*/); +static top_processes_result print_top_consuming_processes(unsigned long n_procs_to_show); typedef struct { mp_range load[3]; @@ -248,7 +248,7 @@ int main(int argc, char **argv) { xasprintf(&top_proc_sc.output, "Top %lu CPU time consuming processes", config.n_procs_to_show); if (top_proc.errorcode == OK) { - for (int i = 0; i < config.n_procs_to_show; i++) { + for (unsigned long i = 0; i < config.n_procs_to_show; i++) { xasprintf(&top_proc_sc.output, "%s\n%s", top_proc_sc.output, top_proc.top_processes[i]); } } @@ -337,7 +337,7 @@ static check_load_config_wrapper process_arguments(int argc, char **argv) { print_help(); exit(STATE_UNKNOWN); case 'n': - result.config.n_procs_to_show = atoi(optarg); + result.config.n_procs_to_show = (unsigned long)atol(optarg); break; case '?': /* help */ usage5(); @@ -441,7 +441,7 @@ int cmpstringp(const void *p1, const void *p2) { } #endif /* PS_USES_PROCPCPU */ -static top_processes_result print_top_consuming_processes(int n_procs_to_show) { +static top_processes_result print_top_consuming_processes(unsigned long n_procs_to_show) { top_processes_result result = { .errorcode = OK, }; @@ -462,7 +462,7 @@ static top_processes_result print_top_consuming_processes(int n_procs_to_show) { #ifdef PS_USES_PROCPCPU qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char *), cmpstringp); #endif /* PS_USES_PROCPCPU */ - int lines_to_show = chld_out.lines < (size_t)(n_procs_to_show + 1) ? (int)chld_out.lines : n_procs_to_show + 1; + unsigned long lines_to_show = chld_out.lines < (size_t)(n_procs_to_show + 1) ? chld_out.lines : n_procs_to_show + 1; result.top_processes = calloc(lines_to_show, sizeof(char *)); if (result.top_processes == NULL) { @@ -471,7 +471,7 @@ static top_processes_result print_top_consuming_processes(int n_procs_to_show) { return result; } - for (int i = 0; i < lines_to_show; i += 1) { + for (unsigned long i = 0; i < lines_to_show; i += 1) { xasprintf(&result.top_processes[i], "%s", chld_out.line[i]); } diff --git a/plugins/check_load.d/config.h b/plugins/check_load.d/config.h index d399c19c..fd735455 100644 --- a/plugins/check_load.d/config.h +++ b/plugins/check_load.d/config.h @@ -6,7 +6,7 @@ typedef struct { mp_thresholds th_load[3]; bool take_into_account_cpus; - int n_procs_to_show; + unsigned long n_procs_to_show; mp_output_format output_format; bool output_format_set; -- cgit v1.2.3-74-g34f1