Changed default test values for check_dns (using nagios.com)
[monitoring-plugins.git] / plugins / negate.c
blob7bfef95a957385cfcbc4b6ffe1c94ca2988220b4
1 /******************************************************************************
3 * Nagios negate plugin
5 * License: GPL
6 * Copyright (c) 2002-2007 nagios-plugins team
8 * Last Modified: $Date$
10 * Description:
12 * This file contains the negate plugin
14 * Negates the status of a plugin (returns OK for CRITICAL, and vice-versa)
16 * License Information:
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 2 of the License, or
21 * (at your option) any later version.
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.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 * $Id$
34 @@-<article>
36 <sect1>
37 <title>Quick Reference</title>
38 <refentry>
39 <refmeta><manvolnum>5<manvolnum></refmeta>
40 <refnamdiv>
41 <refname>&progname;</refname>
42 <refpurpose>&SUMMARY;</refpurpose>
43 </refnamdiv>
44 </refentry>
45 </sect1>
47 <sect1>
48 <title>FAQ</title>
49 </sect1>
51 <sect1>
52 <title>Theory, Installation, and Operation</title>
54 <sect2>
55 <title>General Description</title>
56 <para>
57 &DESCRIPTION;
58 </para>
59 </sect2>
61 <sect2>
62 <title>Future Enhancements</title>
63 <para>ToDo List</para>
64 <itemizedlist>
65 <listitem>Add option to do regex substitution in output text</listitem>
66 </itemizedlist>
67 </sect2>-@@
69 ******************************************************************************/
71 const char *progname = "negate";
72 const char *revision = "$Revision$";
73 const char *copyright = "2002-2007";
74 const char *email = "nagiosplug-devel@lists.sourceforge.net";
76 #define DEFAULT_TIMEOUT 9
78 #include "common.h"
79 #include "utils.h"
80 #include "utils_cmd.h"
82 /* char *command_line; */
84 static const char **process_arguments (int, char **);
85 int validate_arguments (char **);
86 void print_help (void);
87 void print_usage (void);
89 static int state[4] = {
90 STATE_OK,
91 STATE_WARNING,
92 STATE_CRITICAL,
93 STATE_UNKNOWN,
96 int
97 main (int argc, char **argv)
99 int found = 0, result = STATE_UNKNOWN;
100 char *buf;
101 char **command_line;
102 output chld_out, chld_err;
103 int i;
105 setlocale (LC_ALL, "");
106 bindtextdomain (PACKAGE, LOCALEDIR);
107 textdomain (PACKAGE);
109 command_line = (char **) process_arguments (argc, argv);
111 /* Set signal handling and alarm */
112 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
113 die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
115 (void) alarm ((unsigned) timeout_interval);
117 /* catch when the command is quoted */
118 if(command_line[1] == NULL) {
119 result = cmd_run (command_line[0], &chld_out, &chld_err, 0);
120 } else {
121 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
123 if (chld_err.lines > 0) {
124 printf ("Error output from command:\n");
125 for (i = 0; i < chld_err.lines; i++) {
126 printf ("%s\n", chld_err.line[i]);
128 exit (STATE_WARNING);
131 if (chld_out.lines == 0)
132 die (STATE_UNKNOWN, _("No data returned from command\n"));
134 for (i = 0; i < chld_out.lines; i++) {
135 printf ("%s\n", chld_out.line[i]);
138 if (result >= 0 && result <= 4) {
139 exit (state[result]);
140 } else {
141 exit (result);
145 /******************************************************************************
147 <sect2>
148 <title>Functions</title>
150 <sect3>
151 <title>process_arguments</title>
153 <para>This function parses the command line into the needed
154 variables.</para>
156 <para>Aside from the standard 'help' and 'version' options, there
157 is a only a 'timeout' option.</para>
159 </sect3>
161 ******************************************************************************/
165 /* process command-line arguments */
166 static const char **
167 process_arguments (int argc, char **argv)
169 int c;
170 int permute = TRUE;
172 int option = 0;
173 static struct option longopts[] = {
174 {"help", no_argument, 0, 'h'},
175 {"version", no_argument, 0, 'V'},
176 {"timeout", required_argument, 0, 't'},
177 {"ok", required_argument, 0, 'o'},
178 {"warning", required_argument, 0, 'w'},
179 {"critical", required_argument, 0, 'c'},
180 {"unknown", required_argument, 0, 'u'},
181 {0, 0, 0, 0}
184 while (1) {
185 c = getopt_long (argc, argv, "+hVt:o:w:c:u:", longopts, &option);
187 if (c == -1 || c == EOF)
188 break;
190 switch (c) {
191 case '?': /* help */
192 usage5 ();
193 break;
194 case 'h': /* help */
195 print_help ();
196 exit (EXIT_SUCCESS);
197 break;
198 case 'V': /* version */
199 print_revision (progname, revision);
200 exit (EXIT_SUCCESS);
201 case 't': /* timeout period */
202 if (!is_integer (optarg))
203 usage2 (_("Timeout interval must be a positive integer"), optarg);
204 else
205 timeout_interval = atoi (optarg);
206 break;
207 case 'o': /* replacement for OK */
208 if ((state[STATE_OK] = translate_state(optarg)) == ERROR)
209 usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
210 permute = FALSE;
211 break;
213 case 'w': /* replacement for WARNING */
214 if ((state[STATE_WARNING] = translate_state(optarg)) == ERROR)
215 usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
216 permute = FALSE;
217 break;
218 case 'c': /* replacement for CRITICAL */
219 if ((state[STATE_CRITICAL] = translate_state(optarg)) == ERROR)
220 usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
221 permute = FALSE;
222 break;
223 case 'u': /* replacement for UNKNOWN */
224 if ((state[STATE_UNKNOWN] = translate_state(optarg)) == ERROR)
225 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
226 permute = FALSE;
227 break;
231 validate_arguments (&argv[optind]);
233 if (permute) { /* No [owcu] switch specified, default to this */
234 state[STATE_OK] = STATE_CRITICAL;
235 state[STATE_CRITICAL] = STATE_OK;
238 return (const char **) &argv[optind];
242 /******************************************************************************
244 <sect3>
245 <title>validate_arguments</title>
247 <para>No validation is currently done.</para>
249 </sect3>
251 ******************************************************************************/
256 validate_arguments (char **command_line)
258 if (command_line[0] == NULL)
259 usage4 (_("Could not parse arguments"));
261 if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0)
262 usage4 (_("Require path to command"));
265 /******************************************************************************
267 </sect2>
268 </sect1>
269 </article>
271 ******************************************************************************/
274 translate_state (char *state_text)
276 char *temp_ptr;
277 for (temp_ptr = state_text; *temp_ptr; temp_ptr++) {
278 *temp_ptr = toupper(*temp_ptr);
280 if (!strcmp(state_text,"OK") || !strcmp(state_text,"0"))
281 return STATE_OK;
282 if (!strcmp(state_text,"WARNING") || !strcmp(state_text,"1"))
283 return STATE_WARNING;
284 if (!strcmp(state_text,"CRITICAL") || !strcmp(state_text,"2"))
285 return STATE_CRITICAL;
286 if (!strcmp(state_text,"UNKNOWN") || !strcmp(state_text,"3"))
287 return STATE_UNKNOWN;
288 return ERROR;
291 void
292 print_help (void)
294 print_revision (progname, revision);
296 printf (COPYRIGHT, copyright, email);
298 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
299 printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
301 printf ("\n\n");
303 print_usage ();
305 printf (_(UT_HELP_VRSN));
307 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
308 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
310 printf(" -o,--ok=STATUS\n");
311 printf(" -w,--warning=STATUS\n");
312 printf(" -c,--critical=STATUS\n");
313 printf(" -u,--unknown=STATUS\n");
314 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
315 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
316 printf(_(" OK and CRITICAL.\n"));
318 printf ("\n");
319 printf ("%s\n", _("Examples:"));
320 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
321 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
322 printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
323 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
324 printf ("\n");
325 printf ("%s\n", _("Notes:"));
326 printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
327 printf ("%s\n", _("The full path of the plugin must be provided."));
328 printf ("%s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
329 printf ("%s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
330 printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
332 printf (_(UT_SUPPORT));
337 void
338 print_usage (void)
340 printf (_("Usage:"));
341 printf ("%s [-t timeout] [-owcu STATE] <definition of wrapped plugin>\n", progname);