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
40 #include "utils_cmd.h"
42 /* char *command_line; */
44 static const char **process_arguments (int, char **);
45 int validate_arguments (char **);
46 void print_help (void);
47 void print_usage (void);
48 int subst_text
= FALSE
;
50 static int state
[4] = {
58 main (int argc
, char **argv
)
60 int found
= 0, result
= STATE_UNKNOWN
;
63 output chld_out
, chld_err
;
66 setlocale (LC_ALL
, "");
67 bindtextdomain (PACKAGE
, LOCALEDIR
);
70 timeout_interval
= DEFAULT_TIMEOUT
;
72 command_line
= (char **) process_arguments (argc
, argv
);
74 /* Set signal handling and alarm */
75 if (signal (SIGALRM
, timeout_alarm_handler
) == SIG_ERR
)
76 die (STATE_UNKNOWN
, _("Cannot catch SIGALRM"));
78 (void) alarm ((unsigned) timeout_interval
);
80 /* catch when the command is quoted */
81 if(command_line
[1] == NULL
) {
82 result
= cmd_run (command_line
[0], &chld_out
, &chld_err
, 0);
84 result
= cmd_run_array (command_line
, &chld_out
, &chld_err
, 0);
86 if (chld_err
.lines
> 0) {
87 printf ("Error output from command:\n");
88 for (i
= 0; i
< chld_err
.lines
; i
++) {
89 printf ("%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
!= state
[result
] &&
100 result
>= 0 && result
<= 4) {
101 /* Loop over each match found */
102 while ((sub
= strstr (chld_out
.line
[i
], state_text (result
)))) {
103 /* Terminate the first part and skip over the string we'll substitute */
105 sub
+= strlen (state_text (result
));
106 /* then put everything back together */
107 asprintf (&chld_out
.line
[i
], "%s%s%s", chld_out
.line
[i
], state_text (state
[result
]), sub
);
110 printf ("%s\n", chld_out
.line
[i
]);
113 if (result
>= 0 && result
<= 4) {
114 exit (state
[result
]);
121 /* process command-line arguments */
123 process_arguments (int argc
, char **argv
)
129 static struct option longopts
[] = {
130 {"help", no_argument
, 0, 'h'},
131 {"version", no_argument
, 0, 'V'},
132 {"timeout", required_argument
, 0, 't'},
133 {"timeout-result", required_argument
, 0, 'T'},
134 {"ok", required_argument
, 0, 'o'},
135 {"warning", required_argument
, 0, 'w'},
136 {"critical", required_argument
, 0, 'c'},
137 {"unknown", required_argument
, 0, 'u'},
138 {"substitute", no_argument
, 0, 's'},
143 c
= getopt_long (argc
, argv
, "+hVt:T:o:w:c:u:s", longopts
, &option
);
145 if (c
== -1 || c
== EOF
)
156 case 'V': /* version */
157 print_revision (progname
, NP_VERSION
);
159 case 't': /* timeout period */
160 if (!is_integer (optarg
))
161 usage2 (_("Timeout interval must be a positive integer"), optarg
);
163 timeout_interval
= atoi (optarg
);
165 case 'T': /* Result to return on timeouts */
166 if ((timeout_state
= translate_state(optarg
)) == ERROR
)
167 usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
169 case 'o': /* replacement for OK */
170 if ((state
[STATE_OK
] = translate_state(optarg
)) == ERROR
)
171 usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
175 case 'w': /* replacement for WARNING */
176 if ((state
[STATE_WARNING
] = translate_state(optarg
)) == ERROR
)
177 usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
180 case 'c': /* replacement for CRITICAL */
181 if ((state
[STATE_CRITICAL
] = translate_state(optarg
)) == ERROR
)
182 usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
185 case 'u': /* replacement for UNKNOWN */
186 if ((state
[STATE_UNKNOWN
] = translate_state(optarg
)) == ERROR
)
187 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
190 case 's': /* Substitute status text */
196 validate_arguments (&argv
[optind
]);
198 if (permute
) { /* No [owcu] switch specified, default to this */
199 state
[STATE_OK
] = STATE_CRITICAL
;
200 state
[STATE_CRITICAL
] = STATE_OK
;
203 return (const char **) &argv
[optind
];
208 validate_arguments (char **command_line
)
210 if (command_line
[0] == NULL
)
211 usage4 (_("Could not parse arguments"));
213 if (strncmp(command_line
[0],"/",1) != 0 && strncmp(command_line
[0],"./",2) != 0)
214 usage4 (_("Require path to command"));
219 translate_state (char *state_text
)
222 for (temp_ptr
= state_text
; *temp_ptr
; temp_ptr
++) {
223 *temp_ptr
= toupper(*temp_ptr
);
225 if (!strcmp(state_text
,"OK") || !strcmp(state_text
,"0"))
227 if (!strcmp(state_text
,"WARNING") || !strcmp(state_text
,"1"))
228 return STATE_WARNING
;
229 if (!strcmp(state_text
,"CRITICAL") || !strcmp(state_text
,"2"))
230 return STATE_CRITICAL
;
231 if (!strcmp(state_text
,"UNKNOWN") || !strcmp(state_text
,"3"))
232 return STATE_UNKNOWN
;
239 print_revision (progname
, NP_VERSION
);
241 printf (COPYRIGHT
, copyright
, email
);
243 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
244 printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
250 printf (UT_HELP_VRSN
);
252 printf (UT_TIMEOUT
, timeout_interval
);
253 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
254 printf (" -T, --timeout-result=STATUS\n");
255 printf (" %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n"));
257 printf(" -o, --ok=STATUS\n");
258 printf(" -w, --warning=STATUS\n");
259 printf(" -c, --critical=STATUS\n");
260 printf(" -u, --unknown=STATUS\n");
261 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
262 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
263 printf(_(" OK and CRITICAL.\n"));
264 printf(" -s, --substitute\n");
265 printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n"));
268 printf ("%s\n", _("Examples:"));
269 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
270 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
271 printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
272 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
274 printf ("%s\n", _("Notes:"));
275 printf (" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
276 printf (" %s\n", _("The full path of the plugin must be provided."));
277 printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
278 printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
279 printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
281 printf (" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a"));
282 printf (" %s\n", _("plugin by setting the negate timeout a bit lower."));
292 printf ("%s\n", _("Usage:"));
293 printf ("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname
);