negate.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /******************************************************************************
  2. *
  3. * Nagios negate plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2002-2007 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-2007";
  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. static int state[4] = {
  77. STATE_OK,
  78. STATE_WARNING,
  79. STATE_CRITICAL,
  80. STATE_UNKNOWN,
  81. };
  82. int
  83. main (int argc, char **argv)
  84. {
  85. int found = 0, result = STATE_UNKNOWN;
  86. char *buf;
  87. char **command_line;
  88. output chld_out, chld_err;
  89. int i;
  90. setlocale (LC_ALL, "");
  91. bindtextdomain (PACKAGE, LOCALEDIR);
  92. textdomain (PACKAGE);
  93. command_line = (char **) process_arguments (argc, argv);
  94. /* Set signal handling and alarm */
  95. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
  96. die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
  97. (void) alarm ((unsigned) timeout_interval);
  98. /* catch when the command is quoted */
  99. if(command_line[1] == NULL) {
  100. result = cmd_run (command_line[0], &chld_out, &chld_err, 0);
  101. } else {
  102. result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
  103. }
  104. if (chld_err.lines > 0) {
  105. printf ("Error output from command:\n");
  106. for (i = 0; i < chld_err.lines; i++) {
  107. printf ("%s\n", chld_err.line[i]);
  108. }
  109. exit (STATE_WARNING);
  110. }
  111. if (chld_out.lines == 0)
  112. die (STATE_UNKNOWN, _("No data returned from command\n"));
  113. for (i = 0; i < chld_out.lines; i++) {
  114. printf ("%s\n", chld_out.line[i]);
  115. }
  116. if (result >= 0 && result <= 4) {
  117. exit (state[result]);
  118. } else {
  119. exit (result);
  120. }
  121. }
  122. /******************************************************************************
  123. @@-
  124. <sect2>
  125. <title>Functions</title>
  126. <sect3>
  127. <title>process_arguments</title>
  128. <para>This function parses the command line into the needed
  129. variables.</para>
  130. <para>Aside from the standard 'help' and 'version' options, there
  131. is a only a 'timeout' option.</para>
  132. </sect3>
  133. -@@
  134. ******************************************************************************/
  135. /* process command-line arguments */
  136. static const char **
  137. process_arguments (int argc, char **argv)
  138. {
  139. int c;
  140. int permute = TRUE;
  141. int option = 0;
  142. static struct option longopts[] = {
  143. {"help", no_argument, 0, 'h'},
  144. {"version", no_argument, 0, 'V'},
  145. {"timeout", required_argument, 0, 't'},
  146. {"ok", required_argument, 0, 'o'},
  147. {"warning", required_argument, 0, 'w'},
  148. {"critical", required_argument, 0, 'c'},
  149. {"unknown", required_argument, 0, 'u'},
  150. {0, 0, 0, 0}
  151. };
  152. while (1) {
  153. c = getopt_long (argc, argv, "+hVt:o:w:c:u:", longopts, &option);
  154. if (c == -1 || c == EOF)
  155. break;
  156. switch (c) {
  157. case '?': /* help */
  158. usage5 ();
  159. break;
  160. case 'h': /* help */
  161. print_help ();
  162. exit (EXIT_SUCCESS);
  163. break;
  164. case 'V': /* version */
  165. print_revision (progname, revision);
  166. exit (EXIT_SUCCESS);
  167. case 't': /* timeout period */
  168. if (!is_integer (optarg))
  169. usage2 (_("Timeout interval must be a positive integer"), optarg);
  170. else
  171. timeout_interval = atoi (optarg);
  172. break;
  173. case 'o': /* replacement for OK */
  174. if ((state[STATE_OK] = translate_state(optarg)) == ERROR)
  175. usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-4)."));
  176. permute = FALSE;
  177. break;
  178. case 'w': /* replacement for WARNING */
  179. if ((state[STATE_WARNING] = translate_state(optarg)) == ERROR)
  180. usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  181. permute = FALSE;
  182. break;
  183. case 'c': /* replacement for CRITICAL */
  184. if ((state[STATE_CRITICAL] = translate_state(optarg)) == ERROR)
  185. usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  186. permute = FALSE;
  187. break;
  188. case 'u': /* replacement for UNKNOWN */
  189. if ((state[STATE_UNKNOWN] = translate_state(optarg)) == ERROR)
  190. usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  191. permute = FALSE;
  192. break;
  193. }
  194. }
  195. validate_arguments (&argv[optind]);
  196. if (permute) { /* No [owcu] switch specified, default to this */
  197. state[STATE_OK] = STATE_CRITICAL;
  198. state[STATE_CRITICAL] = STATE_OK;
  199. }
  200. return (const char **) &argv[optind];
  201. }
  202. /******************************************************************************
  203. @@-
  204. <sect3>
  205. <title>validate_arguments</title>
  206. <para>No validation is currently done.</para>
  207. </sect3>
  208. -@@
  209. ******************************************************************************/
  210. int
  211. validate_arguments (char **command_line)
  212. {
  213. if (command_line[0] == NULL)
  214. usage4 (_("Could not parse arguments"));
  215. if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0)
  216. usage4 (_("Require path to command"));
  217. }
  218. /******************************************************************************
  219. @@-
  220. </sect2>
  221. </sect1>
  222. </article>
  223. -@@
  224. ******************************************************************************/
  225. int
  226. translate_state (char *state_text)
  227. {
  228. char *temp_ptr;
  229. for (temp_ptr = state_text; *temp_ptr; temp_ptr++) {
  230. *temp_ptr = toupper(*temp_ptr);
  231. }
  232. if (!strcmp(state_text,"OK") || !strcmp(state_text,"0"))
  233. return STATE_OK;
  234. if (!strcmp(state_text,"WARNING") || !strcmp(state_text,"1"))
  235. return STATE_WARNING;
  236. if (!strcmp(state_text,"CRITICAL") || !strcmp(state_text,"2"))
  237. return STATE_CRITICAL;
  238. if (!strcmp(state_text,"UNKNOWN") || !strcmp(state_text,"3"))
  239. return STATE_UNKNOWN;
  240. return ERROR;
  241. }
  242. void
  243. print_help (void)
  244. {
  245. print_revision (progname, revision);
  246. printf (COPYRIGHT, copyright, email);
  247. printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
  248. printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
  249. printf ("\n\n");
  250. print_usage ();
  251. printf (_(UT_HELP_VRSN));
  252. printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
  253. printf (" %s\n", _("Keep timeout lower than the plugin timeout to retain CRITICAL status."));
  254. printf(" -o,--ok=STATUS\n");
  255. printf(" -w,--warning=STATUS\n");
  256. printf(" -c,--critical=STATUS\n");
  257. printf(" -u,--unknown=STATUS\n");
  258. printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
  259. printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
  260. printf(_(" OK and CRITICAL.\n"));
  261. printf ("\n");
  262. printf ("%s\n", _("Examples:"));
  263. printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
  264. printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
  265. printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
  266. printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
  267. printf ("\n");
  268. printf ("%s\n", _("Notes:"));
  269. printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
  270. printf ("%s\n", _("The full path of the plugin must be provided."));
  271. printf ("%s\n", _("If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL."));
  272. printf ("%s\n", _("If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK."));
  273. printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
  274. printf (_(UT_SUPPORT));
  275. }
  276. void
  277. print_usage (void)
  278. {
  279. printf (_("Usage:"));
  280. printf ("%s [-t timeout] [-owcu STATE] <definition of wrapped plugin>\n", progname);
  281. }