1 /*****************************************************************************
3 * Nagios plugins network utilities
6 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
7 * Copyright (c) 2003-2008 Nagios Plugins Development Team
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
35 unsigned int socket_timeout
= DEFAULT_SOCKET_TIMEOUT
;
36 int econn_refuse_state
= STATE_CRITICAL
;
37 int was_refused
= FALSE
;
39 int address_family
= AF_UNSPEC
;
41 int address_family
= AF_INET
;
44 /* handles socket timeouts */
46 socket_timeout_alarm_handler (int sig
)
49 printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout
);
51 printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout
);
53 exit (STATE_CRITICAL
);
57 /* connects to a host on a specified tcp port, sends a string, and gets a
58 response. loops on select-recv until timeout or eof to get all of a
59 multi-packet answer */
61 process_tcp_request2 (const char *server_address
, int server_port
,
62 const char *send_buffer
, char *recv_buffer
, int recv_size
)
73 result
= np_net_connect (server_address
, server_port
, &sd
, IPPROTO_TCP
);
74 if (result
!= STATE_OK
)
75 return STATE_CRITICAL
;
77 send_result
= send (sd
, send_buffer
, strlen (send_buffer
), 0);
78 if (send_result
<0 || (size_t)send_result
!=strlen(send_buffer
)) {
79 printf ("%s\n", _("Send failed"));
80 result
= STATE_WARNING
;
84 /* wait up to the number of seconds for socket timeout
85 minus one for data from the host */
86 tv
.tv_sec
= socket_timeout
- 1;
89 FD_SET (sd
, &readfds
);
90 select (sd
+ 1, &readfds
, NULL
, NULL
, &tv
);
92 /* make sure some data has arrived */
93 if (!FD_ISSET (sd
, &readfds
)) { /* it hasn't */
95 strcpy (recv_buffer
, "");
96 printf ("%s\n", _("No data was received from host!"));
97 result
= STATE_WARNING
;
99 else { /* this one failed, but previous ones worked */
100 recv_buffer
[recv_length
] = 0;
106 recv (sd
, recv_buffer
+ recv_length
,
107 (size_t)recv_size
- recv_length
- 1, 0);
108 if (recv_result
== -1) {
109 /* recv failed, bail out */
110 strcpy (recv_buffer
+ recv_length
, "");
111 result
= STATE_WARNING
;
114 else if (recv_result
== 0) {
116 recv_buffer
[recv_length
] = 0;
119 else { /* we got data! */
120 recv_length
+= recv_result
;
121 if (recv_length
>= recv_size
- 1) {
122 /* buffer full, we're done */
123 recv_buffer
[recv_size
- 1] = 0;
128 /* end if(!FD_ISSET(sd,&readfds)) */
137 /* connects to a host on a specified port, sends a string, and gets a
140 process_request (const char *server_address
, int server_port
, int proto
,
141 const char *send_buffer
, char *recv_buffer
, int recv_size
)
148 result
= np_net_connect (server_address
, server_port
, &sd
, proto
);
149 if (result
!= STATE_OK
)
150 return STATE_CRITICAL
;
152 result
= send_request (sd
, proto
, send_buffer
, recv_buffer
, recv_size
);
160 /* opens a tcp or udp connection to a remote host or local socket */
162 np_net_connect (const char *host_name
, int port
, int *sd
, int proto
)
164 struct addrinfo hints
;
165 struct addrinfo
*r
, *res
;
166 struct sockaddr_un su
;
167 char port_str
[6], host
[MAX_HOST_ADDRESS_LENGTH
];
169 int socktype
, result
;
171 socktype
= (proto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
;
173 /* as long as it doesn't start with a '/', it's assumed a host or ip */
174 if(host_name
[0] != '/'){
175 memset (&hints
, 0, sizeof (hints
));
176 hints
.ai_family
= address_family
;
177 hints
.ai_protocol
= proto
;
178 hints
.ai_socktype
= socktype
;
180 len
= strlen (host_name
);
181 /* check for an [IPv6] address (and strip the brackets) */
182 if (len
>= 2 && host_name
[0] == '[' && host_name
[len
- 1] == ']') {
186 if (len
>= sizeof(host
))
187 return STATE_UNKNOWN
;
188 memcpy (host
, host_name
, len
);
190 snprintf (port_str
, sizeof (port_str
), "%d", port
);
191 result
= getaddrinfo (host
, port_str
, &hints
, &res
);
194 printf ("%s\n", gai_strerror (result
));
195 return STATE_UNKNOWN
;
200 /* attempt to create a socket */
201 *sd
= socket (r
->ai_family
, socktype
, r
->ai_protocol
);
204 printf ("%s\n", _("Socket creation failed"));
206 return STATE_UNKNOWN
;
209 /* attempt to open a connection */
210 result
= connect (*sd
, r
->ai_addr
, r
->ai_addrlen
);
230 /* else the hostname is interpreted as a path to a unix socket */
232 if(strlen(host_name
) >= UNIX_PATH_MAX
){
233 die(STATE_UNKNOWN
, _("Supplied path too long unix domain socket"));
235 memset(&su
, 0, sizeof(su
));
236 su
.sun_family
= AF_UNIX
;
237 strncpy(su
.sun_path
, host_name
, UNIX_PATH_MAX
);
238 *sd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
240 die(STATE_UNKNOWN
, _("Socket creation failed"));
242 result
= connect(*sd
, (struct sockaddr
*)&su
, sizeof(su
));
243 if (result
< 0 && errno
== ECONNREFUSED
)
249 else if (was_refused
) {
250 switch (econn_refuse_state
) { /* a user-defined expected outcome */
252 case STATE_WARNING
: /* user wants WARN or OK on refusal */
253 return econn_refuse_state
;
255 case STATE_CRITICAL
: /* user did not set econn_refuse_state */
256 printf ("%s\n", strerror(errno
));
257 return econn_refuse_state
;
259 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
260 return STATE_UNKNOWN
;
265 printf ("%s\n", strerror(errno
));
266 return STATE_CRITICAL
;
271 send_request (int sd
, int proto
, const char *send_buffer
, char *recv_buffer
, int recv_size
)
273 int result
= STATE_OK
;
279 send_result
= send (sd
, send_buffer
, strlen (send_buffer
), 0);
280 if (send_result
<0 || (size_t)send_result
!=strlen(send_buffer
)) {
281 printf ("%s\n", _("Send failed"));
282 result
= STATE_WARNING
;
285 /* wait up to the number of seconds for socket timeout minus one
286 for data from the host */
287 tv
.tv_sec
= socket_timeout
- 1;
290 FD_SET (sd
, &readfds
);
291 select (sd
+ 1, &readfds
, NULL
, NULL
, &tv
);
293 /* make sure some data has arrived */
294 if (!FD_ISSET (sd
, &readfds
)) {
295 strcpy (recv_buffer
, "");
296 printf ("%s\n", _("No data was received from host!"));
297 result
= STATE_WARNING
;
301 recv_result
= recv (sd
, recv_buffer
, (size_t)recv_size
- 1, 0);
302 if (recv_result
== -1) {
303 strcpy (recv_buffer
, "");
304 if (proto
!= IPPROTO_TCP
)
305 printf ("%s\n", _("Receive failed"));
306 result
= STATE_WARNING
;
309 recv_buffer
[recv_result
] = 0;
311 /* die returned string */
312 recv_buffer
[recv_size
- 1] = 0;
319 is_host (const char *address
)
321 if (is_addr (address
) || is_hostname (address
))
328 host_or_die(const char *str
)
330 if(!str
|| (!is_addr(str
) && !is_hostname(str
)))
331 usage_va(_("Invalid hostname/address - %s"), str
);
335 is_addr (const char *address
)
338 if (address_family
== AF_INET
&& is_inet_addr (address
))
340 else if (address_family
== AF_INET6
&& is_inet6_addr (address
))
343 if (is_inet_addr (address
))
351 resolve_host_or_addr (const char *address
, int family
)
353 struct addrinfo hints
;
354 struct addrinfo
*res
;
357 memset (&hints
, 0, sizeof (hints
));
358 hints
.ai_family
= family
;
359 retval
= getaddrinfo (address
, NULL
, &hints
, &res
);