negate.c 6.9 KB

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