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