negate.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. const char *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. int process_arguments (int, char **);
  48. int validate_arguments (void);
  49. void print_usage (void);
  50. 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\n");
  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. " %s (-h | --help) for detailed help\n"
  157. " %s (-V | --version) for version information\n",
  158. progname, OPTIONS, progname, progname);
  159. }
  160. /******************************************************************************
  161. @@-
  162. <sect3>
  163. <title>process_arguments</title>
  164. <para>This function parses the command line into the needed
  165. variables.</para>
  166. <para>Aside from the standard 'help' and 'version' options, there
  167. is a only a 'timeout' option.No validation is currently done.</para>
  168. </sect3>
  169. -@@
  170. ******************************************************************************/
  171. /* process command-line arguments */
  172. int
  173. process_arguments (int argc, char **argv)
  174. {
  175. int c;
  176. int option_index = 0;
  177. static struct option long_options[] = {
  178. {"help", no_argument, 0, 'h'},
  179. {"version", no_argument, 0, 'V'},
  180. {"timeout", required_argument, 0, 't'},
  181. {0, 0, 0, 0}
  182. };
  183. while (1) {
  184. c = getopt_long (argc, argv, "+hVt:",
  185. long_options, &option_index);
  186. if (c == -1 || c == EOF)
  187. break;
  188. switch (c) {
  189. case '?': /* help */
  190. usage3 ("Unknown argument", optopt);
  191. case 'h': /* help */
  192. print_help ();
  193. exit (EXIT_SUCCESS);
  194. case 'V': /* version */
  195. print_revision (progname, REVISION);
  196. exit (EXIT_SUCCESS);
  197. case 't': /* timeout period */
  198. if (!is_integer (optarg))
  199. usage2 ("Timeout Interval must be an integer", optarg);
  200. timeout_interval = atoi (optarg);
  201. break;
  202. }
  203. }
  204. asprintf (&command_line, "%s", argv[optind]);
  205. for (c = optind+1; c < argc; c++) {
  206. asprintf (&command_line, "%s %s", command_line, argv[c]);
  207. }
  208. return validate_arguments ();
  209. }
  210. /******************************************************************************
  211. @@-
  212. <sect3>
  213. <title>validate_arguments</title>
  214. <para>No validation is currently done.</para>
  215. </sect3>
  216. -@@
  217. ******************************************************************************/
  218. int
  219. validate_arguments ()
  220. {
  221. if (command_line == NULL)
  222. return ERROR;
  223. return STATE_OK;
  224. }
  225. /******************************************************************************
  226. @@-
  227. </sect2>
  228. </sect1>
  229. </article>
  230. -@@
  231. ******************************************************************************/