check_vsz.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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)) {
  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. #ifdef HAVE_GETOPT_H
  122. int option_index = 0;
  123. static struct option long_options[] = {
  124. {"help", no_argument, 0, 'h'},
  125. {"version", no_argument, 0, 'V'},
  126. {"critical", required_argument, 0, 'c'},
  127. {"warning", required_argument, 0, 'w'},
  128. {"command", required_argument, 0, 'C'},
  129. {0, 0, 0, 0}
  130. };
  131. #endif
  132. if (argc < 2)
  133. return ERROR;
  134. while (1) {
  135. #ifdef HAVE_GETOPT_H
  136. c = getopt_long (argc, argv, "+hVc:w:C:", long_options, &option_index);
  137. #else
  138. c = getopt (argc, argv, "+hVc:w:C:");
  139. #endif
  140. if (c == EOF)
  141. break;
  142. switch (c) {
  143. case '?': /* help */
  144. printf ("%s: Unknown argument: %s\n\n", progname, optarg);
  145. print_usage (progname);
  146. exit (STATE_UNKNOWN);
  147. case 'h': /* help */
  148. print_help (progname);
  149. exit (STATE_OK);
  150. case 'V': /* version */
  151. print_revision (progname, "$Revision$");
  152. exit (STATE_OK);
  153. case 'c': /* critical threshold */
  154. if (!is_intnonneg (optarg)) {
  155. printf ("%s: critical threshold must be an integer: %s\n",
  156. progname, optarg);
  157. print_usage (progname);
  158. exit (STATE_UNKNOWN);
  159. }
  160. crit = atoi (optarg);
  161. break;
  162. case 'w': /* warning threshold */
  163. if (!is_intnonneg (optarg)) {
  164. printf ("%s: warning threshold must be an integer: %s\n",
  165. progname, optarg);
  166. print_usage (progname);
  167. exit (STATE_UNKNOWN);
  168. }
  169. warn = atoi (optarg);
  170. break;
  171. case 'C': /* command name */
  172. proc = optarg;
  173. break;
  174. }
  175. }
  176. c = optind;
  177. if (warn == -1) {
  178. if (!is_intnonneg (argv[c])) {
  179. printf ("%s: critical threshold must be an integer: %s\n",
  180. progname, argv[c]);
  181. print_usage (progname);
  182. exit (STATE_UNKNOWN);
  183. }
  184. warn = atoi (argv[c++]);
  185. }
  186. if (crit == -1) {
  187. if (!is_intnonneg (argv[c])) {
  188. printf ("%s: critical threshold must be an integer: %s\n",
  189. progname, argv[c]);
  190. print_usage (progname);
  191. exit (STATE_UNKNOWN);
  192. }
  193. crit = atoi (argv[c++]);
  194. }
  195. if (proc == NULL)
  196. proc = argv[c];
  197. return c;
  198. }
  199. void
  200. print_usage (const char *cmd)
  201. {
  202. printf ("Usage: %s -w <wsize> -c <csize> [-C command]\n"
  203. " %s --help\n" " %s --version\n", cmd, cmd, cmd);
  204. }
  205. void
  206. print_help (const char *cmd)
  207. {
  208. print_revision ("check_vsz", "$Revision$");
  209. printf
  210. ("Copyright (c) 2000 Karl DeBisschop <kdebiss@alum.mit.edu>\n\n"
  211. "This plugin checks the image size of a running program and returns an\n"
  212. "error if the number is above either of the thresholds given.\n\n");
  213. print_usage (cmd);
  214. printf
  215. ("\nOptions:\n"
  216. " -h, --help\n"
  217. " Print detailed help\n"
  218. " -V, --version\n"
  219. " Print version numbers and license information\n"
  220. " -w, --warning=INTEGER\n"
  221. " Program image size necessary to cause a WARNING state\n"
  222. " -c, --critical=INTEGER\n"
  223. " Program image size necessary to cause a CRITICAL state\n"
  224. " -C, --command=STRING\n" " Program to search for [optional]\n");
  225. }