Output plugin's stderr to stderr
[monitoring-plugins.git] / plugins / negate.c
blob50f62d33d30ad61c836e496e93cb0679a06e6420
1 /*****************************************************************************
2 *
3 * Monitoring negate plugin
4 *
5 * License: GPL
6 * Copyright (c) 2002-2008 Monitoring Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the negate plugin
12 * Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).
13 * Can also perform custom state switching.
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.
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.
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/>.
30 *****************************************************************************/
32 const char *progname = "negate";
33 const char *copyright = "2002-2008";
34 const char *email = "devel@monitoring-plugins.org";
36 #define DEFAULT_TIMEOUT 11
38 #include "common.h"
39 #include "utils.h"
40 #include "utils_cmd.h"
42 #include <ctype.h>
44 /* char *command_line; */
46 static const char **process_arguments (int, char **);
47 void validate_arguments (char **);
48 void print_help (void);
49 void print_usage (void);
50 int subst_text = FALSE;
52 static int state[4] = {
53 STATE_OK,
54 STATE_WARNING,
55 STATE_CRITICAL,
56 STATE_UNKNOWN,
59 int
60 main (int argc, char **argv)
62 int result = STATE_UNKNOWN;
63 char *sub;
64 char **command_line;
65 output chld_out, chld_err;
66 int i;
68 setlocale (LC_ALL, "");
69 bindtextdomain (PACKAGE, LOCALEDIR);
70 textdomain (PACKAGE);
72 timeout_interval = DEFAULT_TIMEOUT;
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);
88 if (chld_err.lines > 0) {
89 for (i = 0; i < chld_err.lines; i++) {
90 fprintf (stderr, "%s\n", chld_err.line[i]);
94 /* Return UNKNOWN or worse if no output is returned */
95 if (chld_out.lines == 0)
96 die (max_state_alt (result, STATE_UNKNOWN), _("No data returned from command\n"));
98 for (i = 0; i < chld_out.lines; i++) {
99 if (subst_text && result >= 0 && result <= 4 && result != state[result]) {
100 /* Loop over each match found */
101 while ((sub = strstr (chld_out.line[i], state_text (result)))) {
102 /* Terminate the first part and skip over the string we'll substitute */
103 *sub = '\0';
104 sub += strlen (state_text (result));
105 /* then put everything back together */
106 xasprintf (&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text (state[result]), sub);
109 printf ("%s\n", chld_out.line[i]);
112 if (result >= 0 && result <= 4) {
113 exit (state[result]);
114 } else {
115 exit (result);
120 /* process command-line arguments */
121 static const char **
122 process_arguments (int argc, char **argv)
124 int c;
125 int permute = TRUE;
127 int option = 0;
128 static struct option longopts[] = {
129 {"help", no_argument, 0, 'h'},
130 {"version", no_argument, 0, 'V'},
131 {"timeout", required_argument, 0, 't'},
132 {"timeout-result", required_argument, 0, 'T'},
133 {"ok", required_argument, 0, 'o'},
134 {"warning", required_argument, 0, 'w'},
135 {"critical", required_argument, 0, 'c'},
136 {"unknown", required_argument, 0, 'u'},
137 {"substitute", no_argument, 0, 's'},
138 {0, 0, 0, 0}
141 while (1) {
142 c = getopt_long (argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option);
144 if (c == -1 || c == EOF)
145 break;
147 switch (c) {
148 case '?': /* help */
149 usage5 ();
150 break;
151 case 'h': /* help */
152 print_help ();
153 exit (EXIT_SUCCESS);
154 break;
155 case 'V': /* version */
156 print_revision (progname, NP_VERSION);
157 exit (EXIT_SUCCESS);
158 case 't': /* timeout period */
159 if (!is_integer (optarg))
160 usage2 (_("Timeout interval must be a positive integer"), optarg);
161 else
162 timeout_interval = atoi (optarg);
163 break;
164 case 'T': /* Result to return on timeouts */
165 if ((timeout_state = mp_translate_state(optarg)) == ERROR)
166 usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
167 break;
168 case 'o': /* replacement for OK */
169 if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR)
170 usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
171 permute = FALSE;
172 break;
174 case 'w': /* replacement for WARNING */
175 if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR)
176 usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
177 permute = FALSE;
178 break;
179 case 'c': /* replacement for CRITICAL */
180 if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR)
181 usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
182 permute = FALSE;
183 break;
184 case 'u': /* replacement for UNKNOWN */
185 if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR)
186 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
187 permute = FALSE;
188 break;
189 case 's': /* Substitute status text */
190 subst_text = TRUE;
191 break;
195 validate_arguments (&argv[optind]);
197 if (permute) { /* No [owcu] switch specified, default to this */
198 state[STATE_OK] = STATE_CRITICAL;
199 state[STATE_CRITICAL] = STATE_OK;
202 return (const char **) &argv[optind];
206 void
207 validate_arguments (char **command_line)
209 if (command_line[0] == NULL)
210 usage4 (_("Could not parse arguments"));
212 if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0)
213 usage4 (_("Require path to command"));
217 void
218 print_help (void)
220 print_revision (progname, NP_VERSION);
222 printf (COPYRIGHT, copyright, email);
224 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
225 printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
227 printf ("\n\n");
229 print_usage ();
231 printf (UT_HELP_VRSN);
233 printf (UT_PLUG_TIMEOUT, timeout_interval);
234 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
235 printf (" -T, --timeout-result=STATUS\n");
236 printf (" %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n"));
238 printf(" -o, --ok=STATUS\n");
239 printf(" -w, --warning=STATUS\n");
240 printf(" -c, --critical=STATUS\n");
241 printf(" -u, --unknown=STATUS\n");
242 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
243 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
244 printf(_(" OK and CRITICAL.\n"));
245 printf(" -s, --substitute\n");
246 printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n"));
248 printf ("\n");
249 printf ("%s\n", _("Examples:"));
250 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
251 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
252 printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
253 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
254 printf ("\n");
255 printf ("%s\n", _("Notes:"));
256 printf (" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
257 printf (" %s\n", _("The full path of the plugin must be provided."));
258 printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
259 printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
260 printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
261 printf ("\n");
262 printf (" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a"));
263 printf (" %s\n", _("plugin by setting the negate timeout a bit lower."));
265 printf (UT_SUPPORT);
270 void
271 print_usage (void)
273 printf ("%s\n", _("Usage:"));
274 printf ("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname);