aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Marc Sánchez <sanchezfauste@gmail.com> 2017-09-07 20:55:34 +0200
committerGravatar Marc Sánchez <sanchezfauste@gmail.com> 2017-09-07 20:55:34 +0200
commit015a40e1b590bb847328d51bdfc4b544ae8825d3 (patch)
treea774a1d6c8e7a6aeb361584e71d71367832a1fd3
parent0625fbb53db9401bdce95b1a403a20980a326ec4 (diff)
downloadmonitoring-plugins-015a40e1b590bb847328d51bdfc4b544ae8825d3.tar.gz
Using PS_COMMAND constant and ordering output by procpcpu
-rw-r--r--plugins/check_load.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/plugins/check_load.c b/plugins/check_load.c
index 5d5c115c..6fd895f2 100644
--- a/plugins/check_load.c
+++ b/plugins/check_load.c
@@ -349,14 +349,37 @@ print_usage (void)
printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname);
}
+int cmpstringp(const void *p1, const void *p2) {
+ int procuid = 0;
+ int procpid = 0;
+ int procppid = 0;
+ int procvsz = 0;
+ int procrss = 0;
+ float procpcpu = 0;
+ char procstat[8];
+#ifdef PS_USES_PROCETIME
+ char procetime[MAX_INPUT_BUFFER];
+#endif /* PS_USES_PROCETIME */
+ char procprog[MAX_INPUT_BUFFER];
+ int pos;
+ sscanf (* (char * const *) p1, PS_FORMAT, PS_VARLIST);
+ float procpcpu1 = procpcpu;
+ sscanf (* (char * const *) p2, PS_FORMAT, PS_VARLIST);
+ return procpcpu1 < procpcpu;
+}
+
static int print_top_consuming_processes() {
int i = 0;
struct output chld_out, chld_err;
- char *cmdline = "/bin/ps -aux --sort=-pcpu";
- if(np_runcmd(cmdline, &chld_out, &chld_err, 0) != 0){
- fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline);
+ 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;
+ }
+ if (chld_out.lines < 2) {
+ fprintf(stderr, _("some error occurred getting procs list.\n"));
return STATE_UNKNOWN;
}
+ qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char*), cmpstringp);
int lines_to_show = chld_out.lines < (n_procs_to_show + 1)
? chld_out.lines : n_procs_to_show + 1;
for (i = 0; i < lines_to_show; i += 1) {