negate.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*****************************************************************************
  2. *
  3. * Nagios negate plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2002-2007 Nagios Plugins Development 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. * Can also perform custom state switching.
  16. *
  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 3 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, see <http://www.gnu.org/licenses/>.
  30. *
  31. * $Id$
  32. *
  33. *****************************************************************************/
  34. const char *progname = "negate";
  35. const char *revision = "$Revision$";
  36. const char *copyright = "2002-2007";
  37. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  38. #define DEFAULT_TIMEOUT 9
  39. #include "common.h"
  40. #include "utils.h"
  41. #include "utils_cmd.h"
  42. /* char *command_line; */
  43. static const char **process_arguments (int, char **);
  44. int validate_arguments (char **);
  45. void print_help (void);
  46. void print_usage (void);
  47. static int state[4] = {
  48. STATE_OK,
  49. STATE_WARNING,
  50. STATE_CRITICAL,
  51. STATE_UNKNOWN,
  52. };
  53. int
  54. main (int argc, char **argv)
  55. {
  56. int found = 0, result = STATE_UNKNOWN;
  57. char *buf;
  58. char **command_line;
  59. output chld_out, chld_err;
  60. int i;
  61. setlocale (LC_ALL, "");
  62. bindtextdomain (PACKAGE, LOCALEDIR);
  63. textdomain (PACKAGE);
  64. command_line = (char **) process_arguments (argc, argv);
  65. /* Set signal handling and alarm */
  66. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
  67. die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
  68. (void) alarm ((unsigned) timeout_interval);
  69. /* catch when the command is quoted */
  70. if(command_line[1] == NULL) {
  71. result = cmd_run (command_line[0], &chld_out, &chld_err, 0);
  72. } else {
  73. result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
  74. }
  75. if (chld_err.lines > 0) {
  76. printf ("Error output from command:\n");
  77. for (i = 0; i < chld_err.lines; i++) {
  78. printf ("%s\n", chld_err.line[i]);
  79. }
  80. exit (STATE_WARNING);
  81. }
  82. if (chld_out.lines == 0)
  83. die (STATE_UNKNOWN, _("No data returned from command\n"));
  84. for (i = 0; i < chld_out.lines; i++) {
  85. printf ("%s\n", chld_out.line[i]);
  86. }
  87. if (result >= 0 && result <= 4) {
  88. exit (state[result]);
  89. } else {
  90. exit (result);
  91. }
  92. }
  93. /* process command-line arguments */
  94. static const char **
  95. process_arguments (int argc, char **argv)
  96. {
  97. int c;
  98. int permute = TRUE;
  99. int option = 0;
  100. static struct option longopts[] = {
  101. {"help", no_argument, 0, 'h'},
  102. {"version", no_argument, 0, 'V'},
  103. {"timeout", required_argument, 0, 't'},
  104. {"ok", required_argument, 0, 'o'},
  105. {"warning", required_argument, 0, 'w'},
  106. {"critical", required_argument, 0, 'c'},
  107. {"unknown", required_argument, 0, 'u'},
  108. {0, 0, 0, 0}
  109. };
  110. while (1) {
  111. c = getopt_long (argc, argv, "+hVt:o:w:c:u:", longopts, &option);
  112. if (c == -1 || c == EOF)
  113. break;
  114. switch (c) {
  115. case '?': /* help */
  116. usage5 ();
  117. break;
  118. case 'h': /* help */
  119. print_help ();
  120. exit (EXIT_SUCCESS);
  121. break;
  122. case 'V': /* version */
  123. print_revision (progname, revision);
  124. exit (EXIT_SUCCESS);
  125. case 't': /* timeout period */
  126. if (!is_integer (optarg))
  127. usage2 (_("Timeout interval must be a positive integer"), optarg);
  128. else
  129. timeout_interval = atoi (optarg);
  130. break;
  131. case 'o': /* replacement for OK */
  132. if ((state[STATE_OK] = translate_state(optarg)) == ERROR)
  133. usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  134. permute = FALSE;
  135. break;
  136. case 'w': /* replacement for WARNING */
  137. if ((state[STATE_WARNING] = translate_state(optarg)) == ERROR)
  138. usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  139. permute = FALSE;
  140. break;
  141. case 'c': /* replacement for CRITICAL */
  142. if ((state[STATE_CRITICAL] = translate_state(optarg)) == ERROR)
  143. usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  144. permute = FALSE;
  145. break;
  146. case 'u': /* replacement for UNKNOWN */
  147. if ((state[STATE_UNKNOWN] = translate_state(optarg)) == ERROR)
  148. usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
  149. permute = FALSE;
  150. break;
  151. }
  152. }
  153. validate_arguments (&argv[optind]);
  154. if (permute) { /* No [owcu] switch specified, default to this */
  155. state[STATE_OK] = STATE_CRITICAL;
  156. state[STATE_CRITICAL] = STATE_OK;
  157. }
  158. return (const char **) &argv[optind];
  159. }
  160. int
  161. validate_arguments (char **command_line)
  162. {
  163. if (command_line[0] == NULL)
  164. usage4 (_("Could not parse arguments"));
  165. if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0)
  166. usage4 (_("Require path to command"));
  167. }
  168. int
  169. translate_state (char *state_text)
  170. {
  171. char *temp_ptr;
  172. for (temp_ptr = state_text; *temp_ptr; temp_ptr++) {
  173. *temp_ptr = toupper(*temp_ptr);
  174. }
  175. if (!strcmp(state_text,"OK") || !strcmp(state_text,"0"))
  176. return STATE_OK;
  177. if (!strcmp(state_text,"WARNING") || !strcmp(state_text,"1"))
  178. return STATE_WARNING;
  179. if (!strcmp(state_text,"CRITICAL") || !strcmp(state_text,"2"))
  180. return STATE_CRITICAL;
  181. if (!strcmp(state_text,"UNKNOWN") || !strcmp(state_text,"3"))
  182. return STATE_UNKNOWN;
  183. return ERROR;
  184. }
  185. void
  186. print_help (void)
  187. {
  188. print_revision (progname, revision);
  189. printf (COPYRIGHT, copyright, email);
  190. printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
  191. printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
  192. printf ("\n\n");
  193. print_usage ();
  194. printf (_(UT_HELP_VRSN));
  195. printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
  196. printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
  197. printf(" -o,--ok=STATUS\n");
  198. printf(" -w,--warning=STATUS\n");
  199. printf(" -c,--critical=STATUS\n");
  200. printf(" -u,--unknown=STATUS\n");
  201. printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
  202. printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
  203. printf(_(" OK and CRITICAL.\n"));
  204. printf ("\n");
  205. printf ("%s\n", _("Examples:"));
  206. printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
  207. printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
  208. printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
  209. printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
  210. printf ("\n");
  211. printf ("%s\n", _("Notes:"));
  212. printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
  213. printf ("%s\n", _("The full path of the plugin must be provided."));
  214. printf ("%s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
  215. printf ("%s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
  216. printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
  217. printf (_(UT_SUPPORT));
  218. }
  219. void
  220. print_usage (void)
  221. {
  222. printf (_("Usage:"));
  223. printf ("%s [-t timeout] [-owcu STATE] <definition of wrapped plugin>\n", progname);
  224. }