root/src/modules/ping_icmp.c

Revision b9a4230bc7f65c1268a1569d08ffc92d1074bc68, 17.4 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 9 years ago)

first whack at using fcntl everywhere. Heads up, a lot could break. resf #167.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <netdb.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #ifdef HAVE_SYS_FILIO_H
41 #include <sys/filio.h>
42 #endif
43 #ifdef HAVE_NETINET_IN_SYSTM_H
44 #include <netinet/in_systm.h>
45 #endif
46 #include <netinet/in.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip_icmp.h>
49 #include <math.h>
50 #ifndef MAXFLOAT
51 #include <float.h>
52 #define MAXFLOAT FLT_MAX
53 #endif
54
55 #include "noit_module.h"
56 #include "noit_check.h"
57 #include "noit_check_tools.h"
58 #include "utils/noit_log.h"
59
60 #define PING_INTERVAL 2000 /* 2000ms = 2s */
61 #define PING_COUNT    5
62
63 struct check_info {
64   int check_no;
65   int check_seq_no;
66   int seq;
67   int32_t expected_count;
68   float *turnaround;
69   eventer_t timeout_event;
70 };
71 struct ping_payload {
72   uuid_t checkid;
73   u_int32_t generation;   
74   struct timeval whence;
75   int    check_no;
76   int    check_pack_no;
77   int    check_pack_cnt;
78 };
79 struct ping_closure {
80   noit_module_t *self;
81   noit_check_t *check;
82   void *payload;
83   int payload_len;
84 };
85 static noit_log_stream_t nlerr = NULL;
86 static noit_log_stream_t nldeb = NULL;
87 static int in_cksum(u_short *addr, int len);
88
89 typedef struct  {
90   int ipv4_fd;
91   int ipv6_fd;
92 } ping_icmp_data_t;
93
94 static int ping_icmp_config(noit_module_t *self, noit_hash_table *options) {
95   return 0;
96 }
97 static int ping_icmp_is_complete(noit_module_t *self, noit_check_t *check) {
98   int i;
99   struct check_info *data;
100   data = (struct check_info *)check->closure;
101   for(i=0; i<data->expected_count; i++)
102     if(data->turnaround[i] < 0.0) {
103       noitL(nldeb, "ping_icmp: %s %d is still outstanding.\n",
104             check->target, i);
105       return 0;
106     }
107   return 1;
108 }
109 static void ping_icmp_log_results(noit_module_t *self, noit_check_t *check) {
110   struct check_info *data;
111   double avail, min = MAXFLOAT, max = 0.0, avg = 0.0, cnt;
112   int avail_needed = 100;
113   const char *config_val = NULL;
114   int i, points = 0;
115   char human_buffer[256];
116   stats_t current;
117   struct timeval duration;
118
119   noit_check_stats_clear(&current);
120
121   data = (struct check_info *)check->closure;
122   for(i=0; i<data->expected_count; i++) {
123     if(data->turnaround[i] >= 0.0) {
124       points++;
125       avg += data->turnaround[i];
126       if(data->turnaround[i] > max) max = data->turnaround[i];
127       if(data->turnaround[i] < min) min = data->turnaround[i];
128     }
129   }
130   if(points == 0) {
131     min = 0.0 / 0.0;
132     max = 0.0 / 0.0;
133   }
134   cnt = data->expected_count;
135   avail = (float)points /cnt;
136   avg /= (float)points;
137
138   if(noit_hash_retr_str(check->config, "avail_needed", strlen("avail_needed"),
139                         &config_val))
140     avail_needed = atoi(config_val);
141
142   snprintf(human_buffer, sizeof(human_buffer),
143            "cnt=%d,avail=%0.0f,min=%0.4f,max=%0.4f,avg=%0.4f",
144            (int)cnt, 100.0*avail, min, max, avg);
145   noitL(nldeb, "ping_icmp(%s) [%s]\n", check->target, human_buffer);
146
147   gettimeofday(&current.whence, NULL);
148   sub_timeval(current.whence, check->last_fire_time, &duration);
149   current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
150   current.available = (avail > 0.0) ? NP_AVAILABLE : NP_UNAVAILABLE;
151   current.state = (avail < ((float)avail_needed / 100.0)) ? NP_BAD : NP_GOOD;
152   current.status = human_buffer;
153   noit_stats_set_metric(&current, "count",
154                         METRIC_INT32, &data->expected_count);
155   avail *= 100.0;
156   noit_stats_set_metric(&current, "available", METRIC_DOUBLE, &avail);
157   noit_stats_set_metric(&current, "minimum",
158                         METRIC_DOUBLE, avail > 0.0 ? &min : NULL);
159   noit_stats_set_metric(&current, "maximum",
160                         METRIC_DOUBLE, avail > 0.0 ? &max : NULL);
161   noit_stats_set_metric(&current, "average",
162                         METRIC_DOUBLE, avail > 0.0 ? &avg : NULL);
163   noit_check_set_stats(self, check, &current);
164 }
165 static int ping_icmp_timeout(eventer_t e, int mask,
166                              void *closure, struct timeval *now) {
167   struct ping_closure *pcl = (struct ping_closure *)closure;
168   struct check_info *data;
169   if(!NOIT_CHECK_KILLED(pcl->check) && !NOIT_CHECK_DISABLED(pcl->check)) {
170     ping_icmp_log_results(pcl->self, pcl->check);
171     data = (struct check_info *)pcl->check->closure;
172     data->timeout_event = NULL;
173   }
174   pcl->check->flags &= ~NP_RUNNING;
175   free(pcl);
176   return 0;
177 }
178 static int ping_icmp_handler(eventer_t e, int mask,
179                              void *closure, struct timeval *now) {
180   noit_module_t *self = (noit_module_t *)closure;
181   struct check_info *data;
182   char packet[1500];
183   int packet_len = sizeof(packet);
184   union {
185    struct sockaddr_in  in4;
186    struct sockaddr_in6 in6;
187   } from;
188   unsigned int from_len;
189   struct ip *ip = (struct ip *)packet;
190   struct icmp *icp;
191   struct ping_payload *payload;
192
193   while(1) {
194     int inlen, iphlen;
195     noit_check_t *check;
196     struct timeval tt;
197
198     from_len = sizeof(from);
199
200     inlen = recvfrom(e->fd, packet, packet_len, 0,
201                      (struct sockaddr *)&from, &from_len);
202     gettimeofday(now, NULL); /* set it, as we care about accuracy */
203
204     if(inlen < 0) {
205       if(errno == EAGAIN || errno == EINTR) break;
206       noitLT(nlerr, now, "ping_icmp recvfrom: %s\n", strerror(errno));
207       break;
208     }
209     iphlen = ip->ip_hl << 2;
210     if((inlen-iphlen) != (sizeof(struct icmp)+sizeof(struct ping_payload))) {
211       noitLT(nldeb, now,
212              "ping_icmp bad size: %d+%d\n", iphlen, inlen-iphlen);
213       continue;
214     }
215     icp = (struct icmp *)(packet + iphlen);
216     payload = (struct ping_payload *)(icp + 1);
217     if(icp->icmp_type != ICMP_ECHOREPLY) {
218       continue;
219     }
220     if(icp->icmp_id != (((vpsized_uint)self) & 0xffff)) {
221       noitLT(nlerr, now,
222                "ping_icmp not sent from this instance (%d:%d) vs. %lu\n",
223                icp->icmp_id, ntohs(icp->icmp_seq),
224                (unsigned long)(((vpsized_uint)self) & 0xffff));
225       continue;
226     }
227     check = noit_poller_lookup(payload->checkid);
228     /* make sure this check is from this generation! */
229     if(!check) {
230       char uuid_str[37];
231       uuid_unparse_lower(payload->checkid, uuid_str);
232       noitLT(nlerr, now,
233              "ping_icmp response for unknown check '%s'\n", uuid_str);
234       continue;
235     }
236     if(check->generation != payload->generation) {
237       noitLT(nldeb, now,
238              "ping_icmp response in generation gap\n");
239       continue;
240     }
241     data = (struct check_info *)check->closure;
242
243     /* If there is no timeout_event, the check must have completed.
244      * We have nothing to do. */
245     if(!data->timeout_event) continue;
246
247     /* Sanity check the payload */
248     if(payload->check_no != data->check_no) continue;
249     if(payload->check_pack_cnt != data->expected_count) continue;
250     if(payload->check_pack_no < 0 ||
251        payload->check_pack_no >= data->expected_count) continue;
252
253     sub_timeval(*now, payload->whence, &tt);
254     data->turnaround[payload->check_pack_no] =
255       (float)tt.tv_sec + (float)tt.tv_usec / 1000000.0;
256     if(ping_icmp_is_complete(self, check)) {
257       ping_icmp_log_results(self, check);
258       eventer_remove(data->timeout_event);
259       free(data->timeout_event->closure);
260       eventer_free(data->timeout_event);
261       data->timeout_event = NULL;
262       check->flags &= ~NP_RUNNING;
263     }
264   }
265   return EVENTER_READ;
266 }
267
268 static int ping_icmp_init(noit_module_t *self) {
269   socklen_t on;
270   struct protoent *proto;
271   ping_icmp_data_t *data;
272
273   data = malloc(sizeof(*data));
274   data->ipv4_fd = data->ipv6_fd = -1;
275
276   if ((proto = getprotobyname("icmp")) == NULL) {
277     noitL(noit_error, "Couldn't find 'icmp' protocol\n");
278     return -1;
279   }
280
281   data->ipv4_fd = socket(AF_INET, SOCK_RAW, proto->p_proto);
282   if(data->ipv4_fd < 0) {
283     noitL(noit_error, "ping_icmp: socket failed: %s\n",
284           strerror(errno));
285   }
286   else {
287     socklen_t slen = sizeof(on);
288     if(getsockopt(data->ipv4_fd, SOL_SOCKET, SO_SNDBUF, &on, &slen) == 0) {
289       while(on < (1 << 20)) {
290         on <<= 1;
291         if(setsockopt(data->ipv4_fd, SOL_SOCKET, SO_SNDBUF,
292                       &on, sizeof(on)) != 0) {
293           on >>= 1;
294           break;
295         }
296       }
297       noitL(noit_debug, "ping_icmp: send buffer set to %d\n", on);
298     }
299     else
300       noitL(noit_error, "Cannot get sndbuf size: %s\n", strerror(errno));
301
302     if(eventer_set_fd_nonblocking(data->ipv4_fd)) {
303       close(data->ipv4_fd);
304       data->ipv4_fd = -1;
305       noitL(noit_error,
306             "ping_icmp: could not set socket non-blocking: %s\n",
307             strerror(errno));
308     }
309   }
310   if(data->ipv4_fd >= 0) {
311     eventer_t newe;
312     newe = eventer_alloc();
313     newe->fd = data->ipv4_fd;
314     newe->mask = EVENTER_READ;
315     newe->callback = ping_icmp_handler;
316     newe->closure = self;
317     eventer_add(newe);
318   }
319
320   data->ipv6_fd = socket(AF_INET6, SOCK_RAW, proto->p_proto);
321   if(data->ipv6_fd < 0) {
322     noitL(noit_error, "ping_icmp: socket failed: %s\n",
323           strerror(errno));
324   }
325   else {
326     if(eventer_set_fd_nonblocking(data->ipv6_fd)) {
327       close(data->ipv6_fd);
328       data->ipv6_fd = -1;
329       noitL(noit_error,
330             "ping_icmp: could not set socket non-blocking: %s\n",
331                strerror(errno));
332     }
333   }
334   if(data->ipv6_fd >= 0) {
335     eventer_t newe;
336     newe = eventer_alloc();
337     newe->fd = data->ipv6_fd;
338     newe->mask = EVENTER_READ;
339     newe->callback = ping_icmp_handler;
340     newe->closure = self;
341     eventer_add(newe);
342   }
343
344   noit_module_set_userdata(self, data);
345   return 0;
346 }
347
348 static int ping_icmp_real_send(eventer_t e, int mask,
349                                void *closure, struct timeval *now) {
350   struct ping_closure *pcl = (struct ping_closure *)closure;
351   struct icmp *icp;
352   struct ping_payload *payload;
353   ping_icmp_data_t *data;
354   int i;
355
356   noitLT(nldeb, now, "ping_icmp_real_send(%s)\n", pcl->check->target);
357   data = noit_module_get_userdata(pcl->self);
358   icp = (struct icmp *)pcl->payload;
359   payload = (struct ping_payload *)(icp + 1);
360   gettimeofday(&payload->whence, NULL); /* now isn't accurate enough */
361   icp->icmp_cksum = in_cksum(pcl->payload, pcl->payload_len);
362   if(pcl->check->target_family == AF_INET) {
363     struct sockaddr_in sin;
364     memset(&sin, 0, sizeof(sin));
365     sin.sin_family = AF_INET;
366     memcpy(&sin.sin_addr,
367            &pcl->check->target_addr.addr, sizeof(sin.sin_addr));
368     i = sendto(data->ipv4_fd,
369                pcl->payload, pcl->payload_len, 0,
370                (struct sockaddr *)&sin, sizeof(sin));
371   }
372   else {
373     struct sockaddr_in6 sin;
374     memset(&sin, 0, sizeof(sin));
375     sin.sin6_family = AF_INET6;
376     memcpy(&sin.sin6_addr,
377            &pcl->check->target_addr.addr6, sizeof(sin.sin6_addr));
378     i = sendto(data->ipv6_fd,
379                pcl->payload, pcl->payload_len, 0,
380                (struct sockaddr *)&sin, sizeof(sin));
381   }
382   if(i != pcl->payload_len) {
383     noitLT(nlerr, now, "Error sending ICMP packet to %s: %s\n",
384              pcl->check->target, strerror(errno));
385   }
386   free(pcl->payload);
387   free(pcl);
388   return 0;
389 }
390 static void ping_check_cleanup(noit_module_t *self, noit_check_t *check) {
391   struct check_info *ci = (struct check_info *)check->closure;
392   if(ci) {
393     if(ci->timeout_event) {
394       eventer_remove(ci->timeout_event);
395       free(ci->timeout_event->closure);
396       eventer_free(ci->timeout_event);
397       ci->timeout_event = NULL;
398     }
399     if(ci->turnaround) free(ci->turnaround);
400   }
401 }
402 static int ping_icmp_send(noit_module_t *self, noit_check_t *check) {
403   struct timeval when, p_int;
404   struct icmp *icp;
405   struct ping_payload *payload;
406   struct ping_closure *pcl;
407   struct check_info *ci = (struct check_info *)check->closure;
408   int packet_len, i;
409   eventer_t newe;
410   const char *config_val;
411
412   int interval = PING_INTERVAL;
413   int count = PING_COUNT;
414   if(noit_hash_retr_str(check->config, "interval", strlen("interval"),
415                         &config_val))
416     interval = atoi(config_val);
417   if(noit_hash_retr_str(check->config, "count", strlen("count"),
418                         &config_val))
419     count = atoi(config_val);
420
421   check->flags |= NP_RUNNING;
422   noitL(nldeb, "ping_icmp_send(%p,%s,%d,%d)\n",
423         self, check->target, interval, count);
424
425   /* remove a timeout if we still have one -- we should unless someone
426    * has set a lower timeout than the period.
427    */
428   if(ci->timeout_event) {
429     eventer_remove(ci->timeout_event);
430     free(ci->timeout_event->closure);
431     eventer_free(ci->timeout_event);
432     ci->timeout_event = NULL;
433   }
434
435   gettimeofday(&when, NULL);
436   memcpy(&check->last_fire_time, &when, sizeof(when));
437
438   /* Setup some stuff used in the loop */
439   p_int.tv_sec = interval / 1000;
440   p_int.tv_usec = (interval % 1000) * 1000;
441   packet_len = sizeof(*icp) + sizeof(*payload);
442
443   /* Prep holding spots for return info */
444   ci->expected_count = count;
445   if(ci->turnaround) free(ci->turnaround);
446   ci->turnaround = malloc(count * sizeof(*ci->turnaround));
447
448   ++ci->check_no;
449   for(i=0; i<count; i++) {
450     /* Negative means we've not received a response */
451     ci->turnaround[i] = -1.0;
452
453     newe = eventer_alloc();
454     newe->callback = ping_icmp_real_send;
455     newe->mask = EVENTER_TIMER;
456     memcpy(&newe->whence, &when, sizeof(when));
457     add_timeval(when, p_int, &when); /* Next one is a bit later */
458
459     icp = calloc(1,packet_len);
460     payload = (struct ping_payload *)(icp + 1);
461
462     icp->icmp_type = ICMP_ECHO;
463     icp->icmp_code = 0;
464     icp->icmp_cksum = 0;
465     icp->icmp_seq = htons(ci->seq++);
466     icp->icmp_id = (((vpsized_uint)self) & 0xffff);
467
468     uuid_copy(payload->checkid, check->checkid);
469     payload->generation = check->generation;
470     payload->check_no = ci->check_no;
471     payload->check_pack_no = i;
472     payload->check_pack_cnt = count;
473
474     pcl = calloc(1, sizeof(*pcl));
475     pcl->self = self;
476     pcl->check = check;
477     pcl->payload = icp;
478     pcl->payload_len = packet_len;
479
480     newe->closure = pcl;
481     eventer_add(newe);
482   }
483   newe = eventer_alloc();
484   newe->mask = EVENTER_TIMER;
485   gettimeofday(&when, NULL);
486   p_int.tv_sec = check->timeout / 1000;
487   p_int.tv_usec = (check->timeout % 1000) * 1000;
488   add_timeval(when, p_int, &newe->whence);
489   pcl = calloc(1, sizeof(*pcl));
490   pcl->self = self;
491   pcl->check = check;
492   newe->closure = pcl;
493   newe->callback = ping_icmp_timeout;
494   eventer_add(newe);
495   ci->timeout_event = newe;
496
497   return 0;
498 }
499 static int ping_icmp_initiate_check(noit_module_t *self, noit_check_t *check,
500                                     int once, noit_check_t *cause) {
501   if(!check->closure) check->closure = calloc(1, sizeof(struct check_info));
502   INITIATE_CHECK(ping_icmp_send, self, check);
503   return 0;
504 }
505
506 /*
507  *      I N _ C K S U M
508  *          This is from Mike Muuss's Public Domain code.
509  * Checksum routine for Internet Protocol family headers (C Version)
510  *
511  */
512 static int in_cksum(u_short *addr, int len)
513 {
514   register int nleft = len;
515   register u_short *w = addr;
516   register u_short answer;
517   register int sum = 0;
518
519   /*
520    *  Our algorithm is simple, using a 32 bit accumulator (sum),
521    *  we add sequential 16 bit words to it, and at the end, fold
522    *  back all the carry bits from the top 16 bits into the lower
523    *  16 bits.
524    */
525   while( nleft > 1 )  {
526     sum += *w++;
527     nleft -= 2;
528   }
529
530   /* mop up an odd byte, if necessary */
531   if( nleft == 1 ) {
532     u_short  u = 0;
533
534     *(u_char *)(&u) = *(u_char *)w ;
535     sum += u;
536   }
537
538   /*
539    * add back carry outs from top 16 bits to low 16 bits
540    */
541   sum = (sum >> 16) + (sum & 0xffff);  /* add hi 16 to low 16 */
542   sum += (sum >> 16);      /* add carry */
543   answer = ~sum;        /* truncate to 16 bits */
544   return (answer);
545 }
546
547 static int ping_icmp_onload(noit_image_t *self) {
548   nlerr = noit_log_stream_find("error/ping_icmp");
549   nldeb = noit_log_stream_find("debug/ping_icmp");
550   if(!nlerr) nlerr = noit_stderr;
551   if(!nldeb) nldeb = noit_debug;
552   eventer_name_callback("ping_icmp/timeout", ping_icmp_timeout);
553   eventer_name_callback("ping_icmp/handler", ping_icmp_handler);
554   return 0;
555 }
556 #include "ping_icmp.xmlh"
557 noit_module_t ping_icmp = {
558   {
559     NOIT_MODULE_MAGIC,
560     NOIT_MODULE_ABI_VERSION,
561     "ping_icmp",
562     "ICMP based host availability detection",
563     ping_icmp_xml_description,
564     ping_icmp_onload
565   },
566   ping_icmp_config,
567   ping_icmp_init,
568   ping_icmp_initiate_check,
569   ping_check_cleanup
570 };
571
Note: See TracBrowser for help on using the browser.