negate.c 6.3 KB

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