summaryrefslogtreecommitdiffstats
path: root/plugins/negate.c
blob: ee497cdc2a152564241adc9c6df9cfcffc24a152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*****************************************************************************
 *
 * Monitoring negate plugin
 *
 * License: GPL
 * Copyright (c) 2002-2008 Monitoring Plugins Development Team
 *
 * Description:
 *
 * This file contains the negate plugin
 *
 * Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).
 * Can also perform custom state switching.
 *
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *****************************************************************************/

const char *progname = "negate";
const char *copyright = "2002-2008";
const char *email = "devel@monitoring-plugins.org";

#define DEFAULT_TIMEOUT 11

#include "common.h"
#include "utils.h"
#include "utils_cmd.h"

#include <ctype.h>

/* char *command_line; */

static const char **process_arguments(int, char **);
void validate_arguments(char **);
void print_help(void);
void print_usage(void);
bool subst_text = false;

static int state[4] = {
	STATE_OK,
	STATE_WARNING,
	STATE_CRITICAL,
	STATE_UNKNOWN,
};

int main(int argc, char **argv) {
	int result = STATE_UNKNOWN;
	char *sub;
	char **command_line;
	output chld_out, chld_err;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	timeout_interval = DEFAULT_TIMEOUT;

	command_line = (char **)process_arguments(argc, argv);

	/* Set signal handling and alarm */
	if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
		die(STATE_UNKNOWN, _("Cannot catch SIGALRM"));

	(void)alarm((unsigned)timeout_interval);

	/* catch when the command is quoted */
	if (command_line[1] == NULL) {
		result = cmd_run(command_line[0], &chld_out, &chld_err, 0);
	} else {
		result = cmd_run_array(command_line, &chld_out, &chld_err, 0);
	}
	if (chld_err.lines > 0) {
		for (size_t i = 0; i < chld_err.lines; i++) {
			fprintf(stderr, "%s\n", chld_err.line[i]);
		}
	}

	/* Return UNKNOWN or worse if no output is returned */
	if (chld_out.lines == 0)
		die(max_state_alt(result, STATE_UNKNOWN), _("No data returned from command\n"));

	for (size_t i = 0; i < chld_out.lines; i++) {
		if (subst_text && result >= 0 && result <= 4 && result != state[result]) {
			/* Loop over each match found */
			while ((sub = strstr(chld_out.line[i], state_text(result)))) {
				/* Terminate the first part and skip over the string we'll substitute */
				*sub = '\0';
				sub += strlen(state_text(result));
				/* then put everything back together */
				xasprintf(&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text(state[result]), sub);
			}
		}
		printf("%s\n", chld_out.line[i]);
	}

	if (result >= 0 && result <= 4) {
		exit(state[result]);
	} else {
		exit(result);
	}
}

/* process command-line arguments */
static const char **process_arguments(int argc, char **argv) {
	int c;
	bool permute = true;

	int option = 0;
	static struct option longopts[] = {{"help", no_argument, 0, 'h'},           {"version", no_argument, 0, 'V'},
									   {"timeout", required_argument, 0, 't'},  {"timeout-result", required_argument, 0, 'T'},
									   {"ok", required_argument, 0, 'o'},       {"warning", required_argument, 0, 'w'},
									   {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'},
									   {"substitute", no_argument, 0, 's'},     {0, 0, 0, 0}};

	while (1) {
		c = getopt_long(argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option);

		if (c == -1 || c == EOF)
			break;

		switch (c) {
		case '?': /* help */
			usage5();
			break;
		case 'h': /* help */
			print_help();
			exit(EXIT_SUCCESS);
			break;
		case 'V': /* version */
			print_revision(progname, NP_VERSION);
			exit(EXIT_SUCCESS);
		case 't': /* timeout period */
			if (!is_integer(optarg))
				usage2(_("Timeout interval must be a positive integer"), optarg);
			else
				timeout_interval = atoi(optarg);
			break;
		case 'T': /* Result to return on timeouts */
			if ((timeout_state = mp_translate_state(optarg)) == ERROR)
				usage4(_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
			break;
		case 'o': /* replacement for OK */
			if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR)
				usage4(_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
			permute = false;
			break;

		case 'w': /* replacement for WARNING */
			if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR)
				usage4(_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
			permute = false;
			break;
		case 'c': /* replacement for CRITICAL */
			if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR)
				usage4(_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
			permute = false;
			break;
		case 'u': /* replacement for UNKNOWN */
			if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR)
				usage4(_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
			permute = false;
			break;
		case 's': /* Substitute status text */
			subst_text = true;
			break;
		}
	}

	validate_arguments(&argv[optind]);

	if (permute) { /* No [owcu] switch specified, default to this */
		state[STATE_OK] = STATE_CRITICAL;
		state[STATE_CRITICAL] = STATE_OK;
	}

	return (const char **)&argv[optind];
}

void validate_arguments(char **command_line) {
	if (command_line[0] == NULL)
		usage4(_("Could not parse arguments"));

	if (strncmp(command_line[0], "/", 1) != 0 && strncmp(command_line[0], "./", 2) != 0)
		usage4(_("Require path to command"));
}

void print_help(void) {
	print_revision(progname, NP_VERSION);

	printf(COPYRIGHT, copyright, email);

	printf("%s\n", _("Negates only the return code of a plugin (returns OK for CRITICAL and vice-versa) by default."));
	printf("%s\n", _("Additional switches can be used to control:\n"));
	printf("\t  - which state becomes what\n");
	printf("\t  - changing the plugin output text to match the return code");

	printf("\n\n");

	print_usage();

	printf(UT_HELP_VRSN);

	printf(UT_PLUG_TIMEOUT, timeout_interval);
	printf("    %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
	printf(" -T, --timeout-result=STATUS\n");
	printf("    %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n"));

	printf(" -o, --ok=STATUS\n");
	printf(" -w, --warning=STATUS\n");
	printf(" -c, --critical=STATUS\n");
	printf(" -u, --unknown=STATUS\n");
	printf(_("    STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
	printf(_("    quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
	printf(_("    OK and CRITICAL.\n"));
	printf(" -s, --substitute\n");
	printf(_("    Substitute output text as well. Will only substitute text in CAPITALS\n"));

	printf("\n");
	printf("%s\n", _("Examples:"));
	printf(" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
	printf("    %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
	printf(" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
	printf("    %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
	printf("\n");
	printf("%s\n", _("Notes:"));
	printf(" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
	printf(" %s\n", _("The full path of the plugin must be provided."));
	printf(" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
	printf(" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
	printf(" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
	printf("\n");
	printf(" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a"));
	printf(" %s\n", _("plugin by setting the negate timeout a bit lower."));

	printf(UT_SUPPORT);
}

void print_usage(void) {
	printf("%s\n", _("Usage:"));
	printf("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname);
}