negate.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. printf ("Cannot catch SIGALRM");
  103. return STATE_UNKNOWN;
  104. }
  105. alarm (timeout_interval);
  106. child_process = spopen (command_line);
  107. if (child_process == NULL) {
  108. printf ("Could not open pipe: %s\n", command_line);
  109. exit (STATE_UNKNOWN);
  110. }
  111. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  112. if (child_stderr == NULL) {
  113. printf ("Could not open stderr for %s\n", command_line);
  114. }
  115. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  116. found++;
  117. if (index (input_buffer, '\n')) {
  118. input_buffer[strcspn (input_buffer, "\n")] = 0;
  119. printf ("%s\n", input_buffer);
  120. }
  121. else {
  122. printf ("%s\n", input_buffer);
  123. }
  124. }
  125. if (!found) {
  126. printf ("%s problem - No data recieved from host\nCMD: %s\n", argv[0],
  127. command_line);
  128. exit (STATE_UNKNOWN);
  129. }
  130. /* close the pipe */
  131. result = spclose (child_process);
  132. /* WARNING if output found on stderr */
  133. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  134. result = max_state (result, STATE_WARNING);
  135. /* close stderr */
  136. (void) fclose (child_stderr);
  137. if (result == STATE_OK)
  138. return STATE_CRITICAL;
  139. else if (result == STATE_CRITICAL)
  140. return STATE_OK;
  141. else
  142. return result;
  143. }
  144. void
  145. print_help (void)
  146. {
  147. print_revision (PROGNAME, REVISION);
  148. printf
  149. ("Copyright (c) %s %s <%s>\n\n%s\n",
  150. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  151. print_usage ();
  152. printf
  153. ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n",
  154. DEFAULT_TIMEOUT);
  155. support ();
  156. }
  157. void
  158. print_usage (void)
  159. {
  160. printf ("Usage:\n" " %s %s\n"
  161. #ifdef HAVE_GETOPT_H
  162. " %s (-h | --help) for detailed help\n"
  163. " %s (-V | --version) for version information\n",
  164. #else
  165. " %s -h for detailed help\n"
  166. " %s -V for version information\n",
  167. #endif
  168. PROGNAME, OPTIONS, PROGNAME, PROGNAME);
  169. }
  170. /******************************************************************************
  171. @@-
  172. <sect3>
  173. <title>process_arguments</title>
  174. <para>This function parses the command line into the needed
  175. variables.</para>
  176. <para>Aside from the standard 'help' and 'version' options, there
  177. is a only a 'timeout' option.No validation is currently done.</para>
  178. </sect3>
  179. -@@
  180. ******************************************************************************/
  181. /* process command-line arguments */
  182. int
  183. process_arguments (int argc, char **argv)
  184. {
  185. int c;
  186. #ifdef HAVE_GETOPT_H
  187. int option_index = 0;
  188. static struct option long_options[] = {
  189. {"help", no_argument, 0, 'h'},
  190. {"version", no_argument, 0, 'V'},
  191. {"timeout", required_argument, 0, 't'},
  192. {0, 0, 0, 0}
  193. };
  194. #endif
  195. while (1) {
  196. #ifdef HAVE_GETOPT_H
  197. c = getopt_long (argc, argv, "+?hVt:",
  198. long_options, &option_index);
  199. #else
  200. c = getopt (argc, argv, "+?hVt:");
  201. #endif
  202. if (c == -1 || c == EOF)
  203. break;
  204. switch (c) {
  205. case '?': /* help */
  206. usage2 ("Unknown argument", optarg);
  207. case 'h': /* help */
  208. print_help ();
  209. exit (STATE_OK);
  210. case 'V': /* version */
  211. print_revision (PROGNAME, REVISION);
  212. exit (STATE_OK);
  213. case 't': /* timeout period */
  214. if (!is_integer (optarg))
  215. usage2 ("Timeout Interval must be an integer", optarg);
  216. timeout_interval = atoi (optarg);
  217. break;
  218. }
  219. }
  220. command_line = strscpy (command_line, argv[optind]);
  221. for (c = optind+1; c <= argc; c++) {
  222. command_line = ssprintf (command_line, "%s %s", command_line, argv[c]);
  223. }
  224. return validate_arguments ();
  225. }
  226. /******************************************************************************
  227. @@-
  228. <sect3>
  229. <title>validate_arguments</title>
  230. <para>No validation is currently done.</para>
  231. </sect3>
  232. -@@
  233. ******************************************************************************/
  234. int
  235. validate_arguments ()
  236. {
  237. return OK;
  238. }
  239. /******************************************************************************
  240. @@-
  241. </sect2>
  242. </sect1>
  243. </article>
  244. -@@
  245. ******************************************************************************/