1 /*****************************************************************************
6 * Copyright (c) 2002-2008 Nagios Plugins Development Team
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
= "nagiosplug-devel@lists.sourceforge.net";
36 #define DEFAULT_TIMEOUT 11
42 #include "utils_cmd.h"
44 /* char *command_line; */
46 static const char **process_arguments (int, char **);
47 int validate_arguments (char **);
48 int translate_state (char *);
49 void print_help (void);
50 void print_usage (void);
51 int subst_text
= FALSE
;
53 static int state
[4] = {
61 main (int argc
, char **argv
)
63 int found
= 0, result
= STATE_UNKNOWN
;
66 output chld_out
, chld_err
;
69 setlocale (LC_ALL
, "");
70 bindtextdomain (PACKAGE
, LOCALEDIR
);
73 timeout_interval
= DEFAULT_TIMEOUT
;
75 command_line
= (char **) process_arguments (argc
, argv
);
77 /* Set signal handling and alarm */
78 if (signal (SIGALRM
, timeout_alarm_handler
) == SIG_ERR
)
79 die (STATE_UNKNOWN
, _("Cannot catch SIGALRM"));
81 (void) alarm ((unsigned) timeout_interval
);
83 /* catch when the command is quoted */
84 if(command_line
[1] == NULL
) {
85 result
= cmd_run (command_line
[0], &chld_out
, &chld_err
, 0);
87 result
= cmd_run_array (command_line
, &chld_out
, &chld_err
, 0);
89 if (chld_err
.lines
> 0) {
90 printf ("Error output from command:\n");
91 for (i
= 0; i
< chld_err
.lines
; i
++) {
92 printf ("%s\n", chld_err
.line
[i
]);
97 /* Return UNKNOWN or worse if no output is returned */
98 if (chld_out
.lines
== 0)
99 die (max_state_alt (result
, STATE_UNKNOWN
), _("No data returned from command\n"));
101 for (i
= 0; i
< chld_out
.lines
; i
++) {
102 if (subst_text
&& result
!= state
[result
] &&
103 result
>= 0 && result
<= 4) {
104 /* Loop over each match found */
105 while ((sub
= strstr (chld_out
.line
[i
], state_text (result
)))) {
106 /* Terminate the first part and skip over the string we'll substitute */
108 sub
+= strlen (state_text (result
));
109 /* then put everything back together */
110 xasprintf (&chld_out
.line
[i
], "%s%s%s", chld_out
.line
[i
], state_text (state
[result
]), sub
);
113 printf ("%s\n", chld_out
.line
[i
]);
116 if (result
>= 0 && result
<= 4) {
117 exit (state
[result
]);
124 /* process command-line arguments */
126 process_arguments (int argc
, char **argv
)
132 static struct option longopts
[] = {
133 {"help", no_argument
, 0, 'h'},
134 {"version", no_argument
, 0, 'V'},
135 {"timeout", required_argument
, 0, 't'},
136 {"timeout-result", required_argument
, 0, 'T'},
137 {"ok", required_argument
, 0, 'o'},
138 {"warning", required_argument
, 0, 'w'},
139 {"critical", required_argument
, 0, 'c'},
140 {"unknown", required_argument
, 0, 'u'},
141 {"substitute", no_argument
, 0, 's'},
146 c
= getopt_long (argc
, argv
, "+hVt:T:o:w:c:u:s", longopts
, &option
);
148 if (c
== -1 || c
== EOF
)
159 case 'V': /* version */
160 print_revision (progname
, NP_VERSION
);
162 case 't': /* timeout period */
163 if (!is_integer (optarg
))
164 usage2 (_("Timeout interval must be a positive integer"), optarg
);
166 timeout_interval
= atoi (optarg
);
168 case 'T': /* Result to return on timeouts */
169 if ((timeout_state
= translate_state(optarg
)) == ERROR
)
170 usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
172 case 'o': /* replacement for OK */
173 if ((state
[STATE_OK
] = translate_state(optarg
)) == ERROR
)
174 usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
178 case 'w': /* replacement for WARNING */
179 if ((state
[STATE_WARNING
] = translate_state(optarg
)) == ERROR
)
180 usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
183 case 'c': /* replacement for CRITICAL */
184 if ((state
[STATE_CRITICAL
] = translate_state(optarg
)) == ERROR
)
185 usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
188 case 'u': /* replacement for UNKNOWN */
189 if ((state
[STATE_UNKNOWN
] = translate_state(optarg
)) == ERROR
)
190 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
193 case 's': /* Substitute status text */
199 validate_arguments (&argv
[optind
]);
201 if (permute
) { /* No [owcu] switch specified, default to this */
202 state
[STATE_OK
] = STATE_CRITICAL
;
203 state
[STATE_CRITICAL
] = STATE_OK
;
206 return (const char **) &argv
[optind
];
211 validate_arguments (char **command_line
)
213 if (command_line
[0] == NULL
)
214 usage4 (_("Could not parse arguments"));
216 if (strncmp(command_line
[0],"/",1) != 0 && strncmp(command_line
[0],"./",2) != 0)
217 usage4 (_("Require path to command"));
222 translate_state (char *state_text
)
225 for (temp_ptr
= state_text
; *temp_ptr
; temp_ptr
++) {
226 *temp_ptr
= toupper(*temp_ptr
);
228 if (!strcmp(state_text
,"OK") || !strcmp(state_text
,"0"))
230 if (!strcmp(state_text
,"WARNING") || !strcmp(state_text
,"1"))
231 return STATE_WARNING
;
232 if (!strcmp(state_text
,"CRITICAL") || !strcmp(state_text
,"2"))
233 return STATE_CRITICAL
;
234 if (!strcmp(state_text
,"UNKNOWN") || !strcmp(state_text
,"3"))
235 return STATE_UNKNOWN
;
242 print_revision (progname
, NP_VERSION
);
244 printf (COPYRIGHT
, copyright
, email
);
246 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
247 printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
253 printf (UT_HELP_VRSN
);
255 printf (UT_TIMEOUT
, timeout_interval
);
256 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
257 printf (" -T, --timeout-result=STATUS\n");
258 printf (" %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n"));
260 printf(" -o, --ok=STATUS\n");
261 printf(" -w, --warning=STATUS\n");
262 printf(" -c, --critical=STATUS\n");
263 printf(" -u, --unknown=STATUS\n");
264 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
265 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
266 printf(_(" OK and CRITICAL.\n"));
267 printf(" -s, --substitute\n");
268 printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n"));
271 printf ("%s\n", _("Examples:"));
272 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
273 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
274 printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
275 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
277 printf ("%s\n", _("Notes:"));
278 printf (" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
279 printf (" %s\n", _("The full path of the plugin must be provided."));
280 printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
281 printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
282 printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
284 printf (" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a"));
285 printf (" %s\n", _("plugin by setting the negate timeout a bit lower."));
295 printf ("%s\n", _("Usage:"));
296 printf ("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname
);