diff options
Diffstat (limited to 'plugins/netutils.c')
-rw-r--r-- | plugins/netutils.c | 71 |
1 files changed, 46 insertions, 25 deletions
diff --git a/plugins/netutils.c b/plugins/netutils.c index 5f118a9e..e2916c65 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c | |||
@@ -28,6 +28,8 @@ | |||
28 | *****************************************************************************/ | 28 | *****************************************************************************/ |
29 | 29 | ||
30 | #include "common.h" | 30 | #include "common.h" |
31 | #include "output.h" | ||
32 | #include "states.h" | ||
31 | #include "netutils.h" | 33 | #include "netutils.h" |
32 | 34 | ||
33 | unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; | 35 | unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; |
@@ -43,12 +45,19 @@ int address_family = AF_INET; | |||
43 | 45 | ||
44 | /* handles socket timeouts */ | 46 | /* handles socket timeouts */ |
45 | void socket_timeout_alarm_handler(int sig) { | 47 | void socket_timeout_alarm_handler(int sig) { |
46 | if (sig == SIGALRM) | 48 | mp_subcheck timeout_sc = mp_subcheck_init(); |
47 | printf(_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); | 49 | timeout_sc = mp_set_subcheck_state(timeout_sc, socket_timeout_state); |
48 | else | ||
49 | printf(_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); | ||
50 | 50 | ||
51 | exit(socket_timeout_state); | 51 | if (sig == SIGALRM) { |
52 | xasprintf(&timeout_sc.output, _("Socket timeout after %d seconds\n"), socket_timeout); | ||
53 | } else { | ||
54 | xasprintf(&timeout_sc.output, _("Abnormal timeout after %d seconds\n"), socket_timeout); | ||
55 | } | ||
56 | |||
57 | mp_check overall = mp_check_init(); | ||
58 | mp_add_subcheck_to_check(&overall, timeout_sc); | ||
59 | |||
60 | mp_exit(overall); | ||
52 | } | 61 | } |
53 | 62 | ||
54 | /* connects to a host on a specified tcp port, sends a string, and gets a | 63 | /* connects to a host on a specified tcp port, sends a string, and gets a |
@@ -65,12 +74,13 @@ int process_tcp_request2(const char *server_address, int server_port, const char | |||
65 | int recv_length = 0; | 74 | int recv_length = 0; |
66 | 75 | ||
67 | result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); | 76 | result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); |
68 | if (result != STATE_OK) | 77 | if (result != STATE_OK) { |
69 | return STATE_CRITICAL; | 78 | return STATE_CRITICAL; |
79 | } | ||
70 | 80 | ||
71 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); | 81 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); |
72 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { | 82 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { |
73 | printf("%s\n", _("Send failed")); | 83 | // printf("%s\n", _("Send failed")); |
74 | result = STATE_WARNING; | 84 | result = STATE_WARNING; |
75 | } | 85 | } |
76 | 86 | ||
@@ -87,7 +97,7 @@ int process_tcp_request2(const char *server_address, int server_port, const char | |||
87 | if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ | 97 | if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ |
88 | if (!recv_length) { | 98 | if (!recv_length) { |
89 | strcpy(recv_buffer, ""); | 99 | strcpy(recv_buffer, ""); |
90 | printf("%s\n", _("No data was received from host!")); | 100 | // printf("%s\n", _("No data was received from host!")); |
91 | result = STATE_WARNING; | 101 | result = STATE_WARNING; |
92 | } else { /* this one failed, but previous ones worked */ | 102 | } else { /* this one failed, but previous ones worked */ |
93 | recv_buffer[recv_length] = 0; | 103 | recv_buffer[recv_length] = 0; |
@@ -130,8 +140,9 @@ int process_request(const char *server_address, int server_port, int proto, cons | |||
130 | result = STATE_OK; | 140 | result = STATE_OK; |
131 | 141 | ||
132 | result = np_net_connect(server_address, server_port, &sd, proto); | 142 | result = np_net_connect(server_address, server_port, &sd, proto); |
133 | if (result != STATE_OK) | 143 | if (result != STATE_OK) { |
134 | return STATE_CRITICAL; | 144 | return STATE_CRITICAL; |
145 | } | ||
135 | 146 | ||
136 | result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); | 147 | result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); |
137 | 148 | ||
@@ -169,8 +180,9 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
169 | host_name++; | 180 | host_name++; |
170 | len -= 2; | 181 | len -= 2; |
171 | } | 182 | } |
172 | if (len >= sizeof(host)) | 183 | if (len >= sizeof(host)) { |
173 | return STATE_UNKNOWN; | 184 | return STATE_UNKNOWN; |
185 | } | ||
174 | memcpy(host, host_name, len); | 186 | memcpy(host, host_name, len); |
175 | host[len] = '\0'; | 187 | host[len] = '\0'; |
176 | snprintf(port_str, sizeof(port_str), "%d", port); | 188 | snprintf(port_str, sizeof(port_str), "%d", port); |
@@ -226,13 +238,14 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
226 | die(STATE_UNKNOWN, _("Socket creation failed")); | 238 | die(STATE_UNKNOWN, _("Socket creation failed")); |
227 | } | 239 | } |
228 | result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); | 240 | result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); |
229 | if (result < 0 && errno == ECONNREFUSED) | 241 | if (result < 0 && errno == ECONNREFUSED) { |
230 | was_refused = true; | 242 | was_refused = true; |
243 | } | ||
231 | } | 244 | } |
232 | 245 | ||
233 | if (result == 0) | 246 | if (result == 0) { |
234 | return STATE_OK; | 247 | return STATE_OK; |
235 | else if (was_refused) { | 248 | } else if (was_refused) { |
236 | switch (econn_refuse_state) { /* a user-defined expected outcome */ | 249 | switch (econn_refuse_state) { /* a user-defined expected outcome */ |
237 | case STATE_OK: | 250 | case STATE_OK: |
238 | case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ | 251 | case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ |
@@ -267,7 +280,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
267 | 280 | ||
268 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); | 281 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); |
269 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { | 282 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { |
270 | printf("%s\n", _("Send failed")); | 283 | // printf("%s\n", _("Send failed")); |
271 | result = STATE_WARNING; | 284 | result = STATE_WARNING; |
272 | } | 285 | } |
273 | 286 | ||
@@ -282,7 +295,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
282 | /* make sure some data has arrived */ | 295 | /* make sure some data has arrived */ |
283 | if (!FD_ISSET(sd, &readfds)) { | 296 | if (!FD_ISSET(sd, &readfds)) { |
284 | strcpy(recv_buffer, ""); | 297 | strcpy(recv_buffer, ""); |
285 | printf("%s\n", _("No data was received from host!")); | 298 | // printf("%s\n", _("No data was received from host!")); |
286 | result = STATE_WARNING; | 299 | result = STATE_WARNING; |
287 | } | 300 | } |
288 | 301 | ||
@@ -290,11 +303,13 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
290 | recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); | 303 | recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); |
291 | if (recv_result == -1) { | 304 | if (recv_result == -1) { |
292 | strcpy(recv_buffer, ""); | 305 | strcpy(recv_buffer, ""); |
293 | if (proto != IPPROTO_TCP) | 306 | if (proto != IPPROTO_TCP) { |
294 | printf("%s\n", _("Receive failed")); | 307 | // printf("%s\n", _("Receive failed")); |
308 | } | ||
295 | result = STATE_WARNING; | 309 | result = STATE_WARNING; |
296 | } else | 310 | } else { |
297 | recv_buffer[recv_result] = 0; | 311 | recv_buffer[recv_result] = 0; |
312 | } | ||
298 | 313 | ||
299 | /* die returned string */ | 314 | /* die returned string */ |
300 | recv_buffer[recv_size - 1] = 0; | 315 | recv_buffer[recv_size - 1] = 0; |
@@ -303,26 +318,30 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
303 | } | 318 | } |
304 | 319 | ||
305 | bool is_host(const char *address) { | 320 | bool is_host(const char *address) { |
306 | if (is_addr(address) || is_hostname(address)) | 321 | if (is_addr(address) || is_hostname(address)) { |
307 | return (true); | 322 | return (true); |
323 | } | ||
308 | 324 | ||
309 | return (false); | 325 | return (false); |
310 | } | 326 | } |
311 | 327 | ||
312 | void host_or_die(const char *str) { | 328 | void host_or_die(const char *str) { |
313 | if (!str || (!is_addr(str) && !is_hostname(str))) | 329 | if (!str || (!is_addr(str) && !is_hostname(str))) { |
314 | usage_va(_("Invalid hostname/address - %s"), str); | 330 | usage_va(_("Invalid hostname/address - %s"), str); |
331 | } | ||
315 | } | 332 | } |
316 | 333 | ||
317 | bool is_addr(const char *address) { | 334 | bool is_addr(const char *address) { |
318 | #ifdef USE_IPV6 | 335 | #ifdef USE_IPV6 |
319 | if (address_family == AF_INET && is_inet_addr(address)) | 336 | if (address_family == AF_INET && is_inet_addr(address)) { |
320 | return true; | 337 | return true; |
321 | else if (address_family == AF_INET6 && is_inet6_addr(address)) | 338 | } else if (address_family == AF_INET6 && is_inet6_addr(address)) { |
322 | return true; | 339 | return true; |
340 | } | ||
323 | #else | 341 | #else |
324 | if (is_inet_addr(address)) | 342 | if (is_inet_addr(address)) { |
325 | return (true); | 343 | return (true); |
344 | } | ||
326 | #endif | 345 | #endif |
327 | 346 | ||
328 | return (false); | 347 | return (false); |
@@ -337,11 +356,13 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { | |||
337 | hints.ai_family = family; | 356 | hints.ai_family = family; |
338 | 357 | ||
339 | retval = getaddrinfo(in, NULL, &hints, &res); | 358 | retval = getaddrinfo(in, NULL, &hints, &res); |
340 | if (retval != 0) | 359 | if (retval != 0) { |
341 | return false; | 360 | return false; |
361 | } | ||
342 | 362 | ||
343 | if (ss != NULL) | 363 | if (ss != NULL) { |
344 | memcpy(ss, res->ai_addr, res->ai_addrlen); | 364 | memcpy(ss, res->ai_addr, res->ai_addrlen); |
365 | } | ||
345 | freeaddrinfo(res); | 366 | freeaddrinfo(res); |
346 | return true; | 367 | return true; |
347 | } | 368 | } |