diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-02-25 13:04:43 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-07 23:38:50 +0100 |
commit | 53247ae748174ddb639bb5fd9a19c8a6ad121717 (patch) | |
tree | 0b5ca3ceab794e5105e31633483bb7023469a433 /plugins | |
parent | 1ba8110da71abdccc9ee7f35cd0abefe78b8555b (diff) | |
download | monitoring-plugins-53247ae748174ddb639bb5fd9a19c8a6ad121717.tar.gz |
netutils.c: clang-format + do NOT print here
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/netutils.c | 79 |
1 files changed, 47 insertions, 32 deletions
diff --git a/plugins/netutils.c b/plugins/netutils.c index ee81912a..f5df7f3d 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c | |||
@@ -43,10 +43,11 @@ int address_family = AF_INET; | |||
43 | 43 | ||
44 | /* handles socket timeouts */ | 44 | /* handles socket timeouts */ |
45 | void socket_timeout_alarm_handler(int sig) { | 45 | void socket_timeout_alarm_handler(int sig) { |
46 | if (sig == SIGALRM) | 46 | if (sig == SIGALRM) { |
47 | printf(_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); | 47 | printf(_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); |
48 | else | 48 | } else { |
49 | printf(_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); | 49 | printf(_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); |
50 | } | ||
50 | 51 | ||
51 | exit(socket_timeout_state); | 52 | exit(socket_timeout_state); |
52 | } | 53 | } |
@@ -65,12 +66,13 @@ int process_tcp_request2(const char *server_address, int server_port, const char | |||
65 | int recv_length = 0; | 66 | int recv_length = 0; |
66 | 67 | ||
67 | result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); | 68 | result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); |
68 | if (result != STATE_OK) | 69 | if (result != STATE_OK) { |
69 | return STATE_CRITICAL; | 70 | return STATE_CRITICAL; |
71 | } | ||
70 | 72 | ||
71 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); | 73 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); |
72 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { | 74 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { |
73 | printf("%s\n", _("Send failed")); | 75 | // printf("%s\n", _("Send failed")); |
74 | result = STATE_WARNING; | 76 | result = STATE_WARNING; |
75 | } | 77 | } |
76 | 78 | ||
@@ -87,7 +89,7 @@ int process_tcp_request2(const char *server_address, int server_port, const char | |||
87 | if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ | 89 | if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ |
88 | if (!recv_length) { | 90 | if (!recv_length) { |
89 | strcpy(recv_buffer, ""); | 91 | strcpy(recv_buffer, ""); |
90 | printf("%s\n", _("No data was received from host!")); | 92 | // printf("%s\n", _("No data was received from host!")); |
91 | result = STATE_WARNING; | 93 | result = STATE_WARNING; |
92 | } else { /* this one failed, but previous ones worked */ | 94 | } else { /* this one failed, but previous ones worked */ |
93 | recv_buffer[recv_length] = 0; | 95 | recv_buffer[recv_length] = 0; |
@@ -130,8 +132,9 @@ int process_request(const char *server_address, int server_port, int proto, cons | |||
130 | result = STATE_OK; | 132 | result = STATE_OK; |
131 | 133 | ||
132 | result = np_net_connect(server_address, server_port, &sd, proto); | 134 | result = np_net_connect(server_address, server_port, &sd, proto); |
133 | if (result != STATE_OK) | 135 | if (result != STATE_OK) { |
134 | return STATE_CRITICAL; | 136 | return STATE_CRITICAL; |
137 | } | ||
135 | 138 | ||
136 | result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); | 139 | result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); |
137 | 140 | ||
@@ -169,15 +172,16 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
169 | host_name++; | 172 | host_name++; |
170 | len -= 2; | 173 | len -= 2; |
171 | } | 174 | } |
172 | if (len >= sizeof(host)) | 175 | if (len >= sizeof(host)) { |
173 | return STATE_UNKNOWN; | 176 | return STATE_UNKNOWN; |
177 | } | ||
174 | memcpy(host, host_name, len); | 178 | memcpy(host, host_name, len); |
175 | host[len] = '\0'; | 179 | host[len] = '\0'; |
176 | snprintf(port_str, sizeof(port_str), "%d", port); | 180 | snprintf(port_str, sizeof(port_str), "%d", port); |
177 | result = getaddrinfo(host, port_str, &hints, &res); | 181 | result = getaddrinfo(host, port_str, &hints, &res); |
178 | 182 | ||
179 | if (result != 0) { | 183 | if (result != 0) { |
180 | printf("%s\n", gai_strerror(result)); | 184 | // printf("%s\n", gai_strerror(result)); |
181 | return STATE_UNKNOWN; | 185 | return STATE_UNKNOWN; |
182 | } | 186 | } |
183 | 187 | ||
@@ -187,7 +191,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); | 191 | *sd = socket(r->ai_family, socktype, r->ai_protocol); |
188 | 192 | ||
189 | if (*sd < 0) { | 193 | if (*sd < 0) { |
190 | printf("%s\n", _("Socket creation failed")); | 194 | // printf("%s\n", _("Socket creation failed")); |
191 | freeaddrinfo(r); | 195 | freeaddrinfo(r); |
192 | return STATE_UNKNOWN; | 196 | return STATE_UNKNOWN; |
193 | } | 197 | } |
@@ -226,21 +230,23 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
226 | die(STATE_UNKNOWN, _("Socket creation failed")); | 230 | die(STATE_UNKNOWN, _("Socket creation failed")); |
227 | } | 231 | } |
228 | result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); | 232 | result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); |
229 | if (result < 0 && errno == ECONNREFUSED) | 233 | if (result < 0 && errno == ECONNREFUSED) { |
230 | was_refused = true; | 234 | was_refused = true; |
235 | } | ||
231 | } | 236 | } |
232 | 237 | ||
233 | if (result == 0) | 238 | if (result == 0) { |
234 | return STATE_OK; | 239 | return STATE_OK; |
235 | else if (was_refused) { | 240 | } else if (was_refused) { |
236 | switch (econn_refuse_state) { /* a user-defined expected outcome */ | 241 | switch (econn_refuse_state) { /* a user-defined expected outcome */ |
237 | case STATE_OK: | 242 | case STATE_OK: |
238 | case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ | 243 | 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 */ | 244 | case STATE_CRITICAL: /* user did not set econn_refuse_state, or wanted critical */ |
240 | if (is_socket) | 245 | if (is_socket) { |
241 | printf("connect to file socket %s: %s\n", host_name, strerror(errno)); | 246 | // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); |
242 | else | 247 | } else { |
243 | printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); | 248 | // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); |
249 | } | ||
244 | return STATE_CRITICAL; | 250 | return STATE_CRITICAL; |
245 | break; | 251 | break; |
246 | default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */ | 252 | default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */ |
@@ -248,10 +254,11 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
248 | break; | 254 | break; |
249 | } | 255 | } |
250 | } else { | 256 | } else { |
251 | if (is_socket) | 257 | if (is_socket) { |
252 | printf("connect to file socket %s: %s\n", host_name, strerror(errno)); | 258 | // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); |
253 | else | 259 | } else { |
254 | printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); | 260 | // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); |
261 | } | ||
255 | return STATE_CRITICAL; | 262 | return STATE_CRITICAL; |
256 | } | 263 | } |
257 | } | 264 | } |
@@ -265,7 +272,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
265 | 272 | ||
266 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); | 273 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); |
267 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { | 274 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { |
268 | printf("%s\n", _("Send failed")); | 275 | // printf("%s\n", _("Send failed")); |
269 | result = STATE_WARNING; | 276 | result = STATE_WARNING; |
270 | } | 277 | } |
271 | 278 | ||
@@ -280,7 +287,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
280 | /* make sure some data has arrived */ | 287 | /* make sure some data has arrived */ |
281 | if (!FD_ISSET(sd, &readfds)) { | 288 | if (!FD_ISSET(sd, &readfds)) { |
282 | strcpy(recv_buffer, ""); | 289 | strcpy(recv_buffer, ""); |
283 | printf("%s\n", _("No data was received from host!")); | 290 | // printf("%s\n", _("No data was received from host!")); |
284 | result = STATE_WARNING; | 291 | result = STATE_WARNING; |
285 | } | 292 | } |
286 | 293 | ||
@@ -288,11 +295,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); | 295 | recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); |
289 | if (recv_result == -1) { | 296 | if (recv_result == -1) { |
290 | strcpy(recv_buffer, ""); | 297 | strcpy(recv_buffer, ""); |
291 | if (proto != IPPROTO_TCP) | 298 | if (proto != IPPROTO_TCP) { |
292 | printf("%s\n", _("Receive failed")); | 299 | // printf("%s\n", _("Receive failed")); |
300 | } | ||
293 | result = STATE_WARNING; | 301 | result = STATE_WARNING; |
294 | } else | 302 | } else { |
295 | recv_buffer[recv_result] = 0; | 303 | recv_buffer[recv_result] = 0; |
304 | } | ||
296 | 305 | ||
297 | /* die returned string */ | 306 | /* die returned string */ |
298 | recv_buffer[recv_size - 1] = 0; | 307 | recv_buffer[recv_size - 1] = 0; |
@@ -301,26 +310,30 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
301 | } | 310 | } |
302 | 311 | ||
303 | bool is_host(const char *address) { | 312 | bool is_host(const char *address) { |
304 | if (is_addr(address) || is_hostname(address)) | 313 | if (is_addr(address) || is_hostname(address)) { |
305 | return (true); | 314 | return (true); |
315 | } | ||
306 | 316 | ||
307 | return (false); | 317 | return (false); |
308 | } | 318 | } |
309 | 319 | ||
310 | void host_or_die(const char *str) { | 320 | void host_or_die(const char *str) { |
311 | if (!str || (!is_addr(str) && !is_hostname(str))) | 321 | if (!str || (!is_addr(str) && !is_hostname(str))) { |
312 | usage_va(_("Invalid hostname/address - %s"), str); | 322 | usage_va(_("Invalid hostname/address - %s"), str); |
323 | } | ||
313 | } | 324 | } |
314 | 325 | ||
315 | bool is_addr(const char *address) { | 326 | bool is_addr(const char *address) { |
316 | #ifdef USE_IPV6 | 327 | #ifdef USE_IPV6 |
317 | if (address_family == AF_INET && is_inet_addr(address)) | 328 | if (address_family == AF_INET && is_inet_addr(address)) { |
318 | return true; | 329 | return true; |
319 | else if (address_family == AF_INET6 && is_inet6_addr(address)) | 330 | } else if (address_family == AF_INET6 && is_inet6_addr(address)) { |
320 | return true; | 331 | return true; |
332 | } | ||
321 | #else | 333 | #else |
322 | if (is_inet_addr(address)) | 334 | if (is_inet_addr(address)) { |
323 | return (true); | 335 | return (true); |
336 | } | ||
324 | #endif | 337 | #endif |
325 | 338 | ||
326 | return (false); | 339 | return (false); |
@@ -335,11 +348,13 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { | |||
335 | hints.ai_family = family; | 348 | hints.ai_family = family; |
336 | 349 | ||
337 | retval = getaddrinfo(in, NULL, &hints, &res); | 350 | retval = getaddrinfo(in, NULL, &hints, &res); |
338 | if (retval != 0) | 351 | if (retval != 0) { |
339 | return false; | 352 | return false; |
353 | } | ||
340 | 354 | ||
341 | if (ss != NULL) | 355 | if (ss != NULL) { |
342 | memcpy(ss, res->ai_addr, res->ai_addrlen); | 356 | memcpy(ss, res->ai_addr, res->ai_addrlen); |
357 | } | ||
343 | freeaddrinfo(res); | 358 | freeaddrinfo(res); |
344 | return true; | 359 | return true; |
345 | } | 360 | } |