negate.c 8.9 KB

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