1 /****************************************************************************
3 * Nagios plugins network utilities
6 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8 * Last Modified: $Date$
12 * This file contains commons functions used in many of the plugins.
14 * License Information:
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 ****************************************************************************/
34 #define LOCAL_TIMEOUT_ALARM_HANDLER
39 unsigned int socket_timeout
= DEFAULT_SOCKET_TIMEOUT
;
40 int econn_refuse_state
= STATE_CRITICAL
;
41 int was_refused
= FALSE
;
43 int address_family
= AF_UNSPEC
;
45 int address_family
= AF_INET
;
48 /* handles socket timeouts */
50 socket_timeout_alarm_handler (int sig
)
53 printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout
);
55 printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout
);
57 exit (STATE_CRITICAL
);
61 /* connects to a host on a specified tcp port, sends a string, and gets a
62 response. loops on select-recv until timeout or eof to get all of a
63 multi-packet answer */
65 process_tcp_request2 (const char *server_address
, int server_port
,
66 const char *send_buffer
, char *recv_buffer
, int recv_size
)
77 result
= np_net_connect (server_address
, server_port
, &sd
, IPPROTO_TCP
);
78 if (result
!= STATE_OK
)
79 return STATE_CRITICAL
;
81 send_result
= send (sd
, send_buffer
, strlen (send_buffer
), 0);
82 if (send_result
<0 || (size_t)send_result
!=strlen(send_buffer
)) {
83 printf ("%s\n", _("Send failed"));
84 result
= STATE_WARNING
;
88 /* wait up to the number of seconds for socket timeout
89 minus one for data from the host */
90 tv
.tv_sec
= socket_timeout
- 1;
93 FD_SET (sd
, &readfds
);
94 select (sd
+ 1, &readfds
, NULL
, NULL
, &tv
);
96 /* make sure some data has arrived */
97 if (!FD_ISSET (sd
, &readfds
)) { /* it hasn't */
99 strcpy (recv_buffer
, "");
100 printf ("%s\n", _("No data was received from host!"));
101 result
= STATE_WARNING
;
103 else { /* this one failed, but previous ones worked */
104 recv_buffer
[recv_length
] = 0;
110 recv (sd
, recv_buffer
+ recv_length
,
111 (size_t)recv_size
- recv_length
- 1, 0);
112 if (recv_result
== -1) {
113 /* recv failed, bail out */
114 strcpy (recv_buffer
+ recv_length
, "");
115 result
= STATE_WARNING
;
118 else if (recv_result
== 0) {
120 recv_buffer
[recv_length
] = 0;
123 else { /* we got data! */
124 recv_length
+= recv_result
;
125 if (recv_length
>= recv_size
- 1) {
126 /* buffer full, we're done */
127 recv_buffer
[recv_size
- 1] = 0;
132 /* end if(!FD_ISSET(sd,&readfds)) */
141 /* connects to a host on a specified port, sends a string, and gets a
144 process_request (const char *server_address
, int server_port
, int proto
,
145 const char *send_buffer
, char *recv_buffer
, int recv_size
)
152 result
= np_net_connect (server_address
, server_port
, &sd
, proto
);
153 if (result
!= STATE_OK
)
154 return STATE_CRITICAL
;
156 result
= send_request (sd
, proto
, send_buffer
, recv_buffer
, recv_size
);
164 /* opens a tcp or udp connection to a remote host or local socket */
166 np_net_connect (const char *host_name
, int port
, int *sd
, int proto
)
168 struct addrinfo hints
;
169 struct addrinfo
*r
, *res
;
170 struct sockaddr_un su
;
171 char port_str
[6], host
[MAX_HOST_ADDRESS_LENGTH
];
173 int socktype
, result
;
175 socktype
= (proto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
;
177 /* as long as it doesn't start with a '/', it's assumed a host or ip */
178 if(host_name
[0] != '/'){
179 memset (&hints
, 0, sizeof (hints
));
180 hints
.ai_family
= address_family
;
181 hints
.ai_protocol
= proto
;
182 hints
.ai_socktype
= socktype
;
184 len
= strlen (host_name
);
185 /* check for an [IPv6] address (and strip the brackets) */
186 if (len
>= 2 && host_name
[0] == '[' && host_name
[len
- 1] == ']') {
190 if (len
>= sizeof(host
))
191 return STATE_UNKNOWN
;
192 memcpy (host
, host_name
, len
);
194 snprintf (port_str
, sizeof (port_str
), "%d", port
);
195 result
= getaddrinfo (host
, port_str
, &hints
, &res
);
198 printf ("%s\n", gai_strerror (result
));
199 return STATE_UNKNOWN
;
204 /* attempt to create a socket */
205 *sd
= socket (r
->ai_family
, socktype
, r
->ai_protocol
);
208 printf ("%s\n", _("Socket creation failed"));
210 return STATE_UNKNOWN
;
213 /* attempt to open a connection */
214 result
= connect (*sd
, r
->ai_addr
, r
->ai_addrlen
);
234 /* else the hostname is interpreted as a path to a unix socket */
236 if(strlen(host_name
) >= UNIX_PATH_MAX
){
237 die(STATE_UNKNOWN
, _("Supplied path too long unix domain socket"));
239 memset(&su
, 0, sizeof(su
));
240 su
.sun_family
= AF_UNIX
;
241 strncpy(su
.sun_path
, host_name
, UNIX_PATH_MAX
);
242 *sd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
244 die(STATE_UNKNOWN
, _("Socket creation failed"));
246 result
= connect(*sd
, (struct sockaddr
*)&su
, sizeof(su
));
247 if (result
< 0 && errno
== ECONNREFUSED
)
253 else if (was_refused
) {
254 switch (econn_refuse_state
) { /* a user-defined expected outcome */
256 case STATE_WARNING
: /* user wants WARN or OK on refusal */
257 return econn_refuse_state
;
259 case STATE_CRITICAL
: /* user did not set econn_refuse_state */
260 printf ("%s\n", strerror(errno
));
261 return econn_refuse_state
;
263 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
264 return STATE_UNKNOWN
;
269 printf ("%s\n", strerror(errno
));
270 return STATE_CRITICAL
;
275 send_request (int sd
, int proto
, const char *send_buffer
, char *recv_buffer
, int recv_size
)
277 int result
= STATE_OK
;
283 send_result
= send (sd
, send_buffer
, strlen (send_buffer
), 0);
284 if (send_result
<0 || (size_t)send_result
!=strlen(send_buffer
)) {
285 printf ("%s\n", _("Send failed"));
286 result
= STATE_WARNING
;
289 /* wait up to the number of seconds for socket timeout minus one
290 for data from the host */
291 tv
.tv_sec
= socket_timeout
- 1;
294 FD_SET (sd
, &readfds
);
295 select (sd
+ 1, &readfds
, NULL
, NULL
, &tv
);
297 /* make sure some data has arrived */
298 if (!FD_ISSET (sd
, &readfds
)) {
299 strcpy (recv_buffer
, "");
300 printf ("%s\n", _("No data was received from host!"));
301 result
= STATE_WARNING
;
305 recv_result
= recv (sd
, recv_buffer
, (size_t)recv_size
- 1, 0);
306 if (recv_result
== -1) {
307 strcpy (recv_buffer
, "");
308 if (proto
!= IPPROTO_TCP
)
309 printf ("%s\n", _("Receive failed"));
310 result
= STATE_WARNING
;
313 recv_buffer
[recv_result
] = 0;
315 /* die returned string */
316 recv_buffer
[recv_size
- 1] = 0;
323 is_host (const char *address
)
325 if (is_addr (address
) || is_hostname (address
))
332 host_or_die(const char *str
)
334 if(!str
|| (!is_addr(str
) && !is_hostname(str
)))
335 usage_va(_("Invalid hostname/address - %s"), str
);
339 is_addr (const char *address
)
342 if (address_family
== AF_INET
&& is_inet_addr (address
))
344 else if (address_family
== AF_INET6
&& is_inet6_addr (address
))
347 if (is_inet_addr (address
))
355 resolve_host_or_addr (const char *address
, int family
)
357 struct addrinfo hints
;
358 struct addrinfo
*res
;
361 memset (&hints
, 0, sizeof (hints
));
362 hints
.ai_family
= family
;
363 retval
= getaddrinfo (address
, NULL
, &hints
, &res
);