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 command_line
= (char **) process_arguments (argc
, argv
);
72 /* Set signal handling and alarm */
73 if (signal (SIGALRM
, timeout_alarm_handler
) == SIG_ERR
)
74 die (STATE_UNKNOWN
, _("Cannot catch SIGALRM"));
76 (void) alarm ((unsigned) timeout_interval
);
78 /* catch when the command is quoted */
79 if(command_line
[1] == NULL
) {
80 result
= cmd_run (command_line
[0], &chld_out
, &chld_err
, 0);
82 result
= cmd_run_array (command_line
, &chld_out
, &chld_err
, 0);
84 if (chld_err
.lines
> 0) {
85 printf ("Error output from command:\n");
86 for (i
= 0; i
< chld_err
.lines
; i
++) {
87 printf ("%s\n", chld_err
.line
[i
]);
92 /* Return UNKNOWN or worse if no output is returned */
93 if (chld_out
.lines
== 0)
94 die (max_state_alt (result
, STATE_UNKNOWN
), _("No data returned from command\n"));
96 for (i
= 0; i
< chld_out
.lines
; i
++) {
97 if (subst_text
&& result
!= state
[result
] &&
98 result
>= 0 && result
<= 4) {
99 /* Loop over each match found */
100 while ((sub
= strstr (chld_out
.line
[i
], state_text (result
)))) {
101 /* Terminate the first part and skip over the string we'll substitute */
103 sub
+= strlen (state_text (result
));
104 /* then put everything back together */
105 asprintf (&chld_out
.line
[i
], "%s%s%s", chld_out
.line
[i
], state_text (state
[result
]), sub
);
108 printf ("%s\n", chld_out
.line
[i
]);
111 if (result
>= 0 && result
<= 4) {
112 exit (state
[result
]);
119 /* process command-line arguments */
121 process_arguments (int argc
, char **argv
)
127 static struct option longopts
[] = {
128 {"help", no_argument
, 0, 'h'},
129 {"version", no_argument
, 0, 'V'},
130 {"timeout", required_argument
, 0, 't'},
131 {"ok", required_argument
, 0, 'o'},
132 {"warning", required_argument
, 0, 'w'},
133 {"critical", required_argument
, 0, 'c'},
134 {"unknown", required_argument
, 0, 'u'},
135 {"substitute", no_argument
, 0, 's'},
140 c
= getopt_long (argc
, argv
, "+hVt:o:w:c:u:s", longopts
, &option
);
142 if (c
== -1 || c
== EOF
)
153 case 'V': /* version */
154 print_revision (progname
, NP_VERSION
);
156 case 't': /* timeout period */
157 if (!is_integer (optarg
))
158 usage2 (_("Timeout interval must be a positive integer"), optarg
);
160 timeout_interval
= atoi (optarg
);
162 case 'o': /* replacement for OK */
163 if ((state
[STATE_OK
] = translate_state(optarg
)) == ERROR
)
164 usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
168 case 'w': /* replacement for WARNING */
169 if ((state
[STATE_WARNING
] = translate_state(optarg
)) == ERROR
)
170 usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
173 case 'c': /* replacement for CRITICAL */
174 if ((state
[STATE_CRITICAL
] = translate_state(optarg
)) == ERROR
)
175 usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
178 case 'u': /* replacement for UNKNOWN */
179 if ((state
[STATE_UNKNOWN
] = translate_state(optarg
)) == ERROR
)
180 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
183 case 's': /* Substitute status text */
189 validate_arguments (&argv
[optind
]);
191 if (permute
) { /* No [owcu] switch specified, default to this */
192 state
[STATE_OK
] = STATE_CRITICAL
;
193 state
[STATE_CRITICAL
] = STATE_OK
;
196 return (const char **) &argv
[optind
];
201 validate_arguments (char **command_line
)
203 if (command_line
[0] == NULL
)
204 usage4 (_("Could not parse arguments"));
206 if (strncmp(command_line
[0],"/",1) != 0 && strncmp(command_line
[0],"./",2) != 0)
207 usage4 (_("Require path to command"));
212 translate_state (char *state_text
)
215 for (temp_ptr
= state_text
; *temp_ptr
; temp_ptr
++) {
216 *temp_ptr
= toupper(*temp_ptr
);
218 if (!strcmp(state_text
,"OK") || !strcmp(state_text
,"0"))
220 if (!strcmp(state_text
,"WARNING") || !strcmp(state_text
,"1"))
221 return STATE_WARNING
;
222 if (!strcmp(state_text
,"CRITICAL") || !strcmp(state_text
,"2"))
223 return STATE_CRITICAL
;
224 if (!strcmp(state_text
,"UNKNOWN") || !strcmp(state_text
,"3"))
225 return STATE_UNKNOWN
;
232 print_revision (progname
, NP_VERSION
);
234 printf (COPYRIGHT
, copyright
, email
);
236 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
237 printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
243 printf (_(UT_HELP_VRSN
));
245 printf (_(UT_TIMEOUT
), DEFAULT_TIMEOUT
);
246 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
248 printf(" -o, --ok=STATUS\n");
249 printf(" -w, --warning=STATUS\n");
250 printf(" -c, --critical=STATUS\n");
251 printf(" -u, --unknown=STATUS\n");
252 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
253 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
254 printf(_(" OK and CRITICAL.\n"));
255 printf(" -s, --substitute\n");
256 printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n"));
259 printf ("%s\n", _("Examples:"));
260 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
261 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
262 printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
263 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
265 printf ("%s\n", _("Notes:"));
266 printf (" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
267 printf (" %s\n", _("The full path of the plugin must be provided."));
268 printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
269 printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
270 printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
272 printf (_(UT_SUPPORT
));
280 printf (_("Usage:"));
281 printf ("%s [-t timeout] [-owcu STATE] [-s] <definition of wrapped plugin>\n", progname
);