From b90a5757f77cdc0434fa3f45cf59c63b9e695d90 Mon Sep 17 00:00:00 2001 From: Lorenz <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 19 Sep 2022 10:23:49 +0200 Subject: Display total and scaled load values if check_load scales the values by number of CPUs (#1778) * Renew copyright * Display more verbose output, if scaled load values are used * Actually use scaled value for determining status and print the fitting perfdata depending on input parameters * Add test cases for scaled mode diff --git a/plugins/check_load.c b/plugins/check_load.c index d1bb30a..00f7c87 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -29,7 +29,7 @@ *****************************************************************************/ const char *progname = "check_load"; -const char *copyright = "1999-2007"; +const char *copyright = "1999-2022"; const char *email = "devel@monitoring-plugins.org"; #include "./common.h" @@ -70,7 +70,7 @@ double cload[3] = { 0.0, 0.0, 0.0 }; #define la15 la[2] char *status_line; -int take_into_account_cpus = 0; +bool take_into_account_cpus = false; static void get_threshold(char *arg, double *th) @@ -178,13 +178,6 @@ main (int argc, char **argv) # endif #endif - if (take_into_account_cpus == 1) { - if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) { - la[0] = la[0] / numcpus; - la[1] = la[1] / numcpus; - la[2] = la[2] / numcpus; - } - } if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) { #ifdef HAVE_GETLOADAVG printf (_("Error in getloadavg()\n")); @@ -202,18 +195,49 @@ main (int argc, char **argv) result = STATE_OK; 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 }; + bool is_using_scaled_load_values = false; + + if (take_into_account_cpus == true && (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; + + 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); + } for(i = 0; i < 3; i++) { - if(la[i] > cload[i]) { - result = STATE_CRITICAL; - break; + 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; } - else if(la[i] > wload[i]) result = STATE_WARNING; } printf("LOAD %s - %s|", state_text(result), status_line); - for(i = 0; i < 3; i++) - printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[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]); + } else { + printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); + } + } putchar('\n'); if (n_procs_to_show > 0) { @@ -257,7 +281,7 @@ process_arguments (int argc, char **argv) get_threshold(optarg, cload); break; case 'r': /* Divide load average by number of CPUs */ - take_into_account_cpus = 1; + take_into_account_cpus = true; break; case 'V': /* version */ print_revision (progname, NP_VERSION); diff --git a/plugins/t/check_load.t b/plugins/t/check_load.t index 60837ef..bba8947 100644 --- a/plugins/t/check_load.t +++ b/plugins/t/check_load.t @@ -11,10 +11,12 @@ use NPTest; my $res; my $loadValue = "[0-9]+\.?[0-9]+"; -my $successOutput = "/^LOAD OK - load average: $loadValue, $loadValue, $loadValue/"; -my $failureOutput = "/^LOAD CRITICAL - load average: $loadValue, $loadValue, $loadValue/"; +my $successOutput = "/^LOAD OK - total load average: $loadValue, $loadValue, $loadValue/"; +my $successScaledOutput = "/^LOAD OK - scaled load average: $loadValue, $loadValue, $loadValue - total load average: $loadValue, $loadValue, $loadValue/"; +my $failureOutput = "/^LOAD CRITICAL - total load average: $loadValue, $loadValue, $loadValue/"; +my $failurScaledOutput = "/^LOAD CRITICAL - scaled load average: $loadValue, $loadValue, $loadValue - total load average: $loadValue, $loadValue, $loadValue/"; -plan tests => 11; +plan tests => 13; $res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100" ); cmp_ok( $res->return_code, 'eq', 0, "load not over 100"); @@ -26,7 +28,7 @@ like( $res->output, $failureOutput, "Output OK"); $res = NPTest->testCmd( "./check_load -r -w 0,0,0 -c 0,0,0" ); cmp_ok( $res->return_code, 'eq', 2, "Load over 0 with per cpu division"); -like( $res->output, $failureOutput, "Output OK"); +like( $res->output, $failurScaledOutput, "Output OK"); $res = NPTest->testCmd( "./check_load -w 100 -c 100,110" ); cmp_ok( $res->return_code, 'eq', 0, "Plugin can handle non-triplet-arguments"); @@ -34,3 +36,8 @@ like( $res->output, $successOutput, "Output OK"); like( $res->perf_output, "/load1=$loadValue;100.000;100.000/", "Test handling of non triplet thresholds (load1)"); like( $res->perf_output, "/load5=$loadValue;100.000;110.000/", "Test handling of non triplet thresholds (load5)"); like( $res->perf_output, "/load15=$loadValue;100.000;110.000/", "Test handling of non triplet thresholds (load15)"); + + +$res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100 -r" ); +cmp_ok( $res->return_code, 'eq', 0, "load not over 100"); +like( $res->output, $successScaledOutput, "Output OK"); -- cgit v0.10-9-g596f