summaryrefslogtreecommitdiffstats
path: root/plugins/check_load.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-13 15:36:36 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-13 15:36:36 +0100
commitf2900e0ccf18f321857e4072681ad9c10a0cb67f (patch)
treeaf7e1ea99aede65e818987f22af82735c32257e7 /plugins/check_load.c
parentaf6cfefaacbb15bf371e0d59383b54ff695d1529 (diff)
downloadmonitoring-plugins-f2900e0ccf18f321857e4072681ad9c10a0cb67f.tar.gz
check_load: clang-format
Diffstat (limited to 'plugins/check_load.c')
-rw-r--r--plugins/check_load.c330
1 files changed, 162 insertions, 168 deletions
diff --git a/plugins/check_load.c b/plugins/check_load.c
index 1431d130..e3a45f58 100644
--- a/plugins/check_load.c
+++ b/plugins/check_load.c
@@ -1,32 +1,32 @@
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
31const char *progname = "check_load"; 31const char *progname = "check_load";
32const char *copyright = "1999-2022"; 32const char *copyright = "1999-2022";
@@ -40,127 +40,129 @@ const char *email = "devel@monitoring-plugins.org";
40#include <string.h> 40#include <string.h>
41 41
42#ifdef HAVE_SYS_LOADAVG_H 42#ifdef HAVE_SYS_LOADAVG_H
43#include <sys/loadavg.h> 43# include <sys/loadavg.h>
44#endif 44#endif
45 45
46/* needed for compilation under NetBSD, as suggested by Andy Doran */ 46/* needed for compilation under NetBSD, as suggested by Andy Doran */
47#ifndef LOADAVG_1MIN 47#ifndef LOADAVG_1MIN
48#define LOADAVG_1MIN 0 48# define LOADAVG_1MIN 0
49#define LOADAVG_5MIN 1 49# define LOADAVG_5MIN 1
50#define LOADAVG_15MIN 2 50# define LOADAVG_15MIN 2
51#endif /* !defined LOADAVG_1MIN */ 51#endif /* !defined LOADAVG_1MIN */
52 52
53 53static int process_arguments(int argc, char **argv);
54static int process_arguments (int argc, char **argv); 54static int validate_arguments(void);
55static int validate_arguments (void); 55void print_help(void);
56void print_help (void); 56void print_usage(void);
57void print_usage (void);
58static int print_top_consuming_processes(); 57static int print_top_consuming_processes();
59 58
60static int n_procs_to_show = 0; 59static int n_procs_to_show = 0;
61 60
62/* strictly for pretty-print usage in loops */ 61/* strictly for pretty-print usage in loops */
63static const int nums[3] = { 1, 5, 15 }; 62static const int nums[3] = {1, 5, 15};
64 63
65/* provide some fairly sane defaults */ 64/* provide some fairly sane defaults */
66double wload[3] = { 0.0, 0.0, 0.0 }; 65double wload[3] = {0.0, 0.0, 0.0};
67double cload[3] = { 0.0, 0.0, 0.0 }; 66double cload[3] = {0.0, 0.0, 0.0};
68#define la1 la[0] 67#define la1 la[0]
69#define la5 la[1] 68#define la5 la[1]
70#define la15 la[2] 69#define la15 la[2]
71 70
72char *status_line; 71char *status_line;
73bool take_into_account_cpus = false; 72bool take_into_account_cpus = false;
74 73
75static void 74static void get_threshold(char *arg, double *th) {
76get_threshold(char *arg, double *th)
77{
78 size_t i, n; 75 size_t i, n;
79 int valid = 0; 76 int valid = 0;
80 char *str = arg, *p; 77 char *str = arg, *p;
81 78
82 n = strlen(arg); 79 n = strlen(arg);
83 for(i = 0; i < 3; i++) { 80 for (i = 0; i < 3; i++) {
84 th[i] = strtod(str, &p); 81 th[i] = strtod(str, &p);
85 if(p == str) break; 82 if (p == str) {
83 break;
84 }
86 85
87 valid = 1; 86 valid = 1;
88 str = p + 1; 87 str = p + 1;
89 if(n <= (size_t)(str - arg)) break; 88 if (n <= (size_t)(str - arg)) {
89 break;
90 }
90 } 91 }
91 92
92 /* empty argument or non-floatish, so warn about it and die */ 93 /* empty argument or non-floatish, so warn about it and die */
93 if(!i && !valid) usage (_("Warning threshold must be float or float triplet!\n")); 94 if (!i && !valid) {
95 usage(_("Warning threshold must be float or float triplet!\n"));
96 }
94 97
95 if(i != 2) { 98 if (i != 2) {
96 /* one or more numbers were given, so fill array with last 99 /* one or more numbers were given, so fill array with last
97 * we got (most likely to NOT produce the least expected result) */ 100 * we got (most likely to NOT produce the least expected result) */
98 for(n = i; n < 3; n++) th[n] = th[i]; 101 for (n = i; n < 3; n++) {
102 th[n] = th[i];
103 }
99 } 104 }
100} 105}
101 106
102 107int main(int argc, char **argv) {
103int
104main (int argc, char **argv)
105{
106 int result = -1; 108 int result = -1;
107 int i; 109 int i;
108 long numcpus; 110 long numcpus;
109 111
110 double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about uninitialized arrays */ 112 double la[3] = {0.0, 0.0, 0.0}; /* NetBSD complains about uninitialized arrays */
111#ifndef HAVE_GETLOADAVG 113#ifndef HAVE_GETLOADAVG
112 char input_buffer[MAX_INPUT_BUFFER]; 114 char input_buffer[MAX_INPUT_BUFFER];
113#endif 115#endif
114 116
115 setlocale (LC_ALL, ""); 117 setlocale(LC_ALL, "");
116 bindtextdomain (PACKAGE, LOCALEDIR); 118 bindtextdomain(PACKAGE, LOCALEDIR);
117 textdomain (PACKAGE); 119 textdomain(PACKAGE);
118 setlocale(LC_NUMERIC, "POSIX"); 120 setlocale(LC_NUMERIC, "POSIX");
119 121
120 /* Parse extra opts if any */ 122 /* Parse extra opts if any */
121 argv = np_extra_opts (&argc, argv, progname); 123 argv = np_extra_opts(&argc, argv, progname);
122 124
123 if (process_arguments (argc, argv) == ERROR) 125 if (process_arguments(argc, argv) == ERROR) {
124 usage4 (_("Could not parse arguments")); 126 usage4(_("Could not parse arguments"));
127 }
125 128
126#ifdef HAVE_GETLOADAVG 129#ifdef HAVE_GETLOADAVG
127 result = getloadavg (la, 3); 130 result = getloadavg(la, 3);
128 if (result != 3) 131 if (result != 3) {
129 return STATE_UNKNOWN; 132 return STATE_UNKNOWN;
133 }
130#else 134#else
131 child_process = spopen (PATH_TO_UPTIME); 135 child_process = spopen(PATH_TO_UPTIME);
132 if (child_process == NULL) { 136 if (child_process == NULL) {
133 printf (_("Error opening %s\n"), PATH_TO_UPTIME); 137 printf(_("Error opening %s\n"), PATH_TO_UPTIME);
134 return STATE_UNKNOWN; 138 return STATE_UNKNOWN;
135 } 139 }
136 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); 140 child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r");
137 if (child_stderr == NULL) { 141 if (child_stderr == NULL) {
138 printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME); 142 printf(_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
139 } 143 }
140 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); 144 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process);
141 if(strstr(input_buffer, "load average:")) { 145 if (strstr(input_buffer, "load average:")) {
142 sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15); 146 sscanf(input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
143 } 147 } else if (strstr(input_buffer, "load averages:")) {
144 else if(strstr(input_buffer, "load averages:")) { 148 sscanf(input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15);
145 sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15); 149 } else {
146 } 150 printf(_("could not parse load from uptime %s: %d\n"), PATH_TO_UPTIME, result);
147 else {
148 printf (_("could not parse load from uptime %s: %d\n"), PATH_TO_UPTIME, result);
149 return STATE_UNKNOWN; 151 return STATE_UNKNOWN;
150 } 152 }
151 153
152 result = spclose (child_process); 154 result = spclose(child_process);
153 if (result) { 155 if (result) {
154 printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME); 156 printf(_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
155 return STATE_UNKNOWN; 157 return STATE_UNKNOWN;
156 } 158 }
157#endif 159#endif
158 160
159 if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) { 161 if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
160#ifdef HAVE_GETLOADAVG 162#ifdef HAVE_GETLOADAVG
161 printf (_("Error in getloadavg()\n")); 163 printf(_("Error in getloadavg()\n"));
162#else 164#else
163 printf (_("Error processing %s\n"), PATH_TO_UPTIME); 165 printf(_("Error processing %s\n"), PATH_TO_UPTIME);
164#endif 166#endif
165 return STATE_UNKNOWN; 167 return STATE_UNKNOWN;
166 } 168 }
@@ -171,8 +173,7 @@ main (int argc, char **argv)
171 xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15); 173 xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
172 xasprintf(&status_line, ("total %s"), status_line); 174 xasprintf(&status_line, ("total %s"), status_line);
173 175
174 176 double scaled_la[3] = {0.0, 0.0, 0.0};
175 double scaled_la[3] = { 0.0, 0.0, 0.0 };
176 bool is_using_scaled_load_values = false; 177 bool is_using_scaled_load_values = false;
177 178
178 if (take_into_account_cpus == true && (numcpus = GET_NUMBER_OF_CPUS()) > 0) { 179 if (take_into_account_cpus == true && (numcpus = GET_NUMBER_OF_CPUS()) > 0) {
@@ -187,24 +188,26 @@ main (int argc, char **argv)
187 xasprintf(&status_line, "scaled %s - %s", tmp, status_line); 188 xasprintf(&status_line, "scaled %s - %s", tmp, status_line);
188 } 189 }
189 190
190 for(i = 0; i < 3; i++) { 191 for (i = 0; i < 3; i++) {
191 if (is_using_scaled_load_values) { 192 if (is_using_scaled_load_values) {
192 if(scaled_la[i] > cload[i]) { 193 if (scaled_la[i] > cload[i]) {
193 result = STATE_CRITICAL; 194 result = STATE_CRITICAL;
194 break; 195 break;
196 } else if (scaled_la[i] > wload[i]) {
197 result = STATE_WARNING;
195 } 198 }
196 else if(scaled_la[i] > wload[i]) result = STATE_WARNING;
197 } else { 199 } else {
198 if(la[i] > cload[i]) { 200 if (la[i] > cload[i]) {
199 result = STATE_CRITICAL; 201 result = STATE_CRITICAL;
200 break; 202 break;
203 } else if (la[i] > wload[i]) {
204 result = STATE_WARNING;
201 } 205 }
202 else if(la[i] > wload[i]) result = STATE_WARNING;
203 } 206 }
204 } 207 }
205 208
206 printf("LOAD %s - %s|", state_text(result), status_line); 209 printf("LOAD %s - %s|", state_text(result), status_line);
207 for(i = 0; i < 3; i++) { 210 for (i = 0; i < 3; i++) {
208 if (is_using_scaled_load_values) { 211 if (is_using_scaled_load_values) {
209 printf("load%d=%.3f;;;0; ", nums[i], la[i]); 212 printf("load%d=%.3f;;;0; ", nums[i], la[i]);
210 printf("scaled_load%d=%.3f;%.3f;%.3f;0; ", nums[i], scaled_la[i], wload[i], cload[i]); 213 printf("scaled_load%d=%.3f;%.3f;%.3f;0; ", nums[i], scaled_la[i], wload[i], cload[i]);
@@ -220,32 +223,29 @@ main (int argc, char **argv)
220 return result; 223 return result;
221} 224}
222 225
223
224/* process command-line arguments */ 226/* process command-line arguments */
225static int 227static int process_arguments(int argc, char **argv) {
226process_arguments (int argc, char **argv)
227{
228 int c = 0; 228 int c = 0;
229 229
230 int option = 0; 230 int option = 0;
231 static struct option longopts[] = { 231 static struct option longopts[] = {{"warning", required_argument, 0, 'w'},
232 {"warning", required_argument, 0, 'w'}, 232 {"critical", required_argument, 0, 'c'},
233 {"critical", required_argument, 0, 'c'}, 233 {"percpu", no_argument, 0, 'r'},
234 {"percpu", no_argument, 0, 'r'}, 234 {"version", no_argument, 0, 'V'},
235 {"version", no_argument, 0, 'V'}, 235 {"help", no_argument, 0, 'h'},
236 {"help", no_argument, 0, 'h'}, 236 {"procs-to-show", required_argument, 0, 'n'},
237 {"procs-to-show", required_argument, 0, 'n'}, 237 {0, 0, 0, 0}};
238 {0, 0, 0, 0} 238
239 }; 239 if (argc < 2) {
240
241 if (argc < 2)
242 return ERROR; 240 return ERROR;
241 }
243 242
244 while (1) { 243 while (1) {
245 c = getopt_long (argc, argv, "Vhrc:w:n:", longopts, &option); 244 c = getopt_long(argc, argv, "Vhrc:w:n:", longopts, &option);
246 245
247 if (c == -1 || c == EOF) 246 if (c == -1 || c == EOF) {
248 break; 247 break;
248 }
249 249
250 switch (c) { 250 switch (c) {
251 case 'w': /* warning time threshold */ 251 case 'w': /* warning time threshold */
@@ -257,94 +257,89 @@ process_arguments (int argc, char **argv)
257 case 'r': /* Divide load average by number of CPUs */ 257 case 'r': /* Divide load average by number of CPUs */
258 take_into_account_cpus = true; 258 take_into_account_cpus = true;
259 break; 259 break;
260 case 'V': /* version */ 260 case 'V': /* version */
261 print_revision (progname, NP_VERSION); 261 print_revision(progname, NP_VERSION);
262 exit (STATE_UNKNOWN); 262 exit(STATE_UNKNOWN);
263 case 'h': /* help */ 263 case 'h': /* help */
264 print_help (); 264 print_help();
265 exit (STATE_UNKNOWN); 265 exit(STATE_UNKNOWN);
266 case 'n': 266 case 'n':
267 n_procs_to_show = atoi(optarg); 267 n_procs_to_show = atoi(optarg);
268 break; 268 break;
269 case '?': /* help */ 269 case '?': /* help */
270 usage5 (); 270 usage5();
271 } 271 }
272 } 272 }
273 273
274 c = optind; 274 c = optind;
275 if (c == argc) 275 if (c == argc) {
276 return validate_arguments (); 276 return validate_arguments();
277 }
277 278
278 /* handle the case if both arguments are missing, 279 /* handle the case if both arguments are missing,
279 * but not if only one is given without -c or -w flag */ 280 * but not if only one is given without -c or -w flag */
280 if(c - argc == 2) { 281 if (c - argc == 2) {
281 get_threshold(argv[c++], wload); 282 get_threshold(argv[c++], wload);
282 get_threshold(argv[c++], cload); 283 get_threshold(argv[c++], cload);
283 } 284 } else if (c - argc == 1) {
284 else if(c - argc == 1) {
285 get_threshold(argv[c++], cload); 285 get_threshold(argv[c++], cload);
286 } 286 }
287 287
288 return validate_arguments (); 288 return validate_arguments();
289} 289}
290 290
291 291static int validate_arguments(void) {
292static int
293validate_arguments (void)
294{
295 int i = 0; 292 int i = 0;
296 293
297 /* match cload first, as it will give the most friendly error message 294 /* match cload first, as it will give the most friendly error message
298 * if user hasn't given the -c switch properly */ 295 * if user hasn't given the -c switch properly */
299 for(i = 0; i < 3; i++) { 296 for (i = 0; i < 3; i++) {
300 if(cload[i] < 0) 297 if (cload[i] < 0) {
301 die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]); 298 die(STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
302 if(wload[i] < 0) 299 }
303 die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]); 300 if (wload[i] < 0) {
304 if(wload[i] > cload[i]) 301 die(STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
305 die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]); 302 }
303 if (wload[i] > cload[i]) {
304 die(STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
305 }
306 } 306 }
307 307
308 return OK; 308 return OK;
309} 309}
310 310
311void print_help(void) {
312 print_revision(progname, NP_VERSION);
311 313
312void 314 printf("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
313print_help (void) 315 printf(COPYRIGHT, copyright, email);
314{
315 print_revision (progname, NP_VERSION);
316
317 printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
318 printf (COPYRIGHT, copyright, email);
319 316
320 printf (_("This plugin tests the current system load average.")); 317 printf(_("This plugin tests the current system load average."));
321 318
322 printf ("\n\n"); 319 printf("\n\n");
323 320
324 print_usage (); 321 print_usage();
325 322
326 printf (UT_HELP_VRSN); 323 printf(UT_HELP_VRSN);
327 printf (UT_EXTRA_OPTS); 324 printf(UT_EXTRA_OPTS);
328 325
329 printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15"); 326 printf(" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
330 printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn")); 327 printf(" %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
331 printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15"); 328 printf(" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
332 printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn")); 329 printf(" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
333 printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\"")); 330 printf(" %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
334 printf (" %s\n", "-r, --percpu"); 331 printf(" %s\n", "-r, --percpu");
335 printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)")); 332 printf(" %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
336 printf (" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS"); 333 printf(" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS");
337 printf (" %s\n", _("Number of processes to show when printing the top consuming processes.")); 334 printf(" %s\n", _("Number of processes to show when printing the top consuming processes."));
338 printf (" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0")); 335 printf(" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0"));
339 336
340 printf (UT_SUPPORT); 337 printf(UT_SUPPORT);
341} 338}
342 339
343void 340void print_usage(void) {
344print_usage (void) 341 printf("%s\n", _("Usage:"));
345{ 342 printf("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname);
346 printf ("%s\n", _("Usage:"));
347 printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname);
348} 343}
349 344
350#ifdef PS_USES_PROCPCPU 345#ifdef PS_USES_PROCPCPU
@@ -356,14 +351,14 @@ int cmpstringp(const void *p1, const void *p2) {
356 int procrss = 0; 351 int procrss = 0;
357 float procpcpu = 0; 352 float procpcpu = 0;
358 char procstat[8]; 353 char procstat[8];
359#ifdef PS_USES_PROCETIME 354# ifdef PS_USES_PROCETIME
360 char procetime[MAX_INPUT_BUFFER]; 355 char procetime[MAX_INPUT_BUFFER];
361#endif /* PS_USES_PROCETIME */ 356# endif /* PS_USES_PROCETIME */
362 char procprog[MAX_INPUT_BUFFER]; 357 char procprog[MAX_INPUT_BUFFER];
363 int pos; 358 int pos;
364 sscanf (* (char * const *) p1, PS_FORMAT, PS_VARLIST); 359 sscanf(*(char *const *)p1, PS_FORMAT, PS_VARLIST);
365 float procpcpu1 = procpcpu; 360 float procpcpu1 = procpcpu;
366 sscanf (* (char * const *) p2, PS_FORMAT, PS_VARLIST); 361 sscanf(*(char *const *)p2, PS_FORMAT, PS_VARLIST);
367 return procpcpu1 < procpcpu; 362 return procpcpu1 < procpcpu;
368} 363}
369#endif /* PS_USES_PROCPCPU */ 364#endif /* PS_USES_PROCPCPU */
@@ -371,7 +366,7 @@ int cmpstringp(const void *p1, const void *p2) {
371static int print_top_consuming_processes() { 366static int print_top_consuming_processes() {
372 int i = 0; 367 int i = 0;
373 struct output chld_out, chld_err; 368 struct output chld_out, chld_err;
374 if(np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0){ 369 if (np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0) {
375 fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND); 370 fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND);
376 return STATE_UNKNOWN; 371 return STATE_UNKNOWN;
377 } 372 }
@@ -380,10 +375,9 @@ static int print_top_consuming_processes() {
380 return STATE_UNKNOWN; 375 return STATE_UNKNOWN;
381 } 376 }
382#ifdef PS_USES_PROCPCPU 377#ifdef PS_USES_PROCPCPU
383 qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char*), cmpstringp); 378 qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char *), cmpstringp);
384#endif /* PS_USES_PROCPCPU */ 379#endif /* PS_USES_PROCPCPU */
385 int lines_to_show = chld_out.lines < (size_t)(n_procs_to_show + 1) 380 int lines_to_show = chld_out.lines < (size_t)(n_procs_to_show + 1) ? (int)chld_out.lines : n_procs_to_show + 1;
386 ? (int)chld_out.lines : n_procs_to_show + 1;
387 for (i = 0; i < lines_to_show; i += 1) { 381 for (i = 0; i < lines_to_show; i += 1) {
388 printf("%s\n", chld_out.line[i]); 382 printf("%s\n", chld_out.line[i]);
389 } 383 }