check_vsz.c 7.6 KB

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