negate.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. @@-<article>
  14. <sect1>
  15. <title>Quick Reference</title>
  16. <refentry>
  17. <refmeta><manvolnum>5<manvolnum></refmeta>
  18. <refnamdiv>
  19. <refname>&progname;</refname>
  20. <refpurpose>&SUMMARY;</refpurpose>
  21. </refnamdiv>
  22. </refentry>
  23. </sect1>
  24. <sect1>
  25. <title>FAQ</title>
  26. </sect1>
  27. <sect1>
  28. <title>Theory, Installation, and Operation</title>
  29. <sect2>
  30. <title>General Description</title>
  31. <para>
  32. &DESCRIPTION;
  33. </para>
  34. </sect2>
  35. <sect2>
  36. <title>Future Enhancements</title>
  37. <para>ToDo List</para>
  38. <itemizedlist>
  39. <listitem>Add option to do regex substitution in output text</listitem>
  40. </itemizedlist>
  41. </sect2>-@@
  42. ******************************************************************************/
  43. const char *progname = "negate";
  44. const char *revision = "$Revision$";
  45. const char *copyright = "2002-2003";
  46. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  47. #define DEFAULT_TIMEOUT 9
  48. #include "common.h"
  49. #include "utils.h"
  50. #include "popen.h"
  51. char *command_line;
  52. int process_arguments (int, char **);
  53. int validate_arguments (void);
  54. void print_help (void);
  55. void print_usage (void);
  56. int
  57. main (int argc, char **argv)
  58. {
  59. int found = 0, result = STATE_UNKNOWN;
  60. char *buf;
  61. if (process_arguments (argc, argv) == ERROR)
  62. usage (_("Could not parse arguments\n"));
  63. /* Set signal handling and alarm */
  64. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
  65. die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
  66. (void) alarm ((unsigned) timeout_interval);
  67. child_process = spopen (command_line);
  68. if (child_process == NULL)
  69. die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), command_line);
  70. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  71. if (child_stderr == NULL) {
  72. printf (_("Could not open stderr for %s\n"), command_line);
  73. }
  74. buf = malloc(MAX_INPUT_BUFFER);
  75. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
  76. found++;
  77. printf ("%s", buf);
  78. }
  79. if (!found)
  80. die (STATE_UNKNOWN,
  81. _("%s problem - No data recieved from host\nCMD: %s\n"),\
  82. argv[0], command_line);
  83. /* close the pipe */
  84. result = spclose (child_process);
  85. /* WARNING if output found on stderr */
  86. if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
  87. result = max_state (result, STATE_WARNING);
  88. /* close stderr */
  89. (void) fclose (child_stderr);
  90. if (result == STATE_OK)
  91. exit (STATE_CRITICAL);
  92. else if (result == STATE_CRITICAL)
  93. exit (EXIT_SUCCESS);
  94. else
  95. exit (result);
  96. }
  97. /******************************************************************************
  98. @@-
  99. <sect2>
  100. <title>Functions</title>
  101. <sect3>
  102. <title>process_arguments</title>
  103. <para>This function parses the command line into the needed
  104. variables.</para>
  105. <para>Aside from the standard 'help' and 'version' options, there
  106. is a only a 'timeout' option.</para>
  107. </sect3>
  108. -@@
  109. ******************************************************************************/
  110. /* process command-line arguments */
  111. int
  112. process_arguments (int argc, char **argv)
  113. {
  114. int c;
  115. int option = 0;
  116. static struct option longopts[] = {
  117. {"help", no_argument, 0, 'h'},
  118. {"version", no_argument, 0, 'V'},
  119. {"timeout", required_argument, 0, 't'},
  120. {0, 0, 0, 0}
  121. };
  122. while (1) {
  123. c = getopt_long (argc, argv, "+hVt:",
  124. longopts, &option);
  125. if (c == -1 || c == EOF)
  126. break;
  127. switch (c) {
  128. case '?': /* help */
  129. usage3 (_("Unknown argument"), optopt);
  130. break;
  131. case 'h': /* help */
  132. print_help ();
  133. exit (EXIT_SUCCESS);
  134. break;
  135. case 'V': /* version */
  136. print_revision (progname, revision);
  137. exit (EXIT_SUCCESS);
  138. case 't': /* timeout period */
  139. if (!is_integer (optarg))
  140. usage2 (_("Timeout Interval must be an integer"), optarg);
  141. else
  142. timeout_interval = atoi (optarg);
  143. break;
  144. }
  145. }
  146. asprintf (&command_line, "%s", argv[optind]);
  147. for (c = optind+1; c < argc; c++) {
  148. asprintf (&command_line, "%s %s", command_line, argv[c]);
  149. }
  150. return validate_arguments ();
  151. }
  152. /******************************************************************************
  153. @@-
  154. <sect3>
  155. <title>validate_arguments</title>
  156. <para>No validation is currently done.</para>
  157. </sect3>
  158. -@@
  159. ******************************************************************************/
  160. int
  161. validate_arguments ()
  162. {
  163. if (command_line == NULL)
  164. return ERROR;
  165. return STATE_OK;
  166. }
  167. /******************************************************************************
  168. @@-
  169. </sect2>
  170. </sect1>
  171. </article>
  172. -@@
  173. ******************************************************************************/
  174. void
  175. print_help (void)
  176. {
  177. print_revision (progname, revision);
  178. printf (_(COPYRIGHT), copyright, email);
  179. printf (_("\
  180. Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n\
  181. \n"));
  182. print_usage ();
  183. printf (_(UT_HELP_VRSN));
  184. printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
  185. printf (_("\
  186. [keep timeout than the plugin timeout to retain CRITICAL status]\n"));
  187. printf (_("\
  188. negate \"/usr/local/nagios/libexec/check_ping -H host\"\n\
  189. Run check_ping and invert result. Must use full path to plugin\n\
  190. negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\"\n\
  191. Use single quotes if you need to retain spaces\n"));
  192. printf (_("\
  193. This plugin is a wrapper to take the output of another plugin and invert it.\n\
  194. If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
  195. If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.\n\
  196. Otherwise, the output state of the wrapped plugin is unchanged.\n"));
  197. printf (_(UT_SUPPORT));
  198. }
  199. void
  200. print_usage (void)
  201. {
  202. printf (_("Usage: %s [-t timeout] <definition of wrapped plugin>\n"),
  203. progname);
  204. printf (_(UT_HLP_VRS), progname, progname);
  205. }