summaryrefslogtreecommitdiffstats
path: root/plugins/negate.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/negate.c')
-rw-r--r--plugins/negate.c270
1 files changed, 124 insertions, 146 deletions
diff --git a/plugins/negate.c b/plugins/negate.c
index c5fe7e1..ee497cd 100644
--- a/plugins/negate.c
+++ b/plugins/negate.c
@@ -1,33 +1,33 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring negate plugin 3 * Monitoring negate plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2002-2008 Monitoring Plugins Development Team 6 * Copyright (c) 2002-2008 Monitoring Plugins Development Team
7* 7 *
8* Description: 8 * Description:
9* 9 *
10* This file contains the negate plugin 10 * This file contains the negate plugin
11* 11 *
12* Negates the status of a plugin (returns OK for CRITICAL, and vice-versa). 12 * Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).
13* Can also perform custom state switching. 13 * Can also perform custom state switching.
14* 14 *
15* 15 *
16* This program is free software: you can redistribute it and/or modify 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 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 18 * the Free Software Foundation, either version 3 of the License, or
19* (at your option) any later version. 19 * (at your option) any later version.
20* 20 *
21* This program is distributed in the hope that it will be useful, 21 * This program is distributed in the hope that it will be useful,
22* but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24* GNU General Public License for more details. 24 * GNU General Public License for more details.
25* 25 *
26* You should have received a copy of the GNU General Public License 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/>. 27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28* 28 *
29* 29 *
30*****************************************************************************/ 30 *****************************************************************************/
31 31
32const char *progname = "negate"; 32const char *progname = "negate";
33const char *copyright = "2002-2008"; 33const char *copyright = "2002-2008";
@@ -43,10 +43,10 @@ const char *email = "devel@monitoring-plugins.org";
43 43
44/* char *command_line; */ 44/* char *command_line; */
45 45
46static const char **process_arguments (int, char **); 46static const char **process_arguments(int, char **);
47void validate_arguments (char **); 47void validate_arguments(char **);
48void print_help (void); 48void print_help(void);
49void print_usage (void); 49void print_usage(void);
50bool subst_text = false; 50bool subst_text = false;
51 51
52static int state[4] = { 52static int state[4] = {
@@ -56,185 +56,167 @@ static int state[4] = {
56 STATE_UNKNOWN, 56 STATE_UNKNOWN,
57}; 57};
58 58
59int 59int main(int argc, char **argv) {
60main (int argc, char **argv)
61{
62 int result = STATE_UNKNOWN; 60 int result = STATE_UNKNOWN;
63 char *sub; 61 char *sub;
64 char **command_line; 62 char **command_line;
65 output chld_out, chld_err; 63 output chld_out, chld_err;
66 64
67 setlocale (LC_ALL, ""); 65 setlocale(LC_ALL, "");
68 bindtextdomain (PACKAGE, LOCALEDIR); 66 bindtextdomain(PACKAGE, LOCALEDIR);
69 textdomain (PACKAGE); 67 textdomain(PACKAGE);
70 68
71 timeout_interval = DEFAULT_TIMEOUT; 69 timeout_interval = DEFAULT_TIMEOUT;
72 70
73 command_line = (char **) process_arguments (argc, argv); 71 command_line = (char **)process_arguments(argc, argv);
74 72
75 /* Set signal handling and alarm */ 73 /* Set signal handling and alarm */
76 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) 74 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
77 die (STATE_UNKNOWN, _("Cannot catch SIGALRM")); 75 die(STATE_UNKNOWN, _("Cannot catch SIGALRM"));
78 76
79 (void) alarm ((unsigned) timeout_interval); 77 (void)alarm((unsigned)timeout_interval);
80 78
81 /* catch when the command is quoted */ 79 /* catch when the command is quoted */
82 if(command_line[1] == NULL) { 80 if (command_line[1] == NULL) {
83 result = cmd_run (command_line[0], &chld_out, &chld_err, 0); 81 result = cmd_run(command_line[0], &chld_out, &chld_err, 0);
84 } else { 82 } else {
85 result = cmd_run_array (command_line, &chld_out, &chld_err, 0); 83 result = cmd_run_array(command_line, &chld_out, &chld_err, 0);
86 } 84 }
87 if (chld_err.lines > 0) { 85 if (chld_err.lines > 0) {
88 for (size_t i = 0; i < chld_err.lines; i++) { 86 for (size_t i = 0; i < chld_err.lines; i++) {
89 fprintf (stderr, "%s\n", chld_err.line[i]); 87 fprintf(stderr, "%s\n", chld_err.line[i]);
90 } 88 }
91 } 89 }
92 90
93 /* Return UNKNOWN or worse if no output is returned */ 91 /* Return UNKNOWN or worse if no output is returned */
94 if (chld_out.lines == 0) 92 if (chld_out.lines == 0)
95 die (max_state_alt (result, STATE_UNKNOWN), _("No data returned from command\n")); 93 die(max_state_alt(result, STATE_UNKNOWN), _("No data returned from command\n"));
96 94
97 for (size_t i = 0; i < chld_out.lines; i++) { 95 for (size_t i = 0; i < chld_out.lines; i++) {
98 if (subst_text && result >= 0 && result <= 4 && result != state[result]) { 96 if (subst_text && result >= 0 && result <= 4 && result != state[result]) {
99 /* Loop over each match found */ 97 /* Loop over each match found */
100 while ((sub = strstr (chld_out.line[i], state_text (result)))) { 98 while ((sub = strstr(chld_out.line[i], state_text(result)))) {
101 /* Terminate the first part and skip over the string we'll substitute */ 99 /* Terminate the first part and skip over the string we'll substitute */
102 *sub = '\0'; 100 *sub = '\0';
103 sub += strlen (state_text (result)); 101 sub += strlen(state_text(result));
104 /* then put everything back together */ 102 /* then put everything back together */
105 xasprintf (&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text (state[result]), sub); 103 xasprintf(&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text(state[result]), sub);
106 } 104 }
107 } 105 }
108 printf ("%s\n", chld_out.line[i]); 106 printf("%s\n", chld_out.line[i]);
109 } 107 }
110 108
111 if (result >= 0 && result <= 4) { 109 if (result >= 0 && result <= 4) {
112 exit (state[result]); 110 exit(state[result]);
113 } else { 111 } else {
114 exit (result); 112 exit(result);
115 } 113 }
116} 114}
117 115
118
119/* process command-line arguments */ 116/* process command-line arguments */
120static const char ** 117static const char **process_arguments(int argc, char **argv) {
121process_arguments (int argc, char **argv)
122{
123 int c; 118 int c;
124 bool permute = true; 119 bool permute = true;
125 120
126 int option = 0; 121 int option = 0;
127 static struct option longopts[] = { 122 static struct option longopts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'},
128 {"help", no_argument, 0, 'h'}, 123 {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'},
129 {"version", no_argument, 0, 'V'}, 124 {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'},
130 {"timeout", required_argument, 0, 't'}, 125 {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'},
131 {"timeout-result", required_argument, 0, 'T'}, 126 {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}};
132 {"ok", required_argument, 0, 'o'},
133 {"warning", required_argument, 0, 'w'},
134 {"critical", required_argument, 0, 'c'},
135 {"unknown", required_argument, 0, 'u'},
136 {"substitute", no_argument, 0, 's'},
137 {0, 0, 0, 0}
138 };
139 127
140 while (1) { 128 while (1) {
141 c = getopt_long (argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option); 129 c = getopt_long(argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option);
142 130
143 if (c == -1 || c == EOF) 131 if (c == -1 || c == EOF)
144 break; 132 break;
145 133
146 switch (c) { 134 switch (c) {
147 case '?': /* help */ 135 case '?': /* help */
148 usage5 (); 136 usage5();
149 break; 137 break;
150 case 'h': /* help */ 138 case 'h': /* help */
151 print_help (); 139 print_help();
152 exit (EXIT_SUCCESS); 140 exit(EXIT_SUCCESS);
153 break; 141 break;
154 case 'V': /* version */ 142 case 'V': /* version */
155 print_revision (progname, NP_VERSION); 143 print_revision(progname, NP_VERSION);
156 exit (EXIT_SUCCESS); 144 exit(EXIT_SUCCESS);
157 case 't': /* timeout period */ 145 case 't': /* timeout period */
158 if (!is_integer (optarg)) 146 if (!is_integer(optarg))
159 usage2 (_("Timeout interval must be a positive integer"), optarg); 147 usage2(_("Timeout interval must be a positive integer"), optarg);
160 else 148 else
161 timeout_interval = atoi (optarg); 149 timeout_interval = atoi(optarg);
162 break; 150 break;
163 case 'T': /* Result to return on timeouts */ 151 case 'T': /* Result to return on timeouts */
164 if ((timeout_state = mp_translate_state(optarg)) == ERROR) 152 if ((timeout_state = mp_translate_state(optarg)) == ERROR)
165 usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); 153 usage4(_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
166 break; 154 break;
167 case 'o': /* replacement for OK */ 155 case 'o': /* replacement for OK */
168 if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR) 156 if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR)
169 usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); 157 usage4(_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
170 permute = false; 158 permute = false;
171 break; 159 break;
172 160
173 case 'w': /* replacement for WARNING */ 161 case 'w': /* replacement for WARNING */
174 if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) 162 if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR)
175 usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); 163 usage4(_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
176 permute = false; 164 permute = false;
177 break; 165 break;
178 case 'c': /* replacement for CRITICAL */ 166 case 'c': /* replacement for CRITICAL */
179 if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) 167 if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR)
180 usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); 168 usage4(_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
181 permute = false; 169 permute = false;
182 break; 170 break;
183 case 'u': /* replacement for UNKNOWN */ 171 case 'u': /* replacement for UNKNOWN */
184 if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) 172 if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR)
185 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); 173 usage4(_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
186 permute = false; 174 permute = false;
187 break; 175 break;
188 case 's': /* Substitute status text */ 176 case 's': /* Substitute status text */
189 subst_text = true; 177 subst_text = true;
190 break; 178 break;
191 } 179 }
192 } 180 }
193 181
194 validate_arguments (&argv[optind]); 182 validate_arguments(&argv[optind]);
195 183
196 if (permute) { /* No [owcu] switch specified, default to this */ 184 if (permute) { /* No [owcu] switch specified, default to this */
197 state[STATE_OK] = STATE_CRITICAL; 185 state[STATE_OK] = STATE_CRITICAL;
198 state[STATE_CRITICAL] = STATE_OK; 186 state[STATE_CRITICAL] = STATE_OK;
199 } 187 }
200 188
201 return (const char **) &argv[optind]; 189 return (const char **)&argv[optind];
202} 190}
203 191
204 192void validate_arguments(char **command_line) {
205void
206validate_arguments (char **command_line)
207{
208 if (command_line[0] == NULL) 193 if (command_line[0] == NULL)
209 usage4 (_("Could not parse arguments")); 194 usage4(_("Could not parse arguments"));
210 195
211 if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0) 196 if (strncmp(command_line[0], "/", 1) != 0 && strncmp(command_line[0], "./", 2) != 0)
212 usage4 (_("Require path to command")); 197 usage4(_("Require path to command"));
213} 198}
214 199
200void print_help(void) {
201 print_revision(progname, NP_VERSION);
215 202
216void 203 printf(COPYRIGHT, copyright, email);
217print_help (void)
218{
219 print_revision (progname, NP_VERSION);
220
221 printf (COPYRIGHT, copyright, email);
222 204
223 printf ("%s\n", _("Negates only the return code of a plugin (returns OK for CRITICAL and vice-versa) by default.")); 205 printf("%s\n", _("Negates only the return code of a plugin (returns OK for CRITICAL and vice-versa) by default."));
224 printf ("%s\n", _("Additional switches can be used to control:\n")); 206 printf("%s\n", _("Additional switches can be used to control:\n"));
225 printf ("\t - which state becomes what\n"); 207 printf("\t - which state becomes what\n");
226 printf ("\t - changing the plugin output text to match the return code"); 208 printf("\t - changing the plugin output text to match the return code");
227 209
228 printf ("\n\n"); 210 printf("\n\n");
229 211
230 print_usage (); 212 print_usage();
231 213
232 printf (UT_HELP_VRSN); 214 printf(UT_HELP_VRSN);
233 215
234 printf (UT_PLUG_TIMEOUT, timeout_interval); 216 printf(UT_PLUG_TIMEOUT, timeout_interval);
235 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status.")); 217 printf(" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
236 printf (" -T, --timeout-result=STATUS\n"); 218 printf(" -T, --timeout-result=STATUS\n");
237 printf (" %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n")); 219 printf(" %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n"));
238 220
239 printf(" -o, --ok=STATUS\n"); 221 printf(" -o, --ok=STATUS\n");
240 printf(" -w, --warning=STATUS\n"); 222 printf(" -w, --warning=STATUS\n");
@@ -246,31 +228,27 @@ print_help (void)
246 printf(" -s, --substitute\n"); 228 printf(" -s, --substitute\n");
247 printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n")); 229 printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n"));
248 230
249 printf ("\n"); 231 printf("\n");
250 printf ("%s\n", _("Examples:")); 232 printf("%s\n", _("Examples:"));
251 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host"); 233 printf(" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
252 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin")); 234 printf(" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
253 printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'"); 235 printf(" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
254 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL")); 236 printf(" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
255 printf ("\n"); 237 printf("\n");
256 printf ("%s\n", _("Notes:")); 238 printf("%s\n", _("Notes:"));
257 printf (" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it.")); 239 printf(" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
258 printf (" %s\n", _("The full path of the plugin must be provided.")); 240 printf(" %s\n", _("The full path of the plugin must be provided."));
259 printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL.")); 241 printf(" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
260 printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK.")); 242 printf(" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
261 printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); 243 printf(" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
262 printf ("\n"); 244 printf("\n");
263 printf (" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a")); 245 printf(" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a"));
264 printf (" %s\n", _("plugin by setting the negate timeout a bit lower.")); 246 printf(" %s\n", _("plugin by setting the negate timeout a bit lower."));
265 247
266 printf (UT_SUPPORT); 248 printf(UT_SUPPORT);
267} 249}
268 250
269 251void print_usage(void) {
270 252 printf("%s\n", _("Usage:"));
271void 253 printf("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname);
272print_usage (void)
273{
274 printf ("%s\n", _("Usage:"));
275 printf ("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname);
276} 254}