summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_fping.c122
1 files changed, 67 insertions, 55 deletions
diff --git a/plugins/check_fping.c b/plugins/check_fping.c
index c1d03ece..b85397d5 100644
--- a/plugins/check_fping.c
+++ b/plugins/check_fping.c
@@ -91,39 +91,47 @@ int main(int argc, char **argv) {
91 /* Parse extra opts if any */ 91 /* Parse extra opts if any */
92 argv = np_extra_opts(&argc, argv, progname); 92 argv = np_extra_opts(&argc, argv, progname);
93 93
94 if (process_arguments(argc, argv) == ERROR) 94 if (process_arguments(argc, argv) == ERROR) {
95 usage4(_("Could not parse arguments")); 95 usage4(_("Could not parse arguments"));
96 }
96 97
97 server = strscpy(server, server_name); 98 server = strscpy(server, server_name);
98 99
99 /* compose the command */ 100 /* compose the command */
100 if (target_timeout) 101 if (target_timeout) {
101 xasprintf(&option_string, "%s-t %d ", option_string, target_timeout); 102 xasprintf(&option_string, "%s-t %d ", option_string, target_timeout);
102 if (packet_interval) 103 }
104 if (packet_interval) {
103 xasprintf(&option_string, "%s-p %d ", option_string, packet_interval); 105 xasprintf(&option_string, "%s-p %d ", option_string, packet_interval);
104 if (sourceip) 106 }
107 if (sourceip) {
105 xasprintf(&option_string, "%s-S %s ", option_string, sourceip); 108 xasprintf(&option_string, "%s-S %s ", option_string, sourceip);
106 if (sourceif) 109 }
110 if (sourceif) {
107 xasprintf(&option_string, "%s-I %s ", option_string, sourceif); 111 xasprintf(&option_string, "%s-I %s ", option_string, sourceif);
108 if (dontfrag) 112 }
113 if (dontfrag) {
109 xasprintf(&option_string, "%s-M ", option_string); 114 xasprintf(&option_string, "%s-M ", option_string);
110 if (randomize_packet_data) 115 }
116 if (randomize_packet_data) {
111 xasprintf(&option_string, "%s-R ", option_string); 117 xasprintf(&option_string, "%s-R ", option_string);
112 118 }
113 119
114#ifdef PATH_TO_FPING6 120#ifdef PATH_TO_FPING6
115 if (address_family != AF_INET && is_inet6_addr(server)) 121 if (address_family != AF_INET && is_inet6_addr(server)) {
116 fping_prog = strdup(PATH_TO_FPING6); 122 fping_prog = strdup(PATH_TO_FPING6);
117 else 123 } else {
118 fping_prog = strdup(PATH_TO_FPING); 124 fping_prog = strdup(PATH_TO_FPING);
125 }
119#else 126#else
120 fping_prog = strdup(PATH_TO_FPING); 127 fping_prog = strdup(PATH_TO_FPING);
121#endif 128#endif
122 129
123 xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, packet_size, packet_count, server); 130 xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, packet_size, packet_count, server);
124 131
125 if (verbose) 132 if (verbose) {
126 printf("%s\n", command_line); 133 printf("%s\n", command_line);
134 }
127 135
128 /* run the command */ 136 /* run the command */
129 child_process = spopen(command_line); 137 child_process = spopen(command_line);
@@ -138,16 +146,18 @@ int main(int argc, char **argv) {
138 } 146 }
139 147
140 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 148 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
141 if (verbose) 149 if (verbose) {
142 printf("%s", input_buffer); 150 printf("%s", input_buffer);
151 }
143 status = max_state(status, textscan(input_buffer)); 152 status = max_state(status, textscan(input_buffer));
144 } 153 }
145 154
146 /* If we get anything on STDERR, at least set warning */ 155 /* If we get anything on STDERR, at least set warning */
147 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { 156 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
148 status = max_state(status, STATE_WARNING); 157 status = max_state(status, STATE_WARNING);
149 if (verbose) 158 if (verbose) {
150 printf("%s", input_buffer); 159 printf("%s", input_buffer);
160 }
151 status = max_state(status, textscan(input_buffer)); 161 status = max_state(status, textscan(input_buffer));
152 } 162 }
153 (void)fclose(child_stderr); 163 (void)fclose(child_stderr);
@@ -221,16 +231,17 @@ int textscan(char *buf) {
221 rtastr = 1 + index(rtastr, '/'); 231 rtastr = 1 + index(rtastr, '/');
222 loss = strtod(losstr, NULL); 232 loss = strtod(losstr, NULL);
223 rta = strtod(rtastr, NULL); 233 rta = strtod(rtastr, NULL);
224 if (cpl_p && loss > cpl) 234 if (cpl_p && loss > cpl) {
225 status = STATE_CRITICAL; 235 status = STATE_CRITICAL;
226 else if (crta_p && rta > crta) 236 } else if (crta_p && rta > crta) {
227 status = STATE_CRITICAL; 237 status = STATE_CRITICAL;
228 else if (wpl_p && loss > wpl) 238 } else if (wpl_p && loss > wpl) {
229 status = STATE_WARNING; 239 status = STATE_WARNING;
230 else if (wrta_p && rta > wrta) 240 } else if (wrta_p && rta > wrta) {
231 status = STATE_WARNING; 241 status = STATE_WARNING;
232 else 242 } else {
233 status = STATE_OK; 243 status = STATE_OK;
244 }
234 die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta, 245 die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta,
235 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), 246 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100),
236 fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); 247 fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0));
@@ -241,19 +252,21 @@ int textscan(char *buf) {
241 losstr = strstr(buf, "="); 252 losstr = strstr(buf, "=");
242 xmtstr = 1 + losstr; 253 xmtstr = 1 + losstr;
243 xmt = strtod(xmtstr, NULL); 254 xmt = strtod(xmtstr, NULL);
244 if (xmt == 0) 255 if (xmt == 0) {
245 die(STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name); 256 die(STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
257 }
246 losstr = 1 + strstr(losstr, "/"); 258 losstr = 1 + strstr(losstr, "/");
247 losstr = 1 + strstr(losstr, "/"); 259 losstr = 1 + strstr(losstr, "/");
248 loss = strtod(losstr, NULL); 260 loss = strtod(losstr, NULL);
249 if (atoi(losstr) == 100) 261 if (atoi(losstr) == 100) {
250 status = STATE_CRITICAL; 262 status = STATE_CRITICAL;
251 else if (cpl_p && loss > cpl) 263 } else if (cpl_p && loss > cpl) {
252 status = STATE_CRITICAL; 264 status = STATE_CRITICAL;
253 else if (wpl_p && loss > wpl) 265 } else if (wpl_p && loss > wpl) {
254 status = STATE_WARNING; 266 status = STATE_WARNING;
255 else 267 } else {
256 status = STATE_OK; 268 status = STATE_OK;
269 }
257 /* loss=%.0f%%;%d;%d;0;100 */ 270 /* loss=%.0f%%;%d;%d;0;100 */
258 die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss, 271 die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss,
259 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100)); 272 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100));
@@ -271,30 +284,20 @@ int process_arguments(int argc, char **argv) {
271 char *rv[2]; 284 char *rv[2];
272 285
273 int option = 0; 286 int option = 0;
274 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, 287 static struct option longopts[] = {
275 {"sourceip", required_argument, 0, 'S'}, 288 {"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'},
276 {"sourceif", required_argument, 0, 'I'}, 289 {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"alive", no_argument, 0, 'a'},
277 {"critical", required_argument, 0, 'c'}, 290 {"bytes", required_argument, 0, 'b'}, {"number", required_argument, 0, 'n'}, {"target-timeout", required_argument, 0, 'T'},
278 {"warning", required_argument, 0, 'w'}, 291 {"interval", required_argument, 0, 'i'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'},
279 {"alive", no_argument, 0, 'a'}, 292 {"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'},
280 {"bytes", required_argument, 0, 'b'}, 293 {"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, {0, 0, 0, 0}};
281 {"number", required_argument, 0, 'n'},
282 {"target-timeout", required_argument, 0, 'T'},
283 {"interval", required_argument, 0, 'i'},
284 {"verbose", no_argument, 0, 'v'},
285 {"version", no_argument, 0, 'V'},
286 {"help", no_argument, 0, 'h'},
287 {"use-ipv4", no_argument, 0, '4'},
288 {"use-ipv6", no_argument, 0, '6'},
289 {"dontfrag", no_argument, 0, 'M'},
290 {"random", no_argument, 0, 'R'},
291 {0, 0, 0, 0}};
292 294
293 rv[PL] = NULL; 295 rv[PL] = NULL;
294 rv[RTA] = NULL; 296 rv[RTA] = NULL;
295 297
296 if (argc < 2) 298 if (argc < 2) {
297 return ERROR; 299 return ERROR;
300 }
298 301
299 if (!is_option(argv[1])) { 302 if (!is_option(argv[1])) {
300 server_name = argv[1]; 303 server_name = argv[1];
@@ -306,8 +309,9 @@ int process_arguments(int argc, char **argv) {
306 while (1) { 309 while (1) {
307 c = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); 310 c = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option);
308 311
309 if (c == -1 || c == EOF || c == 1) 312 if (c == -1 || c == EOF || c == 1) {
310 break; 313 break;
314 }
311 315
312 switch (c) { 316 switch (c) {
313 case '?': /* print short usage statement if args not parsable */ 317 case '?': /* print short usage statement if args not parsable */
@@ -376,28 +380,32 @@ int process_arguments(int argc, char **argv) {
376 } 380 }
377 break; 381 break;
378 case 'b': /* bytes per packet */ 382 case 'b': /* bytes per packet */
379 if (is_intpos(optarg)) 383 if (is_intpos(optarg)) {
380 packet_size = atoi(optarg); 384 packet_size = atoi(optarg);
381 else 385 } else {
382 usage(_("Packet size must be a positive integer")); 386 usage(_("Packet size must be a positive integer"));
387 }
383 break; 388 break;
384 case 'n': /* number of packets */ 389 case 'n': /* number of packets */
385 if (is_intpos(optarg)) 390 if (is_intpos(optarg)) {
386 packet_count = atoi(optarg); 391 packet_count = atoi(optarg);
387 else 392 } else {
388 usage(_("Packet count must be a positive integer")); 393 usage(_("Packet count must be a positive integer"));
394 }
389 break; 395 break;
390 case 'T': /* timeout in msec */ 396 case 'T': /* timeout in msec */
391 if (is_intpos(optarg)) 397 if (is_intpos(optarg)) {
392 target_timeout = atoi(optarg); 398 target_timeout = atoi(optarg);
393 else 399 } else {
394 usage(_("Target timeout must be a positive integer")); 400 usage(_("Target timeout must be a positive integer"));
401 }
395 break; 402 break;
396 case 'i': /* interval in msec */ 403 case 'i': /* interval in msec */
397 if (is_intpos(optarg)) 404 if (is_intpos(optarg)) {
398 packet_interval = atoi(optarg); 405 packet_interval = atoi(optarg);
399 else 406 } else {
400 usage(_("Interval must be a positive integer")); 407 usage(_("Interval must be a positive integer"));
408 }
401 break; 409 break;
402 case 'R': 410 case 'R':
403 randomize_packet_data = true; 411 randomize_packet_data = true;
@@ -408,8 +416,9 @@ int process_arguments(int argc, char **argv) {
408 } 416 }
409 } 417 }
410 418
411 if (server_name == NULL) 419 if (server_name == NULL) {
412 usage4(_("Hostname was not supplied")); 420 usage4(_("Hostname was not supplied"));
421 }
413 422
414 return OK; 423 return OK;
415} 424}
@@ -419,15 +428,18 @@ int get_threshold(char *arg, char *rv[2]) {
419 char *arg2 = NULL; 428 char *arg2 = NULL;
420 429
421 arg1 = strscpy(arg1, arg); 430 arg1 = strscpy(arg1, arg);
422 if (strpbrk(arg1, ",:")) 431 if (strpbrk(arg1, ",:")) {
423 arg2 = 1 + strpbrk(arg1, ",:"); 432 arg2 = 1 + strpbrk(arg1, ",:");
433 }
424 434
425 if (arg2) { 435 if (arg2) {
426 arg1[strcspn(arg1, ",:")] = 0; 436 arg1[strcspn(arg1, ",:")] = 0;
427 if (strstr(arg1, "%") && strstr(arg2, "%")) 437 if (strstr(arg1, "%") && strstr(arg2, "%")) {
428 die(STATE_UNKNOWN, _("%s: Only one threshold may be packet loss (%s)\n"), progname, arg); 438 die(STATE_UNKNOWN, _("%s: Only one threshold may be packet loss (%s)\n"), progname, arg);
429 if (!strstr(arg1, "%") && !strstr(arg2, "%")) 439 }
440 if (!strstr(arg1, "%") && !strstr(arg2, "%")) {
430 die(STATE_UNKNOWN, _("%s: Only one threshold must be packet loss (%s)\n"), progname, arg); 441 die(STATE_UNKNOWN, _("%s: Only one threshold must be packet loss (%s)\n"), progname, arg);
442 }
431 } 443 }
432 444
433 if (arg2 && strstr(arg2, "%")) { 445 if (arg2 && strstr(arg2, "%")) {