root/src/modules/ping_icmp.c

Revision 78402cdb08d09655ada4b082b6ea4fda7553dadf, 15.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

make the checks non-recurrent if the period is zero. This preps for causal firing of events.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <netdb.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <sys/ioctl.h>
14 #include <netinet/in.h>
15 #include <netinet/ip.h>
16 #include <netinet/ip_icmp.h>
17 #include <math.h>
18
19 #include "noit_module.h"
20 #include "noit_poller.h"
21 #include "utils/noit_log.h"
22
23 #define PING_INTERVAL 2000 /* 2000ms = 2s */
24 #define PING_COUNT    5
25
26 struct check_info {
27   int check_no;
28   int check_seq_no;
29   int seq;
30   int expected_count;
31   float *turnaround;
32   eventer_t timeout_event;
33 };
34 struct ping_payload {
35   uuid_t checkid;
36   struct timeval whence;
37   int    check_no;
38   int    check_pack_no;
39   int    check_pack_cnt;
40 };
41 struct ping_closure {
42   noit_module_t *self;
43   noit_check_t check;
44   void *payload;
45   int payload_len;
46 };
47 static noit_log_stream_t nlerr = NULL;
48 static noit_log_stream_t nldeb = NULL;
49 static int ping_icmp_recur_handler(eventer_t e, int mask, void *closure,
50                                    struct timeval *now);
51 static int in_cksum(u_short *addr, int len);
52
53 typedef struct  {
54   int ipv4_fd;
55   int ipv6_fd;
56 } ping_icmp_data_t;
57
58 static int ping_icmp_config(noit_module_t *self, noit_hash_table *options) {
59   return 0;
60 }
61 static int ping_icmp_is_complete(noit_module_t *self, noit_check_t check) {
62   int i;
63   struct check_info *data;
64   data = (struct check_info *)check->closure;
65   for(i=0; i<data->expected_count; i++)
66     if(data->turnaround[i] == 0.0) {
67       noitL(nldeb, "ping_icmp: %s %d is still outstanding.\n",
68             check->target, i);
69       return 0;
70     }
71   return 1;
72 }
73 static void ping_icmp_log_results(noit_module_t *self, noit_check_t check) {
74   struct check_info *data;
75   float avail, min = MAXFLOAT, max = 0.0, avg = 0.0, cnt;
76   int i, points = 0;
77   char human_buffer[256];
78   stats_t current;
79   struct timeval duration;
80
81   data = (struct check_info *)check->closure;
82   for(i=0; i<data->expected_count; i++) {
83     if(data->turnaround[i] != 0) {
84       points++;
85       avg += data->turnaround[i];
86       if(data->turnaround[i] > max) max = data->turnaround[i];
87       if(data->turnaround[i] < min) min = data->turnaround[i];
88     }
89   }
90   if(points == 0) {
91     min = 0.0 / 0.0;
92     max = 0.0 / 0.0;
93   }
94   cnt = data->expected_count;
95   avail = (float)points /cnt;
96   avg /= (float)points;
97
98   snprintf(human_buffer, sizeof(human_buffer),
99            "cnt=%d,avail=%0.0f,min=%0.4f,max=%0.4f,avg=%0.4f",
100            (int)cnt, 100.0*avail, min, max, avg);
101   noitL(nldeb, "ping_icmp(%s) [%s]\n", check->target, human_buffer);
102
103   gettimeofday(&current.whence, NULL);
104   sub_timeval(current.whence, check->last_fire_time, &duration);
105   current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
106   current.available = (avail > 0.0) ? NP_AVAILABLE : NP_UNAVAILABLE;
107   current.state = (avail < 1.0) ? NP_BAD : NP_GOOD;
108   current.status = human_buffer;
109   noit_poller_set_state(check, &current);
110 }
111 static int ping_icmp_timeout(eventer_t e, int mask,
112                              void *closure, struct timeval *now) {
113   struct ping_closure *pcl = (struct ping_closure *)closure;
114   struct check_info *data;
115   ping_icmp_log_results(pcl->self, pcl->check);
116   data = (struct check_info *)pcl->check->closure;
117   data->timeout_event = NULL;
118   pcl->check->flags &= ~NP_RUNNING;
119   free(pcl);
120   return 0;
121 }
122 static int ping_icmp_handler(eventer_t e, int mask,
123                              void *closure, struct timeval *now) {
124   noit_module_t *self = (noit_module_t *)closure;
125   struct check_info *data;
126   char packet[1500];
127   int packet_len = sizeof(packet);
128   union {
129    struct sockaddr_in  in4;
130    struct sockaddr_in6 in6;
131   } from;
132   unsigned int from_len;
133   struct ip *ip = (struct ip *)packet;;
134   struct icmp *icp;
135   struct ping_payload *payload;
136
137   while(1) {
138     int inlen, iphlen;
139     noit_check_t check;
140     struct timeval tt;
141
142     from_len = sizeof(from);
143
144     inlen = recvfrom(e->fd, packet, packet_len, 0,
145                      (struct sockaddr *)&from, &from_len);
146     gettimeofday(now, NULL); /* set it, as we care about accuracy */
147
148     if(inlen < 0) {
149       if(errno == EAGAIN || errno == EINTR) break;
150       noitLT(nlerr, now, "ping_icmp recvfrom: %s\n", strerror(errno));
151       break;
152     }
153     iphlen = ip->ip_hl << 2;
154     if((inlen-iphlen) != (sizeof(struct icmp)+sizeof(struct ping_payload))) {
155       noitLT(nlerr, now,
156              "ping_icmp bad size: %d+%d\n", iphlen, inlen-iphlen);
157       continue;
158     }
159     icp = (struct icmp *)(packet + iphlen);
160     payload = (struct ping_payload *)(icp + 1);
161     if(icp->icmp_type != ICMP_ECHOREPLY) {
162       continue;
163     }
164     if(icp->icmp_id != (((vpsized_uint)self) & 0xffff)) {
165       noitLT(nlerr, now,
166                "ping_icmp not sent from this instance (%d:%d) vs. %d\n",
167                icp->icmp_id, ntohs(icp->icmp_seq),
168                (((vpsized_uint)self) & 0xffff));
169       continue;
170     }
171     check = noit_poller_lookup(payload->checkid);
172     if(!check) {
173       char uuid_str[37];
174       uuid_unparse_lower(payload->checkid, uuid_str);
175       noitLT(nlerr, now,
176              "ping_icmp response for unknown check '%s'\n", uuid_str);
177       continue;
178     }
179     data = (struct check_info *)check->closure;
180
181     /* If there is no timeout_event, the check must have completed.
182      * We have nothing to do. */
183     if(!data->timeout_event) continue;
184
185     /* Sanity check the payload */
186     if(payload->check_no != data->check_no) continue;
187     if(payload->check_pack_cnt != data->expected_count) continue;
188     if(payload->check_pack_no < 0 ||
189        payload->check_pack_no >= data->expected_count) continue;
190
191     sub_timeval(*now, payload->whence, &tt);
192     data->turnaround[payload->check_pack_no] =
193       (float)tt.tv_sec + (float)tt.tv_usec / 1000000.0;
194     if(ping_icmp_is_complete(self, check)) {
195       ping_icmp_log_results(self, check);
196       eventer_remove(data->timeout_event);
197       free(data->timeout_event->closure);
198       eventer_free(data->timeout_event);
199       data->timeout_event = NULL;
200       check->flags &= ~NP_RUNNING;
201     }
202   }
203   return EVENTER_READ;
204 }
205
206 static int ping_icmp_init(noit_module_t *self) {
207   socklen_t on;
208   struct protoent *proto;
209   ping_icmp_data_t *data;
210
211   data = malloc(sizeof(*data));
212   data->ipv4_fd = data->ipv6_fd = -1;
213
214   if ((proto = getprotobyname("icmp")) == NULL) {
215     noitL(nlerr, "Couldn't find 'icmp' protocol\n");
216     return -1;
217   }
218
219   data->ipv4_fd = socket(AF_INET, SOCK_RAW, proto->p_proto);
220   if(data->ipv4_fd < 0) {
221     noitL(nlerr, "ping_icmp: socket failed: %s\n",
222           strerror(errno));
223   }
224   else {
225     on = 1;
226     if(ioctl(data->ipv4_fd, FIONBIO, &on)) {
227       close(data->ipv4_fd);
228       data->ipv4_fd = -1;
229       noitL(nlerr,
230             "ping_icmp: could not set socket non-blocking: %s\n",
231             strerror(errno));
232     }
233   }
234   if(data->ipv4_fd >= 0) {
235     eventer_t newe;
236     newe = eventer_alloc();
237     newe->fd = data->ipv4_fd;
238     newe->mask = EVENTER_READ;
239     newe->callback = ping_icmp_handler;
240     newe->closure = self;
241     eventer_add(newe);
242   }
243
244   data->ipv6_fd = socket(AF_INET6, SOCK_RAW, proto->p_proto);
245   if(data->ipv6_fd < 0) {
246     noitL(nlerr, "ping_icmp: socket failed: %s\n",
247           strerror(errno));
248   }
249   else {
250     on = 1;
251     if(ioctl(data->ipv6_fd, FIONBIO, &on)) {
252       close(data->ipv6_fd);
253       data->ipv6_fd = -1;
254       noitL(nlerr,
255             "ping_icmp: could not set socket non-blocking: %s\n",
256                strerror(errno));
257     }
258   }
259   if(data->ipv6_fd >= 0) {
260     eventer_t newe;
261     newe = eventer_alloc();
262     newe->fd = data->ipv6_fd;
263     newe->mask = EVENTER_READ;
264     newe->callback = ping_icmp_handler;
265     newe->closure = self;
266     eventer_add(newe);
267   }
268
269   noit_module_set_userdata(self, data);
270   return 0;
271 }
272
273 static int ping_icmp_real_send(eventer_t e, int mask,
274                                void *closure, struct timeval *now) {
275   struct ping_closure *pcl = (struct ping_closure *)closure;
276   struct icmp *icp;
277   struct ping_payload *payload;
278   ping_icmp_data_t *data;
279   int i;
280
281   noitLT(nldeb, now, "ping_icmp_real_send(%s)\n", pcl->check->target);
282   data = noit_module_get_userdata(pcl->self);
283   icp = (struct icmp *)pcl->payload;
284   payload = (struct ping_payload *)(icp + 1);
285   gettimeofday(&payload->whence, NULL); /* now isn't accurate enough */
286   icp->icmp_cksum = in_cksum(pcl->payload, pcl->payload_len);
287   if(pcl->check->target_family == AF_INET) {
288     struct sockaddr_in sin;
289     memset(&sin, 0, sizeof(sin));
290     sin.sin_family = AF_INET;
291     memcpy(&sin.sin_addr,
292            &pcl->check->target_addr.addr, sizeof(sin.sin_addr));
293     i = sendto(data->ipv4_fd,
294                pcl->payload, pcl->payload_len, 0,
295                (struct sockaddr *)&sin, sizeof(sin));
296   }
297   else {
298     struct sockaddr_in6 sin;
299     memset(&sin, 0, sizeof(sin));
300     sin.sin6_family = AF_INET6;
301     memcpy(&sin.sin6_addr,
302            &pcl->check->target_addr.addr6, sizeof(sin.sin6_addr));
303     i = sendto(data->ipv6_fd,
304                pcl->payload, pcl->payload_len, 0,
305                (struct sockaddr *)&sin, sizeof(sin));
306   }
307   if(i != pcl->payload_len) {
308     noitLT(nlerr, now, "Error sending ICMP packet to %s: %s\n",
309              pcl->check->target, strerror(errno));
310   }
311   free(pcl->payload);
312   free(pcl);
313   return 0;
314 }
315 static int ping_icmp_send(noit_module_t *self, noit_check_t check) {
316   struct timeval when, p_int;
317   struct icmp *icp;
318   struct ping_payload *payload;
319   struct ping_closure *pcl;
320   struct check_info *ci = (struct check_info *)check->closure;
321   int packet_len, i;
322   eventer_t newe;
323   const char *config_val;
324
325   int interval = PING_INTERVAL;
326   int count = PING_COUNT;
327   if(noit_hash_retrieve(check->config, "interval", strlen("interval"),
328                         (void **)&config_val))
329     interval = atoi(config_val);
330   if(noit_hash_retrieve(check->config, "count", strlen("count"),
331                         (void **)&config_val))
332     count = atoi(config_val);
333
334   check->flags |= NP_RUNNING;
335   noitL(nldeb, "ping_icmp_send(%p,%s,%d,%d)\n",
336         self, check->target, interval, count);
337
338   /* remove a timeout if we still have one -- we should unless someone
339    * has set a lower timeout than the period.
340    */
341   if(ci->timeout_event) {
342     eventer_remove(ci->timeout_event);
343     free(ci->timeout_event->closure);
344     eventer_free(ci->timeout_event);
345     ci->timeout_event = NULL;
346   }
347
348   gettimeofday(&when, NULL);
349   memcpy(&check->last_fire_time, &when, sizeof(when));
350
351   /* Setup some stuff used in the loop */
352   p_int.tv_sec = interval / 1000;
353   p_int.tv_usec = (interval % 1000) * 1000;
354   packet_len = sizeof(*icp) + sizeof(*payload);
355
356   /* Prep holding spots for return info */
357   ci->expected_count = count;
358   if(ci->turnaround) free(ci->turnaround);
359   ci->turnaround = calloc(count, sizeof(*ci->turnaround));
360
361   ++ci->check_no;
362   for(i=0; i<count; i++) {
363     newe = eventer_alloc();
364     newe->callback = ping_icmp_real_send;
365     newe->mask = EVENTER_TIMER;
366     memcpy(&newe->whence, &when, sizeof(when));
367     add_timeval(when, p_int, &when); /* Next one is a bit later */
368
369     icp = malloc(packet_len);
370     payload = (struct ping_payload *)(icp + 1);
371
372     icp->icmp_type = ICMP_ECHO;
373     icp->icmp_code = 0;
374     icp->icmp_cksum = 0;
375     icp->icmp_seq = htons(ci->seq++);
376     icp->icmp_id = (((vpsized_uint)self) & 0xffff);
377
378     uuid_copy(payload->checkid, check->checkid);
379     payload->check_no = ci->check_no;
380     payload->check_pack_no = i;
381     payload->check_pack_cnt = count;
382
383     pcl = calloc(1, sizeof(*pcl));
384     pcl->self = self;
385     pcl->check = check;
386     pcl->payload = icp;
387     pcl->payload_len = packet_len;
388
389     newe->closure = pcl;
390     eventer_add(newe);
391   }
392   newe = eventer_alloc();
393   newe->mask = EVENTER_TIMER;
394   gettimeofday(&when, NULL);
395   p_int.tv_sec = check->timeout / 1000;
396   p_int.tv_usec = (check->timeout % 1000) * 1000;
397   add_timeval(when, p_int, &newe->whence);
398   pcl = calloc(1, sizeof(*pcl));
399   pcl->self = self;
400   pcl->check = check;
401   newe->closure = pcl;
402   newe->callback = ping_icmp_timeout;
403   eventer_add(newe);
404   ci->timeout_event = newe;
405
406   return 0;
407 }
408 static int ping_icmp_schedule_next(noit_module_t *self,
409                                    eventer_t e, noit_check_t check,
410                                    struct timeval *now) {
411   eventer_t newe;
412   struct timeval last_check = { 0L, 0L };
413   struct timeval period, earliest;
414   struct ping_closure *pcl;
415
416   if(check->period == 0) return 0;
417
418   /* If we have an event, we know when we intended it to fire.  This means
419    * we should schedule that point + period.
420    */
421   if(now)
422     memcpy(&earliest, now, sizeof(earliest));
423   else
424     gettimeofday(&earliest, NULL);
425   if(e) memcpy(&last_check, &e->whence, sizeof(last_check));
426   period.tv_sec = check->period / 1000;
427   period.tv_usec = (check->period % 1000) * 1000;
428
429   newe = eventer_alloc();
430   memcpy(&newe->whence, &last_check, sizeof(last_check));
431   add_timeval(newe->whence, period, &newe->whence);
432   if(compare_timeval(newe->whence, earliest) < 0)
433     memcpy(&newe->whence, &earliest, sizeof(earliest));
434   newe->mask = EVENTER_TIMER;
435   newe->callback = ping_icmp_recur_handler;
436   pcl = calloc(1, sizeof(*pcl));
437   pcl->self = self;
438   pcl->check = check;
439   newe->closure = pcl;
440
441   eventer_add(newe);
442   check->fire_event = newe;
443   return 0;
444 }
445 static int ping_icmp_recur_handler(eventer_t e, int mask, void *closure,
446                                    struct timeval *now) {
447   struct ping_closure *cl = (struct ping_closure *)closure;
448   ping_icmp_schedule_next(cl->self, e, cl->check, now);
449   ping_icmp_send(cl->self, cl->check);
450   free(cl);
451   return 0;
452 }
453 static int ping_icmp_initiate_check(noit_module_t *self, noit_check_t check,
454                                     int once) {
455   if(!check->closure) check->closure = calloc(1, sizeof(struct check_info));
456   if(once) {
457     ping_icmp_send(self, check);
458     return 0;
459   }
460   if(!check->fire_event)
461     ping_icmp_schedule_next(self, NULL, check, NULL);
462   return 0;
463 }
464
465 /*
466  *      I N _ C K S U M
467  *          This is from Mike Muuss's Public Domain code.
468  * Checksum routine for Internet Protocol family headers (C Version)
469  *
470  */
471 static int in_cksum(u_short *addr, int len)
472 {
473   register int nleft = len;
474   register u_short *w = addr;
475   register u_short answer;
476   register int sum = 0;
477
478   /*
479    *  Our algorithm is simple, using a 32 bit accumulator (sum),
480    *  we add sequential 16 bit words to it, and at the end, fold
481    *  back all the carry bits from the top 16 bits into the lower
482    *  16 bits.
483    */
484   while( nleft > 1 )  {
485     sum += *w++;
486     nleft -= 2;
487   }
488
489   /* mop up an odd byte, if necessary */
490   if( nleft == 1 ) {
491     u_short  u = 0;
492
493     *(u_char *)(&u) = *(u_char *)w ;
494     sum += u;
495   }
496
497   /*
498    * add back carry outs from top 16 bits to low 16 bits
499    */
500   sum = (sum >> 16) + (sum & 0xffff);  /* add hi 16 to low 16 */
501   sum += (sum >> 16);      /* add carry */
502   answer = ~sum;        /* truncate to 16 bits */
503   return (answer);
504 }
505
506 static int ping_icmp_onload(noit_module_t *self) {
507   nlerr = noit_log_stream_find("error/ping_icmp");
508   nldeb = noit_log_stream_find("debug/ping_icmp");
509   if(!nlerr) nlerr = noit_stderr;
510   if(!nldeb) nldeb = noit_debug;
511   eventer_name_callback("ping_icmp/recur_handler", ping_icmp_recur_handler);
512   eventer_name_callback("ping_icmp/timeout", ping_icmp_timeout);
513   eventer_name_callback("ping_icmp/handler", ping_icmp_handler);
514   return 0;
515 }
516 noit_module_t ping_icmp = {
517   NOIT_MODULE_MAGIC,
518   NOIT_MODULE_ABI_VERSION,
519   "ping_icmp",
520   "ICMP based host availability detection",
521   ping_icmp_onload,
522   ping_icmp_config,
523   ping_icmp_init,
524   ping_icmp_initiate_check
525 };
526
Note: See TracBrowser for help on using the browser.