summaryrefslogtreecommitdiffstats
path: root/plugins/check_load.c
diff options
context:
space:
mode:
authorMarc Sánchez <sanchezfauste@gmail.com>2017-09-07 13:31:01 +0200
committerMarc Sánchez <sanchezfauste@gmail.com>2017-09-07 13:31:01 +0200
commit1410ffff281bc82d423459198cb4ea3f393b1e92 (patch)
tree0b1b480a10b9eda539f4c5093d87e6c0c33e85cb /plugins/check_load.c
parent9661ee74885834f7b69ab0874c4e65bed0b871c9 (diff)
downloadmonitoring-plugins-1410ffff281bc82d423459198cb4ea3f393b1e92.tar.gz
Adding print top consuming processes option to check_load
-W, --print-top-warning Print top consuming processes on WARNING status -C, --print-top-critical Print top consuming processes on CRITICAL status -n, --procs-to-show=NUMBER_OF_PROCS Number of processes to show when printing top consuming processes. Not useful without -W or -C. Default value is 5
Diffstat (limited to 'plugins/check_load.c')
-rw-r--r--plugins/check_load.c50
1 files changed, 48 insertions, 2 deletions
diff --git a/plugins/check_load.c b/plugins/check_load.c
index b1cc498f..b6cfc656 100644
--- a/plugins/check_load.c
+++ b/plugins/check_load.c
@@ -33,6 +33,7 @@ const char *copyright = "1999-2007";
33const char *email = "devel@monitoring-plugins.org"; 33const 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,11 @@ static int process_arguments (int argc, char **argv);
52static int validate_arguments (void); 53static int validate_arguments (void);
53void print_help (void); 54void print_help (void);
54void print_usage (void); 55void print_usage (void);
56static int print_top_consuming_processes();
57
58static int n_procs_to_show = 5;
59static int print_top_procs_on_warning = 0;
60static int print_top_procs_on_critical = 0;
55 61
56/* strictly for pretty-print usage in loops */ 62/* strictly for pretty-print usage in loops */
57static const int nums[3] = { 1, 5, 15 }; 63static const int nums[3] = { 1, 5, 15 };
@@ -210,6 +216,11 @@ main (int argc, char **argv)
210 printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); 216 printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
211 217
212 putchar('\n'); 218 putchar('\n');
219 if (result == STATE_CRITICAL && print_top_procs_on_critical) {
220 print_top_consuming_processes();
221 } else if (result == STATE_WARNING && print_top_procs_on_warning) {
222 print_top_consuming_processes();
223 }
213 return result; 224 return result;
214} 225}
215 226
@@ -227,6 +238,9 @@ process_arguments (int argc, char **argv)
227 {"percpu", no_argument, 0, 'r'}, 238 {"percpu", no_argument, 0, 'r'},
228 {"version", no_argument, 0, 'V'}, 239 {"version", no_argument, 0, 'V'},
229 {"help", no_argument, 0, 'h'}, 240 {"help", no_argument, 0, 'h'},
241 {"print-top-warning", no_argument, 0, 'W'},
242 {"print-top-critical", no_argument, 0, 'C'},
243 {"procs-to-show", required_argument, 0, 'n'},
230 {0, 0, 0, 0} 244 {0, 0, 0, 0}
231 }; 245 };
232 246
@@ -234,7 +248,7 @@ process_arguments (int argc, char **argv)
234 return ERROR; 248 return ERROR;
235 249
236 while (1) { 250 while (1) {
237 c = getopt_long (argc, argv, "Vhrc:w:", longopts, &option); 251 c = getopt_long (argc, argv, "Vhrc:w:WCn:", longopts, &option);
238 252
239 if (c == -1 || c == EOF) 253 if (c == -1 || c == EOF)
240 break; 254 break;
@@ -255,6 +269,15 @@ process_arguments (int argc, char **argv)
255 case 'h': /* help */ 269 case 'h': /* help */
256 print_help (); 270 print_help ();
257 exit (STATE_UNKNOWN); 271 exit (STATE_UNKNOWN);
272 case 'n':
273 n_procs_to_show = atoi(optarg);
274 break;
275 case 'W':
276 print_top_procs_on_warning = 1;
277 break;
278 case 'C':
279 print_top_procs_on_critical = 1;
280 break;
258 case '?': /* help */ 281 case '?': /* help */
259 usage5 (); 282 usage5 ();
260 } 283 }
@@ -324,6 +347,13 @@ print_help (void)
324 printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); 347 printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
325 printf (" %s\n", "-r, --percpu"); 348 printf (" %s\n", "-r, --percpu");
326 printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)")); 349 printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
350 printf (" %s\n", "-W, --print-top-warning");
351 printf (" %s\n", _("Print top consuming processes on WARNING status"));
352 printf (" %s\n", "-C, --print-top-critical");
353 printf (" %s\n", _("Print top consuming processes on CRITICAL status"));
354 printf (" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS");
355 printf (" %s\n", _("Number of processes to show when printing top consuming"));
356 printf (" %s\n", _("processes. Not useful without -W or -C. Default value is 5"));
327 357
328 printf (UT_SUPPORT); 358 printf (UT_SUPPORT);
329} 359}
@@ -332,5 +362,21 @@ void
332print_usage (void) 362print_usage (void)
333{ 363{
334 printf ("%s\n", _("Usage:")); 364 printf ("%s\n", _("Usage:"));
335 printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname); 365 printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-W] [-C] [-n NUMBER_OF_PROCS]\n", progname);
366}
367
368static int print_top_consuming_processes() {
369 int i = 0;
370 struct output chld_out, chld_err;
371 char *cmdline = "/bin/ps -aux --sort=-pcpu";
372 if(np_runcmd(cmdline, &chld_out, &chld_err, 0) != 0){
373 fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline);
374 return STATE_UNKNOWN;
375 }
376 int lines_to_show = chld_out.lines < (n_procs_to_show + 1)
377 ? chld_out.lines : n_procs_to_show + 1;
378 for (i = 0; i < lines_to_show; i += 1) {
379 printf("%s\n", chld_out.line[i]);
380 }
381 return OK;
336} 382}