diff options
Diffstat (limited to 'plugins/check_load.c')
-rw-r--r-- | plugins/check_load.c | 122 |
1 files changed, 73 insertions, 49 deletions
diff --git a/plugins/check_load.c b/plugins/check_load.c index fdfa1da..388714f 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c | |||
@@ -1,41 +1,43 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_load plugin | 3 | * Monitoring check_load plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 1999-2007 Monitoring Plugins Development Team | 6 | * Copyright (c) 1999-2007 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
10 | * This file contains the check_load plugin | 10 | * This file contains the check_load plugin |
11 | * | 11 | * |
12 | * This plugin tests the current system load average. | 12 | * This plugin tests the current system load average. |
13 | * | 13 | * |
14 | * | 14 | * |
15 | * This program is free software: you can redistribute it and/or modify | 15 | * This program is free software: you can redistribute it and/or modify |
16 | * it under the terms of the GNU General Public License as published by | 16 | * it under the terms of the GNU General Public License as published by |
17 | * the Free Software Foundation, either version 3 of the License, or | 17 | * the Free Software Foundation, either version 3 of the License, or |
18 | * (at your option) any later version. | 18 | * (at your option) any later version. |
19 | * | 19 | * |
20 | * This program is distributed in the hope that it will be useful, | 20 | * This program is distributed in the hope that it will be useful, |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | * GNU General Public License for more details. | 23 | * GNU General Public License for more details. |
24 | * | 24 | * |
25 | * You should have received a copy of the GNU General Public License | 25 | * You should have received a copy of the GNU General Public License |
26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | * | 27 | * |
28 | * | 28 | * |
29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
30 | 30 | ||
31 | const char *progname = "check_load"; | 31 | const char *progname = "check_load"; |
32 | const char *copyright = "1999-2007"; | 32 | const char *copyright = "1999-2022"; |
33 | const char *email = "devel@monitoring-plugins.org"; | 33 | const char *email = "devel@monitoring-plugins.org"; |
34 | 34 | ||
35 | #include "common.h" | 35 | #include "./common.h" |
36 | #include "runcmd.h" | 36 | #include "./runcmd.h" |
37 | #include "utils.h" | 37 | #include "./utils.h" |
38 | #include "popen.h" | 38 | #include "./popen.h" |
39 | |||
40 | #include <string.h> | ||
39 | 41 | ||
40 | #ifdef HAVE_SYS_LOADAVG_H | 42 | #ifdef HAVE_SYS_LOADAVG_H |
41 | #include <sys/loadavg.h> | 43 | #include <sys/loadavg.h> |
@@ -68,7 +70,7 @@ double cload[3] = { 0.0, 0.0, 0.0 }; | |||
68 | #define la15 la[2] | 70 | #define la15 la[2] |
69 | 71 | ||
70 | char *status_line; | 72 | char *status_line; |
71 | int take_into_account_cpus = 0; | 73 | bool take_into_account_cpus = false; |
72 | 74 | ||
73 | static void | 75 | static void |
74 | get_threshold(char *arg, double *th) | 76 | get_threshold(char *arg, double *th) |
@@ -101,7 +103,7 @@ get_threshold(char *arg, double *th) | |||
101 | int | 103 | int |
102 | main (int argc, char **argv) | 104 | main (int argc, char **argv) |
103 | { | 105 | { |
104 | int result; | 106 | int result = -1; |
105 | int i; | 107 | int i; |
106 | long numcpus; | 108 | long numcpus; |
107 | 109 | ||
@@ -164,7 +166,7 @@ main (int argc, char **argv) | |||
164 | sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15); | 166 | sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15); |
165 | } | 167 | } |
166 | else { | 168 | else { |
167 | printf (_("could not parse load from uptime %s: %s\n"), PATH_TO_UPTIME, result); | 169 | printf (_("could not parse load from uptime %s: %d\n"), PATH_TO_UPTIME, result); |
168 | return STATE_UNKNOWN; | 170 | return STATE_UNKNOWN; |
169 | } | 171 | } |
170 | 172 | ||
@@ -176,13 +178,6 @@ main (int argc, char **argv) | |||
176 | # endif | 178 | # endif |
177 | #endif | 179 | #endif |
178 | 180 | ||
179 | if (take_into_account_cpus == 1) { | ||
180 | if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) { | ||
181 | la[0] = la[0] / numcpus; | ||
182 | la[1] = la[1] / numcpus; | ||
183 | la[2] = la[2] / numcpus; | ||
184 | } | ||
185 | } | ||
186 | if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) { | 181 | if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) { |
187 | #ifdef HAVE_GETLOADAVG | 182 | #ifdef HAVE_GETLOADAVG |
188 | printf (_("Error in getloadavg()\n")); | 183 | printf (_("Error in getloadavg()\n")); |
@@ -200,18 +195,49 @@ main (int argc, char **argv) | |||
200 | result = STATE_OK; | 195 | result = STATE_OK; |
201 | 196 | ||
202 | xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15); | 197 | xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15); |
198 | xasprintf(&status_line, ("total %s"), status_line); | ||
199 | |||
200 | |||
201 | double scaled_la[3] = { 0.0, 0.0, 0.0 }; | ||
202 | bool is_using_scaled_load_values = false; | ||
203 | |||
204 | if (take_into_account_cpus == true && (numcpus = GET_NUMBER_OF_CPUS()) > 0) { | ||
205 | is_using_scaled_load_values = true; | ||
206 | |||
207 | scaled_la[0] = la[0] / numcpus; | ||
208 | scaled_la[1] = la[1] / numcpus; | ||
209 | scaled_la[2] = la[2] / numcpus; | ||
210 | |||
211 | char *tmp = NULL; | ||
212 | xasprintf(&tmp, _("load average: %.2f, %.2f, %.2f"), scaled_la[0], scaled_la[1], scaled_la[2]); | ||
213 | xasprintf(&status_line, "scaled %s - %s", tmp, status_line); | ||
214 | } | ||
203 | 215 | ||
204 | for(i = 0; i < 3; i++) { | 216 | for(i = 0; i < 3; i++) { |
205 | if(la[i] > cload[i]) { | 217 | if (is_using_scaled_load_values) { |
206 | result = STATE_CRITICAL; | 218 | if(scaled_la[i] > cload[i]) { |
207 | break; | 219 | result = STATE_CRITICAL; |
220 | break; | ||
221 | } | ||
222 | else if(scaled_la[i] > wload[i]) result = STATE_WARNING; | ||
223 | } else { | ||
224 | if(la[i] > cload[i]) { | ||
225 | result = STATE_CRITICAL; | ||
226 | break; | ||
227 | } | ||
228 | else if(la[i] > wload[i]) result = STATE_WARNING; | ||
208 | } | 229 | } |
209 | else if(la[i] > wload[i]) result = STATE_WARNING; | ||
210 | } | 230 | } |
211 | 231 | ||
212 | printf("LOAD %s - %s|", state_text(result), status_line); | 232 | printf("LOAD %s - %s|", state_text(result), status_line); |
213 | for(i = 0; i < 3; i++) | 233 | for(i = 0; i < 3; i++) { |
214 | printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); | 234 | if (is_using_scaled_load_values) { |
235 | printf("load%d=%.3f;;;0; ", nums[i], la[i]); | ||
236 | printf("scaled_load%d=%.3f;%.3f;%.3f;0; ", nums[i], scaled_la[i], wload[i], cload[i]); | ||
237 | } else { | ||
238 | printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); | ||
239 | } | ||
240 | } | ||
215 | 241 | ||
216 | putchar('\n'); | 242 | putchar('\n'); |
217 | if (n_procs_to_show > 0) { | 243 | if (n_procs_to_show > 0) { |
@@ -255,7 +281,7 @@ process_arguments (int argc, char **argv) | |||
255 | get_threshold(optarg, cload); | 281 | get_threshold(optarg, cload); |
256 | break; | 282 | break; |
257 | case 'r': /* Divide load average by number of CPUs */ | 283 | case 'r': /* Divide load average by number of CPUs */ |
258 | take_into_account_cpus = 1; | 284 | take_into_account_cpus = true; |
259 | break; | 285 | break; |
260 | case 'V': /* version */ | 286 | case 'V': /* version */ |
261 | print_revision (progname, NP_VERSION); | 287 | print_revision (progname, NP_VERSION); |
@@ -289,7 +315,6 @@ process_arguments (int argc, char **argv) | |||
289 | } | 315 | } |
290 | 316 | ||
291 | 317 | ||
292 | |||
293 | static int | 318 | static int |
294 | validate_arguments (void) | 319 | validate_arguments (void) |
295 | { | 320 | { |
@@ -310,7 +335,6 @@ validate_arguments (void) | |||
310 | } | 335 | } |
311 | 336 | ||
312 | 337 | ||
313 | |||
314 | void | 338 | void |
315 | print_help (void) | 339 | print_help (void) |
316 | { | 340 | { |
@@ -321,7 +345,7 @@ print_help (void) | |||
321 | 345 | ||
322 | printf (_("This plugin tests the current system load average.")); | 346 | printf (_("This plugin tests the current system load average.")); |
323 | 347 | ||
324 | printf ("\n\n"); | 348 | printf ("\n\n"); |
325 | 349 | ||
326 | print_usage (); | 350 | print_usage (); |
327 | 351 | ||
@@ -329,15 +353,15 @@ print_help (void) | |||
329 | printf (UT_EXTRA_OPTS); | 353 | printf (UT_EXTRA_OPTS); |
330 | 354 | ||
331 | printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15"); | 355 | printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15"); |
332 | printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn")); | 356 | printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn")); |
333 | printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15"); | 357 | printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15"); |
334 | printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn")); | 358 | printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn")); |
335 | printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); | 359 | printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); |
336 | printf (" %s\n", "-r, --percpu"); | 360 | printf (" %s\n", "-r, --percpu"); |
337 | printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)")); | 361 | printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)")); |
338 | printf (" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS"); | 362 | printf (" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS"); |
339 | printf (" %s\n", _("Number of processes to show when printing the top consuming processes.")); | 363 | printf (" %s\n", _("Number of processes to show when printing the top consuming processes.")); |
340 | printf (" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0")); | 364 | printf (" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0")); |
341 | 365 | ||
342 | printf (UT_SUPPORT); | 366 | printf (UT_SUPPORT); |
343 | } | 367 | } |
@@ -345,8 +369,8 @@ print_help (void) | |||
345 | void | 369 | void |
346 | print_usage (void) | 370 | print_usage (void) |
347 | { | 371 | { |
348 | printf ("%s\n", _("Usage:")); | 372 | printf ("%s\n", _("Usage:")); |
349 | printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname); | 373 | printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname); |
350 | } | 374 | } |
351 | 375 | ||
352 | #ifdef PS_USES_PROCPCPU | 376 | #ifdef PS_USES_PROCPCPU |
@@ -384,8 +408,8 @@ static int print_top_consuming_processes() { | |||
384 | #ifdef PS_USES_PROCPCPU | 408 | #ifdef PS_USES_PROCPCPU |
385 | qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char*), cmpstringp); | 409 | qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char*), cmpstringp); |
386 | #endif /* PS_USES_PROCPCPU */ | 410 | #endif /* PS_USES_PROCPCPU */ |
387 | int lines_to_show = chld_out.lines < (n_procs_to_show + 1) | 411 | int lines_to_show = chld_out.lines < (size_t)(n_procs_to_show + 1) |
388 | ? chld_out.lines : n_procs_to_show + 1; | 412 | ? (int)chld_out.lines : n_procs_to_show + 1; |
389 | for (i = 0; i < lines_to_show; i += 1) { | 413 | for (i = 0; i < lines_to_show; i += 1) { |
390 | printf("%s\n", chld_out.line[i]); | 414 | printf("%s\n", chld_out.line[i]); |
391 | } | 415 | } |