check_vsz.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #define 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 (char *cmd);
  46. void print_usage (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 = NULL;
  60. if (!process_arguments (argc, argv)) {
  61. printf ("%s: failure parsing arguments\n", my_basename (argv[0]));
  62. print_help (my_basename (argv[0]));
  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. message = malloc ((size_t) 1);
  75. message[0] = 0;
  76. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  77. line++;
  78. /* skip the first line */
  79. if (line == 1)
  80. continue;
  81. if (sscanf (input_buffer, VSZ_FORMAT, &proc_size, proc_name) == 2) {
  82. if (proc == NULL) {
  83. if (proc_size > warn) {
  84. len = strlen (message) + strlen (proc_name) + 23;
  85. message = realloc (message, len);
  86. if (message == NULL)
  87. terminate (STATE_UNKNOWN,
  88. "check_vsz: could not malloc message (1)");
  89. sprintf (message, "%s %s(%d)", message, proc_name, proc_size);
  90. result = max_state (result, STATE_WARNING);
  91. }
  92. if (proc_size > crit) {
  93. result = STATE_CRITICAL;
  94. }
  95. }
  96. else if (strstr (proc_name, proc)) {
  97. len = strlen (message) + 21;
  98. message = realloc (message, len);
  99. if (message == NULL)
  100. terminate (STATE_UNKNOWN,
  101. "check_vsz: could not malloc message (2)");
  102. sprintf (message, "%s %d", message, proc_size);
  103. if (proc_size > warn) {
  104. result = max_state (result, STATE_WARNING);
  105. }
  106. if (proc_size > crit) {
  107. result = STATE_CRITICAL;
  108. }
  109. }
  110. }
  111. }
  112. /* If we get anything on STDERR, at least set warning */
  113. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  114. result = max_state (result, STATE_WARNING);
  115. (void) fclose (child_stderr);
  116. /* close the pipe */
  117. if (spclose (child_process))
  118. result = max_state (result, STATE_WARNING);
  119. if (result == STATE_OK)
  120. printf ("ok (all VSZ<%d): %s\n", warn, message);
  121. else if (result == STATE_UNKNOWN)
  122. printf ("Unable to read output\n");
  123. else if (result == STATE_WARNING)
  124. printf ("WARNING (VSZ>%d):%s\n", warn, message);
  125. else
  126. printf ("CRITICAL (VSZ>%d):%s\n", crit, message);
  127. return result;
  128. }
  129. int
  130. process_arguments (int argc, char **argv)
  131. {
  132. int c;
  133. #ifdef HAVE_GETOPT_H
  134. int option_index = 0;
  135. static struct option long_options[] = {
  136. {"help", no_argument, 0, 'h'},
  137. {"version", no_argument, 0, 'V'},
  138. {"critical", required_argument, 0, 'c'},
  139. {"warning", required_argument, 0, 'w'},
  140. {"command", required_argument, 0, 'C'},
  141. {0, 0, 0, 0}
  142. };
  143. #endif
  144. if (argc < 2)
  145. return ERROR;
  146. while (1) {
  147. #ifdef HAVE_GETOPT_H
  148. c = getopt_long (argc, argv, "+hVc:w:C:", long_options, &option_index);
  149. #else
  150. c = getopt (argc, argv, "+hVc:w:C:");
  151. #endif
  152. if (c == EOF)
  153. break;
  154. switch (c) {
  155. case '?': /* help */
  156. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  157. print_usage (my_basename (argv[0]));
  158. exit (STATE_UNKNOWN);
  159. case 'h': /* help */
  160. print_help (my_basename (argv[0]));
  161. exit (STATE_OK);
  162. case 'V': /* version */
  163. print_revision (my_basename (argv[0]), "$Revision$");
  164. exit (STATE_OK);
  165. case 'c': /* critical threshold */
  166. if (!is_intnonneg (optarg)) {
  167. printf ("%s: critical threshold must be an integer: %s\n",
  168. my_basename (argv[0]), optarg);
  169. print_usage (my_basename (argv[0]));
  170. exit (STATE_UNKNOWN);
  171. }
  172. crit = atoi (optarg);
  173. break;
  174. case 'w': /* warning threshold */
  175. if (!is_intnonneg (optarg)) {
  176. printf ("%s: warning threshold must be an integer: %s\n",
  177. my_basename (argv[0]), optarg);
  178. print_usage (my_basename (argv[0]));
  179. exit (STATE_UNKNOWN);
  180. }
  181. warn = atoi (optarg);
  182. break;
  183. case 'C': /* command name */
  184. proc = malloc (strlen (optarg) + 1);
  185. if (proc == NULL)
  186. terminate (STATE_UNKNOWN,
  187. "check_vsz: failed malloc of proc in process_arguments");
  188. strcpy (proc, optarg);
  189. break;
  190. }
  191. }
  192. c = optind;
  193. if (warn == -1) {
  194. if (!is_intnonneg (argv[c])) {
  195. printf ("%s: critical threshold must be an integer: %s\n",
  196. PROGNAME, argv[c]);
  197. print_usage (PROGNAME);
  198. exit (STATE_UNKNOWN);
  199. }
  200. warn = atoi (argv[c++]);
  201. }
  202. if (crit == -1) {
  203. if (!is_intnonneg (argv[c])) {
  204. printf ("%s: critical threshold must be an integer: %s\n",
  205. PROGNAME, argv[c]);
  206. print_usage (PROGNAME);
  207. exit (STATE_UNKNOWN);
  208. }
  209. crit = atoi (argv[c++]);
  210. }
  211. if (proc == NULL) {
  212. proc = malloc (strlen (argv[c]) + 1);
  213. if (proc == NULL)
  214. terminate (STATE_UNKNOWN,
  215. "check_vsz: failed malloc of proc in process_arguments");
  216. strcpy (proc, argv[c]);
  217. }
  218. return c;
  219. }
  220. void
  221. print_usage (char *cmd)
  222. {
  223. printf ("Usage: %s -w <wsize> -c <csize> [-C command]\n"
  224. " %s --help\n" " %s --version\n", cmd, cmd, cmd);
  225. }
  226. void
  227. print_help (char *cmd)
  228. {
  229. print_revision ("check_vsz", "$Revision$");
  230. printf
  231. ("Copyright (c) 2000 Karl DeBisschop <kdebiss@alum.mit.edu>\n\n"
  232. "This plugin checks the image size of a running program and returns an\n"
  233. "error if the number is above either of the thresholds given.\n\n");
  234. print_usage (cmd);
  235. printf
  236. ("\nOptions:\n"
  237. " -h, --help\n"
  238. " Print detailed help\n"
  239. " -V, --version\n"
  240. " Print version numbers and license information\n"
  241. " -w, --warning=INTEGER\n"
  242. " Program image size necessary to cause a WARNING state\n"
  243. " -c, --critical=INTEGER\n"
  244. " Program image size necessary to cause a CRITICAL state\n"
  245. " -C, --command=STRING\n" " Program to search for [optional]\n");
  246. }