1 /******************************************************************************
6 * Copyright (c) 2002-2007 nagios-plugins team
8 * Last Modified: $Date$
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.
37 <title>Quick Reference</title>
39 <refmeta><manvolnum>5<manvolnum></refmeta>
41 <refname>&progname;</refname>
42 <refpurpose>&SUMMARY;</refpurpose>
52 <title>Theory, Installation, and Operation</title>
55 <title>General Description</title>
62 <title>Future Enhancements</title>
63 <para>ToDo List</para>
65 <listitem>Add option to do regex substitution in output text</listitem>
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
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] = {
97 main (int argc
, char **argv
)
99 int found
= 0, result
= STATE_UNKNOWN
;
102 output chld_out
, chld_err
;
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);
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
]);
145 /******************************************************************************
148 <title>Functions</title>
151 <title>process_arguments</title>
153 <para>This function parses the command line into the needed
156 <para>Aside from the standard 'help' and 'version' options, there
157 is a only a 'timeout' option.</para>
161 ******************************************************************************/
165 /* process command-line arguments */
167 process_arguments (int argc
, char **argv
)
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'},
185 c
= getopt_long (argc
, argv
, "+hVt:o:w:c:u:", longopts
, &option
);
187 if (c
== -1 || c
== EOF
)
198 case 'V': /* version */
199 print_revision (progname
, revision
);
201 case 't': /* timeout period */
202 if (!is_integer (optarg
))
203 usage2 (_("Timeout interval must be a positive integer"), optarg
);
205 timeout_interval
= atoi (optarg
);
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)."));
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)."));
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)."));
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)."));
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 /******************************************************************************
245 <title>validate_arguments</title>
247 <para>No validation is currently done.</para>
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 /******************************************************************************
271 ******************************************************************************/
274 translate_state (char *state_text
)
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"))
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
;
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."));
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"));
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"));
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
));
340 printf (_("Usage:"));
341 printf ("%s [-t timeout] [-owcu STATE] <definition of wrapped plugin>\n", progname
);