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 *****************************************************************************/
35 const char *progname = "negate";
36 const char *revision = "$Revision$";
37 const char *copyright = "2002-2007";
38 const char *email = "nagiosplug-devel@lists.sourceforge.net";
40 #define DEFAULT_TIMEOUT 9
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);
53 static int state[4] = {
54 STATE_OK,
55 STATE_WARNING,
56 STATE_CRITICAL,
57 STATE_UNKNOWN,
58 };
60 int
61 main (int argc, char **argv)
62 {
63 int found = 0, result = STATE_UNKNOWN;
64 char *buf;
65 char **command_line;
66 output chld_out, chld_err;
67 int i;
69 setlocale (LC_ALL, "");
70 bindtextdomain (PACKAGE, LOCALEDIR);
71 textdomain (PACKAGE);
73 command_line = (char **) process_arguments (argc, argv);
75 /* Set signal handling and alarm */
76 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
77 die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
79 (void) alarm ((unsigned) timeout_interval);
81 /* catch when the command is quoted */
82 if(command_line[1] == NULL) {
83 result = cmd_run (command_line[0], &chld_out, &chld_err, 0);
84 } else {
85 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
86 }
87 if (chld_err.lines > 0) {
88 printf ("Error output from command:\n");
89 for (i = 0; i < chld_err.lines; i++) {
90 printf ("%s\n", chld_err.line[i]);
91 }
92 exit (STATE_WARNING);
93 }
95 if (chld_out.lines == 0)
96 die (STATE_UNKNOWN, _("No data returned from command\n"));
98 for (i = 0; i < chld_out.lines; i++) {
99 printf ("%s\n", chld_out.line[i]);
100 }
102 if (result >= 0 && result <= 4) {
103 exit (state[result]);
104 } else {
105 exit (result);
106 }
107 }
110 /* process command-line arguments */
111 static const char **
112 process_arguments (int argc, char **argv)
113 {
114 int c;
115 int permute = TRUE;
117 int option = 0;
118 static struct option longopts[] = {
119 {"help", no_argument, 0, 'h'},
120 {"version", no_argument, 0, 'V'},
121 {"timeout", required_argument, 0, 't'},
122 {"ok", required_argument, 0, 'o'},
123 {"warning", required_argument, 0, 'w'},
124 {"critical", required_argument, 0, 'c'},
125 {"unknown", required_argument, 0, 'u'},
126 {0, 0, 0, 0}
127 };
129 while (1) {
130 c = getopt_long (argc, argv, "+hVt:o:w:c:u:", longopts, &option);
132 if (c == -1 || c == EOF)
133 break;
135 switch (c) {
136 case '?': /* help */
137 usage5 ();
138 break;
139 case 'h': /* help */
140 print_help ();
141 exit (EXIT_SUCCESS);
142 break;
143 case 'V': /* version */
144 print_revision (progname, revision);
145 exit (EXIT_SUCCESS);
146 case 't': /* timeout period */
147 if (!is_integer (optarg))
148 usage2 (_("Timeout interval must be a positive integer"), optarg);
149 else
150 timeout_interval = atoi (optarg);
151 break;
152 case 'o': /* replacement for OK */
153 if ((state[STATE_OK] = translate_state(optarg)) == ERROR)
154 usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
155 permute = FALSE;
156 break;
158 case 'w': /* replacement for WARNING */
159 if ((state[STATE_WARNING] = translate_state(optarg)) == ERROR)
160 usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
161 permute = FALSE;
162 break;
163 case 'c': /* replacement for CRITICAL */
164 if ((state[STATE_CRITICAL] = translate_state(optarg)) == ERROR)
165 usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
166 permute = FALSE;
167 break;
168 case 'u': /* replacement for UNKNOWN */
169 if ((state[STATE_UNKNOWN] = translate_state(optarg)) == ERROR)
170 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
171 permute = FALSE;
172 break;
173 }
174 }
176 validate_arguments (&argv[optind]);
178 if (permute) { /* No [owcu] switch specified, default to this */
179 state[STATE_OK] = STATE_CRITICAL;
180 state[STATE_CRITICAL] = STATE_OK;
181 }
183 return (const char **) &argv[optind];
184 }
187 int
188 validate_arguments (char **command_line)
189 {
190 if (command_line[0] == NULL)
191 usage4 (_("Could not parse arguments"));
193 if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0)
194 usage4 (_("Require path to command"));
195 }
198 int
199 translate_state (char *state_text)
200 {
201 char *temp_ptr;
202 for (temp_ptr = state_text; *temp_ptr; temp_ptr++) {
203 *temp_ptr = toupper(*temp_ptr);
204 }
205 if (!strcmp(state_text,"OK") || !strcmp(state_text,"0"))
206 return STATE_OK;
207 if (!strcmp(state_text,"WARNING") || !strcmp(state_text,"1"))
208 return STATE_WARNING;
209 if (!strcmp(state_text,"CRITICAL") || !strcmp(state_text,"2"))
210 return STATE_CRITICAL;
211 if (!strcmp(state_text,"UNKNOWN") || !strcmp(state_text,"3"))
212 return STATE_UNKNOWN;
213 return ERROR;
214 }
216 void
217 print_help (void)
218 {
219 print_revision (progname, revision);
221 printf (COPYRIGHT, copyright, email);
223 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
224 printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
226 printf ("\n\n");
228 print_usage ();
230 printf (_(UT_HELP_VRSN));
232 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
233 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
235 printf(" -o,--ok=STATUS\n");
236 printf(" -w,--warning=STATUS\n");
237 printf(" -c,--critical=STATUS\n");
238 printf(" -u,--unknown=STATUS\n");
239 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
240 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
241 printf(_(" OK and CRITICAL.\n"));
243 printf ("\n");
244 printf ("%s\n", _("Examples:"));
245 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
246 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
247 printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
248 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
249 printf ("\n");
250 printf ("%s\n", _("Notes:"));
251 printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
252 printf ("%s\n", _("The full path of the plugin must be provided."));
253 printf ("%s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
254 printf ("%s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
255 printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
257 printf (_(UT_SUPPORT));
258 }
262 void
263 print_usage (void)
264 {
265 printf (_("Usage:"));
266 printf ("%s [-t timeout] [-owcu STATE] <definition of wrapped plugin>\n", progname);
267 }