negate.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "utils_cmd.h"
  71. //char *command_line;
  72. static const char **process_arguments (int, char **);
  73. int validate_arguments (char **);
  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. char **command_line;
  82. output chld_out, chld_err;
  83. int i;
  84. setlocale (LC_ALL, "");
  85. bindtextdomain (PACKAGE, LOCALEDIR);
  86. textdomain (PACKAGE);
  87. command_line = (char **) process_arguments (argc, argv);
  88. /* Set signal handling and alarm */
  89. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
  90. die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
  91. (void) alarm ((unsigned) timeout_interval);
  92. /* catch when the command is quoted */
  93. if(command_line[1] == NULL) {
  94. result = cmd_run (command_line[0], &chld_out, &chld_err, 0);
  95. } else {
  96. result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
  97. }
  98. if (chld_err.lines > 0) {
  99. printf ("Error output from command:\n");
  100. for (i = 0; i < chld_err.lines; i++) {
  101. printf ("%s\n", chld_err.line[i]);
  102. }
  103. exit (STATE_WARNING);
  104. }
  105. if (chld_out.lines == 0)
  106. die (STATE_UNKNOWN, _("No data returned from command\n"));
  107. for (i = 0; i < chld_out.lines; i++) {
  108. printf ("%s\n", chld_out.line[i]);
  109. }
  110. if (result == STATE_OK)
  111. exit (STATE_CRITICAL);
  112. else if (result == STATE_CRITICAL)
  113. exit (EXIT_SUCCESS);
  114. else
  115. exit (result);
  116. }
  117. /******************************************************************************
  118. @@-
  119. <sect2>
  120. <title>Functions</title>
  121. <sect3>
  122. <title>process_arguments</title>
  123. <para>This function parses the command line into the needed
  124. variables.</para>
  125. <para>Aside from the standard 'help' and 'version' options, there
  126. is a only a 'timeout' option.</para>
  127. </sect3>
  128. -@@
  129. ******************************************************************************/
  130. /* process command-line arguments */
  131. static const char **
  132. process_arguments (int argc, char **argv)
  133. {
  134. int c;
  135. int option = 0;
  136. static struct option longopts[] = {
  137. {"help", no_argument, 0, 'h'},
  138. {"version", no_argument, 0, 'V'},
  139. {"timeout", required_argument, 0, 't'},
  140. {0, 0, 0, 0}
  141. };
  142. while (1) {
  143. c = getopt_long (argc, argv, "+hVt:", longopts, &option);
  144. if (c == -1 || c == EOF)
  145. break;
  146. switch (c) {
  147. case '?': /* help */
  148. usage5 ();
  149. break;
  150. case 'h': /* help */
  151. print_help ();
  152. exit (EXIT_SUCCESS);
  153. break;
  154. case 'V': /* version */
  155. print_revision (progname, revision);
  156. exit (EXIT_SUCCESS);
  157. case 't': /* timeout period */
  158. if (!is_integer (optarg))
  159. usage2 (_("Timeout interval must be a positive integer"), optarg);
  160. else
  161. timeout_interval = atoi (optarg);
  162. break;
  163. }
  164. }
  165. validate_arguments (&argv[optind]);
  166. return (const char **) &argv[optind];
  167. }
  168. /******************************************************************************
  169. @@-
  170. <sect3>
  171. <title>validate_arguments</title>
  172. <para>No validation is currently done.</para>
  173. </sect3>
  174. -@@
  175. ******************************************************************************/
  176. int
  177. validate_arguments (char **command_line)
  178. {
  179. if (command_line[0] == NULL)
  180. usage4 (_("Could not parse arguments"));
  181. if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0)
  182. usage4 (_("Require path to command"));
  183. }
  184. /******************************************************************************
  185. @@-
  186. </sect2>
  187. </sect1>
  188. </article>
  189. -@@
  190. ******************************************************************************/
  191. void
  192. print_help (void)
  193. {
  194. print_revision (progname, revision);
  195. printf (COPYRIGHT, copyright, email);
  196. printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL, and vice-versa)."));
  197. printf ("\n\n");
  198. print_usage ();
  199. printf (_(UT_HELP_VRSN));
  200. printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
  201. printf (" %s\n", _("[keep timeout than the plugin timeout to retain CRITICAL status]"));
  202. printf ("\n");
  203. printf ("%s\n", _("Examples:"));
  204. printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
  205. printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
  206. printf (" %s\n", "negate /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
  207. printf (" %s\n", _("Use single quotes if you need to retain spaces"));
  208. printf (_(UT_VERBOSE));
  209. printf ("\n");
  210. printf ("%s\n", _("Notes:"));
  211. printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
  212. printf ("%s\n", _("The full path of the plugin must be provided."));
  213. printf ("%s\n", _("If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL."));
  214. printf ("%s\n", _("If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK."));
  215. printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
  216. printf (_(UT_SUPPORT));
  217. }
  218. void
  219. print_usage (void)
  220. {
  221. printf (_("Usage:"));
  222. printf ("%s [-t timeout] <definition of wrapped plugin>\n",progname);
  223. }