check_procs: Assume we have stat()
[monitoring-plugins.git] / plugins / netutils.c
blob1d6100abf843efd3f25a8209adbdcfa4042d8fd4
1 /*****************************************************************************
2 *
3 * Nagios plugins network utilities
4 *
5 * License: GPL
6 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
7 * Copyright (c) 2003-2008 Nagios Plugins Development Team
8 *
9 * Description:
11 * This file contains commons functions used in many of the plugins.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 *****************************************************************************/
30 #define LOCAL_TIMEOUT_ALARM_HANDLER
32 #include "common.h"
33 #include "netutils.h"
35 int econn_refuse_state = STATE_CRITICAL;
36 int was_refused = FALSE;
37 #if USE_IPV6
38 int address_family = AF_UNSPEC;
39 #else
40 int address_family = AF_INET;
41 #endif
43 /* handles socket timeouts */
44 void
45 socket_timeout_alarm_handler (int sig)
47 if (sig == SIGALRM)
48 printf (_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);
49 else
50 printf (_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);
52 exit (socket_timeout_state);
56 /* connects to a host on a specified tcp port, sends a string, and gets a
57 response. loops on select-recv until timeout or eof to get all of a
58 multi-packet answer */
59 int
60 process_tcp_request2 (const char *server_address, int server_port,
61 const char *send_buffer, char *recv_buffer, int recv_size)
64 int result;
65 int send_result;
66 int recv_result;
67 int sd;
68 struct timeval tv;
69 fd_set readfds;
70 int recv_length = 0;
72 result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP);
73 if (result != STATE_OK)
74 return STATE_CRITICAL;
76 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
77 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
78 printf ("%s\n", _("Send failed"));
79 result = STATE_WARNING;
82 while (1) {
83 /* wait up to the number of seconds for socket timeout
84 minus one for data from the host */
85 tv.tv_sec = socket_timeout - 1;
86 tv.tv_usec = 0;
87 FD_ZERO (&readfds);
88 FD_SET (sd, &readfds);
89 select (sd + 1, &readfds, NULL, NULL, &tv);
91 /* make sure some data has arrived */
92 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
93 if (!recv_length) {
94 strcpy (recv_buffer, "");
95 printf ("%s\n", _("No data was received from host!"));
96 result = STATE_WARNING;
98 else { /* this one failed, but previous ones worked */
99 recv_buffer[recv_length] = 0;
101 break;
103 else { /* it has */
104 recv_result =
105 recv (sd, recv_buffer + recv_length,
106 (size_t)recv_size - recv_length - 1, 0);
107 if (recv_result == -1) {
108 /* recv failed, bail out */
109 strcpy (recv_buffer + recv_length, "");
110 result = STATE_WARNING;
111 break;
113 else if (recv_result == 0) {
114 /* end of file ? */
115 recv_buffer[recv_length] = 0;
116 break;
118 else { /* we got data! */
119 recv_length += recv_result;
120 if (recv_length >= recv_size - 1) {
121 /* buffer full, we're done */
122 recv_buffer[recv_size - 1] = 0;
123 break;
127 /* end if(!FD_ISSET(sd,&readfds)) */
129 /* end while(1) */
131 close (sd);
132 return result;
136 /* connects to a host on a specified port, sends a string, and gets a
137 response */
139 process_request (const char *server_address, int server_port, int proto,
140 const char *send_buffer, char *recv_buffer, int recv_size)
142 int result;
143 int sd;
145 result = STATE_OK;
147 result = np_net_connect (server_address, server_port, &sd, proto);
148 if (result != STATE_OK)
149 return STATE_CRITICAL;
151 result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
153 close (sd);
155 return result;
159 /* opens a tcp or udp connection to a remote host or local socket */
161 np_net_connect (const char *host_name, int port, int *sd, int proto)
163 struct addrinfo hints;
164 struct addrinfo *r, *res;
165 struct sockaddr_un su;
166 char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
167 size_t len;
168 int socktype, result;
170 socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
172 /* as long as it doesn't start with a '/', it's assumed a host or ip */
173 if(host_name[0] != '/'){
174 memset (&hints, 0, sizeof (hints));
175 hints.ai_family = address_family;
176 hints.ai_protocol = proto;
177 hints.ai_socktype = socktype;
179 len = strlen (host_name);
180 /* check for an [IPv6] address (and strip the brackets) */
181 if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') {
182 host_name++;
183 len -= 2;
185 if (len >= sizeof(host))
186 return STATE_UNKNOWN;
187 memcpy (host, host_name, len);
188 host[len] = '\0';
189 snprintf (port_str, sizeof (port_str), "%d", port);
190 result = getaddrinfo (host, port_str, &hints, &res);
192 if (result != 0) {
193 printf ("%s\n", gai_strerror (result));
194 return STATE_UNKNOWN;
197 r = res;
198 while (r) {
199 /* attempt to create a socket */
200 *sd = socket (r->ai_family, socktype, r->ai_protocol);
202 if (*sd < 0) {
203 printf ("%s\n", _("Socket creation failed"));
204 freeaddrinfo (r);
205 return STATE_UNKNOWN;
208 /* attempt to open a connection */
209 result = connect (*sd, r->ai_addr, r->ai_addrlen);
211 if (result == 0) {
212 was_refused = FALSE;
213 break;
216 if (result < 0) {
217 switch (errno) {
218 case ECONNREFUSED:
219 was_refused = TRUE;
220 break;
224 close (*sd);
225 r = r->ai_next;
227 freeaddrinfo (res);
229 /* else the hostname is interpreted as a path to a unix socket */
230 else {
231 if(strlen(host_name) >= UNIX_PATH_MAX){
232 die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
234 memset(&su, 0, sizeof(su));
235 su.sun_family = AF_UNIX;
236 strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
237 *sd = socket(PF_UNIX, SOCK_STREAM, 0);
238 if(*sd < 0){
239 die(STATE_UNKNOWN, _("Socket creation failed"));
241 result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
242 if (result < 0 && errno == ECONNREFUSED)
243 was_refused = TRUE;
246 if (result == 0)
247 return STATE_OK;
248 else if (was_refused) {
249 switch (econn_refuse_state) { /* a user-defined expected outcome */
250 case STATE_OK:
251 case STATE_WARNING: /* user wants WARN or OK on refusal */
252 return econn_refuse_state;
253 break;
254 case STATE_CRITICAL: /* user did not set econn_refuse_state */
255 printf ("%s\n", strerror(errno));
256 return econn_refuse_state;
257 break;
258 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
259 return STATE_UNKNOWN;
260 break;
263 else {
264 printf ("%s\n", strerror(errno));
265 return STATE_CRITICAL;
270 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
272 int result = STATE_OK;
273 int send_result;
274 int recv_result;
275 struct timeval tv;
276 fd_set readfds;
278 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
279 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
280 printf ("%s\n", _("Send failed"));
281 result = STATE_WARNING;
284 /* wait up to the number of seconds for socket timeout minus one
285 for data from the host */
286 tv.tv_sec = socket_timeout - 1;
287 tv.tv_usec = 0;
288 FD_ZERO (&readfds);
289 FD_SET (sd, &readfds);
290 select (sd + 1, &readfds, NULL, NULL, &tv);
292 /* make sure some data has arrived */
293 if (!FD_ISSET (sd, &readfds)) {
294 strcpy (recv_buffer, "");
295 printf ("%s\n", _("No data was received from host!"));
296 result = STATE_WARNING;
299 else {
300 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
301 if (recv_result == -1) {
302 strcpy (recv_buffer, "");
303 if (proto != IPPROTO_TCP)
304 printf ("%s\n", _("Receive failed"));
305 result = STATE_WARNING;
307 else
308 recv_buffer[recv_result] = 0;
310 /* die returned string */
311 recv_buffer[recv_size - 1] = 0;
313 return result;
318 is_host (const char *address)
320 if (is_addr (address) || is_hostname (address))
321 return (TRUE);
323 return (FALSE);
326 void
327 host_or_die(const char *str)
329 if(!str || (!is_addr(str) && !is_hostname(str)))
330 usage_va(_("Invalid hostname/address - %s"), str);
334 is_addr (const char *address)
336 #ifdef USE_IPV6
337 if (address_family == AF_INET && is_inet_addr (address))
338 return TRUE;
339 else if (address_family == AF_INET6 && is_inet6_addr (address))
340 return TRUE;
341 #else
342 if (is_inet_addr (address))
343 return (TRUE);
344 #endif
346 return (FALSE);
350 resolve_host_or_addr (const char *address, int family)
352 struct addrinfo hints;
353 struct addrinfo *res;
354 int retval;
356 memset (&hints, 0, sizeof (hints));
357 hints.ai_family = family;
358 retval = getaddrinfo (address, NULL, &hints, &res);
360 if (retval != 0)
361 return FALSE;
362 else {
363 freeaddrinfo (res);
364 return TRUE;