summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-12 11:24:53 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-12 11:24:53 +0100
commit19514d21b90f3ed5e1925fa2a96b3bb9cfc40f02 (patch)
tree42a56637ad817390efed00679d23ee66660c75f9
parentc2ff4da7866acc9196642bf32c2891f205f51a9b (diff)
downloadmonitoring-plugins-19514d21b90f3ed5e1925fa2a96b3bb9cfc40f02.tar.gz
check_ping: clang-format
-rw-r--r--plugins/check_ping.c138
1 files changed, 86 insertions, 52 deletions
diff --git a/plugins/check_ping.c b/plugins/check_ping.c
index 4aafaf41..526b8618 100644
--- a/plugins/check_ping.c
+++ b/plugins/check_ping.c
@@ -89,8 +89,9 @@ int main(int argc, char **argv) {
89 /* Parse extra opts if any */ 89 /* Parse extra opts if any */
90 argv = np_extra_opts(&argc, argv, progname); 90 argv = np_extra_opts(&argc, argv, progname);
91 91
92 if (process_arguments(argc, argv) == ERROR) 92 if (process_arguments(argc, argv) == ERROR) {
93 usage4(_("Could not parse arguments")); 93 usage4(_("Could not parse arguments"));
94 }
94 95
95 /* Set signal handling and alarm */ 96 /* Set signal handling and alarm */
96 if (signal(SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { 97 if (signal(SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
@@ -108,15 +109,16 @@ int main(int argc, char **argv) {
108 for (i = 0; i < n_addresses; i++) { 109 for (i = 0; i < n_addresses; i++) {
109 110
110#ifdef PING6_COMMAND 111#ifdef PING6_COMMAND
111 if (address_family != AF_INET && is_inet6_addr(addresses[i])) 112 if (address_family != AF_INET && is_inet6_addr(addresses[i])) {
112 rawcmd = strdup(PING6_COMMAND); 113 rawcmd = strdup(PING6_COMMAND);
113 else 114 } else {
114 rawcmd = strdup(PING_COMMAND); 115 rawcmd = strdup(PING_COMMAND);
116 }
115#else 117#else
116 rawcmd = strdup(PING_COMMAND); 118 rawcmd = strdup(PING_COMMAND);
117#endif 119#endif
118 120
119 /* does the host address of number of packets argument come first? */ 121 /* does the host address of number of packets argument come first? */
120#ifdef PING_PACKETS_FIRST 122#ifdef PING_PACKETS_FIRST
121# ifdef PING_HAS_TIMEOUT 123# ifdef PING_HAS_TIMEOUT
122 xasprintf(&cmd, rawcmd, timeout_interval, max_packets, addresses[i]); 124 xasprintf(&cmd, rawcmd, timeout_interval, max_packets, addresses[i]);
@@ -127,8 +129,9 @@ int main(int argc, char **argv) {
127 xasprintf(&cmd, rawcmd, addresses[i], max_packets); 129 xasprintf(&cmd, rawcmd, addresses[i], max_packets);
128#endif 130#endif
129 131
130 if (verbose >= 2) 132 if (verbose >= 2) {
131 printf("CMD: %s\n", cmd); 133 printf("CMD: %s\n", cmd);
134 }
132 135
133 /* run the command */ 136 /* run the command */
134 this_result = run_ping(cmd, addresses[i]); 137 this_result = run_ping(cmd, addresses[i]);
@@ -138,24 +141,29 @@ int main(int argc, char **argv) {
138 die(STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n")); 141 die(STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n"));
139 } 142 }
140 143
141 if (pl >= cpl || rta >= crta || rta < 0) 144 if (pl >= cpl || rta >= crta || rta < 0) {
142 this_result = STATE_CRITICAL; 145 this_result = STATE_CRITICAL;
143 else if (pl >= wpl || rta >= wrta) 146 } else if (pl >= wpl || rta >= wrta) {
144 this_result = STATE_WARNING; 147 this_result = STATE_WARNING;
145 else if (pl >= 0 && rta >= 0) 148 } else if (pl >= 0 && rta >= 0) {
146 this_result = max_state(STATE_OK, this_result); 149 this_result = max_state(STATE_OK, this_result);
150 }
147 151
148 if (n_addresses > 1 && this_result != STATE_UNKNOWN) 152 if (n_addresses > 1 && this_result != STATE_UNKNOWN) {
149 die(STATE_OK, "%s is alive\n", addresses[i]); 153 die(STATE_OK, "%s is alive\n", addresses[i]);
154 }
150 155
151 if (display_html == true) 156 if (display_html == true) {
152 printf("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]); 157 printf("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
153 if (pl == 100) 158 }
159 if (pl == 100) {
154 printf(_("PING %s - %sPacket loss = %d%%"), state_text(this_result), warn_text, pl); 160 printf(_("PING %s - %sPacket loss = %d%%"), state_text(this_result), warn_text, pl);
155 else 161 } else {
156 printf(_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text(this_result), warn_text, pl, rta); 162 printf(_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text(this_result), warn_text, pl, rta);
157 if (display_html == true) 163 }
164 if (display_html == true) {
158 printf("</A>"); 165 printf("</A>");
166 }
159 167
160 /* Print performance data */ 168 /* Print performance data */
161 if (pl != 100) { 169 if (pl != 100) {
@@ -166,8 +174,9 @@ int main(int argc, char **argv) {
166 } 174 }
167 printf(" %s\n", perfdata("pl", (long)pl, "%", wpl > 0 ? true : false, wpl, cpl > 0 ? true : false, cpl, true, 0, false, 0)); 175 printf(" %s\n", perfdata("pl", (long)pl, "%", wpl > 0 ? true : false, wpl, cpl > 0 ? true : false, cpl, true, 0, false, 0));
168 176
169 if (verbose >= 2) 177 if (verbose >= 2) {
170 printf("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl); 178 printf("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
179 }
171 180
172 result = max_state(result, this_result); 181 result = max_state(result, this_result);
173 free(rawcmd); 182 free(rawcmd);
@@ -191,21 +200,25 @@ int process_arguments(int argc, char **argv) {
191 {"use-ipv6", no_argument, 0, '6'}, 200 {"use-ipv6", no_argument, 0, '6'},
192 {0, 0, 0, 0}}; 201 {0, 0, 0, 0}};
193 202
194 if (argc < 2) 203 if (argc < 2) {
195 return ERROR; 204 return ERROR;
205 }
196 206
197 for (c = 1; c < argc; c++) { 207 for (c = 1; c < argc; c++) {
198 if (strcmp("-to", argv[c]) == 0) 208 if (strcmp("-to", argv[c]) == 0) {
199 strcpy(argv[c], "-t"); 209 strcpy(argv[c], "-t");
200 if (strcmp("-nohtml", argv[c]) == 0) 210 }
211 if (strcmp("-nohtml", argv[c]) == 0) {
201 strcpy(argv[c], "-n"); 212 strcpy(argv[c], "-n");
213 }
202 } 214 }
203 215
204 while (1) { 216 while (1) {
205 c = getopt_long(argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option); 217 c = getopt_long(argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
206 218
207 if (c == -1 || c == EOF) 219 if (c == -1 || c == EOF) {
208 break; 220 break;
221 }
209 222
210 switch (c) { 223 switch (c) {
211 case '?': /* usage */ 224 case '?': /* usage */
@@ -241,8 +254,9 @@ int process_arguments(int argc, char **argv) {
241 if (n_addresses > max_addr) { 254 if (n_addresses > max_addr) {
242 max_addr *= 2; 255 max_addr *= 2;
243 addresses = realloc(addresses, sizeof(char *) * max_addr); 256 addresses = realloc(addresses, sizeof(char *) * max_addr);
244 if (addresses == NULL) 257 if (addresses == NULL) {
245 die(STATE_UNKNOWN, _("Could not realloc() addresses\n")); 258 die(STATE_UNKNOWN, _("Could not realloc() addresses\n"));
259 }
246 } 260 }
247 addresses[n_addresses - 1] = ptr; 261 addresses[n_addresses - 1] = ptr;
248 if ((ptr = index(ptr, ','))) { 262 if ((ptr = index(ptr, ','))) {
@@ -254,10 +268,11 @@ int process_arguments(int argc, char **argv) {
254 } 268 }
255 break; 269 break;
256 case 'p': /* number of packets to send */ 270 case 'p': /* number of packets to send */
257 if (is_intnonneg(optarg)) 271 if (is_intnonneg(optarg)) {
258 max_packets = atoi(optarg); 272 max_packets = atoi(optarg);
259 else 273 } else {
260 usage2(_("<max_packets> (%s) must be a non-negative number\n"), optarg); 274 usage2(_("<max_packets> (%s) must be a non-negative number\n"), optarg);
275 }
261 break; 276 break;
262 case 'n': /* no HTML */ 277 case 'n': /* no HTML */
263 display_html = false; 278 display_html = false;
@@ -275,8 +290,9 @@ int process_arguments(int argc, char **argv) {
275 } 290 }
276 291
277 c = optind; 292 c = optind;
278 if (c == argc) 293 if (c == argc) {
279 return validate_arguments(); 294 return validate_arguments();
295 }
280 296
281 if (addresses[0] == NULL) { 297 if (addresses[0] == NULL) {
282 if (!is_host(argv[c])) { 298 if (!is_host(argv[c])) {
@@ -284,8 +300,9 @@ int process_arguments(int argc, char **argv) {
284 } else { 300 } else {
285 addresses[0] = argv[c++]; 301 addresses[0] = argv[c++];
286 n_addresses++; 302 n_addresses++;
287 if (c == argc) 303 if (c == argc) {
288 return validate_arguments(); 304 return validate_arguments();
305 }
289 } 306 }
290 } 307 }
291 308
@@ -295,8 +312,9 @@ int process_arguments(int argc, char **argv) {
295 return ERROR; 312 return ERROR;
296 } else { 313 } else {
297 wpl = atoi(argv[c++]); 314 wpl = atoi(argv[c++]);
298 if (c == argc) 315 if (c == argc) {
299 return validate_arguments(); 316 return validate_arguments();
317 }
300 } 318 }
301 } 319 }
302 320
@@ -306,8 +324,9 @@ int process_arguments(int argc, char **argv) {
306 return ERROR; 324 return ERROR;
307 } else { 325 } else {
308 cpl = atoi(argv[c++]); 326 cpl = atoi(argv[c++]);
309 if (c == argc) 327 if (c == argc) {
310 return validate_arguments(); 328 return validate_arguments();
329 }
311 } 330 }
312 } 331 }
313 332
@@ -317,8 +336,9 @@ int process_arguments(int argc, char **argv) {
317 return ERROR; 336 return ERROR;
318 } else { 337 } else {
319 wrta = atof(argv[c++]); 338 wrta = atof(argv[c++]);
320 if (c == argc) 339 if (c == argc) {
321 return validate_arguments(); 340 return validate_arguments();
341 }
322 } 342 }
323 } 343 }
324 344
@@ -328,8 +348,9 @@ int process_arguments(int argc, char **argv) {
328 return ERROR; 348 return ERROR;
329 } else { 349 } else {
330 crta = atof(argv[c++]); 350 crta = atof(argv[c++]);
331 if (c == argc) 351 if (c == argc) {
332 return validate_arguments(); 352 return validate_arguments();
353 }
333 } 354 }
334 } 355 }
335 356
@@ -346,12 +367,13 @@ int process_arguments(int argc, char **argv) {
346} 367}
347 368
348int get_threshold(char *arg, float *trta, int *tpl) { 369int get_threshold(char *arg, float *trta, int *tpl) {
349 if (is_intnonneg(arg) && sscanf(arg, "%f", trta) == 1) 370 if (is_intnonneg(arg) && sscanf(arg, "%f", trta) == 1) {
350 return OK; 371 return OK;
351 else if (strpbrk(arg, ",:") && strstr(arg, "%") && sscanf(arg, "%f%*[:,]%d%%", trta, tpl) == 2) 372 } else if (strpbrk(arg, ",:") && strstr(arg, "%") && sscanf(arg, "%f%*[:,]%d%%", trta, tpl) == 2) {
352 return OK; 373 return OK;
353 else if (strstr(arg, "%") && sscanf(arg, "%d%%", tpl) == 1) 374 } else if (strstr(arg, "%") && sscanf(arg, "%d%%", tpl) == 1) {
354 return OK; 375 return OK;
376 }
355 377
356 usage2(_("%s: Warning threshold must be integer or percentage!\n\n"), arg); 378 usage2(_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
357 return STATE_UNKNOWN; 379 return STATE_UNKNOWN;
@@ -381,16 +403,19 @@ int validate_arguments() {
381 return ERROR; 403 return ERROR;
382 } 404 }
383 405
384 if (max_packets == -1) 406 if (max_packets == -1) {
385 max_packets = DEFAULT_MAX_PACKETS; 407 max_packets = DEFAULT_MAX_PACKETS;
408 }
386 409
387 max_seconds = crta / 1000.0 * max_packets + max_packets; 410 max_seconds = crta / 1000.0 * max_packets + max_packets;
388 if (max_seconds > timeout_interval) 411 if (max_seconds > timeout_interval) {
389 timeout_interval = (int)max_seconds; 412 timeout_interval = (int)max_seconds;
413 }
390 414
391 for (i = 0; i < n_addresses; i++) { 415 for (i = 0; i < n_addresses; i++) {
392 if (!is_host(addresses[i])) 416 if (!is_host(addresses[i])) {
393 usage2(_("Invalid hostname/address"), addresses[i]); 417 usage2(_("Invalid hostname/address"), addresses[i]);
418 }
394 } 419 }
395 420
396 if (n_addresses == 0) { 421 if (n_addresses == 0) {
@@ -405,17 +430,20 @@ int run_ping(const char *cmd, const char *addr) {
405 int result = STATE_UNKNOWN; 430 int result = STATE_UNKNOWN;
406 int match; 431 int match;
407 432
408 if ((child_process = spopen(cmd)) == NULL) 433 if ((child_process = spopen(cmd)) == NULL) {
409 die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd); 434 die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd);
435 }
410 436
411 child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); 437 child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r");
412 if (child_stderr == NULL) 438 if (child_stderr == NULL) {
413 printf(_("Cannot open stderr for %s\n"), cmd); 439 printf(_("Cannot open stderr for %s\n"), cmd);
440 }
414 441
415 while (fgets(buf, MAX_INPUT_BUFFER - 1, child_process)) { 442 while (fgets(buf, MAX_INPUT_BUFFER - 1, child_process)) {
416 443
417 if (verbose >= 3) 444 if (verbose >= 3) {
418 printf("Output: %s", buf); 445 printf("Output: %s", buf);
446 }
419 447
420 result = max_state(result, error_scan(buf, addr)); 448 result = max_state(result, error_scan(buf, addr));
421 449
@@ -430,8 +458,9 @@ int run_ping(const char *cmd, const char *addr) {
430 (sscanf(buf, "%*d packets transmitted, %*d received, %d%% packet loss, time%n", &pl, &match) && match) || 458 (sscanf(buf, "%*d packets transmitted, %*d received, %d%% packet loss, time%n", &pl, &match) && match) ||
431 (sscanf(buf, "%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss%n", &pl, &match) && match) || 459 (sscanf(buf, "%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss%n", &pl, &match) && match) ||
432 (sscanf(buf, "%*d packets transmitted %*d received, +%*d errors, %d%% packet loss%n", &pl, &match) && match) || 460 (sscanf(buf, "%*d packets transmitted %*d received, +%*d errors, %d%% packet loss%n", &pl, &match) && match) ||
433 (sscanf(buf, "%*[^(](%d%% %*[^)])%n", &pl, &match) && match)) 461 (sscanf(buf, "%*[^(](%d%% %*[^)])%n", &pl, &match) && match)) {
434 continue; 462 continue;
463 }
435 464
436 /* get the round trip average */ 465 /* get the round trip average */
437 else if ((sscanf(buf, "round-trip min/avg/max = %*f/%f/%*f%n", &rta, &match) && match) || 466 else if ((sscanf(buf, "round-trip min/avg/max = %*f/%f/%*f%n", &rta, &match) && match) ||
@@ -442,13 +471,15 @@ int run_ping(const char *cmd, const char *addr) {
442 (sscanf(buf, "round-trip (ms) min/avg/max = %*f/%f/%*f%n", &rta, &match) && match) || 471 (sscanf(buf, "round-trip (ms) min/avg/max = %*f/%f/%*f%n", &rta, &match) && match) ||
443 (sscanf(buf, "round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f%n", &rta, &match) && match) || 472 (sscanf(buf, "round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f%n", &rta, &match) && match) ||
444 (sscanf(buf, "rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms%n", &rta, &match) && match) || 473 (sscanf(buf, "rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms%n", &rta, &match) && match) ||
445 (sscanf(buf, "%*[^=] = %*fms, %*[^=] = %*fms, %*[^=] = %fms%n", &rta, &match) && match)) 474 (sscanf(buf, "%*[^=] = %*fms, %*[^=] = %*fms, %*[^=] = %fms%n", &rta, &match) && match)) {
446 continue; 475 continue;
476 }
447 } 477 }
448 478
449 /* this is needed because there is no rta if all packets are lost */ 479 /* this is needed because there is no rta if all packets are lost */
450 if (pl == 100) 480 if (pl == 100) {
451 rta = crta; 481 rta = crta;
482 }
452 483
453 /* check stderr, setting at least WARNING if there is output here */ 484 /* check stderr, setting at least WARNING if there is output here */
454 /* Add warning into warn_text */ 485 /* Add warning into warn_text */
@@ -474,39 +505,42 @@ int run_ping(const char *cmd, const char *addr) {
474 505
475 spclose(child_process); 506 spclose(child_process);
476 507
477 if (warn_text == NULL) 508 if (warn_text == NULL) {
478 warn_text = strdup(""); 509 warn_text = strdup("");
510 }
479 511
480 return result; 512 return result;
481} 513}
482 514
483int error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) { 515int error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) {
484 if (strstr(buf, "Network is unreachable") || strstr(buf, "Destination Net Unreachable") || strstr(buf, "No route")) 516 if (strstr(buf, "Network is unreachable") || strstr(buf, "Destination Net Unreachable") || strstr(buf, "No route")) {
485 die(STATE_CRITICAL, _("CRITICAL - Network Unreachable (%s)\n"), addr); 517 die(STATE_CRITICAL, _("CRITICAL - Network Unreachable (%s)\n"), addr);
486 else if (strstr(buf, "Destination Host Unreachable") || strstr(buf, "Address unreachable")) 518 } else if (strstr(buf, "Destination Host Unreachable") || strstr(buf, "Address unreachable")) {
487 die(STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)\n"), addr); 519 die(STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)\n"), addr);
488 else if (strstr(buf, "Destination Port Unreachable") || strstr(buf, "Port unreachable")) 520 } else if (strstr(buf, "Destination Port Unreachable") || strstr(buf, "Port unreachable")) {
489 die(STATE_CRITICAL, _("CRITICAL - Bogus ICMP: Port Unreachable (%s)\n"), addr); 521 die(STATE_CRITICAL, _("CRITICAL - Bogus ICMP: Port Unreachable (%s)\n"), addr);
490 else if (strstr(buf, "Destination Protocol Unreachable")) 522 } else if (strstr(buf, "Destination Protocol Unreachable")) {
491 die(STATE_CRITICAL, _("CRITICAL - Bogus ICMP: Protocol Unreachable (%s)\n"), addr); 523 die(STATE_CRITICAL, _("CRITICAL - Bogus ICMP: Protocol Unreachable (%s)\n"), addr);
492 else if (strstr(buf, "Destination Net Prohibited")) 524 } else if (strstr(buf, "Destination Net Prohibited")) {
493 die(STATE_CRITICAL, _("CRITICAL - Network Prohibited (%s)\n"), addr); 525 die(STATE_CRITICAL, _("CRITICAL - Network Prohibited (%s)\n"), addr);
494 else if (strstr(buf, "Destination Host Prohibited")) 526 } else if (strstr(buf, "Destination Host Prohibited")) {
495 die(STATE_CRITICAL, _("CRITICAL - Host Prohibited (%s)\n"), addr); 527 die(STATE_CRITICAL, _("CRITICAL - Host Prohibited (%s)\n"), addr);
496 else if (strstr(buf, "Packet filtered") || strstr(buf, "Administratively prohibited")) 528 } else if (strstr(buf, "Packet filtered") || strstr(buf, "Administratively prohibited")) {
497 die(STATE_CRITICAL, _("CRITICAL - Packet Filtered (%s)\n"), addr); 529 die(STATE_CRITICAL, _("CRITICAL - Packet Filtered (%s)\n"), addr);
498 else if (strstr(buf, "unknown host")) 530 } else if (strstr(buf, "unknown host")) {
499 die(STATE_CRITICAL, _("CRITICAL - Host not found (%s)\n"), addr); 531 die(STATE_CRITICAL, _("CRITICAL - Host not found (%s)\n"), addr);
500 else if (strstr(buf, "Time to live exceeded") || strstr(buf, "Time exceeded")) 532 } else if (strstr(buf, "Time to live exceeded") || strstr(buf, "Time exceeded")) {
501 die(STATE_CRITICAL, _("CRITICAL - Time to live exceeded (%s)\n"), addr); 533 die(STATE_CRITICAL, _("CRITICAL - Time to live exceeded (%s)\n"), addr);
502 else if (strstr(buf, "Destination unreachable: ")) 534 } else if (strstr(buf, "Destination unreachable: ")) {
503 die(STATE_CRITICAL, _("CRITICAL - Destination Unreachable (%s)\n"), addr); 535 die(STATE_CRITICAL, _("CRITICAL - Destination Unreachable (%s)\n"), addr);
536 }
504 537
505 if (strstr(buf, "(DUP!)") || strstr(buf, "DUPLICATES FOUND")) { 538 if (strstr(buf, "(DUP!)") || strstr(buf, "DUPLICATES FOUND")) {
506 if (warn_text == NULL) 539 if (warn_text == NULL) {
507 warn_text = strdup(_(WARN_DUPLICATES)); 540 warn_text = strdup(_(WARN_DUPLICATES));
508 else if (!strstr(warn_text, _(WARN_DUPLICATES)) && xasprintf(&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1) 541 } else if (!strstr(warn_text, _(WARN_DUPLICATES)) && xasprintf(&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1) {
509 die(STATE_UNKNOWN, _("Unable to realloc warn_text\n")); 542 die(STATE_UNKNOWN, _("Unable to realloc warn_text\n"));
543 }
510 return (STATE_WARNING); 544 return (STATE_WARNING);
511 } 545 }
512 546