summaryrefslogtreecommitdiffstats
path: root/plugins/check_ping.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-09-15 13:18:17 +0200
committerGitHub <noreply@github.com>2025-09-15 13:18:17 +0200
commit8ef825d85fb4d09c32ca44c545d6eb8d995ddea4 (patch)
tree5ab7b18797dfd5849dec7827c87ca3bb5fcb0993 /plugins/check_ping.c
parenta3cf9041af810770daf5d9b83f1906fa9bb0dd11 (diff)
parent204cf956f0b3db90d079321ee957b3860da7e33f (diff)
downloadmonitoring-plugins-8ef825d85fb4d09c32ca44c545d6eb8d995ddea4.tar.gz
Merge pull request #2149 from RincewindsHat/clang-format
Clang format
Diffstat (limited to 'plugins/check_ping.c')
-rw-r--r--plugins/check_ping.c125
1 files changed, 86 insertions, 39 deletions
diff --git a/plugins/check_ping.c b/plugins/check_ping.c
index 6bcdeaad..61feb958 100644
--- a/plugins/check_ping.c
+++ b/plugins/check_ping.c
@@ -135,7 +135,8 @@ int main(int argc, char **argv) {
135 die(STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n")); 135 die(STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n"));
136 } 136 }
137 137
138 if (pinged.packet_loss >= config.cpl || pinged.round_trip_average >= config.crta || pinged.round_trip_average < 0) { 138 if (pinged.packet_loss >= config.cpl || pinged.round_trip_average >= config.crta ||
139 pinged.round_trip_average < 0) {
139 pinged.state = STATE_CRITICAL; 140 pinged.state = STATE_CRITICAL;
140 } else if (pinged.packet_loss >= config.wpl || pinged.round_trip_average >= config.wrta) { 141 } else if (pinged.packet_loss >= config.wpl || pinged.round_trip_average >= config.wrta) {
141 pinged.state = STATE_WARNING; 142 pinged.state = STATE_WARNING;
@@ -151,10 +152,11 @@ int main(int argc, char **argv) {
151 printf("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, config.addresses[i]); 152 printf("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, config.addresses[i]);
152 } 153 }
153 if (pinged.packet_loss == 100) { 154 if (pinged.packet_loss == 100) {
154 printf(_("PING %s - %sPacket loss = %d%%"), state_text(pinged.state), warn_text, pinged.packet_loss); 155 printf(_("PING %s - %sPacket loss = %d%%"), state_text(pinged.state), warn_text,
156 pinged.packet_loss);
155 } else { 157 } else {
156 printf(_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text(pinged.state), warn_text, pinged.packet_loss, 158 printf(_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text(pinged.state),
157 pinged.round_trip_average); 159 warn_text, pinged.packet_loss, pinged.round_trip_average);
158 } 160 }
159 if (config.display_html) { 161 if (config.display_html) {
160 printf("</A>"); 162 printf("</A>");
@@ -162,14 +164,16 @@ int main(int argc, char **argv) {
162 164
163 /* Print performance data */ 165 /* Print performance data */
164 if (pinged.packet_loss != 100) { 166 if (pinged.packet_loss != 100) {
165 printf("|%s", fperfdata("rta", pinged.round_trip_average, "ms", (bool)(config.wrta > 0), config.wrta, (bool)(config.crta > 0), 167 printf("|%s",
166 config.crta, true, 0, false, 0)); 168 fperfdata("rta", pinged.round_trip_average, "ms", (bool)(config.wrta > 0),
169 config.wrta, (bool)(config.crta > 0), config.crta, true, 0, false, 0));
167 } else { 170 } else {
168 printf("| rta=U;%f;%f;;", config.wrta, config.crta); 171 printf("| rta=U;%f;%f;;", config.wrta, config.crta);
169 } 172 }
170 173
171 printf(" %s\n", perfdata("pl", (long)pinged.packet_loss, "%", (bool)(config.wpl > 0), config.wpl, (bool)(config.cpl > 0), 174 printf(" %s\n",
172 config.cpl, true, 0, false, 0)); 175 perfdata("pl", (long)pinged.packet_loss, "%", (bool)(config.wpl > 0), config.wpl,
176 (bool)(config.cpl > 0), config.cpl, true, 0, false, 0));
173 177
174 if (verbose >= 2) { 178 if (verbose >= 2) {
175 printf("%f:%d%% %f:%d%%\n", config.wrta, config.wpl, config.crta, config.cpl); 179 printf("%f:%d%% %f:%d%%\n", config.wrta, config.wpl, config.crta, config.cpl);
@@ -254,7 +258,8 @@ check_ping_config_wrapper process_arguments(int argc, char **argv) {
254 result.config.n_addresses++; 258 result.config.n_addresses++;
255 if (result.config.n_addresses > max_addr) { 259 if (result.config.n_addresses > max_addr) {
256 max_addr *= 2; 260 max_addr *= 2;
257 result.config.addresses = realloc(result.config.addresses, sizeof(char *) * max_addr); 261 result.config.addresses =
262 realloc(result.config.addresses, sizeof(char *) * max_addr);
258 if (result.config.addresses == NULL) { 263 if (result.config.addresses == NULL) {
259 die(STATE_UNKNOWN, _("Could not realloc() addresses\n")); 264 die(STATE_UNKNOWN, _("Could not realloc() addresses\n"));
260 } 265 }
@@ -411,13 +416,15 @@ check_ping_config_wrapper validate_arguments(check_ping_config_wrapper config_wr
411 } 416 }
412 417
413 if (config_wrapper.config.wrta > config_wrapper.config.crta) { 418 if (config_wrapper.config.wrta > config_wrapper.config.crta) {
414 printf(_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), config_wrapper.config.wrta, config_wrapper.config.crta); 419 printf(_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), config_wrapper.config.wrta,
420 config_wrapper.config.crta);
415 config_wrapper.errorcode = ERROR; 421 config_wrapper.errorcode = ERROR;
416 return config_wrapper; 422 return config_wrapper;
417 } 423 }
418 424
419 if (config_wrapper.config.wpl > config_wrapper.config.cpl) { 425 if (config_wrapper.config.wpl > config_wrapper.config.cpl) {
420 printf(_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), config_wrapper.config.wpl, config_wrapper.config.cpl); 426 printf(_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), config_wrapper.config.wpl,
427 config_wrapper.config.cpl);
421 config_wrapper.errorcode = ERROR; 428 config_wrapper.errorcode = ERROR;
422 return config_wrapper; 429 return config_wrapper;
423 } 430 }
@@ -426,7 +433,8 @@ check_ping_config_wrapper validate_arguments(check_ping_config_wrapper config_wr
426 config_wrapper.config.max_packets = DEFAULT_MAX_PACKETS; 433 config_wrapper.config.max_packets = DEFAULT_MAX_PACKETS;
427 } 434 }
428 435
429 double max_seconds = (config_wrapper.config.crta / 1000.0 * config_wrapper.config.max_packets) + config_wrapper.config.max_packets; 436 double max_seconds = (config_wrapper.config.crta / 1000.0 * config_wrapper.config.max_packets) +
437 config_wrapper.config.max_packets;
430 if (max_seconds > timeout_interval) { 438 if (max_seconds > timeout_interval) {
431 timeout_interval = (unsigned int)max_seconds; 439 timeout_interval = (unsigned int)max_seconds;
432 } 440 }
@@ -470,37 +478,70 @@ ping_result run_ping(const char *cmd, const char *addr, double crta) {
470 478
471 /* get the percent loss statistics */ 479 /* get the percent loss statistics */
472 int match = 0; 480 int match = 0;
473 if ((sscanf(buf, "%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss%n", &result.packet_loss, &match) == 481 if ((sscanf(
474 1 && 482 buf,
483 "%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss%n",
484 &result.packet_loss, &match) == 1 &&
475 match) || 485 match) ||
476 (sscanf(buf, "%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss%n", &result.packet_loss, 486 (sscanf(buf,
477 &match) == 1 && 487 "%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet "
488 "loss%n",
489 &result.packet_loss, &match) == 1 &&
490 match) ||
491 (sscanf(buf,
492 "%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss%n",
493 &result.packet_loss, &match) == 1 &&
494 match) ||
495 (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% packet loss%n",
496 &result.packet_loss, &match) == 1 &&
497 match) ||
498 (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% loss, time%n",
499 &result.packet_loss, &match) == 1 &&
478 match) || 500 match) ||
479 (sscanf(buf, "%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss%n", &result.packet_loss, &match) == 1 && 501 (sscanf(buf, "%*d packets transmitted, %*d received, %d%% loss, time%n",
502 &result.packet_loss, &match) == 1 &&
480 match) || 503 match) ||
481 (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% packet loss%n", &result.packet_loss, &match) == 1 && match) || 504 (sscanf(buf, "%*d packets transmitted, %*d received, %d%% packet loss, time%n",
482 (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% loss, time%n", &result.packet_loss, &match) == 1 && match) || 505 &result.packet_loss, &match) == 1 &&
483 (sscanf(buf, "%*d packets transmitted, %*d received, %d%% loss, time%n", &result.packet_loss, &match) == 1 && match) || 506 match) == 1 ||
484 (sscanf(buf, "%*d packets transmitted, %*d received, %d%% packet loss, time%n", &result.packet_loss, &match) == 1 && match) == 507 (sscanf(buf, "%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss%n",
485 1 || 508 &result.packet_loss, &match) == 1 &&
486 (sscanf(buf, "%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss%n", &result.packet_loss, &match) == 1 &&
487 match) || 509 match) ||
488 (sscanf(buf, "%*d packets transmitted %*d received, +%*d errors, %d%% packet loss%n", &result.packet_loss, &match) == 1 && 510 (sscanf(buf, "%*d packets transmitted %*d received, +%*d errors, %d%% packet loss%n",
511 &result.packet_loss, &match) == 1 &&
489 match) || 512 match) ||
490 (sscanf(buf, "%*[^(](%d%% %*[^)])%n", &result.packet_loss, &match) == 1 && match)) { 513 (sscanf(buf, "%*[^(](%d%% %*[^)])%n", &result.packet_loss, &match) == 1 && match)) {
491 continue; 514 continue;
492 } 515 }
493 516
494 /* get the round trip average */ 517 /* get the round trip average */
495 if ((sscanf(buf, "round-trip min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average, &match) == 1 && match) || 518 if ((sscanf(buf, "round-trip min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average,
496 (sscanf(buf, "round-trip min/avg/max/mdev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || 519 &match) == 1 &&
497 (sscanf(buf, "round-trip min/avg/max/sdev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || 520 match) ||
498 (sscanf(buf, "round-trip min/avg/max/stddev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || 521 (sscanf(buf, "round-trip min/avg/max/mdev = %*f/%lf/%*f/%*f%n",
499 (sscanf(buf, "round-trip min/avg/max/std-dev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || 522 &result.round_trip_average, &match) == 1 &&
500 (sscanf(buf, "round-trip (ms) min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average, &match) == 1 && match) || 523 match) ||
501 (sscanf(buf, "round-trip (ms) min/avg/max/stddev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) == 1 && match) || 524 (sscanf(buf, "round-trip min/avg/max/sdev = %*f/%lf/%*f/%*f%n",
502 (sscanf(buf, "rtt min/avg/max/mdev = %*f/%lf/%*f/%*f ms%n", &result.round_trip_average, &match) == 1 && match) || 525 &result.round_trip_average, &match) == 1 &&
503 (sscanf(buf, "%*[^=] = %*fms, %*[^=] = %*fms, %*[^=] = %lfms%n", &result.round_trip_average, &match) == 1 && match)) { 526 match) ||
527 (sscanf(buf, "round-trip min/avg/max/stddev = %*f/%lf/%*f/%*f%n",
528 &result.round_trip_average, &match) == 1 &&
529 match) ||
530 (sscanf(buf, "round-trip min/avg/max/std-dev = %*f/%lf/%*f/%*f%n",
531 &result.round_trip_average, &match) == 1 &&
532 match) ||
533 (sscanf(buf, "round-trip (ms) min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average,
534 &match) == 1 &&
535 match) ||
536 (sscanf(buf, "round-trip (ms) min/avg/max/stddev = %*f/%lf/%*f/%*f%n",
537 &result.round_trip_average, &match) == 1 &&
538 match) ||
539 (sscanf(buf, "rtt min/avg/max/mdev = %*f/%lf/%*f/%*f ms%n", &result.round_trip_average,
540 &match) == 1 &&
541 match) ||
542 (sscanf(buf, "%*[^=] = %*fms, %*[^=] = %*fms, %*[^=] = %lfms%n",
543 &result.round_trip_average, &match) == 1 &&
544 match)) {
504 continue; 545 continue;
505 } 546 }
506 } 547 }
@@ -513,7 +554,8 @@ ping_result run_ping(const char *cmd, const char *addr, double crta) {
513 /* check stderr, setting at least WARNING if there is output here */ 554 /* check stderr, setting at least WARNING if there is output here */
514 /* Add warning into warn_text */ 555 /* Add warning into warn_text */
515 while (fgets(buf, MAX_INPUT_BUFFER - 1, child_stderr)) { 556 while (fgets(buf, MAX_INPUT_BUFFER - 1, child_stderr)) {
516 if (!strstr(buf, "WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP") && !strstr(buf, "Warning: time of day goes back") 557 if (!strstr(buf, "WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP") &&
558 !strstr(buf, "Warning: time of day goes back")
517 559
518 ) { 560 ) {
519 if (verbose >= 3) { 561 if (verbose >= 3) {
@@ -524,7 +566,8 @@ ping_result run_ping(const char *cmd, const char *addr, double crta) {
524 if (warn_text == NULL) { 566 if (warn_text == NULL) {
525 warn_text = strdup(_("System call sent warnings to stderr ")); 567 warn_text = strdup(_("System call sent warnings to stderr "));
526 } else { 568 } else {
527 xasprintf(&warn_text, "%s %s", warn_text, _("System call sent warnings to stderr ")); 569 xasprintf(&warn_text, "%s %s", warn_text,
570 _("System call sent warnings to stderr "));
528 } 571 }
529 } 572 }
530 } 573 }
@@ -542,7 +585,8 @@ ping_result run_ping(const char *cmd, const char *addr, double crta) {
542} 585}
543 586
544mp_state_enum error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) { 587mp_state_enum error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) {
545 if (strstr(buf, "Network is unreachable") || strstr(buf, "Destination Net Unreachable") || strstr(buf, "No route")) { 588 if (strstr(buf, "Network is unreachable") || strstr(buf, "Destination Net Unreachable") ||
589 strstr(buf, "No route")) {
546 die(STATE_CRITICAL, _("CRITICAL - Network Unreachable (%s)\n"), addr); 590 die(STATE_CRITICAL, _("CRITICAL - Network Unreachable (%s)\n"), addr);
547 } else if (strstr(buf, "Destination Host Unreachable") || strstr(buf, "Address unreachable")) { 591 } else if (strstr(buf, "Destination Host Unreachable") || strstr(buf, "Address unreachable")) {
548 die(STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)\n"), addr); 592 die(STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)\n"), addr);
@@ -567,7 +611,8 @@ mp_state_enum error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) {
567 if (strstr(buf, "(DUP!)") || strstr(buf, "DUPLICATES FOUND")) { 611 if (strstr(buf, "(DUP!)") || strstr(buf, "DUPLICATES FOUND")) {
568 if (warn_text == NULL) { 612 if (warn_text == NULL) {
569 warn_text = strdup(_(WARN_DUPLICATES)); 613 warn_text = strdup(_(WARN_DUPLICATES));
570 } else if (!strstr(warn_text, _(WARN_DUPLICATES)) && xasprintf(&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1) { 614 } else if (!strstr(warn_text, _(WARN_DUPLICATES)) &&
615 xasprintf(&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1) {
571 die(STATE_UNKNOWN, _("Unable to realloc warn_text\n")); 616 die(STATE_UNKNOWN, _("Unable to realloc warn_text\n"));
572 } 617 }
573 return STATE_WARNING; 618 return STATE_WARNING;
@@ -613,8 +658,10 @@ void print_help(void) {
613 printf("%s\n", _("percentage of packet loss to trigger an alarm state.")); 658 printf("%s\n", _("percentage of packet loss to trigger an alarm state."));
614 659
615 printf("\n"); 660 printf("\n");
616 printf("%s\n", _("This plugin uses the ping command to probe the specified host for packet loss")); 661 printf("%s\n",
617 printf("%s\n", _("(percentage) and round trip average (milliseconds). It can produce HTML output.")); 662 _("This plugin uses the ping command to probe the specified host for packet loss"));
663 printf("%s\n",
664 _("(percentage) and round trip average (milliseconds). It can produce HTML output."));
618 665
619 printf(UT_SUPPORT); 666 printf(UT_SUPPORT);
620} 667}