4
0

negate.c 6.8 KB

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