negate.c 6.8 KB

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