Revert "check_disk - show all disks if state is ok and option error only is used"
[monitoring-plugins.git] / plugins / netutils.c
blob1bb4f07620b6b162eca4a73950a65b177592747b
1 /*****************************************************************************
2 *
3 * Monitoring Plugins network utilities
4 *
5 * License: GPL
6 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
7 * Copyright (c) 2003-2008 Monitoring 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 #include "common.h"
31 #include "netutils.h"
33 unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
34 unsigned int socket_timeout_state = STATE_CRITICAL;
36 int econn_refuse_state = STATE_CRITICAL;
37 int was_refused = FALSE;
38 #if USE_IPV6
39 int address_family = AF_UNSPEC;
40 #else
41 int address_family = AF_INET;
42 #endif
44 /* handles socket timeouts */
45 void
46 socket_timeout_alarm_handler (int sig)
48 if (sig == SIGALRM)
49 printf (_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);
50 else
51 printf (_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);
53 exit (socket_timeout_state);
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 */
60 int
61 process_tcp_request2 (const char *server_address, int server_port,
62 const char *send_buffer, char *recv_buffer, int recv_size)
65 int result;
66 int send_result;
67 int recv_result;
68 int sd;
69 struct timeval tv;
70 fd_set readfds;
71 int recv_length = 0;
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;
83 while (1) {
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;
87 tv.tv_usec = 0;
88 FD_ZERO (&readfds);
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 */
94 if (!recv_length) {
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;
102 break;
104 else { /* it has */
105 recv_result =
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;
112 break;
114 else if (recv_result == 0) {
115 /* end of file ? */
116 recv_buffer[recv_length] = 0;
117 break;
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;
124 break;
128 /* end if(!FD_ISSET(sd,&readfds)) */
130 /* end while(1) */
132 close (sd);
133 return result;
137 /* connects to a host on a specified port, sends a string, and gets a
138 response */
140 process_request (const char *server_address, int server_port, int proto,
141 const char *send_buffer, char *recv_buffer, int recv_size)
143 int result;
144 int sd;
146 result = STATE_OK;
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);
154 close (sd);
156 return result;
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 /* send back STATE_UNKOWN if there's an error
165 send back STATE_OK if we connect
166 send back STATE_CRITICAL if we can't connect.
167 Let upstream figure out what to send to the user. */
168 struct addrinfo hints;
169 struct addrinfo *r, *res;
170 struct sockaddr_un su;
171 char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
172 size_t len;
173 int socktype, result;
174 short is_socket = (host_name[0] == '/');
176 socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
178 /* as long as it doesn't start with a '/', it's assumed a host or ip */
179 if (!is_socket){
180 memset (&hints, 0, sizeof (hints));
181 hints.ai_family = address_family;
182 hints.ai_protocol = proto;
183 hints.ai_socktype = socktype;
185 len = strlen (host_name);
186 /* check for an [IPv6] address (and strip the brackets) */
187 if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') {
188 host_name++;
189 len -= 2;
191 if (len >= sizeof(host))
192 return STATE_UNKNOWN;
193 memcpy (host, host_name, len);
194 host[len] = '\0';
195 snprintf (port_str, sizeof (port_str), "%d", port);
196 result = getaddrinfo (host, port_str, &hints, &res);
198 if (result != 0) {
199 printf ("%s\n", gai_strerror (result));
200 return STATE_UNKNOWN;
203 r = res;
204 while (r) {
205 /* attempt to create a socket */
206 *sd = socket (r->ai_family, socktype, r->ai_protocol);
208 if (*sd < 0) {
209 printf ("%s\n", _("Socket creation failed"));
210 freeaddrinfo (r);
211 return STATE_UNKNOWN;
214 /* attempt to open a connection */
215 result = connect (*sd, r->ai_addr, r->ai_addrlen);
217 if (result == 0) {
218 was_refused = FALSE;
219 break;
222 if (result < 0) {
223 switch (errno) {
224 case ECONNREFUSED:
225 was_refused = TRUE;
226 break;
230 close (*sd);
231 r = r->ai_next;
233 freeaddrinfo (res);
235 /* else the hostname is interpreted as a path to a unix socket */
236 else {
237 if(strlen(host_name) >= UNIX_PATH_MAX){
238 die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
240 memset(&su, 0, sizeof(su));
241 su.sun_family = AF_UNIX;
242 strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
243 *sd = socket(PF_UNIX, SOCK_STREAM, 0);
244 if(*sd < 0){
245 die(STATE_UNKNOWN, _("Socket creation failed"));
247 result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
248 if (result < 0 && errno == ECONNREFUSED)
249 was_refused = TRUE;
252 if (result == 0)
253 return STATE_OK;
254 else if (was_refused) {
255 switch (econn_refuse_state) { /* a user-defined expected outcome */
256 case STATE_OK:
257 case STATE_WARNING: /* user wants WARN or OK on refusal, or... */
258 case STATE_CRITICAL: /* user did not set econn_refuse_state, or wanted critical */
259 if (is_socket)
260 printf("connect to file socket %s: %s\n", host_name, strerror(errno));
261 else
262 printf("connect to address %s and port %d: %s\n",
263 host_name, port, strerror(errno));
264 return STATE_CRITICAL;
265 break;
266 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
267 return STATE_UNKNOWN;
268 break;
271 else {
272 if (is_socket)
273 printf("connect to file socket %s: %s\n", host_name, strerror(errno));
274 else
275 printf("connect to address %s and port %d: %s\n",
276 host_name, port, strerror(errno));
277 return STATE_CRITICAL;
282 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
284 int result = STATE_OK;
285 int send_result;
286 int recv_result;
287 struct timeval tv;
288 fd_set readfds;
290 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
291 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
292 printf ("%s\n", _("Send failed"));
293 result = STATE_WARNING;
296 /* wait up to the number of seconds for socket timeout minus one
297 for data from the host */
298 tv.tv_sec = socket_timeout - 1;
299 tv.tv_usec = 0;
300 FD_ZERO (&readfds);
301 FD_SET (sd, &readfds);
302 select (sd + 1, &readfds, NULL, NULL, &tv);
304 /* make sure some data has arrived */
305 if (!FD_ISSET (sd, &readfds)) {
306 strcpy (recv_buffer, "");
307 printf ("%s\n", _("No data was received from host!"));
308 result = STATE_WARNING;
311 else {
312 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
313 if (recv_result == -1) {
314 strcpy (recv_buffer, "");
315 if (proto != IPPROTO_TCP)
316 printf ("%s\n", _("Receive failed"));
317 result = STATE_WARNING;
319 else
320 recv_buffer[recv_result] = 0;
322 /* die returned string */
323 recv_buffer[recv_size - 1] = 0;
325 return result;
330 is_host (const char *address)
332 if (is_addr (address) || is_hostname (address))
333 return (TRUE);
335 return (FALSE);
338 void
339 host_or_die(const char *str)
341 if(!str || (!is_addr(str) && !is_hostname(str)))
342 usage_va(_("Invalid hostname/address - %s"), str);
346 is_addr (const char *address)
348 #ifdef USE_IPV6
349 if (address_family == AF_INET && is_inet_addr (address))
350 return TRUE;
351 else if (address_family == AF_INET6 && is_inet6_addr (address))
352 return TRUE;
353 #else
354 if (is_inet_addr (address))
355 return (TRUE);
356 #endif
358 return (FALSE);
362 dns_lookup (const char *in, struct sockaddr_storage *ss, int family)
364 struct addrinfo hints;
365 struct addrinfo *res;
366 int retval;
368 memset (&hints, 0, sizeof(struct addrinfo));
369 hints.ai_family = family;
371 retval = getaddrinfo (in, NULL, &hints, &res);
372 if (retval != 0)
373 return FALSE;
375 if (ss != NULL)
376 memcpy (ss, res->ai_addr, res->ai_addrlen);
377 freeaddrinfo (res);
378 return TRUE;