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 int econn_refuse_state
= STATE_CRITICAL
;
36 int was_refused
= FALSE
;
38 int address_family
= AF_UNSPEC
;
40 int address_family
= AF_INET
;
43 /* handles socket timeouts */
45 socket_timeout_alarm_handler (int sig
)
48 printf (_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state
), socket_timeout
);
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 */
60 process_tcp_request2 (const char *server_address
, int server_port
,
61 const char *send_buffer
, char *recv_buffer
, int recv_size
)
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
;
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;
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 */
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;
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
;
113 else if (recv_result
== 0) {
115 recv_buffer
[recv_length
] = 0;
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;
127 /* end if(!FD_ISSET(sd,&readfds)) */
136 /* connects to a host on a specified port, sends a string, and gets a
139 process_request (const char *server_address
, int server_port
, int proto
,
140 const char *send_buffer
, char *recv_buffer
, int recv_size
)
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
);
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
];
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] == ']') {
185 if (len
>= sizeof(host
))
186 return STATE_UNKNOWN
;
187 memcpy (host
, host_name
, len
);
189 snprintf (port_str
, sizeof (port_str
), "%d", port
);
190 result
= getaddrinfo (host
, port_str
, &hints
, &res
);
193 printf ("%s\n", gai_strerror (result
));
194 return STATE_UNKNOWN
;
199 /* attempt to create a socket */
200 *sd
= socket (r
->ai_family
, socktype
, r
->ai_protocol
);
203 printf ("%s\n", _("Socket creation failed"));
205 return STATE_UNKNOWN
;
208 /* attempt to open a connection */
209 result
= connect (*sd
, r
->ai_addr
, r
->ai_addrlen
);
229 /* else the hostname is interpreted as a path to a unix socket */
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);
239 die(STATE_UNKNOWN
, _("Socket creation failed"));
241 result
= connect(*sd
, (struct sockaddr
*)&su
, sizeof(su
));
242 if (result
< 0 && errno
== ECONNREFUSED
)
248 else if (was_refused
) {
249 switch (econn_refuse_state
) { /* a user-defined expected outcome */
251 case STATE_WARNING
: /* user wants WARN or OK on refusal */
252 return econn_refuse_state
;
254 case STATE_CRITICAL
: /* user did not set econn_refuse_state */
255 printf ("%s\n", strerror(errno
));
256 return econn_refuse_state
;
258 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
259 return STATE_UNKNOWN
;
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
;
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;
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
;
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
;
308 recv_buffer
[recv_result
] = 0;
310 /* die returned string */
311 recv_buffer
[recv_size
- 1] = 0;
318 is_host (const char *address
)
320 if (is_addr (address
) || is_hostname (address
))
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
)
337 if (address_family
== AF_INET
&& is_inet_addr (address
))
339 else if (address_family
== AF_INET6
&& is_inet6_addr (address
))
342 if (is_inet_addr (address
))
350 resolve_host_or_addr (const char *address
, int family
)
352 struct addrinfo hints
;
353 struct addrinfo
*res
;
356 memset (&hints
, 0, sizeof (hints
));
357 hints
.ai_family
= family
;
358 retval
= getaddrinfo (address
, NULL
, &hints
, &res
);