This patch adds support for using the readcli lib.
[monitoring-plugins.git] / plugins / negate.c
blobb320e3563210d2cbf23efe7163d2e68b1aa46dd0
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 printf ("Error output from command:\n");
90 for (i = 0; i < chld_err.lines; i++) {
91 printf ("%s\n", chld_err.line[i]);
93 exit (STATE_WARNING);
96 /* Return UNKNOWN or worse if no output is returned */
97 if (chld_out.lines == 0)
98 die (max_state_alt (result, STATE_UNKNOWN), _("No data returned from command\n"));
100 for (i = 0; i < chld_out.lines; i++) {
101 if (subst_text && result >= 0 && result <= 4 && result != state[result]) {
102 /* Loop over each match found */
103 while ((sub = strstr (chld_out.line[i], state_text (result)))) {
104 /* Terminate the first part and skip over the string we'll substitute */
105 *sub = '\0';
106 sub += strlen (state_text (result));
107 /* then put everything back together */
108 xasprintf (&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text (state[result]), sub);
111 printf ("%s\n", chld_out.line[i]);
114 if (result >= 0 && result <= 4) {
115 exit (state[result]);
116 } else {
117 exit (result);
122 /* process command-line arguments */
123 static const char **
124 process_arguments (int argc, char **argv)
126 int c;
127 int permute = TRUE;
129 int option = 0;
130 static struct option longopts[] = {
131 {"help", no_argument, 0, 'h'},
132 {"version", no_argument, 0, 'V'},
133 {"timeout", required_argument, 0, 't'},
134 {"timeout-result", required_argument, 0, 'T'},
135 {"ok", required_argument, 0, 'o'},
136 {"warning", required_argument, 0, 'w'},
137 {"critical", required_argument, 0, 'c'},
138 {"unknown", required_argument, 0, 'u'},
139 {"substitute", no_argument, 0, 's'},
140 {0, 0, 0, 0}
143 while (1) {
144 c = getopt_long (argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option);
146 if (c == -1 || c == EOF)
147 break;
149 switch (c) {
150 case '?': /* help */
151 usage5 ();
152 break;
153 case 'h': /* help */
154 print_help ();
155 exit (EXIT_SUCCESS);
156 break;
157 case 'V': /* version */
158 print_revision (progname, NP_VERSION);
159 exit (EXIT_SUCCESS);
160 case 't': /* timeout period */
161 if (!is_integer (optarg))
162 usage2 (_("Timeout interval must be a positive integer"), optarg);
163 else
164 timeout_interval = atoi (optarg);
165 break;
166 case 'T': /* Result to return on timeouts */
167 if ((timeout_state = mp_translate_state(optarg)) == ERROR)
168 usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
169 break;
170 case 'o': /* replacement for OK */
171 if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR)
172 usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
173 permute = FALSE;
174 break;
176 case 'w': /* replacement for WARNING */
177 if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR)
178 usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
179 permute = FALSE;
180 break;
181 case 'c': /* replacement for CRITICAL */
182 if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR)
183 usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
184 permute = FALSE;
185 break;
186 case 'u': /* replacement for UNKNOWN */
187 if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR)
188 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
189 permute = FALSE;
190 break;
191 case 's': /* Substitute status text */
192 subst_text = TRUE;
193 break;
197 validate_arguments (&argv[optind]);
199 if (permute) { /* No [owcu] switch specified, default to this */
200 state[STATE_OK] = STATE_CRITICAL;
201 state[STATE_CRITICAL] = STATE_OK;
204 return (const char **) &argv[optind];
208 void
209 validate_arguments (char **command_line)
211 if (command_line[0] == NULL)
212 usage4 (_("Could not parse arguments"));
214 if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0)
215 usage4 (_("Require path to command"));
219 void
220 print_help (void)
222 print_revision (progname, NP_VERSION);
224 printf (COPYRIGHT, copyright, email);
226 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
227 printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
229 printf ("\n\n");
231 print_usage ();
233 printf (UT_HELP_VRSN);
235 printf (UT_PLUG_TIMEOUT, timeout_interval);
236 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
237 printf (" -T, --timeout-result=STATUS\n");
238 printf (" %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n"));
240 printf(" -o, --ok=STATUS\n");
241 printf(" -w, --warning=STATUS\n");
242 printf(" -c, --critical=STATUS\n");
243 printf(" -u, --unknown=STATUS\n");
244 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
245 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
246 printf(_(" OK and CRITICAL.\n"));
247 printf(" -s, --substitute\n");
248 printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n"));
250 printf ("\n");
251 printf ("%s\n", _("Examples:"));
252 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
253 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
254 printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
255 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
256 printf ("\n");
257 printf ("%s\n", _("Notes:"));
258 printf (" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
259 printf (" %s\n", _("The full path of the plugin must be provided."));
260 printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
261 printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
262 printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
263 printf ("\n");
264 printf (" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a"));
265 printf (" %s\n", _("plugin by setting the negate timeout a bit lower."));
267 printf (UT_SUPPORT);
272 void
273 print_usage (void)
275 printf ("%s\n", _("Usage:"));
276 printf ("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname);