negate.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /******************************************************************************
  2. *
  3. * Program: Inverting plugin wrapper for Nagios
  4. * License: GPL
  5. *
  6. * License Information:
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * $Id$
  23. *
  24. *****************************************************************************/
  25. #define PROGNAME "negate"
  26. #define REVISION "$Revision$"
  27. #define COPYRIGHT "2002"
  28. #define AUTHOR "Karl DeBisschop"
  29. #define EMAIL "kdebisschop@users.sourceforge.net"
  30. #define SUMMARY "Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n"
  31. #define OPTIONS "\
  32. \[-t timeout] <definition of wrapped plugin>"
  33. #define LONGOPTIONS "\
  34. -t, --timeout=INTEGER\n\
  35. Terminate test if timeout limit is exceeded (default: %d)\n\
  36. [keep this less than the plugin timeout to retain CRITICAL status]\n"
  37. #define DESCRIPTION "\
  38. This plugin is a wrapper to take the output of another plugin and invert it.\n\
  39. If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
  40. If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.\n\
  41. Otherwise, the output state of the wrapped plugin is unchanged.\n"
  42. #define DEFAULT_TIMEOUT 9
  43. #include "common.h"
  44. #include "utils.h"
  45. #include "popen.h"
  46. char *command_line;
  47. static int process_arguments (int, char **);
  48. static int validate_arguments (void);
  49. static void print_usage (void);
  50. static void print_help (void);
  51. /******************************************************************************
  52. The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
  53. tags in the comments. With in the tags, the XML is assembled sequentially.
  54. You can define entities in tags. You also have all the #defines available as
  55. entities.
  56. Please note that all tags must be lowercase to use the DocBook XML DTD.
  57. @@-<article>
  58. <sect1>
  59. <title>Quick Reference</title>
  60. <!-- The refentry forms a manpage -->
  61. <refentry>
  62. <refmeta>
  63. <manvolnum>5<manvolnum>
  64. </refmeta>
  65. <refnamdiv>
  66. <refname>&PROGNAME;</refname>
  67. <refpurpose>&SUMMARY;</refpurpose>
  68. </refnamdiv>
  69. </refentry>
  70. </sect1>
  71. <sect1>
  72. <title>FAQ</title>
  73. </sect1>
  74. <sect1>
  75. <title>Theory, Installation, and Operation</title>
  76. <sect2>
  77. <title>General Description</title>
  78. <para>
  79. &DESCRIPTION;
  80. </para>
  81. </sect2>
  82. <sect2>
  83. <title>Future Enhancements</title>
  84. <para>ToDo List</para>
  85. <itemizedlist>
  86. <listitem>Add option to do regex substitution in output text</listitem>
  87. </itemizedlist>
  88. </sect2>
  89. <sect2>
  90. <title>Functions</title>
  91. -@@
  92. ******************************************************************************/
  93. int
  94. main (int argc, char **argv)
  95. {
  96. int found = 0, result = STATE_UNKNOWN;
  97. char input_buffer[MAX_INPUT_BUFFER];
  98. if (process_arguments (argc, argv) == ERROR)
  99. usage ("Could not parse arguments");
  100. /* Set signal handling and alarm */
  101. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
  102. terminate (STATE_UNKNOWN, "Cannot catch SIGALRM");
  103. (void) alarm ((unsigned) timeout_interval);
  104. child_process = spopen (command_line);
  105. if (child_process == NULL)
  106. terminate (STATE_UNKNOWN, "Could not open pipe: %s\n", command_line);
  107. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  108. if (child_stderr == NULL) {
  109. printf ("Could not open stderr for %s\n", command_line);
  110. }
  111. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  112. found++;
  113. if (strchr (input_buffer, '\n')) {
  114. input_buffer[strcspn (input_buffer, "\n")] = 0;
  115. printf ("%s\n", input_buffer);
  116. }
  117. else {
  118. printf ("%s\n", input_buffer);
  119. }
  120. }
  121. if (!found)
  122. terminate (STATE_UNKNOWN,\
  123. "%s problem - No data recieved from host\nCMD: %s\n",\
  124. argv[0], command_line);
  125. /* close the pipe */
  126. result = spclose (child_process);
  127. /* WARNING if output found on stderr */
  128. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  129. result = max_state (result, STATE_WARNING);
  130. /* close stderr */
  131. (void) fclose (child_stderr);
  132. if (result == STATE_OK)
  133. exit (STATE_CRITICAL);
  134. else if (result == STATE_CRITICAL)
  135. exit (EXIT_SUCCESS);
  136. else
  137. exit (result);
  138. }
  139. void
  140. print_help (void)
  141. {
  142. print_revision (PROGNAME, REVISION);
  143. printf
  144. ("Copyright (c) %s %s <%s>\n\n%s\n",
  145. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  146. print_usage ();
  147. printf
  148. ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n",
  149. DEFAULT_TIMEOUT);
  150. support ();
  151. }
  152. void
  153. print_usage (void)
  154. {
  155. printf ("Usage:\n" " %s %s\n"
  156. #ifdef HAVE_GETOPT_H
  157. " %s (-h | --help) for detailed help\n"
  158. " %s (-V | --version) for version information\n",
  159. #else
  160. " %s -h for detailed help\n"
  161. " %s -V for version information\n",
  162. #endif
  163. PROGNAME, OPTIONS, PROGNAME, PROGNAME);
  164. }
  165. /******************************************************************************
  166. @@-
  167. <sect3>
  168. <title>process_arguments</title>
  169. <para>This function parses the command line into the needed
  170. variables.</para>
  171. <para>Aside from the standard 'help' and 'version' options, there
  172. is a only a 'timeout' option.No validation is currently done.</para>
  173. </sect3>
  174. -@@
  175. ******************************************************************************/
  176. /* process command-line arguments */
  177. int
  178. process_arguments (int argc, char **argv)
  179. {
  180. int c;
  181. #ifdef HAVE_GETOPT_H
  182. int option_index = 0;
  183. static struct option long_options[] = {
  184. {"help", no_argument, 0, 'h'},
  185. {"version", no_argument, 0, 'V'},
  186. {"timeout", required_argument, 0, 't'},
  187. {0, 0, 0, 0}
  188. };
  189. #endif
  190. while (1) {
  191. #ifdef HAVE_GETOPT_H
  192. c = getopt_long (argc, argv, "+?hVt:",
  193. long_options, &option_index);
  194. #else
  195. c = getopt (argc, argv, "+?hVt:");
  196. #endif
  197. if (c == -1 || c == EOF)
  198. break;
  199. switch (c) {
  200. case '?': /* help */
  201. usage2 ("Unknown argument", optarg);
  202. case 'h': /* help */
  203. print_help ();
  204. exit (EXIT_SUCCESS);
  205. case 'V': /* version */
  206. print_revision (PROGNAME, REVISION);
  207. exit (EXIT_SUCCESS);
  208. case 't': /* timeout period */
  209. if (!is_integer (optarg))
  210. usage2 ("Timeout Interval must be an integer", optarg);
  211. timeout_interval = atoi (optarg);
  212. break;
  213. }
  214. }
  215. command_line = strscpy (command_line, argv[optind]);
  216. for (c = optind+1; c <= argc; c++) {
  217. asprintf (&command_line, "%s %s", command_line, argv[c]);
  218. }
  219. return validate_arguments ();
  220. }
  221. /******************************************************************************
  222. @@-
  223. <sect3>
  224. <title>validate_arguments</title>
  225. <para>No validation is currently done.</para>
  226. </sect3>
  227. -@@
  228. ******************************************************************************/
  229. int
  230. validate_arguments ()
  231. {
  232. return STATE_OK;
  233. }
  234. /******************************************************************************
  235. @@-
  236. </sect2>
  237. </sect1>
  238. </article>
  239. -@@
  240. ******************************************************************************/