check_vsz.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /******************************************************************************
  2. *
  3. * CHECK_VSZ.C
  4. *
  5. * Program: Process plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999,2000 Karl DeBisschop <kdebiss@alum.mit.edu>
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Description:
  12. *
  13. * This plugin will check for processes whose total image size exceeds
  14. * the warning or critical thresholds given on the command line. With
  15. * no command_name, everything that shows up on ps is evaluated.
  16. * Otherwise, only jobs with the command_name given are examined.
  17. * This program is particularly useful if you have to run a piece of
  18. * commercial software that has a memory leak. With it you can shut
  19. * down and restart the processes whenever the program threatens to
  20. * take over your system.
  21. *
  22. * Modifications:
  23. *
  24. * 11-18-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
  25. * change to getopt, use print_help
  26. * 08-18-1999 Ethan Galstad (nagios@nagios.org)
  27. * Changed code to use common include file
  28. * Changed fclose() to pclose()
  29. * 09-09-1999 Ethan Galstad (nagios@nagios.org)
  30. * Changed popen()/pclose() to spopen()/spclose()
  31. * 11-18-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
  32. * set STATE_WARNING of stderr written or nonzero status returned
  33. *
  34. *****************************************************************************/
  35. const char *progname = "check_vsz";
  36. #define REVISION "$Revision$"
  37. #define COPYRIGHT "1999-2002"
  38. #define AUTHOR "Karl DeBisschop"
  39. #define EMAIL "karl@debisschop.net"
  40. #define SUMMARY "Check the image size of a running program.\n"
  41. #include "common.h"
  42. #include "popen.h"
  43. #include "utils.h"
  44. int process_arguments (int argc, char **argv);
  45. void print_help (const char *cmd);
  46. void print_usage (const char *cmd);
  47. int warn = -1;
  48. int crit = -1;
  49. char *proc = NULL;
  50. int
  51. main (int argc, char **argv)
  52. {
  53. int len;
  54. int result = STATE_OK;
  55. int line = 0;
  56. int proc_size = -1;
  57. char input_buffer[MAX_INPUT_BUFFER];
  58. char proc_name[MAX_INPUT_BUFFER];
  59. char *message = "";
  60. if (process_arguments (argc, argv) == ERROR) {
  61. printf ("%s: failure parsing arguments\n", progname);
  62. print_help (progname);
  63. return STATE_UNKNOWN;
  64. }
  65. /* run the command */
  66. child_process = spopen (VSZ_COMMAND);
  67. if (child_process == NULL) {
  68. printf ("Unable to open pipe: %s\n", VSZ_COMMAND);
  69. return STATE_UNKNOWN;
  70. }
  71. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  72. if (child_stderr == NULL)
  73. printf ("Could not open stderr for %s\n", VSZ_COMMAND);
  74. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  75. line++;
  76. /* skip the first line */
  77. if (line == 1)
  78. continue;
  79. if (sscanf (input_buffer, VSZ_FORMAT, &proc_size, proc_name) == 2) {
  80. if (proc == NULL) {
  81. if (proc_size > warn) {
  82. asprintf (&message, "%s %s(%d)", message, proc_name, proc_size);
  83. result = max_state (result, STATE_WARNING);
  84. }
  85. if (proc_size > crit) {
  86. result = STATE_CRITICAL;
  87. }
  88. }
  89. else if (strstr (proc_name, proc)) {
  90. asprintf (&message, "%s %d", message, proc_size);
  91. if (proc_size > warn) {
  92. result = max_state (result, STATE_WARNING);
  93. }
  94. if (proc_size > crit) {
  95. result = STATE_CRITICAL;
  96. }
  97. }
  98. }
  99. }
  100. /* If we get anything on STDERR, at least set warning */
  101. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  102. result = max_state (result, STATE_WARNING);
  103. (void) fclose (child_stderr);
  104. /* close the pipe */
  105. if (spclose (child_process))
  106. result = max_state (result, STATE_WARNING);
  107. if (result == STATE_OK)
  108. printf ("ok (all VSZ<%d): %s\n", warn, message);
  109. else if (result == STATE_UNKNOWN)
  110. printf ("Unable to read output\n");
  111. else if (result == STATE_WARNING)
  112. printf ("WARNING (VSZ>%d):%s\n", warn, message);
  113. else
  114. printf ("CRITICAL (VSZ>%d):%s\n", crit, message);
  115. return result;
  116. }
  117. int
  118. process_arguments (int argc, char **argv)
  119. {
  120. int c;
  121. int option_index = 0;
  122. static struct option long_options[] = {
  123. {"help", no_argument, 0, 'h'},
  124. {"version", no_argument, 0, 'V'},
  125. {"critical", required_argument, 0, 'c'},
  126. {"warning", required_argument, 0, 'w'},
  127. {"command", required_argument, 0, 'C'},
  128. {0, 0, 0, 0}
  129. };
  130. if (argc < 2)
  131. return ERROR;
  132. while (1) {
  133. c = getopt_long (argc, argv, "+hVc:w:C:", long_options, &option_index);
  134. if (c == EOF)
  135. break;
  136. switch (c) {
  137. case '?': /* help */
  138. print_usage (progname);
  139. exit (STATE_UNKNOWN);
  140. case 'h': /* help */
  141. print_help (progname);
  142. exit (STATE_OK);
  143. case 'V': /* version */
  144. print_revision (progname, "$Revision$");
  145. exit (STATE_OK);
  146. case 'c': /* critical threshold */
  147. if (!is_intnonneg (optarg)) {
  148. printf ("%s: critical threshold must be an integer: %s\n",
  149. progname, optarg);
  150. print_usage (progname);
  151. exit (STATE_UNKNOWN);
  152. }
  153. crit = atoi (optarg);
  154. break;
  155. case 'w': /* warning threshold */
  156. if (!is_intnonneg (optarg)) {
  157. printf ("%s: warning threshold must be an integer: %s\n",
  158. progname, optarg);
  159. print_usage (progname);
  160. exit (STATE_UNKNOWN);
  161. }
  162. warn = atoi (optarg);
  163. break;
  164. case 'C': /* command name */
  165. proc = optarg;
  166. break;
  167. }
  168. }
  169. c = optind;
  170. if (warn == -1) {
  171. if (!is_intnonneg (argv[c])) {
  172. printf ("%s: critical threshold must be an integer: %s\n",
  173. progname, argv[c]);
  174. print_usage (progname);
  175. exit (STATE_UNKNOWN);
  176. }
  177. warn = atoi (argv[c++]);
  178. }
  179. if (crit == -1) {
  180. if (!is_intnonneg (argv[c])) {
  181. printf ("%s: critical threshold must be an integer: %s\n",
  182. progname, argv[c]);
  183. print_usage (progname);
  184. exit (STATE_UNKNOWN);
  185. }
  186. crit = atoi (argv[c++]);
  187. }
  188. if (proc == NULL)
  189. proc = argv[c];
  190. return c;
  191. }
  192. void
  193. print_usage (const char *cmd)
  194. {
  195. printf ("Usage: %s -w <wsize> -c <csize> [-C command]\n"
  196. " %s --help\n" " %s --version\n", cmd, cmd, cmd);
  197. }
  198. void
  199. print_help (const char *cmd)
  200. {
  201. print_revision ("check_vsz", "$Revision$");
  202. printf
  203. ("Copyright (c) 2000 Karl DeBisschop <kdebiss@alum.mit.edu>\n\n"
  204. "This plugin checks the image size of a running program and returns an\n"
  205. "error if the number is above either of the thresholds given.\n\n");
  206. print_usage (cmd);
  207. printf
  208. ("\nOptions:\n"
  209. " -h, --help\n"
  210. " Print detailed help\n"
  211. " -V, --version\n"
  212. " Print version numbers and license information\n"
  213. " -w, --warning=INTEGER\n"
  214. " Program image size necessary to cause a WARNING state\n"
  215. " -c, --critical=INTEGER\n"
  216. " Program image size necessary to cause a CRITICAL state\n"
  217. " -C, --command=STRING\n" " Program to search for [optional]\n");
  218. }