diff options
Diffstat (limited to 'plugins/check_load.c')
-rw-r--r-- | plugins/check_load.c | 69 |
1 files changed, 63 insertions, 6 deletions
diff --git a/plugins/check_load.c b/plugins/check_load.c index cde63e56..0e4de54e 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c | |||
@@ -33,6 +33,7 @@ const char *copyright = "1999-2007"; | |||
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 "utils.h" | 37 | #include "utils.h" |
37 | #include "popen.h" | 38 | #include "popen.h" |
38 | 39 | ||
@@ -52,6 +53,9 @@ static int process_arguments (int argc, char **argv); | |||
52 | static int validate_arguments (void); | 53 | static int validate_arguments (void); |
53 | void print_help (void); | 54 | void print_help (void); |
54 | void print_usage (void); | 55 | void print_usage (void); |
56 | static int print_top_consuming_processes(); | ||
57 | |||
58 | static int n_procs_to_show = 0; | ||
55 | 59 | ||
56 | /* strictly for pretty-print usage in loops */ | 60 | /* strictly for pretty-print usage in loops */ |
57 | static const int nums[3] = { 1, 5, 15 }; | 61 | static const int nums[3] = { 1, 5, 15 }; |
@@ -160,7 +164,7 @@ main (int argc, char **argv) | |||
160 | sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15); | 164 | sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15); |
161 | } | 165 | } |
162 | else { | 166 | else { |
163 | printf (_("could not parse load from uptime: %s\n"), result, PATH_TO_UPTIME); | 167 | printf (_("could not parse load from uptime %s: %s\n"), PATH_TO_UPTIME, result); |
164 | return STATE_UNKNOWN; | 168 | return STATE_UNKNOWN; |
165 | } | 169 | } |
166 | 170 | ||
@@ -205,11 +209,14 @@ main (int argc, char **argv) | |||
205 | else if(la[i] > wload[i]) result = STATE_WARNING; | 209 | else if(la[i] > wload[i]) result = STATE_WARNING; |
206 | } | 210 | } |
207 | 211 | ||
208 | printf("%s - %s|", state_text(result), status_line); | 212 | printf("LOAD %s - %s|", state_text(result), status_line); |
209 | for(i = 0; i < 3; i++) | 213 | for(i = 0; i < 3; i++) |
210 | printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); | 214 | printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); |
211 | 215 | ||
212 | putchar('\n'); | 216 | putchar('\n'); |
217 | if (n_procs_to_show > 0) { | ||
218 | print_top_consuming_processes(); | ||
219 | } | ||
213 | return result; | 220 | return result; |
214 | } | 221 | } |
215 | 222 | ||
@@ -227,6 +234,7 @@ process_arguments (int argc, char **argv) | |||
227 | {"percpu", no_argument, 0, 'r'}, | 234 | {"percpu", no_argument, 0, 'r'}, |
228 | {"version", no_argument, 0, 'V'}, | 235 | {"version", no_argument, 0, 'V'}, |
229 | {"help", no_argument, 0, 'h'}, | 236 | {"help", no_argument, 0, 'h'}, |
237 | {"procs-to-show", required_argument, 0, 'n'}, | ||
230 | {0, 0, 0, 0} | 238 | {0, 0, 0, 0} |
231 | }; | 239 | }; |
232 | 240 | ||
@@ -234,7 +242,7 @@ process_arguments (int argc, char **argv) | |||
234 | return ERROR; | 242 | return ERROR; |
235 | 243 | ||
236 | while (1) { | 244 | while (1) { |
237 | c = getopt_long (argc, argv, "Vhrc:w:", longopts, &option); | 245 | c = getopt_long (argc, argv, "Vhrc:w:n:", longopts, &option); |
238 | 246 | ||
239 | if (c == -1 || c == EOF) | 247 | if (c == -1 || c == EOF) |
240 | break; | 248 | break; |
@@ -251,10 +259,13 @@ process_arguments (int argc, char **argv) | |||
251 | break; | 259 | break; |
252 | case 'V': /* version */ | 260 | case 'V': /* version */ |
253 | print_revision (progname, NP_VERSION); | 261 | print_revision (progname, NP_VERSION); |
254 | exit (STATE_OK); | 262 | exit (STATE_UNKNOWN); |
255 | case 'h': /* help */ | 263 | case 'h': /* help */ |
256 | print_help (); | 264 | print_help (); |
257 | exit (STATE_OK); | 265 | exit (STATE_UNKNOWN); |
266 | case 'n': | ||
267 | n_procs_to_show = atoi(optarg); | ||
268 | break; | ||
258 | case '?': /* help */ | 269 | case '?': /* help */ |
259 | usage5 (); | 270 | usage5 (); |
260 | } | 271 | } |
@@ -324,6 +335,9 @@ print_help (void) | |||
324 | printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); | 335 | printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); |
325 | printf (" %s\n", "-r, --percpu"); | 336 | printf (" %s\n", "-r, --percpu"); |
326 | printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)")); | 337 | 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"); | ||
339 | 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")); | ||
327 | 341 | ||
328 | printf (UT_SUPPORT); | 342 | printf (UT_SUPPORT); |
329 | } | 343 | } |
@@ -332,5 +346,48 @@ void | |||
332 | print_usage (void) | 346 | print_usage (void) |
333 | { | 347 | { |
334 | printf ("%s\n", _("Usage:")); | 348 | printf ("%s\n", _("Usage:")); |
335 | printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname); | 349 | printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname); |
350 | } | ||
351 | |||
352 | #ifdef PS_USES_PROCPCPU | ||
353 | int cmpstringp(const void *p1, const void *p2) { | ||
354 | int procuid = 0; | ||
355 | int procpid = 0; | ||
356 | int procppid = 0; | ||
357 | int procvsz = 0; | ||
358 | int procrss = 0; | ||
359 | float procpcpu = 0; | ||
360 | char procstat[8]; | ||
361 | #ifdef PS_USES_PROCETIME | ||
362 | char procetime[MAX_INPUT_BUFFER]; | ||
363 | #endif /* PS_USES_PROCETIME */ | ||
364 | char procprog[MAX_INPUT_BUFFER]; | ||
365 | int pos; | ||
366 | sscanf (* (char * const *) p1, PS_FORMAT, PS_VARLIST); | ||
367 | float procpcpu1 = procpcpu; | ||
368 | sscanf (* (char * const *) p2, PS_FORMAT, PS_VARLIST); | ||
369 | return procpcpu1 < procpcpu; | ||
370 | } | ||
371 | #endif /* PS_USES_PROCPCPU */ | ||
372 | |||
373 | static int print_top_consuming_processes() { | ||
374 | int i = 0; | ||
375 | struct output chld_out, chld_err; | ||
376 | if(np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0){ | ||
377 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND); | ||
378 | return STATE_UNKNOWN; | ||
379 | } | ||
380 | if (chld_out.lines < 2) { | ||
381 | fprintf(stderr, _("some error occurred getting procs list.\n")); | ||
382 | return STATE_UNKNOWN; | ||
383 | } | ||
384 | #ifdef PS_USES_PROCPCPU | ||
385 | qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char*), cmpstringp); | ||
386 | #endif /* PS_USES_PROCPCPU */ | ||
387 | int lines_to_show = chld_out.lines < (n_procs_to_show + 1) | ||
388 | ? chld_out.lines : n_procs_to_show + 1; | ||
389 | for (i = 0; i < lines_to_show; i += 1) { | ||
390 | printf("%s\n", chld_out.line[i]); | ||
391 | } | ||
392 | return OK; | ||
336 | } | 393 | } |