4
0

negate.c 8.1 KB

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