Code

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