1 /*****************************************************************************
3 * Monitoring negate plugin
6 * Copyright (c) 2002-2008 Monitoring 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
= "devel@monitoring-plugins.org";
36 #define DEFAULT_TIMEOUT 11
40 #include "utils_cmd.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] = {
60 main (int argc
, char **argv
)
62 int result
= STATE_UNKNOWN
;
65 output chld_out
, chld_err
;
68 setlocale (LC_ALL
, "");
69 bindtextdomain (PACKAGE
, LOCALEDIR
);
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);
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 */
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
]);
120 /* process command-line arguments */
122 process_arguments (int argc
, char **argv
)
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'},
142 c
= getopt_long (argc
, argv
, "+hVt:T:o:w:c:u:s", longopts
, &option
);
144 if (c
== -1 || c
== EOF
)
155 case 'V': /* version */
156 print_revision (progname
, NP_VERSION
);
158 case 't': /* timeout period */
159 if (!is_integer (optarg
))
160 usage2 (_("Timeout interval must be a positive integer"), optarg
);
162 timeout_interval
= atoi (optarg
);
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)."));
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)."));
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)."));
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)."));
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)."));
189 case 's': /* Substitute status text */
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
];
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"));
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."));
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"));
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"));
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."));
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."));
273 printf ("%s\n", _("Usage:"));
274 printf ("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname
);