diff options
Diffstat (limited to 'plugins/check_tcp.c')
-rw-r--r-- | plugins/check_tcp.c | 71 |
1 files changed, 35 insertions, 36 deletions
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index 1d307cf3..01dd35eb 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c | |||
@@ -41,7 +41,7 @@ const char *email = "devel@monitoring-plugins.org"; | |||
41 | #include <sys/select.h> | 41 | #include <sys/select.h> |
42 | 42 | ||
43 | #ifdef HAVE_SSL | 43 | #ifdef HAVE_SSL |
44 | static int check_cert = FALSE; | 44 | static bool check_cert = false; |
45 | static int days_till_exp_warn, days_till_exp_crit; | 45 | static int days_till_exp_warn, days_till_exp_crit; |
46 | # define my_recv(buf, len) ((flags & FLAG_SSL) ? np_net_ssl_read(buf, len) : read(sd, buf, len)) | 46 | # define my_recv(buf, len) ((flags & FLAG_SSL) ? np_net_ssl_read(buf, len) : read(sd, buf, len)) |
47 | # define my_send(buf, len) ((flags & FLAG_SSL) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0)) | 47 | # define my_send(buf, len) ((flags & FLAG_SSL) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0)) |
@@ -65,12 +65,12 @@ static int READ_TIMEOUT = 2; | |||
65 | 65 | ||
66 | static int server_port = 0; | 66 | static int server_port = 0; |
67 | static char *server_address = NULL; | 67 | static char *server_address = NULL; |
68 | static int host_specified = FALSE; | 68 | static bool host_specified = false; |
69 | static char *server_send = NULL; | 69 | static char *server_send = NULL; |
70 | static char *server_quit = NULL; | 70 | static char *server_quit = NULL; |
71 | static char **server_expect; | 71 | static char **server_expect; |
72 | static size_t server_expect_count = 0; | 72 | static size_t server_expect_count = 0; |
73 | static size_t maxbytes = 0; | 73 | static ssize_t maxbytes = 0; |
74 | static char **warn_codes = NULL; | 74 | static char **warn_codes = NULL; |
75 | static size_t warn_codes_count = 0; | 75 | static size_t warn_codes_count = 0; |
76 | static char **crit_codes = NULL; | 76 | static char **crit_codes = NULL; |
@@ -88,7 +88,7 @@ static int match_flags = NP_MATCH_EXACT; | |||
88 | 88 | ||
89 | #ifdef HAVE_SSL | 89 | #ifdef HAVE_SSL |
90 | static char *sni = NULL; | 90 | static char *sni = NULL; |
91 | static int sni_specified = FALSE; | 91 | static bool sni_specified = false; |
92 | #endif | 92 | #endif |
93 | 93 | ||
94 | #define FLAG_SSL 0x01 | 94 | #define FLAG_SSL 0x01 |
@@ -102,11 +102,9 @@ int | |||
102 | main (int argc, char **argv) | 102 | main (int argc, char **argv) |
103 | { | 103 | { |
104 | int result = STATE_UNKNOWN; | 104 | int result = STATE_UNKNOWN; |
105 | int i; | ||
106 | char *status = NULL; | 105 | char *status = NULL; |
107 | struct timeval tv; | 106 | struct timeval tv; |
108 | struct timeval timeout; | 107 | struct timeval timeout; |
109 | size_t len; | ||
110 | int match = -1; | 108 | int match = -1; |
111 | fd_set rfds; | 109 | fd_set rfds; |
112 | 110 | ||
@@ -121,10 +119,10 @@ main (int argc, char **argv) | |||
121 | if(progname != NULL) progname++; | 119 | if(progname != NULL) progname++; |
122 | else progname = argv[0]; | 120 | else progname = argv[0]; |
123 | 121 | ||
124 | len = strlen(progname); | 122 | size_t prog_name_len = strlen(progname); |
125 | if(len > 6 && !memcmp(progname, "check_", 6)) { | 123 | if(prog_name_len > 6 && !memcmp(progname, "check_", 6)) { |
126 | SERVICE = strdup(progname + 6); | 124 | SERVICE = strdup(progname + 6); |
127 | for(i = 0; i < len - 6; i++) | 125 | for(size_t i = 0; i < prog_name_len - 6; i++) |
128 | SERVICE[i] = toupper(SERVICE[i]); | 126 | SERVICE[i] = toupper(SERVICE[i]); |
129 | } | 127 | } |
130 | 128 | ||
@@ -247,7 +245,7 @@ main (int argc, char **argv) | |||
247 | #ifdef HAVE_SSL | 245 | #ifdef HAVE_SSL |
248 | if (flags & FLAG_SSL){ | 246 | if (flags & FLAG_SSL){ |
249 | result = np_net_ssl_init_with_hostname(sd, (sni_specified ? sni : NULL)); | 247 | result = np_net_ssl_init_with_hostname(sd, (sni_specified ? sni : NULL)); |
250 | if (result == STATE_OK && check_cert == TRUE) { | 248 | if (result == STATE_OK && check_cert) { |
251 | result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit); | 249 | result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit); |
252 | } | 250 | } |
253 | } | 251 | } |
@@ -275,19 +273,21 @@ main (int argc, char **argv) | |||
275 | printf("Quit string: %s\n", server_quit); | 273 | printf("Quit string: %s\n", server_quit); |
276 | } | 274 | } |
277 | printf("server_expect_count: %d\n", (int)server_expect_count); | 275 | printf("server_expect_count: %d\n", (int)server_expect_count); |
278 | for(i = 0; i < server_expect_count; i++) | 276 | for(size_t i = 0; i < server_expect_count; i++) |
279 | printf("\t%d: %s\n", i, server_expect[i]); | 277 | printf("\t%zd: %s\n", i, server_expect[i]); |
280 | } | 278 | } |
281 | 279 | ||
282 | /* if(len) later on, we know we have a non-NULL response */ | 280 | /* if(len) later on, we know we have a non-NULL response */ |
283 | len = 0; | 281 | ssize_t len = 0; |
282 | |||
284 | if (server_expect_count) { | 283 | if (server_expect_count) { |
284 | ssize_t received = 0; | ||
285 | 285 | ||
286 | /* watch for the expect string */ | 286 | /* watch for the expect string */ |
287 | while ((i = my_recv(buffer, sizeof(buffer))) > 0) { | 287 | while ((received = my_recv(buffer, sizeof(buffer))) > 0) { |
288 | status = realloc(status, len + i + 1); | 288 | status = realloc(status, len + received + 1); |
289 | memcpy(&status[len], buffer, i); | 289 | memcpy(&status[len], buffer, received); |
290 | len += i; | 290 | len += received; |
291 | status[len] = '\0'; | 291 | status[len] = '\0'; |
292 | 292 | ||
293 | /* stop reading if user-forced */ | 293 | /* stop reading if user-forced */ |
@@ -307,6 +307,7 @@ main (int argc, char **argv) | |||
307 | if(select(sd + 1, &rfds, NULL, NULL, &timeout) <= 0) | 307 | if(select(sd + 1, &rfds, NULL, NULL, &timeout) <= 0) |
308 | break; | 308 | break; |
309 | } | 309 | } |
310 | |||
310 | if (match == NP_MATCH_RETRY) | 311 | if (match == NP_MATCH_RETRY) |
311 | match = NP_MATCH_FAILURE; | 312 | match = NP_MATCH_FAILURE; |
312 | 313 | ||
@@ -378,18 +379,18 @@ main (int argc, char **argv) | |||
378 | if(match == NP_MATCH_FAILURE) | 379 | if(match == NP_MATCH_FAILURE) |
379 | printf ("|%s", | 380 | printf ("|%s", |
380 | fperfdata ("time", elapsed_time, "s", | 381 | fperfdata ("time", elapsed_time, "s", |
381 | (flags & FLAG_TIME_WARN ? TRUE : FALSE), 0, | 382 | (flags & FLAG_TIME_WARN ? true : false), 0, |
382 | (flags & FLAG_TIME_CRIT ? TRUE : FALSE), 0, | 383 | (flags & FLAG_TIME_CRIT ? true : false), 0, |
383 | TRUE, 0, | 384 | true, 0, |
384 | TRUE, socket_timeout) | 385 | true, socket_timeout) |
385 | ); | 386 | ); |
386 | else | 387 | else |
387 | printf("|%s", | 388 | printf("|%s", |
388 | fperfdata ("time", elapsed_time, "s", | 389 | fperfdata ("time", elapsed_time, "s", |
389 | (flags & FLAG_TIME_WARN ? TRUE : FALSE), warning_time, | 390 | (flags & FLAG_TIME_WARN ? true : false), warning_time, |
390 | (flags & FLAG_TIME_CRIT ? TRUE : FALSE), critical_time, | 391 | (flags & FLAG_TIME_CRIT ? true : false), critical_time, |
391 | TRUE, 0, | 392 | true, 0, |
392 | TRUE, socket_timeout) | 393 | true, socket_timeout) |
393 | ); | 394 | ); |
394 | 395 | ||
395 | putchar('\n'); | 396 | putchar('\n'); |
@@ -399,11 +400,9 @@ main (int argc, char **argv) | |||
399 | 400 | ||
400 | 401 | ||
401 | /* process command-line arguments */ | 402 | /* process command-line arguments */ |
402 | static int | 403 | static int process_arguments (int argc, char **argv) { |
403 | process_arguments (int argc, char **argv) | ||
404 | { | ||
405 | int c; | 404 | int c; |
406 | int escape = 0; | 405 | bool escape = false; |
407 | char *temp; | 406 | char *temp; |
408 | 407 | ||
409 | enum { | 408 | enum { |
@@ -492,7 +491,7 @@ process_arguments (int argc, char **argv) | |||
492 | #endif | 491 | #endif |
493 | break; | 492 | break; |
494 | case 'H': /* hostname */ | 493 | case 'H': /* hostname */ |
495 | host_specified = TRUE; | 494 | host_specified = true; |
496 | server_address = optarg; | 495 | server_address = optarg; |
497 | break; | 496 | break; |
498 | case 'c': /* critical */ | 497 | case 'c': /* critical */ |
@@ -527,7 +526,7 @@ process_arguments (int argc, char **argv) | |||
527 | server_port = atoi (optarg); | 526 | server_port = atoi (optarg); |
528 | break; | 527 | break; |
529 | case 'E': | 528 | case 'E': |
530 | escape = 1; | 529 | escape = true; |
531 | break; | 530 | break; |
532 | case 's': | 531 | case 's': |
533 | if (escape) | 532 | if (escape) |
@@ -601,7 +600,7 @@ process_arguments (int argc, char **argv) | |||
601 | usage2 (_("Invalid certificate expiration period"), optarg); | 600 | usage2 (_("Invalid certificate expiration period"), optarg); |
602 | days_till_exp_warn = atoi (optarg); | 601 | days_till_exp_warn = atoi (optarg); |
603 | } | 602 | } |
604 | check_cert = TRUE; | 603 | check_cert = true; |
605 | flags |= FLAG_SSL; | 604 | flags |= FLAG_SSL; |
606 | break; | 605 | break; |
607 | # endif /* USE_OPENSSL */ | 606 | # endif /* USE_OPENSSL */ |
@@ -617,7 +616,7 @@ process_arguments (int argc, char **argv) | |||
617 | case SNI_OPTION: | 616 | case SNI_OPTION: |
618 | #ifdef HAVE_SSL | 617 | #ifdef HAVE_SSL |
619 | flags |= FLAG_SSL; | 618 | flags |= FLAG_SSL; |
620 | sni_specified = TRUE; | 619 | sni_specified = true; |
621 | sni = optarg; | 620 | sni = optarg; |
622 | #else | 621 | #else |
623 | die (STATE_UNKNOWN, _("Invalid option - SSL is not available")); | 622 | die (STATE_UNKNOWN, _("Invalid option - SSL is not available")); |
@@ -630,15 +629,15 @@ process_arguments (int argc, char **argv) | |||
630 | } | 629 | } |
631 | 630 | ||
632 | c = optind; | 631 | c = optind; |
633 | if(host_specified == FALSE && c < argc) | 632 | if(!host_specified && c < argc) |
634 | server_address = strdup (argv[c++]); | 633 | server_address = strdup (argv[c++]); |
635 | 634 | ||
636 | if (server_address == NULL) | 635 | if (server_address == NULL) |
637 | usage4 (_("You must provide a server address")); | 636 | usage4 (_("You must provide a server address")); |
638 | else if (server_address[0] != '/' && is_host (server_address) == FALSE) | 637 | else if (server_address[0] != '/' && !is_host(server_address)) |
639 | die (STATE_CRITICAL, "%s %s - %s: %s\n", SERVICE, state_text(STATE_CRITICAL), _("Invalid hostname, address or socket"), server_address); | 638 | die (STATE_CRITICAL, "%s %s - %s: %s\n", SERVICE, state_text(STATE_CRITICAL), _("Invalid hostname, address or socket"), server_address); |
640 | 639 | ||
641 | return TRUE; | 640 | return OK; |
642 | } | 641 | } |
643 | 642 | ||
644 | 643 | ||