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
;
172 int socktype
, result
;
174 socktype
= (proto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
;
176 /* as long as it doesn't start with a '/', it's assumed a host or ip */
177 if(host_name
[0] != '/'){
178 memset (&hints
, 0, sizeof (hints
));
179 hints
.ai_family
= address_family
;
180 hints
.ai_protocol
= proto
;
181 hints
.ai_socktype
= socktype
;
183 snprintf (port_str
, sizeof (port_str
), "%d", port
);
184 result
= getaddrinfo (host_name
, port_str
, &hints
, &res
);
187 printf ("%s\n", gai_strerror (result
));
188 return STATE_UNKNOWN
;
193 /* attempt to create a socket */
194 *sd
= socket (r
->ai_family
, socktype
, r
->ai_protocol
);
197 printf ("%s\n", _("Socket creation failed"));
199 return STATE_UNKNOWN
;
202 /* attempt to open a connection */
203 result
= connect (*sd
, r
->ai_addr
, r
->ai_addrlen
);
223 /* else the hostname is interpreted as a path to a unix socket */
225 if(strlen(host_name
) >= UNIX_PATH_MAX
){
226 die(STATE_UNKNOWN
, _("Supplied path too long unix domain socket"));
228 memset(&su
, 0, sizeof(su
));
229 su
.sun_family
= AF_UNIX
;
230 strncpy(su
.sun_path
, host_name
, UNIX_PATH_MAX
);
231 *sd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
233 die(STATE_UNKNOWN
, _("Socket creation failed"));
235 result
= connect(*sd
, (struct sockaddr
*)&su
, sizeof(su
));
236 if (result
< 0 && errno
== ECONNREFUSED
)
242 else if (was_refused
) {
243 switch (econn_refuse_state
) { /* a user-defined expected outcome */
245 case STATE_WARNING
: /* user wants WARN or OK on refusal */
246 return econn_refuse_state
;
248 case STATE_CRITICAL
: /* user did not set econn_refuse_state */
249 printf ("%s\n", strerror(errno
));
250 return econn_refuse_state
;
252 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
253 return STATE_UNKNOWN
;
258 printf ("%s\n", strerror(errno
));
259 return STATE_CRITICAL
;
264 send_request (int sd
, int proto
, const char *send_buffer
, char *recv_buffer
, int recv_size
)
266 int result
= STATE_OK
;
272 send_result
= send (sd
, send_buffer
, strlen (send_buffer
), 0);
273 if (send_result
<0 || (size_t)send_result
!=strlen(send_buffer
)) {
274 printf ("%s\n", _("Send failed"));
275 result
= STATE_WARNING
;
278 /* wait up to the number of seconds for socket timeout minus one
279 for data from the host */
280 tv
.tv_sec
= socket_timeout
- 1;
283 FD_SET (sd
, &readfds
);
284 select (sd
+ 1, &readfds
, NULL
, NULL
, &tv
);
286 /* make sure some data has arrived */
287 if (!FD_ISSET (sd
, &readfds
)) {
288 strcpy (recv_buffer
, "");
289 printf ("%s\n", _("No data was received from host!"));
290 result
= STATE_WARNING
;
294 recv_result
= recv (sd
, recv_buffer
, (size_t)recv_size
- 1, 0);
295 if (recv_result
== -1) {
296 strcpy (recv_buffer
, "");
297 if (proto
!= IPPROTO_TCP
)
298 printf ("%s\n", _("Receive failed"));
299 result
= STATE_WARNING
;
302 recv_buffer
[recv_result
] = 0;
304 /* die returned string */
305 recv_buffer
[recv_size
- 1] = 0;
312 is_host (const char *address
)
314 if (is_addr (address
) || is_hostname (address
))
321 host_or_die(const char *str
)
323 if(!str
|| (!is_addr(str
) && !is_hostname(str
)))
324 usage_va(_("Invalid hostname/address - %s"), str
);
328 is_addr (const char *address
)
331 if (address_family
== AF_INET
&& is_inet_addr (address
))
333 else if (address_family
== AF_INET6
&& is_inet6_addr (address
))
336 if (is_inet_addr (address
))
344 resolve_host_or_addr (const char *address
, int family
)
346 struct addrinfo hints
;
347 struct addrinfo
*res
;
350 memset (&hints
, 0, sizeof (hints
));
351 hints
.ai_family
= family
;
352 retval
= getaddrinfo (address
, NULL
, &hints
, &res
);