summaryrefslogtreecommitdiffstats
path: root/plugins/netutils.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-09 11:35:03 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-09 11:35:03 +0100
commit95311d2e2c03d94f6bae0cacbac75fc1cd25b0e4 (patch)
tree960c12b0c7e87c508bcfcea2a99eec6fe34b03ee /plugins/netutils.c
parente3d2fccade397bbd6b318495192b4ca346becef0 (diff)
parent6dbc84f93595be81aee4de4ac3122b942047c22d (diff)
downloadmonitoring-plugins-95311d2e2c03d94f6bae0cacbac75fc1cd25b0e4.tar.gz
Merge branch 'master' into refactor/check_game
Diffstat (limited to 'plugins/netutils.c')
-rw-r--r--plugins/netutils.c93
1 files changed, 58 insertions, 35 deletions
diff --git a/plugins/netutils.c b/plugins/netutils.c
index ee81912a..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
33unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; 35unsigned 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 */
45void socket_timeout_alarm_handler(int sig) { 47void 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,15 +180,16 @@ 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);
177 result = getaddrinfo(host, port_str, &hints, &res); 189 result = getaddrinfo(host, port_str, &hints, &res);
178 190
179 if (result != 0) { 191 if (result != 0) {
180 printf("%s\n", gai_strerror(result)); 192 // printf("%s\n", gai_strerror(result));
181 return STATE_UNKNOWN; 193 return STATE_UNKNOWN;
182 } 194 }
183 195
@@ -187,7 +199,7 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) {
187 *sd = socket(r->ai_family, socktype, r->ai_protocol); 199 *sd = socket(r->ai_family, socktype, r->ai_protocol);
188 200
189 if (*sd < 0) { 201 if (*sd < 0) {
190 printf("%s\n", _("Socket creation failed")); 202 // printf("%s\n", _("Socket creation failed"));
191 freeaddrinfo(r); 203 freeaddrinfo(r);
192 return STATE_UNKNOWN; 204 return STATE_UNKNOWN;
193 } 205 }
@@ -226,21 +238,23 @@ 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... */
239 case STATE_CRITICAL: /* user did not set econn_refuse_state, or wanted critical */ 252 case STATE_CRITICAL: /* user did not set econn_refuse_state, or wanted critical */
240 if (is_socket) 253 if (is_socket) {
241 printf("connect to file socket %s: %s\n", host_name, strerror(errno)); 254 // printf("connect to file socket %s: %s\n", host_name, strerror(errno));
242 else 255 } else {
243 printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); 256 // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno));
257 }
244 return STATE_CRITICAL; 258 return STATE_CRITICAL;
245 break; 259 break;
246 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */ 260 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
@@ -248,10 +262,11 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) {
248 break; 262 break;
249 } 263 }
250 } else { 264 } else {
251 if (is_socket) 265 if (is_socket) {
252 printf("connect to file socket %s: %s\n", host_name, strerror(errno)); 266 // printf("connect to file socket %s: %s\n", host_name, strerror(errno));
253 else 267 } else {
254 printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); 268 // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno));
269 }
255 return STATE_CRITICAL; 270 return STATE_CRITICAL;
256 } 271 }
257} 272}
@@ -265,7 +280,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer,
265 280
266 send_result = send(sd, send_buffer, strlen(send_buffer), 0); 281 send_result = send(sd, send_buffer, strlen(send_buffer), 0);
267 if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { 282 if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) {
268 printf("%s\n", _("Send failed")); 283 // printf("%s\n", _("Send failed"));
269 result = STATE_WARNING; 284 result = STATE_WARNING;
270 } 285 }
271 286
@@ -280,7 +295,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer,
280 /* make sure some data has arrived */ 295 /* make sure some data has arrived */
281 if (!FD_ISSET(sd, &readfds)) { 296 if (!FD_ISSET(sd, &readfds)) {
282 strcpy(recv_buffer, ""); 297 strcpy(recv_buffer, "");
283 printf("%s\n", _("No data was received from host!")); 298 // printf("%s\n", _("No data was received from host!"));
284 result = STATE_WARNING; 299 result = STATE_WARNING;
285 } 300 }
286 301
@@ -288,11 +303,13 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer,
288 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);
289 if (recv_result == -1) { 304 if (recv_result == -1) {
290 strcpy(recv_buffer, ""); 305 strcpy(recv_buffer, "");
291 if (proto != IPPROTO_TCP) 306 if (proto != IPPROTO_TCP) {
292 printf("%s\n", _("Receive failed")); 307 // printf("%s\n", _("Receive failed"));
308 }
293 result = STATE_WARNING; 309 result = STATE_WARNING;
294 } else 310 } else {
295 recv_buffer[recv_result] = 0; 311 recv_buffer[recv_result] = 0;
312 }
296 313
297 /* die returned string */ 314 /* die returned string */
298 recv_buffer[recv_size - 1] = 0; 315 recv_buffer[recv_size - 1] = 0;
@@ -301,26 +318,30 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer,
301} 318}
302 319
303bool is_host(const char *address) { 320bool is_host(const char *address) {
304 if (is_addr(address) || is_hostname(address)) 321 if (is_addr(address) || is_hostname(address)) {
305 return (true); 322 return (true);
323 }
306 324
307 return (false); 325 return (false);
308} 326}
309 327
310void host_or_die(const char *str) { 328void host_or_die(const char *str) {
311 if (!str || (!is_addr(str) && !is_hostname(str))) 329 if (!str || (!is_addr(str) && !is_hostname(str))) {
312 usage_va(_("Invalid hostname/address - %s"), str); 330 usage_va(_("Invalid hostname/address - %s"), str);
331 }
313} 332}
314 333
315bool is_addr(const char *address) { 334bool is_addr(const char *address) {
316#ifdef USE_IPV6 335#ifdef USE_IPV6
317 if (address_family == AF_INET && is_inet_addr(address)) 336 if (address_family == AF_INET && is_inet_addr(address)) {
318 return true; 337 return true;
319 else if (address_family == AF_INET6 && is_inet6_addr(address)) 338 } else if (address_family == AF_INET6 && is_inet6_addr(address)) {
320 return true; 339 return true;
340 }
321#else 341#else
322 if (is_inet_addr(address)) 342 if (is_inet_addr(address)) {
323 return (true); 343 return (true);
344 }
324#endif 345#endif
325 346
326 return (false); 347 return (false);
@@ -335,11 +356,13 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) {
335 hints.ai_family = family; 356 hints.ai_family = family;
336 357
337 retval = getaddrinfo(in, NULL, &hints, &res); 358 retval = getaddrinfo(in, NULL, &hints, &res);
338 if (retval != 0) 359 if (retval != 0) {
339 return false; 360 return false;
361 }
340 362
341 if (ss != NULL) 363 if (ss != NULL) {
342 memcpy(ss, res->ai_addr, res->ai_addrlen); 364 memcpy(ss, res->ai_addr, res->ai_addrlen);
365 }
343 freeaddrinfo(res); 366 freeaddrinfo(res);
344 return true; 367 return true;
345} 368}