diff options
-rw-r--r-- | plugins/check_procs.c | 184 |
1 files changed, 76 insertions, 108 deletions
diff --git a/plugins/check_procs.c b/plugins/check_procs.c index 9a3dc015..0b0a3c13 100644 --- a/plugins/check_procs.c +++ b/plugins/check_procs.c | |||
@@ -34,6 +34,17 @@ | |||
34 | * | 34 | * |
35 | ******************************************************************************/ | 35 | ******************************************************************************/ |
36 | 36 | ||
37 | #define PROGNAME "check_snmp" | ||
38 | #define REVISION "$Revision$" | ||
39 | #define COPYRIGHT "1999-2002" | ||
40 | #define AUTHOR "Ethan Galstad" | ||
41 | #define EMAIL "nagios@nagios.org" | ||
42 | #define SUMMARY "Check the number of currently running processes and generates WARNING or\n\ | ||
43 | CRITICAL states if the process count is outside the specified threshold\n\ | ||
44 | ranges. The process count can be filtered by process owner, parent process\n\ | ||
45 | PID, current state (e.g., 'Z'), or may be the total number of running\n\ | ||
46 | processes\n" | ||
47 | |||
37 | #include "config.h" | 48 | #include "config.h" |
38 | #include <pwd.h> | 49 | #include <pwd.h> |
39 | #include "common.h" | 50 | #include "common.h" |
@@ -41,10 +52,9 @@ | |||
41 | #include "utils.h" | 52 | #include "utils.h" |
42 | 53 | ||
43 | int process_arguments (int, char **); | 54 | int process_arguments (int, char **); |
44 | int call_getopt (int, char **); | ||
45 | int validate_arguments (void); | 55 | int validate_arguments (void); |
46 | void print_usage (void); | 56 | void print_usage (void); |
47 | void print_help (char *); | 57 | void print_help (void); |
48 | 58 | ||
49 | int wmax = -1; | 59 | int wmax = -1; |
50 | int cmax = -1; | 60 | int cmax = -1; |
@@ -65,7 +75,7 @@ int ppid; | |||
65 | char *statopts = NULL; | 75 | char *statopts = NULL; |
66 | char *prog = NULL; | 76 | char *prog = NULL; |
67 | char *args = NULL; | 77 | char *args = NULL; |
68 | char *format = NULL; | 78 | char *fmt = NULL; |
69 | char tmp[MAX_INPUT_BUFFER]; | 79 | char tmp[MAX_INPUT_BUFFER]; |
70 | 80 | ||
71 | int | 81 | int |
@@ -188,29 +198,35 @@ main (int argc, char **argv) | |||
188 | if (verbose && (options & USER)) | 198 | if (verbose && (options & USER)) |
189 | printf ("%d ", uid); | 199 | printf ("%d ", uid); |
190 | 200 | ||
191 | if (cmax >= 0 && cmin >= 0 && cmax < cmin) { | 201 | if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) { |
202 | if (result == STATE_UNKNOWN) | ||
203 | result = STATE_OK; | ||
204 | printf (fmt, "OK", procs); | ||
205 | return result; | ||
206 | } | ||
207 | else if (cmax >= 0 && cmin >= 0 && cmax < cmin) { | ||
192 | if (procs > cmax && procs < cmin) { | 208 | if (procs > cmax && procs < cmin) { |
193 | printf (format, "CRITICAL", procs); | 209 | printf (fmt, "CRITICAL", procs); |
194 | return STATE_CRITICAL; | 210 | return STATE_CRITICAL; |
195 | } | 211 | } |
196 | } | 212 | } |
197 | else if (cmax >= 0 && procs > cmax) { | 213 | else if (cmax >= 0 && procs > cmax) { |
198 | printf (format, "CRITICAL", procs); | 214 | printf (fmt, "CRITICAL", procs); |
199 | return STATE_CRITICAL; | 215 | return STATE_CRITICAL; |
200 | } | 216 | } |
201 | else if (cmin >= 0 && procs < cmin) { | 217 | else if (cmin >= 0 && procs < cmin) { |
202 | printf (format, "CRITICAL", procs); | 218 | printf (fmt, "CRITICAL", procs); |
203 | return STATE_CRITICAL; | 219 | return STATE_CRITICAL; |
204 | } | 220 | } |
205 | 221 | ||
206 | if (wmax >= 0 && wmin >= 0 && wmax < wmin) { | 222 | if (wmax >= 0 && wmin >= 0 && wmax < wmin) { |
207 | if (procs > wmax && procs < wmin) { | 223 | if (procs > wmax && procs < wmin) { |
208 | printf (format, "CRITICAL", procs); | 224 | printf (fmt, "CRITICAL", procs); |
209 | return STATE_CRITICAL; | 225 | return STATE_CRITICAL; |
210 | } | 226 | } |
211 | } | 227 | } |
212 | else if (wmax >= 0 && procs > wmax) { | 228 | else if (wmax >= 0 && procs > wmax) { |
213 | printf (format, "WARNING", procs); | 229 | printf (fmt, "WARNING", procs); |
214 | if ( !(result == STATE_CRITICAL) ) { | 230 | if ( !(result == STATE_CRITICAL) ) { |
215 | return STATE_WARNING; | 231 | return STATE_WARNING; |
216 | } | 232 | } |
@@ -220,7 +236,7 @@ main (int argc, char **argv) | |||
220 | /*return max (result, STATE_WARNING); */ | 236 | /*return max (result, STATE_WARNING); */ |
221 | } | 237 | } |
222 | else if (wmin >= 0 && procs < wmin) { | 238 | else if (wmin >= 0 && procs < wmin) { |
223 | printf (format, "WARNING", procs); | 239 | printf (fmt, "WARNING", procs); |
224 | if ( !(result == STATE_CRITICAL) ) { | 240 | if ( !(result == STATE_CRITICAL) ) { |
225 | return STATE_WARNING; | 241 | return STATE_WARNING; |
226 | } | 242 | } |
@@ -230,7 +246,7 @@ main (int argc, char **argv) | |||
230 | /*return max (result, STATE_WARNING); */ | 246 | /*return max (result, STATE_WARNING); */ |
231 | } | 247 | } |
232 | 248 | ||
233 | printf (format, "OK", procs); | 249 | printf (fmt, "OK", procs); |
234 | if ( result == STATE_UNKNOWN ) { | 250 | if ( result == STATE_UNKNOWN ) { |
235 | result = STATE_OK; | 251 | result = STATE_OK; |
236 | } | 252 | } |
@@ -241,39 +257,6 @@ main (int argc, char **argv) | |||
241 | int | 257 | int |
242 | process_arguments (int argc, char **argv) | 258 | process_arguments (int argc, char **argv) |
243 | { | 259 | { |
244 | int c; | ||
245 | |||
246 | if (argc < 2) | ||
247 | return ERROR; | ||
248 | |||
249 | for (c = 1; c < argc; c++) | ||
250 | if (strcmp ("-to", argv[c]) == 0) | ||
251 | strcpy (argv[c], "-t"); | ||
252 | |||
253 | c = 0; | ||
254 | while (c += (call_getopt (argc - c, &argv[c]))) { | ||
255 | if (argc <= c) | ||
256 | break; | ||
257 | if (wmax == -1) | ||
258 | wmax = atoi (argv[c]); | ||
259 | else if (cmax == -1) | ||
260 | cmax = atoi (argv[c]); | ||
261 | else if (statopts == NULL) { | ||
262 | statopts = strscpy (statopts, argv[c]); | ||
263 | format = | ||
264 | strscat (format, | ||
265 | ssprintf (NULL, "%sSTATE = %s", (options ? ", " : ""), | ||
266 | statopts)); | ||
267 | options |= STAT; | ||
268 | } | ||
269 | } | ||
270 | |||
271 | return validate_arguments (); | ||
272 | } | ||
273 | |||
274 | int | ||
275 | call_getopt (int argc, char **argv) | ||
276 | { | ||
277 | int c, i = 1; | 260 | int c, i = 1; |
278 | char *user; | 261 | char *user; |
279 | struct passwd *pw; | 262 | struct passwd *pw; |
@@ -294,45 +277,35 @@ call_getopt (int argc, char **argv) | |||
294 | }; | 277 | }; |
295 | #endif | 278 | #endif |
296 | 279 | ||
280 | asprintf (&fmt, ""); | ||
281 | |||
282 | for (c = 1; c < argc; c++) | ||
283 | if (strcmp ("-to", argv[c]) == 0) | ||
284 | strcpy (argv[c], "-t"); | ||
285 | |||
297 | while (1) { | 286 | while (1) { |
298 | #ifdef HAVE_GETOPT_H | 287 | #ifdef HAVE_GETOPT_H |
299 | c = | 288 | c = getopt_long (argc, argv, "Vvht:c:w:p:s:u:C:a:", long_options, &option_index); |
300 | getopt_long (argc, argv, "+Vvht:c:w:p:s:u:C:a:", long_options, | ||
301 | &option_index); | ||
302 | #else | 289 | #else |
303 | c = getopt (argc, argv, "+Vvht:c:w:p:s:u:C:a:"); | 290 | c = getopt (argc, argv, "Vvht:c:w:p:s:u:C:a:"); |
304 | #endif | 291 | #endif |
305 | 292 | if (c == -1 || c == EOF) | |
306 | if (c == EOF) | ||
307 | break; | 293 | break; |
308 | 294 | ||
309 | i++; | ||
310 | switch (c) { | ||
311 | case 't': | ||
312 | case 'c': | ||
313 | case 'w': | ||
314 | case 'p': | ||
315 | case 's': | ||
316 | case 'a': | ||
317 | case 'u': | ||
318 | case 'C': | ||
319 | i++; | ||
320 | } | ||
321 | |||
322 | switch (c) { | 295 | switch (c) { |
323 | case '?': /* help */ | 296 | case '?': /* help */ |
324 | print_usage (); | 297 | print_usage (); |
325 | exit (STATE_UNKNOWN); | 298 | exit (STATE_UNKNOWN); |
326 | case 'h': /* help */ | 299 | case 'h': /* help */ |
327 | print_help (my_basename (argv[0])); | 300 | print_help (); |
328 | exit (STATE_OK); | 301 | exit (STATE_OK); |
329 | case 'V': /* version */ | 302 | case 'V': /* version */ |
330 | print_revision (my_basename (argv[0]), "$Revision$"); | 303 | print_revision (PROGNAME, REVISION); |
331 | exit (STATE_OK); | 304 | exit (STATE_OK); |
332 | case 't': /* timeout period */ | 305 | case 't': /* timeout period */ |
333 | if (!is_integer (optarg)) { | 306 | if (!is_integer (optarg)) { |
334 | printf ("%s: Timeout Interval must be an integer!\n\n", | 307 | printf ("%s: Timeout Interval must be an integer!\n\n", |
335 | my_basename (argv[0])); | 308 | my_basename (argv[0])); |
336 | print_usage (); | 309 | print_usage (); |
337 | exit (STATE_UNKNOWN); | 310 | exit (STATE_UNKNOWN); |
338 | } | 311 | } |
@@ -354,7 +327,7 @@ call_getopt (int argc, char **argv) | |||
354 | } | 327 | } |
355 | else { | 328 | else { |
356 | printf ("%s: Critical Process Count must be an integer!\n\n", | 329 | printf ("%s: Critical Process Count must be an integer!\n\n", |
357 | my_basename (argv[0])); | 330 | my_basename (argv[0])); |
358 | print_usage (); | 331 | print_usage (); |
359 | exit (STATE_UNKNOWN); | 332 | exit (STATE_UNKNOWN); |
360 | } | 333 | } |
@@ -374,29 +347,23 @@ call_getopt (int argc, char **argv) | |||
374 | } | 347 | } |
375 | else { | 348 | else { |
376 | printf ("%s: Warning Process Count must be an integer!\n\n", | 349 | printf ("%s: Warning Process Count must be an integer!\n\n", |
377 | my_basename (argv[0])); | 350 | my_basename (argv[0])); |
378 | print_usage (); | 351 | print_usage (); |
379 | exit (STATE_UNKNOWN); | 352 | exit (STATE_UNKNOWN); |
380 | } | 353 | } |
381 | case 'p': /* process id */ | 354 | case 'p': /* process id */ |
382 | if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) { | 355 | if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) { |
383 | format = | 356 | asprintf (&fmt, "%s%sPPID = %d", (options ? ", " : ""), ppid); |
384 | strscat (format, | ||
385 | ssprintf (NULL, "%sPPID = %d", (options ? ", " : ""), | ||
386 | ppid)); | ||
387 | options |= PPID; | 357 | options |= PPID; |
388 | break; | 358 | break; |
389 | } | 359 | } |
390 | printf ("%s: Parent Process ID must be an integer!\n\n", | 360 | printf ("%s: Parent Process ID must be an integer!\n\n", |
391 | my_basename (argv[0])); | 361 | my_basename (argv[0])); |
392 | print_usage (); | 362 | print_usage (); |
393 | exit (STATE_UNKNOWN); | 363 | exit (STATE_UNKNOWN); |
394 | case 's': /* status */ | 364 | case 's': /* status */ |
395 | statopts = strscpy (statopts, optarg); | 365 | asprintf (&statopts, "%s", optarg); |
396 | format = | 366 | asprintf (&fmt, "%s%sSTATE = %s", fmt, (options ? ", " : ""), statopts); |
397 | strscat (format, | ||
398 | ssprintf (NULL, "%sSTATE = %s", (options ? ", " : ""), | ||
399 | statopts)); | ||
400 | options |= STAT; | 367 | options |= STAT; |
401 | break; | 368 | break; |
402 | case 'u': /* user or user id */ | 369 | case 'u': /* user or user id */ |
@@ -422,25 +389,19 @@ call_getopt (int argc, char **argv) | |||
422 | uid = pw->pw_uid; | 389 | uid = pw->pw_uid; |
423 | } | 390 | } |
424 | user = pw->pw_name; | 391 | user = pw->pw_name; |
425 | format = | 392 | asprintf (&fmt, "%s%sUID = %d (%s)", (options ? ", " : ""), fmt, |
426 | strscat (format, | 393 | uid, user); |
427 | ssprintf (NULL, "%sUID = %d (%s)", (options ? ", " : ""), | ||
428 | uid, user)); | ||
429 | options |= USER; | 394 | options |= USER; |
430 | break; | 395 | break; |
431 | case 'C': /* command */ | 396 | case 'C': /* command */ |
432 | prog = strscpy (prog, optarg); | 397 | asprintf (&prog, "%s", optarg); |
433 | format = | 398 | asprintf (&fmt, "%s%scommand name %s", fmt, (options ? ", " : ""), |
434 | strscat (format, | 399 | prog); |
435 | ssprintf (NULL, "%scommand name %s", (options ? ", " : ""), | ||
436 | prog)); | ||
437 | options |= PROG; | 400 | options |= PROG; |
438 | break; | 401 | break; |
439 | case 'a': /* args (full path name with args) */ | 402 | case 'a': /* args (full path name with args) */ |
440 | args = strscpy (args, optarg); | 403 | asprintf (&args, "%s", optarg); |
441 | format = | 404 | asprintf (&fmt, "%s%sargs %s", fmt, (options ? ", " : ""), args); |
442 | strscat (format, | ||
443 | ssprintf (NULL, "%sargs %s", (options ? ", " : ""), args)); | ||
444 | options |= ARGS; | 405 | options |= ARGS; |
445 | break; | 406 | break; |
446 | case 'v': /* command */ | 407 | case 'v': /* command */ |
@@ -448,7 +409,19 @@ call_getopt (int argc, char **argv) | |||
448 | break; | 409 | break; |
449 | } | 410 | } |
450 | } | 411 | } |
451 | return i; | 412 | |
413 | c = optind; | ||
414 | if (wmax == -1 && argv[c]) | ||
415 | wmax = atoi (argv[c++]); | ||
416 | if (cmax == -1 && argv[c]) | ||
417 | cmax = atoi (argv[c++]); | ||
418 | if (statopts == NULL && argv[c]) { | ||
419 | asprintf (&statopts, "%s", argv[c++]); | ||
420 | asprintf (&fmt, "%s%sSTATE = %s", fmt, (options ? ", " : ""), statopts); | ||
421 | options |= STAT; | ||
422 | } | ||
423 | |||
424 | return validate_arguments (); | ||
452 | } | 425 | } |
453 | 426 | ||
454 | 427 | ||
@@ -456,7 +429,7 @@ int | |||
456 | validate_arguments () | 429 | validate_arguments () |
457 | { | 430 | { |
458 | 431 | ||
459 | if (wmax >= 0 && wmin == -1) | 432 | if (wmax >= 0 && wmin == -1) |
460 | wmin = 0; | 433 | wmin = 0; |
461 | if (cmax >= 0 && cmin == -1) | 434 | if (cmax >= 0 && cmin == -1) |
462 | cmin = 0; | 435 | cmin = 0; |
@@ -471,18 +444,17 @@ validate_arguments () | |||
471 | } | 444 | } |
472 | } | 445 | } |
473 | 446 | ||
474 | if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) { | 447 | /* if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) { */ |
475 | printf ("At least one threshold must be set\n"); | 448 | /* printf ("At least one threshold must be set\n"); */ |
476 | return ERROR; | 449 | /* return ERROR; */ |
477 | } | 450 | /* } */ |
478 | 451 | ||
479 | if (options == 0) { | 452 | if (options == 0) { |
480 | options = 1; | 453 | options = 1; |
481 | format = ssprintf (format, "%%s - %%d processes running\n"); | 454 | asprintf (&fmt, "%%s - %%d processes running\n"); |
482 | } | 455 | } |
483 | else { | 456 | else { |
484 | format = | 457 | asprintf (&fmt, "%%s - %%d processes running with %s\n", fmt); |
485 | ssprintf (format, "%%s - %%d processes running with %s\n", format); | ||
486 | } | 458 | } |
487 | 459 | ||
488 | return options; | 460 | return options; |
@@ -490,16 +462,12 @@ validate_arguments () | |||
490 | 462 | ||
491 | 463 | ||
492 | void | 464 | void |
493 | print_help (char *cmd) | 465 | print_help (void) |
494 | { | 466 | { |
495 | print_revision (cmd, "$Revision$"); | 467 | print_revision (PROGNAME, REVISION); |
496 | printf | 468 | printf |
497 | ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n" | 469 | ("Copyright (c) %s %s <%s>\n\n%s\n", |
498 | "This plugin checks the number of currently running processes and\n" | 470 | COPYRIGHT, AUTHOR, EMAIL, SUMMARY); |
499 | "generates WARNING or CRITICAL states if the process count is outside\n" | ||
500 | "the specified threshold ranges. The process count can be filtered by\n" | ||
501 | "process owner, parent process PID, current state (e.g., 'Z'), or may\n" | ||
502 | "be the total number of running processes\n\n"); | ||
503 | print_usage (); | 471 | print_usage (); |
504 | printf | 472 | printf |
505 | ("\nRequired Arguments:\n" | 473 | ("\nRequired Arguments:\n" |