root/src/stratcon_jlog_streamer.c

Revision a15dc32994262a4ec0dbef107b5a630aea06633e, 53.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

explicit castings

  • 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 #include "eventer/eventer.h"
35 #include "noit_conf.h"
36 #include "utils/noit_hash.h"
37 #include "utils/noit_log.h"
38 #include "utils/noit_getip.h"
39 #include "noit_jlog_listener.h"
40 #include "noit_rest.h"
41 #include "stratcon_datastore.h"
42 #include "stratcon_jlog_streamer.h"
43 #include "stratcon_iep.h"
44
45 #include <unistd.h>
46 #include <assert.h>
47 #include <errno.h>
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #ifdef HAVE_SYS_FILIO_H
51 #include <sys/filio.h>
52 #endif
53 #include <netinet/in.h>
54 #include <sys/un.h>
55 #include <arpa/inet.h>
56
57 pthread_mutex_t noits_lock;
58 noit_hash_table noits = NOIT_HASH_EMPTY;
59 pthread_mutex_t noit_ip_by_cn_lock;
60 noit_hash_table noit_ip_by_cn = NOIT_HASH_EMPTY;
61 static uuid_t self_stratcon_id;
62 static char self_stratcon_hostname[256] = "\0";
63 static struct sockaddr_in self_stratcon_ip;
64
65 static struct timeval DEFAULT_NOIT_PERIOD_TV = { 5UL, 0UL };
66
67 static void noit_connection_initiate_connection(noit_connection_ctx_t *ctx);
68
69 static const char *feed_type_to_str(int jlog_feed_cmd) {
70   switch(jlog_feed_cmd) {
71     case NOIT_JLOG_DATA_FEED: return "durable/storage";
72     case NOIT_JLOG_DATA_TEMP_FEED: return "transient/iep";
73   }
74   return "unknown";
75 }
76
77 static int
78 remote_str_sort(const void *a, const void *b) {
79   int rv;
80   noit_connection_ctx_t * const *actx = a;
81   noit_connection_ctx_t * const *bctx = b;
82   jlog_streamer_ctx_t *ajctx = (*actx)->consumer_ctx;
83   jlog_streamer_ctx_t *bjctx = (*bctx)->consumer_ctx;
84   rv = strcmp((*actx)->remote_str, (*bctx)->remote_str);
85   if(rv) return rv;
86   return (ajctx->jlog_feed_cmd < bjctx->jlog_feed_cmd) ? -1 :
87            ((ajctx->jlog_feed_cmd == bjctx->jlog_feed_cmd) ? 0 : 1);
88 }
89 static void
90 nc_print_noit_conn_brief(noit_console_closure_t ncct,
91                           noit_connection_ctx_t *ctx) {
92   jlog_streamer_ctx_t *jctx = ctx->consumer_ctx;
93   struct timeval now, diff, session_duration;
94   const char *feedtype = "unknown";
95   const char *lasttime = "never";
96   if(ctx->last_connect.tv_sec != 0) {
97     char cmdbuf[4096];
98     time_t r = ctx->last_connect.tv_sec;
99     struct tm tbuf, *tm;
100     tm = gmtime_r(&r, &tbuf);
101     strftime(cmdbuf, sizeof(cmdbuf), "%Y-%m-%d %H:%M:%S UTC", tm);
102     lasttime = cmdbuf;
103   }
104   nc_printf(ncct, "%s [%s]:\n\tLast connect: %s\n", ctx->remote_str,
105             ctx->remote_cn ? "connected" :
106                              (ctx->retry_event ? "disconnected" :
107                                                    "connecting"), lasttime);
108   if(ctx->e) {
109     char buff[128];
110     const char *addrstr = NULL;
111     struct sockaddr_in6 addr6;
112     socklen_t len = sizeof(addr6);
113     if(getsockname(ctx->e->fd, (struct sockaddr *)&addr6, &len) == 0) {
114       unsigned short port = 0;
115       if(addr6.sin6_family == AF_INET) {
116         addrstr = inet_ntop(addr6.sin6_family,
117                             &((struct sockaddr_in *)&addr6)->sin_addr,
118                             buff, sizeof(buff));
119         memcpy(&port, &((struct sockaddr_in *)&addr6)->sin_port, sizeof(port));
120         port = ntohs(port);
121       }
122       else if(addr6.sin6_family == AF_INET6) {
123         addrstr = inet_ntop(addr6.sin6_family, &addr6.sin6_addr,
124                             buff, sizeof(buff));
125         port = ntohs(addr6.sin6_port);
126       }
127       if(addrstr != NULL)
128         nc_printf(ncct, "\tLocal address is %s:%u\n", buff, port);
129       else
130         nc_printf(ncct, "\tLocal address not interpretable\n");
131     }
132     else {
133       nc_printf(ncct, "\tLocal address error[%d]: %s\n",
134                 ctx->e->fd, strerror(errno));
135     }
136   }
137   feedtype = feed_type_to_str(ntohl(jctx->jlog_feed_cmd));
138   nc_printf(ncct, "\tJLog event streamer [%s]\n", feedtype);
139   gettimeofday(&now, NULL);
140   if(ctx->retry_event) {
141     sub_timeval(ctx->retry_event->whence, now, &diff);
142     nc_printf(ncct, "\tNext attempt in %lld.%06us\n",
143               (long long)diff.tv_sec, (unsigned int) diff.tv_usec);
144   }
145   else if(ctx->remote_cn) {
146     nc_printf(ncct, "\tRemote CN: '%s'\n",
147               ctx->remote_cn ? ctx->remote_cn : "???");
148     if(ctx->consumer_callback == stratcon_jlog_recv_handler) {
149       struct timeval last;
150       double session_duration_seconds;
151       const char *state = "unknown";
152
153       switch(jctx->state) {
154         case JLOG_STREAMER_WANT_INITIATE: state = "initiate"; break;
155         case JLOG_STREAMER_WANT_COUNT: state = "waiting for next batch"; break;
156         case JLOG_STREAMER_WANT_ERROR: state = "waiting for error"; break;
157         case JLOG_STREAMER_WANT_HEADER: state = "reading header"; break;
158         case JLOG_STREAMER_WANT_BODY: state = "reading body"; break;
159         case JLOG_STREAMER_IS_ASYNC: state = "asynchronously processing"; break;
160         case JLOG_STREAMER_WANT_CHKPT: state = "checkpointing"; break;
161       }
162       last.tv_sec = jctx->header.tv_sec;
163       last.tv_usec = jctx->header.tv_usec;
164       sub_timeval(now, last, &diff);
165       sub_timeval(now, ctx->last_connect, &session_duration);
166       session_duration_seconds = session_duration.tv_sec +
167                                  (double)session_duration.tv_usec/1000000.0;
168       nc_printf(ncct, "\tState: %s\n"
169                       "\tNext checkpoint: [%08x:%08x]\n"
170                       "\tLast event: %lld.%06us ago\n"
171                       "\tEvents this session: %llu (%0.2f/s)\n"
172                       "\tOctets this session: %llu (%0.2f/s)\n",
173                 state,
174                 jctx->header.chkpt.log, jctx->header.chkpt.marker,
175                 (long long)diff.tv_sec, (unsigned int)diff.tv_usec,
176                 jctx->total_events,
177                 (double)jctx->total_events/session_duration_seconds,
178                 jctx->total_bytes_read,
179                 (double)jctx->total_bytes_read/session_duration_seconds);
180     }
181     else {
182       nc_printf(ncct, "\tUnknown type.\n");
183     }
184   }
185 }
186
187 jlog_streamer_ctx_t *
188 stratcon_jlog_streamer_datastore_ctx_alloc(void) {
189   jlog_streamer_ctx_t *ctx;
190   ctx = stratcon_jlog_streamer_ctx_alloc();
191   ctx->jlog_feed_cmd = htonl(NOIT_JLOG_DATA_FEED);
192   ctx->push = stratcon_datastore_push;
193   return ctx;
194 }
195 jlog_streamer_ctx_t *
196 stratcon_jlog_streamer_ctx_alloc(void) {
197   jlog_streamer_ctx_t *ctx;
198   ctx = calloc(1, sizeof(*ctx));
199   return ctx;
200 }
201 noit_connection_ctx_t *
202 noit_connection_ctx_alloc(void) {
203   noit_connection_ctx_t *ctx, **pctx;
204   ctx = calloc(1, sizeof(*ctx));
205   ctx->refcnt = 1;
206   pctx = malloc(sizeof(*pctx));
207   *pctx = ctx;
208   pthread_mutex_lock(&noits_lock);
209   noit_hash_store(&noits, (const char *)pctx, sizeof(*pctx), ctx);
210   pthread_mutex_unlock(&noits_lock);
211   return ctx;
212 }
213 int
214 noit_connection_reinitiate(eventer_t e, int mask, void *closure,
215                          struct timeval *now) {
216   noit_connection_ctx_t *ctx = closure;
217   ctx->retry_event = NULL;
218   noit_connection_initiate_connection(closure);
219   return 0;
220 }
221 void
222 noit_connection_schedule_reattempt(noit_connection_ctx_t *ctx,
223                                    struct timeval *now) {
224   struct timeval __now, interval;
225   const char *v;
226   u_int32_t min_interval = 1000, max_interval = 8000;
227
228   noit_connection_disable_timeout(ctx);
229   if(ctx->remote_cn) {
230     free(ctx->remote_cn);
231     ctx->remote_cn = NULL;
232   }
233   if(noit_hash_retr_str(ctx->config,
234                         "reconnect_initial_interval",
235                         strlen("reconnect_initial_interval"),
236                         &v)) {
237     min_interval = MAX(atoi(v), 100); /* .1 second minimum */
238   }
239   if(noit_hash_retr_str(ctx->config,
240                         "reconnect_maximum_interval",
241                         strlen("reconnect_maximum_interval"),
242                         &v)) {
243     max_interval = MIN(atoi(v), 3600*1000); /* 1 hour maximum */
244   }
245   if(ctx->current_backoff == 0) ctx->current_backoff = min_interval;
246   else {
247     ctx->current_backoff *= 2;
248     ctx->current_backoff = MAX(min_interval, ctx->current_backoff);
249     ctx->current_backoff = MIN(max_interval, ctx->current_backoff);
250   }
251   if(!now) {
252     gettimeofday(&__now, NULL);
253     now = &__now;
254   }
255   interval.tv_sec = ctx->current_backoff / 1000;
256   interval.tv_usec = (ctx->current_backoff % 1000) * 1000;
257   noitL(noit_debug, "Next jlog_streamer attempt in %ums\n",
258         ctx->current_backoff);
259   if(ctx->retry_event)
260     eventer_remove(ctx->retry_event);
261   else
262     ctx->retry_event = eventer_alloc();
263   ctx->retry_event->callback = noit_connection_reinitiate;
264   ctx->retry_event->closure = ctx;
265   ctx->retry_event->mask = EVENTER_TIMER;
266   add_timeval(*now, interval, &ctx->retry_event->whence);
267   eventer_add(ctx->retry_event);
268 }
269 static void
270 noit_connection_ctx_free(noit_connection_ctx_t *ctx) {
271   if(ctx->remote_cn) free(ctx->remote_cn);
272   if(ctx->remote_str) free(ctx->remote_str);
273   if(ctx->retry_event) {
274     eventer_remove(ctx->retry_event);
275     eventer_free(ctx->retry_event);
276   }
277   if(ctx->timeout_event) {
278     eventer_remove(ctx->timeout_event);
279     eventer_free(ctx->timeout_event);
280   }
281   ctx->consumer_free(ctx->consumer_ctx);
282   free(ctx);
283 }
284 void
285 noit_connection_ctx_deref(noit_connection_ctx_t *ctx) {
286   if(noit_atomic_dec32(&ctx->refcnt) == 0)
287     noit_connection_ctx_free(ctx);
288 }
289 void
290 noit_connection_ctx_dealloc(noit_connection_ctx_t *ctx) {
291   noit_connection_ctx_t **pctx = &ctx;
292   pthread_mutex_lock(&noits_lock);
293   noit_hash_delete(&noits, (const char *)pctx, sizeof(*pctx),
294                    free, (void (*)(void *))noit_connection_ctx_deref);
295   pthread_mutex_unlock(&noits_lock);
296 }
297 void
298 jlog_streamer_ctx_free(void *cl) {
299   jlog_streamer_ctx_t *ctx = cl;
300   if(ctx->buffer) free(ctx->buffer);
301   free(ctx);
302 }
303
304 #define Eread(a,b) e->opset->read(e->fd, (a), (b), &mask, e)
305 static int
306 __read_on_ctx(eventer_t e, jlog_streamer_ctx_t *ctx, int *newmask) {
307   int len, mask;
308   while(ctx->bytes_read < ctx->bytes_expected) {
309     len = Eread(ctx->buffer + ctx->bytes_read,
310                 ctx->bytes_expected - ctx->bytes_read);
311     if(len < 0) {
312       *newmask = mask;
313       return -1;
314     }
315     /* if we get 0 inside SSL, and there was a real error, we
316      * will actually get a -1 here.
317      * if(len == 0) return ctx->bytes_read;
318      */
319     ctx->total_bytes_read += len;
320     ctx->bytes_read += len;
321   }
322   assert(ctx->bytes_read == ctx->bytes_expected);
323   return ctx->bytes_read;
324 }
325 #define FULLREAD(e,ctx,size) do { \
326   int mask, len; \
327   if(!ctx->bytes_expected) { \
328     ctx->bytes_expected = size; \
329     if(ctx->buffer) free(ctx->buffer); \
330     ctx->buffer = malloc(size + 1); \
331     if(ctx->buffer == NULL) { \
332       noitL(noit_error, "malloc(%lu) failed.\n", (long unsigned int)size + 1); \
333       goto socket_error; \
334     } \
335     ctx->buffer[size] = '\0'; \
336   } \
337   len = __read_on_ctx(e, ctx, &mask); \
338   if(len < 0) { \
339     if(errno == EAGAIN) return mask | EVENTER_EXCEPTION; \
340     noitL(noit_error, "[%s] SSL read error: %s\n", nctx->remote_str, strerror(errno)); \
341     goto socket_error; \
342   } \
343   ctx->bytes_read = 0; \
344   ctx->bytes_expected = 0; \
345   if(len != size) { \
346     noitL(noit_error, "[%s] SSL short read [%d] (%d/%lu).  Reseting connection.\n", \
347           nctx->remote_str, ctx->state, len, (long unsigned int)size); \
348     goto socket_error; \
349   } \
350 } while(0)
351
352 int
353 noit_connection_session_timeout(eventer_t e, int mask, void *closure,
354                                 struct timeval *now) {
355   noit_connection_ctx_t *nctx = closure;
356   eventer_t fde = nctx->e;
357   nctx->timeout_event = NULL;
358   noitL(noit_error, "Timing out jlog session: %s\n",
359         nctx->remote_cn ? nctx->remote_cn : "(null)");
360   if(fde)
361     eventer_trigger(fde, EVENTER_EXCEPTION);
362   return 0;
363 }
364 int
365 stratcon_jlog_recv_handler(eventer_t e, int mask, void *closure,
366                            struct timeval *now) {
367   noit_connection_ctx_t *nctx = closure;
368   jlog_streamer_ctx_t *ctx = nctx->consumer_ctx;
369   jlog_streamer_ctx_t dummy;
370   int len;
371   jlog_id n_chkpt;
372
373   if(mask & EVENTER_EXCEPTION || nctx->wants_shutdown) {
374     if(write(e->fd, e, 0) == -1)
375       noitL(noit_error, "socket error: %s\n", strerror(errno));
376  socket_error:
377     ctx->state = JLOG_STREAMER_WANT_INITIATE;
378     ctx->count = 0;
379     ctx->needs_chkpt = 0;
380     ctx->bytes_read = 0;
381     ctx->bytes_expected = 0;
382     if(ctx->buffer) free(ctx->buffer);
383     ctx->buffer = NULL;
384     noit_connection_schedule_reattempt(nctx, now);
385     eventer_remove_fd(e->fd);
386     nctx->e = NULL;
387     e->opset->close(e->fd, &mask, e);
388     return 0;
389   }
390
391   noit_connection_update_timeout(nctx);
392   while(1) {
393     switch(ctx->state) {
394       case JLOG_STREAMER_WANT_INITIATE:
395         len = e->opset->write(e->fd, &ctx->jlog_feed_cmd,
396                               sizeof(ctx->jlog_feed_cmd),
397                               &mask, e);
398         if(len < 0) {
399           if(errno == EAGAIN) return mask | EVENTER_EXCEPTION;
400           goto socket_error;
401         }
402         if(len != sizeof(ctx->jlog_feed_cmd)) {
403           noitL(noit_error, "short write [%d/%d] on initiating stream.\n",
404                 (int)len, (int)sizeof(ctx->jlog_feed_cmd));
405           goto socket_error;
406         }
407         ctx->state = JLOG_STREAMER_WANT_COUNT;
408         break;
409
410       case JLOG_STREAMER_WANT_ERROR:
411         FULLREAD(e, ctx, 0 - ctx->count);
412         noitL(noit_error, "[%s] %.*s\n", nctx->remote_str,
413               0 - ctx->count, ctx->buffer);
414         free(ctx->buffer); ctx->buffer = NULL;
415         goto socket_error;
416         break;
417
418       case JLOG_STREAMER_WANT_COUNT:
419         FULLREAD(e, ctx, sizeof(u_int32_t));
420         memcpy(&dummy.count, ctx->buffer, sizeof(u_int32_t));
421         ctx->count = ntohl(dummy.count);
422         ctx->needs_chkpt = 0;
423         free(ctx->buffer); ctx->buffer = NULL;
424         if(ctx->count < 0)
425           ctx->state = JLOG_STREAMER_WANT_ERROR;
426         else
427           ctx->state = JLOG_STREAMER_WANT_HEADER;
428         break;
429
430       case JLOG_STREAMER_WANT_HEADER:
431         if(ctx->count == 0) {
432           ctx->state = JLOG_STREAMER_WANT_COUNT;
433           break;
434         }
435         FULLREAD(e, ctx, sizeof(ctx->header));
436         memcpy(&dummy.header, ctx->buffer, sizeof(ctx->header));
437         ctx->header.chkpt.log = ntohl(dummy.header.chkpt.log);
438         ctx->header.chkpt.marker = ntohl(dummy.header.chkpt.marker);
439         ctx->header.tv_sec = ntohl(dummy.header.tv_sec);
440         ctx->header.tv_usec = ntohl(dummy.header.tv_usec);
441         ctx->header.message_len = ntohl(dummy.header.message_len);
442         free(ctx->buffer); ctx->buffer = NULL;
443         ctx->state = JLOG_STREAMER_WANT_BODY;
444         break;
445
446       case JLOG_STREAMER_WANT_BODY:
447         FULLREAD(e, ctx, (unsigned long)ctx->header.message_len);
448         if(ctx->header.message_len > 0) {
449           ctx->needs_chkpt = 1;
450           ctx->push(DS_OP_INSERT, &nctx->r.remote, nctx->remote_cn,
451                     ctx->buffer, NULL);
452         }
453         else if(ctx->buffer)
454           free(ctx->buffer);
455         /* Don't free the buffer, it's used by the datastore process. */
456         ctx->buffer = NULL;
457         ctx->count--;
458         ctx->total_events++;
459         if(ctx->count == 0 && ctx->needs_chkpt) {
460           eventer_t completion_e;
461           eventer_remove_fd(e->fd);
462           completion_e = eventer_alloc();
463           memcpy(completion_e, e, sizeof(*e));
464           nctx->e = completion_e;
465           completion_e->mask = EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION;
466           ctx->state = JLOG_STREAMER_IS_ASYNC;
467           ctx->push(DS_OP_CHKPT, &nctx->r.remote, nctx->remote_cn,
468                     NULL, completion_e);
469           noitL(noit_debug, "Pushing %s batch async [%s]: [%u/%u]\n",
470                 feed_type_to_str(ntohl(ctx->jlog_feed_cmd)),
471                 nctx->remote_cn ? nctx->remote_cn : "(null)",
472                 ctx->header.chkpt.log, ctx->header.chkpt.marker);
473           noit_connection_disable_timeout(nctx);
474           return 0;
475         }
476         else if(ctx->count == 0)
477           ctx->state = JLOG_STREAMER_WANT_CHKPT;
478         else
479           ctx->state = JLOG_STREAMER_WANT_HEADER;
480         break;
481
482       case JLOG_STREAMER_IS_ASYNC:
483         ctx->state = JLOG_STREAMER_WANT_CHKPT; /* falls through */
484       case JLOG_STREAMER_WANT_CHKPT:
485         noitL(noit_debug, "Pushing %s checkpoint [%s]: [%u/%u]\n",
486               feed_type_to_str(ntohl(ctx->jlog_feed_cmd)),
487               nctx->remote_cn ? nctx->remote_cn : "(null)",
488               ctx->header.chkpt.log, ctx->header.chkpt.marker);
489         n_chkpt.log = htonl(ctx->header.chkpt.log);
490         n_chkpt.marker = htonl(ctx->header.chkpt.marker);
491
492         /* screw short writes.  I'd rather die than not write my data! */
493         len = e->opset->write(e->fd, &n_chkpt, sizeof(jlog_id),
494                               &mask, e);
495         if(len < 0) {
496           if(errno == EAGAIN) return mask | EVENTER_EXCEPTION;
497           goto socket_error;
498         }
499         if(len != sizeof(jlog_id)) {
500           noitL(noit_error, "short write on checkpointing stream.\n");
501           goto socket_error;
502         }
503         ctx->state = JLOG_STREAMER_WANT_COUNT;
504         break;
505     }
506   }
507   /* never get here */
508 }
509
510 int
511 noit_connection_ssl_upgrade(eventer_t e, int mask, void *closure,
512                             struct timeval *now) {
513   noit_connection_ctx_t *nctx = closure;
514   int rv;
515   const char *error = NULL;
516
517   rv = eventer_SSL_connect(e, &mask);
518   if(rv > 0) {
519     eventer_ssl_ctx_t *sslctx;
520     e->callback = nctx->consumer_callback;
521     /* We must make a copy of the acceptor_closure_t for each new
522      * connection.
523      */
524     if((sslctx = eventer_get_eventer_ssl_ctx(e)) != NULL) {
525       const char *cn, *end;
526       void *vcn;
527       cn = eventer_ssl_get_peer_subject(sslctx);
528       if(cn && (cn = strstr(cn, "CN=")) != NULL) {
529         cn += 3;
530         end = cn;
531         while(*end && *end != '/') end++;
532         nctx->remote_cn = malloc(end - cn + 1);
533         memcpy(nctx->remote_cn, cn, end - cn);
534         nctx->remote_cn[end-cn] = '\0';
535       }
536       if(nctx->config &&
537          noit_hash_retrieve(nctx->config, "cn", 2, &vcn)) {
538         const char *cn_expected = vcn;
539         if(!nctx->remote_cn ||
540            strcmp(nctx->remote_cn, cn_expected)) {
541           error = "jlog connect CN mismatch\n";
542           goto error;
543         }
544       }
545     }
546     return e->callback(e, mask, e->closure, now);
547   }
548   if(errno == EAGAIN) return mask | EVENTER_EXCEPTION;
549   noitL(noit_debug, "jlog streamer SSL upgrade failed.\n");
550
551  error:
552   if(error) noitL(noit_error, "%s", error);
553   eventer_remove_fd(e->fd);
554   nctx->e = NULL;
555   e->opset->close(e->fd, &mask, e);
556   noit_connection_schedule_reattempt(nctx, now);
557   return 0;
558 }
559 int
560 noit_connection_complete_connect(eventer_t e, int mask, void *closure,
561                                  struct timeval *now) {
562   noit_connection_ctx_t *nctx = closure;
563   const char *cert, *key, *ca, *ciphers, *crl = NULL;
564   char remote_str[128], tmp_str[128];
565   eventer_ssl_ctx_t *sslctx;
566   int aerrno, len;
567   socklen_t aerrno_len = sizeof(aerrno);
568
569   if(getsockopt(e->fd,SOL_SOCKET,SO_ERROR, &aerrno, &aerrno_len) == 0)
570     if(aerrno != 0) goto connect_error;
571   aerrno = 0;
572
573   if(mask & EVENTER_EXCEPTION) {
574     if(aerrno == 0 && (write(e->fd, e, 0) == -1))
575       aerrno = errno;
576  connect_error:
577     switch(nctx->r.remote.sa_family) {
578       case AF_INET:
579         len = sizeof(struct sockaddr_in);
580         inet_ntop(nctx->r.remote.sa_family, &nctx->r.remote_in.sin_addr,
581                   tmp_str, len);
582         snprintf(remote_str, sizeof(remote_str), "%s:%d",
583                  tmp_str, ntohs(nctx->r.remote_in.sin_port));
584         break;
585       case AF_INET6:
586         len = sizeof(struct sockaddr_in6);
587         inet_ntop(nctx->r.remote.sa_family, &nctx->r.remote_in6.sin6_addr,
588                   tmp_str, len);
589         snprintf(remote_str, sizeof(remote_str), "%s:%d",
590                  tmp_str, ntohs(nctx->r.remote_in6.sin6_port));
591        break;
592       case AF_UNIX:
593         snprintf(remote_str, sizeof(remote_str), "%s", nctx->r.remote_un.sun_path);
594         break;
595       default:
596         snprintf(remote_str, sizeof(remote_str), "(unknown)");
597     }
598     noitL(noit_error, "Error connecting to %s: %s\n",
599           remote_str, strerror(aerrno));
600     eventer_remove_fd(e->fd);
601     nctx->e = NULL;
602     e->opset->close(e->fd, &mask, e);
603     noit_connection_schedule_reattempt(nctx, now);
604     return 0;
605   }
606
607 #define SSLCONFGET(var,name) do { \
608   if(!noit_hash_retr_str(nctx->sslconfig, name, strlen(name), \
609                          &var)) var = NULL; } while(0)
610   SSLCONFGET(cert, "certificate_file");
611   SSLCONFGET(key, "key_file");
612   SSLCONFGET(ca, "ca_chain");
613   SSLCONFGET(ciphers, "ciphers");
614   SSLCONFGET(crl, "crl");
615   sslctx = eventer_ssl_ctx_new(SSL_CLIENT, cert, key, ca, ciphers);
616   if(!sslctx) goto connect_error;
617   if(crl) {
618     if(!eventer_ssl_use_crl(sslctx, crl)) {
619       noitL(noit_error, "Failed to load CRL from %s\n", crl);
620       eventer_ssl_ctx_free(sslctx);
621       goto connect_error;
622     }
623   }
624
625   memcpy(&nctx->last_connect, now, sizeof(*now));
626   eventer_ssl_ctx_set_verify(sslctx, eventer_ssl_verify_cert,
627                              nctx->sslconfig);
628   EVENTER_ATTACH_SSL(e, sslctx);
629   e->callback = noit_connection_ssl_upgrade;
630   return e->callback(e, mask, closure, now);
631 }
632 static void
633 noit_connection_initiate_connection(noit_connection_ctx_t *nctx) {
634   struct timeval __now;
635   eventer_t e;
636   int rv, fd = -1;
637 #ifdef SO_KEEPALIVE
638   int optval;
639   socklen_t optlen = sizeof(optval);
640 #endif
641
642   nctx->e = NULL;
643   if(nctx->wants_permanent_shutdown) {
644     noit_connection_ctx_dealloc(nctx);
645     return;
646   }
647   /* Open a socket */
648   fd = socket(nctx->r.remote.sa_family, SOCK_STREAM, 0);
649   if(fd < 0) goto reschedule;
650
651   /* Make it non-blocking */
652   if(eventer_set_fd_nonblocking(fd)) goto reschedule;
653 #define set_or_bail(type, opt, val) do { \
654   optval = val; \
655   optlen = sizeof(optval); \
656   if(setsockopt(fd, type, opt, &optval, optlen) < 0) { \
657     noitL(noit_error, "Cannot set " #type "/" #opt " on jlog socket: %s\n", \
658           strerror(errno)); \
659     goto reschedule; \
660   } \
661 } while(0)
662 #ifdef SO_KEEPALIVE
663   set_or_bail(SOL_SOCKET, SO_KEEPALIVE, 1);
664 #endif
665 #ifdef TCP_KEEPALIVE_THRESHOLD
666   set_or_bail(IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, 10 * 1000);
667 #endif
668 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
669   set_or_bail(IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, 30 * 1000);
670 #endif
671 #ifdef TCP_CONN_NOTIFY_THRESHOLD
672   set_or_bail(IPPROTO_TCP, TCP_CONN_NOTIFY_THRESHOLD, 10 * 1000);
673 #endif
674 #ifdef TCP_CONN_ABORT_THRESHOLD
675   set_or_bail(IPPROTO_TCP, TCP_CONN_ABORT_THRESHOLD, 30 * 1000);
676 #endif
677
678   /* Initiate a connection */
679   rv = connect(fd, &nctx->r.remote, nctx->remote_len);
680   if(rv == -1 && errno != EINPROGRESS) goto reschedule;
681
682   /* Register a handler for connection completion */
683   e = eventer_alloc();
684   e->fd = fd;
685   e->mask = EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION;
686   e->callback = noit_connection_complete_connect;
687   e->closure = nctx;
688   nctx->e = e;
689   eventer_add(e);
690
691   noit_connection_update_timeout(nctx);
692   return;
693
694  reschedule:
695   if(fd >= 0) close(fd);
696   gettimeofday(&__now, NULL);
697   noit_connection_schedule_reattempt(nctx, &__now);
698   return;
699 }
700
701 int
702 noit_connection_update_timeout(noit_connection_ctx_t *nctx) {
703   struct timeval now, diff;
704   if(nctx->max_silence == 0) return 0;
705
706   diff.tv_sec = nctx->max_silence / 1000;
707   diff.tv_usec = (nctx->max_silence % 1000) * 1000;
708   gettimeofday(&now, NULL);
709
710   if(!nctx->timeout_event) {
711     nctx->timeout_event = eventer_alloc();
712     nctx->timeout_event->mask = EVENTER_TIMER;
713     nctx->timeout_event->closure = nctx;
714     nctx->timeout_event->callback = noit_connection_session_timeout;
715     add_timeval(now, diff, &nctx->timeout_event->whence);
716     eventer_add(nctx->timeout_event);
717   }
718   else {
719     add_timeval(now, diff, &nctx->timeout_event->whence);
720     eventer_update(nctx->timeout_event, EVENTER_TIMER);
721   }
722   return 0;
723 }
724
725 int
726 noit_connection_disable_timeout(noit_connection_ctx_t *nctx) {
727   if(nctx->timeout_event) {
728     eventer_remove(nctx->timeout_event);
729     eventer_free(nctx->timeout_event);
730     nctx->timeout_event = NULL;
731   }
732   return 0;
733 }
734
735 int
736 initiate_noit_connection(const char *host, unsigned short port,
737                          noit_hash_table *sslconfig, noit_hash_table *config,
738                          eventer_func_t handler, void *closure,
739                          void (*freefunc)(void *)) {
740   noit_connection_ctx_t *ctx;
741   const char *stimeout;
742   int8_t family;
743   int rv;
744   union {
745     struct in_addr addr4;
746     struct in6_addr addr6;
747   } a;
748
749   if(host[0] == '/') {
750     family = AF_UNIX;
751   }
752   else {
753     family = AF_INET;
754     rv = inet_pton(family, host, &a);
755     if(rv != 1) {
756       family = AF_INET6;
757       rv = inet_pton(family, host, &a);
758       if(rv != 1) {
759         noitL(noit_stderr, "Cannot translate '%s' to IP\n", host);
760         return -1;
761       }
762     }
763   }
764
765   ctx = noit_connection_ctx_alloc();
766   ctx->remote_str = calloc(1, strlen(host) + 7);
767   snprintf(ctx->remote_str, strlen(host) + 7,
768            "%s:%d", host, port);
769  
770   memset(&ctx->r, 0, sizeof(ctx->r));
771   if(family == AF_UNIX) {
772     struct sockaddr_un *s = &ctx->r.remote_un;
773     s->sun_family = AF_UNIX;
774     strncpy(s->sun_path, host, sizeof(s->sun_path)-1);
775     ctx->remote_len = sizeof(*s);
776   }
777   else if(family == AF_INET) {
778     struct sockaddr_in *s = &ctx->r.remote_in;
779     s->sin_family = family;
780     s->sin_port = htons(port);
781     memcpy(&s->sin_addr, &a, sizeof(struct in_addr));
782     ctx->remote_len = sizeof(*s);
783   }
784   else {
785     struct sockaddr_in6 *s = &ctx->r.remote_in6;
786     s->sin6_family = family;
787     s->sin6_port = htons(port);
788     memcpy(&s->sin6_addr, &a, sizeof(a));
789     ctx->remote_len = sizeof(*s);
790   }
791
792   if(ctx->sslconfig)
793     noit_hash_delete_all(ctx->sslconfig, free, free);
794   else
795     ctx->sslconfig = calloc(1, sizeof(noit_hash_table));
796   noit_hash_merge_as_dict(ctx->sslconfig, sslconfig);
797   if(ctx->config)
798     noit_hash_delete_all(ctx->config, free, free);
799   else
800     ctx->config = calloc(1, sizeof(noit_hash_table));
801   noit_hash_merge_as_dict(ctx->config, config);
802
803   if(noit_hash_retr_str(ctx->config, "timeout", strlen("timeout"), &stimeout))
804     ctx->max_silence = atoi(stimeout);
805   else
806     ctx->max_silence = DEFAULT_NOIT_CONNECTION_TIMEOUT;
807   ctx->consumer_callback = handler;
808   ctx->consumer_free = freefunc;
809   ctx->consumer_ctx = closure;
810   noit_connection_initiate_connection(ctx);
811   return 0;
812 }
813
814 void
815 stratcon_streamer_connection(const char *toplevel, const char *destination,
816                              eventer_func_t handler,
817                              void *(*handler_alloc)(void), void *handler_ctx,
818                              void (*handler_free)(void *)) {
819   int i, cnt = 0;
820   noit_conf_section_t *noit_configs;
821   char path[256];
822
823   snprintf(path, sizeof(path), "/%s/noits//noit", toplevel ? toplevel : "*");
824   noit_configs = noit_conf_get_sections(NULL, path, &cnt);
825   noitL(noit_error, "Found %d %s stanzas\n", cnt, path);
826   for(i=0; i<cnt; i++) {
827     char address[256];
828     unsigned short port;
829     int portint;
830     noit_hash_table *sslconfig, *config;
831
832     if(!noit_conf_get_stringbuf(noit_configs[i],
833                                 "ancestor-or-self::node()/@address",
834                                 address, sizeof(address))) {
835       noitL(noit_error, "address attribute missing in noit %d\n", i+1);
836       continue;
837     }
838     /* if destination is specified, exact match it */
839     if(destination && strcmp(address, destination)) continue;
840
841     if(!noit_conf_get_int(noit_configs[i],
842                           "ancestor-or-self::node()/@port", &portint))
843       portint = 0;
844     port = (unsigned short) portint;
845     if(address[0] != '/' && (portint == 0 || (port != portint))) {
846       /* UNIX sockets don't require a port (they'll ignore it if specified */
847       noitL(noit_stderr,
848             "Invalid port [%d] specified in stanza %d\n", port, i+1);
849       continue;
850     }
851     sslconfig = noit_conf_get_hash(noit_configs[i], "sslconfig");
852     config = noit_conf_get_hash(noit_configs[i], "config");
853
854     noitL(noit_error, "initiating to %s\n", address);
855     initiate_noit_connection(address, port, sslconfig, config,
856                              handler,
857                              handler_alloc ? handler_alloc() : handler_ctx,
858                              handler_free);
859     noit_hash_destroy(sslconfig,free,free);
860     free(sslconfig);
861     noit_hash_destroy(config,free,free);
862     free(config);
863   }
864   free(noit_configs);
865 }
866 int
867 stratcon_find_noit_ip_by_cn(const char *cn, char *ip, int len) {
868   int rv = -1;
869   void *vip;
870   pthread_mutex_lock(&noit_ip_by_cn_lock);
871   if(noit_hash_retrieve(&noit_ip_by_cn, cn, strlen(cn), &vip)) {
872     int new_len;
873     char *new_ip = (char *)vip;
874     new_len = strlen(new_ip);
875     strlcpy(ip, new_ip, len);
876     if(new_len >= len) rv = new_len+1;
877     else rv = 0;
878   }
879   pthread_mutex_unlock(&noit_ip_by_cn_lock);
880   return rv;
881 }
882 void
883 stratcon_jlog_streamer_recache_noit() {
884   int di, cnt;
885   noit_conf_section_t *noit_configs;
886   noit_configs = noit_conf_get_sections(NULL, "//noits//noit", &cnt);
887   pthread_mutex_lock(&noit_ip_by_cn_lock);
888   noit_hash_delete_all(&noit_ip_by_cn, free, free);
889   for(di=0; di<cnt; di++) {
890     char address[64];
891     if(noit_conf_get_stringbuf(noit_configs[di], "self::node()/@address",
892                                  address, sizeof(address))) {
893       char expected_cn[256];
894       if(noit_conf_get_stringbuf(noit_configs[di], "self::node()/config/cn",
895                                  expected_cn, sizeof(expected_cn)))
896         noit_hash_store(&noit_ip_by_cn,
897                         strdup(expected_cn), strlen(expected_cn),
898                         strdup(address));
899     }
900   }
901   free(noit_configs);
902   pthread_mutex_unlock(&noit_ip_by_cn_lock);
903 }
904 void
905 stratcon_jlog_streamer_reload(const char *toplevel) {
906   /* flush and repopulate the cn cache */
907   stratcon_jlog_streamer_recache_noit();
908   if(!stratcon_datastore_get_enabled()) return;
909   stratcon_streamer_connection(toplevel, NULL,
910                                stratcon_jlog_recv_handler,
911                                (void *(*)())stratcon_jlog_streamer_datastore_ctx_alloc,
912                                NULL,
913                                jlog_streamer_ctx_free);
914 }
915
916 static int
917 stratcon_console_show_noits(noit_console_closure_t ncct,
918                             int argc, char **argv,
919                             noit_console_state_t *dstate,
920                             void *closure) {
921   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
922   const char *key_id;
923   int klen, n = 0, i;
924   void *vconn;
925   noit_connection_ctx_t **ctx;
926
927   pthread_mutex_lock(&noits_lock);
928   ctx = malloc(sizeof(*ctx) * noits.size);
929   while(noit_hash_next(&noits, &iter, &key_id, &klen,
930                        &vconn)) {
931     ctx[n] = (noit_connection_ctx_t *)vconn;
932     noit_atomic_inc32(&ctx[n]->refcnt);
933     n++;
934   }
935   pthread_mutex_unlock(&noits_lock);
936   qsort(ctx, n, sizeof(*ctx), remote_str_sort);
937   for(i=0; i<n; i++) {
938     nc_print_noit_conn_brief(ncct, ctx[i]);
939     noit_connection_ctx_deref(ctx[i]);
940   }
941   free(ctx);
942   return 0;
943 }
944
945 static void
946 emit_noit_info_metrics(struct timeval *now, const char *uuid_str,
947                        noit_connection_ctx_t *nctx) {
948   struct timeval last, session_duration, diff;
949   u_int64_t session_duration_ms, last_event_ms;
950   jlog_streamer_ctx_t *jctx = nctx->consumer_ctx;
951   char str[1024], *wr;
952   int len;
953   void *vcn;
954   const char *cn_expected;
955   const char *feedtype = "unknown";
956
957   if(jctx->push == stratcon_datastore_push)
958     feedtype = "storage";
959   else if(jctx->push == stratcon_iep_line_processor)
960     feedtype = "iep";
961   if(NULL != (wr = strchr(feedtype, '/'))) feedtype = wr+1;
962
963   noit_hash_retrieve(nctx->config, "cn", 2, &vcn);
964   if(!vcn) return;
965   cn_expected = vcn;
966
967   snprintf(str, sizeof(str), "M\t%lu.%03lu\t%s\t%s`%s`",
968            (long unsigned int)now->tv_sec,
969            (long unsigned int)now->tv_usec/1000UL,
970            uuid_str, cn_expected, feedtype);
971   wr = str + strlen(str);
972   len = sizeof(str) - (wr - str);
973
974   /* Now we write NAME TYPE VALUE into wr each time and push it */
975 #define push_noit_m_str(name, value) do { \
976   snprintf(wr, len, "%s\ts\t%s\n", name, value); \
977   stratcon_datastore_push(DS_OP_INSERT, \
978                           (struct sockaddr *)&self_stratcon_ip, \
979                           self_stratcon_hostname, strdup(str), NULL); \
980   stratcon_iep_line_processor(DS_OP_INSERT, \
981                               (struct sockaddr *)&self_stratcon_ip, \
982                               self_stratcon_hostname, strdup(str), NULL); \
983 } while(0)
984 #define push_noit_m_u64(name, value) do { \
985   snprintf(wr, len, "%s\tL\t%llu\n", name, (long long unsigned int)value); \
986   stratcon_datastore_push(DS_OP_INSERT, \
987                           (struct sockaddr *)&self_stratcon_ip, \
988                           self_stratcon_hostname, strdup(str), NULL); \
989   stratcon_iep_line_processor(DS_OP_INSERT, \
990                               (struct sockaddr *)&self_stratcon_ip, \
991                               self_stratcon_hostname, strdup(str), NULL); \
992 } while(0)
993
994   last.tv_sec = jctx->header.tv_sec;
995   last.tv_usec = jctx->header.tv_usec;
996   sub_timeval(*now, last, &diff);
997   last_event_ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
998   sub_timeval(*now, nctx->last_connect, &session_duration);
999   session_duration_ms = session_duration.tv_sec * 1000 +
1000                         session_duration.tv_usec / 1000;
1001
1002   push_noit_m_str("state", nctx->remote_cn ? "connected" :
1003                              (nctx->retry_event ? "disconnected" :
1004                                                   "connecting"));
1005   push_noit_m_u64("last_event_age_ms", last_event_ms);
1006   push_noit_m_u64("session_length_ms", last_event_ms);
1007 }
1008 static int
1009 periodic_noit_metrics(eventer_t e, int mask, void *closure,
1010                       struct timeval *now) {
1011   struct timeval whence = DEFAULT_NOIT_PERIOD_TV;
1012   noit_connection_ctx_t **ctxs;
1013   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
1014   const char *key_id;
1015   void *vconn;
1016   int klen, n = 0, i;
1017   char str[1024];
1018   char uuid_str[UUID_STR_LEN+1];
1019
1020   uuid_unparse_lower(self_stratcon_id, uuid_str);
1021
1022   if(closure == NULL) {
1023     /* Only do this the first time we get called */
1024     char ip_str[128];
1025     inet_ntop(AF_INET, &self_stratcon_ip.sin_addr, ip_str,
1026               sizeof(ip_str));
1027     snprintf(str, sizeof(str), "C\t%lu.%03lu\t%s\t%s\tstratcon\t%s\n",
1028              (long unsigned int)now->tv_sec,
1029              (long unsigned int)now->tv_usec/1000UL, uuid_str, ip_str,
1030              self_stratcon_hostname);
1031     stratcon_datastore_push(DS_OP_INSERT,
1032                             (struct sockaddr *)&self_stratcon_ip,
1033                             self_stratcon_hostname, strdup(str), NULL);
1034     stratcon_iep_line_processor(DS_OP_INSERT,
1035                                 (struct sockaddr *)&self_stratcon_ip,
1036                                 self_stratcon_hostname, strdup(str), NULL);
1037   }
1038
1039   pthread_mutex_lock(&noits_lock);
1040   ctxs = malloc(sizeof(*ctxs) * noits.size);
1041   while(noit_hash_next(&noits, &iter, &key_id, &klen,
1042                        &vconn)) {
1043     ctxs[n] = (noit_connection_ctx_t *)vconn;
1044     noit_atomic_inc32(&ctxs[n]->refcnt);
1045     n++;
1046   }
1047   pthread_mutex_unlock(&noits_lock);
1048
1049   snprintf(str, sizeof(str), "S\t%lu.%03lu\t%s\tG\tA\t0\tok\n",
1050            (long unsigned int)now->tv_sec,
1051            (long unsigned int)now->tv_usec/1000UL, uuid_str);
1052   stratcon_datastore_push(DS_OP_INSERT,
1053                           (struct sockaddr *)&self_stratcon_ip,
1054                           self_stratcon_hostname, strdup(str), NULL);
1055   stratcon_iep_line_processor(DS_OP_INSERT, \
1056                               (struct sockaddr *)&self_stratcon_ip, \
1057                               self_stratcon_hostname, strdup(str), NULL); \
1058   for(i=0; i<n; i++) {
1059     emit_noit_info_metrics(now, uuid_str, ctxs[i]);
1060     noit_connection_ctx_deref(ctxs[i]);
1061   }
1062   free(ctxs);
1063   stratcon_datastore_push(DS_OP_CHKPT,
1064                           (struct sockaddr *)&self_stratcon_ip,
1065                           self_stratcon_hostname, NULL, NULL);
1066   stratcon_iep_line_processor(DS_OP_CHKPT, \
1067                               (struct sockaddr *)&self_stratcon_ip, \
1068                               self_stratcon_hostname, NULL, NULL); \
1069
1070   add_timeval(e->whence, whence, &whence);
1071   eventer_add_at(periodic_noit_metrics, (void *)0x1, whence);
1072   return 0;
1073 }
1074
1075 static int
1076 rest_show_noits(noit_http_rest_closure_t *restc,
1077                 int npats, char **pats) {
1078   xmlDocPtr doc;
1079   xmlNodePtr root;
1080   noit_hash_table seen = NOIT_HASH_EMPTY;
1081   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
1082   char path[256];
1083   const char *key_id;
1084   const char *type = NULL, *want_cn = NULL;
1085   int klen, n = 0, i, di, cnt;
1086   void *vconn;
1087   noit_connection_ctx_t **ctxs;
1088   noit_conf_section_t *noit_configs;
1089   struct timeval now, diff, last;
1090   xmlNodePtr node;
1091   noit_http_request *req = noit_http_session_request(restc->http_ctx);
1092
1093   noit_http_process_querystring(req);
1094   type = noit_http_request_querystring(req, "type");
1095   want_cn = noit_http_request_querystring(req, "cn");
1096
1097   gettimeofday(&now, NULL);
1098
1099   pthread_mutex_lock(&noits_lock);
1100   ctxs = malloc(sizeof(*ctxs) * noits.size);
1101   while(noit_hash_next(&noits, &iter, &key_id, &klen,
1102                        &vconn)) {
1103     ctxs[n] = (noit_connection_ctx_t *)vconn;
1104     noit_atomic_inc32(&ctxs[n]->refcnt);
1105     n++;
1106   }
1107   pthread_mutex_unlock(&noits_lock);
1108   qsort(ctxs, n, sizeof(*ctxs), remote_str_sort);
1109
1110   doc = xmlNewDoc((xmlChar *)"1.0");
1111   root = xmlNewDocNode(doc, NULL, (xmlChar *)"noits", NULL);
1112   xmlDocSetRootElement(doc, root);
1113
1114   for(i=0; i<n; i++) {
1115     char buff[256];
1116     const char *feedtype = "unknown", *state = "unknown";
1117     noit_connection_ctx_t *ctx = ctxs[i];
1118     jlog_streamer_ctx_t *jctx = ctx->consumer_ctx;
1119
1120     feedtype = feed_type_to_str(ntohl(jctx->jlog_feed_cmd));
1121
1122     /* If the user requested a specific type and we're not it, skip. */
1123     if(type && strcmp(feedtype, type)) continue;
1124     /* If the user wants a specific CN... limit to that. */
1125     if(want_cn && (!ctx->remote_cn || strcmp(want_cn, ctx->remote_cn)))
1126       continue;
1127
1128     node = xmlNewNode(NULL, (xmlChar *)"noit");
1129     snprintf(buff, sizeof(buff), "%llu.%06d",
1130              (long long unsigned)ctx->last_connect.tv_sec,
1131              (int)ctx->last_connect.tv_usec);
1132     xmlSetProp(node, (xmlChar *)"last_connect", (xmlChar *)buff);
1133     xmlSetProp(node, (xmlChar *)"state", ctx->remote_cn ?
1134                (xmlChar *)"connected" :
1135                (ctx->retry_event ? (xmlChar *)"disconnected" :
1136                                     (xmlChar *)"connecting"));
1137     if(ctx->e) {
1138       char buff[128];
1139       const char *addrstr = NULL;
1140       struct sockaddr_in6 addr6;
1141       socklen_t len = sizeof(addr6);
1142       if(getsockname(ctx->e->fd, (struct sockaddr *)&addr6, &len) == 0) {
1143         unsigned short port = 0;
1144         if(addr6.sin6_family == AF_INET) {
1145           addrstr = inet_ntop(addr6.sin6_family,
1146                               &((struct sockaddr_in *)&addr6)->sin_addr,
1147                               buff, sizeof(buff));
1148           memcpy(&port, &((struct sockaddr_in *)&addr6)->sin_port, sizeof(port));
1149           port = ntohs(port);
1150         }
1151         else if(addr6.sin6_family == AF_INET6) {
1152           addrstr = inet_ntop(addr6.sin6_family, &addr6.sin6_addr,
1153                               buff, sizeof(buff));
1154           port = ntohs(addr6.sin6_port);
1155         }
1156         if(addrstr != NULL) {
1157           snprintf(buff + strlen(buff), sizeof(buff) - strlen(buff),
1158                    ":%u", port);
1159           xmlSetProp(node, (xmlChar *)"local", (xmlChar *)buff);
1160         }
1161       }
1162     }
1163     noit_hash_replace(&seen, strdup(ctx->remote_str), strlen(ctx->remote_str),
1164                       0, free, NULL);
1165     xmlSetProp(node, (xmlChar *)"remote", (xmlChar *)ctx->remote_str);
1166     xmlSetProp(node, (xmlChar *)"type", (xmlChar *)feedtype);
1167     if(ctx->retry_event) {
1168       sub_timeval(ctx->retry_event->whence, now, &diff);
1169       snprintf(buff, sizeof(buff), "%llu.%06d",
1170                (long long unsigned)diff.tv_sec, (int)diff.tv_usec);
1171       xmlSetProp(node, (xmlChar *)"next_attempt", (xmlChar *)buff);
1172     }
1173     else if(ctx->remote_cn) {
1174       if(ctx->remote_cn)
1175         xmlSetProp(node, (xmlChar *)"remote_cn", (xmlChar *)ctx->remote_cn);
1176  
1177       switch(jctx->state) {
1178         case JLOG_STREAMER_WANT_INITIATE: state = "initiate"; break;
1179         case JLOG_STREAMER_WANT_COUNT: state = "waiting for next batch"; break;
1180         case JLOG_STREAMER_WANT_ERROR: state = "waiting for error"; break;
1181         case JLOG_STREAMER_WANT_HEADER: state = "reading header"; break;
1182         case JLOG_STREAMER_WANT_BODY: state = "reading body"; break;
1183         case JLOG_STREAMER_IS_ASYNC: state = "asynchronously processing"; break;
1184         case JLOG_STREAMER_WANT_CHKPT: state = "checkpointing"; break;
1185       }
1186       xmlSetProp(node, (xmlChar *)"state", (xmlChar *)state);
1187       snprintf(buff, sizeof(buff), "%08x:%08x",
1188                jctx->header.chkpt.log, jctx->header.chkpt.marker);
1189       xmlSetProp(node, (xmlChar *)"checkpoint", (xmlChar *)buff);
1190       snprintf(buff, sizeof(buff), "%llu",
1191                (unsigned long long)jctx->total_events);
1192       xmlSetProp(node, (xmlChar *)"session_events", (xmlChar *)buff);
1193       snprintf(buff, sizeof(buff), "%llu",
1194                (unsigned long long)jctx->total_bytes_read);
1195       xmlSetProp(node, (xmlChar *)"session_bytes", (xmlChar *)buff);
1196  
1197       sub_timeval(now, ctx->last_connect, &diff);
1198       snprintf(buff, sizeof(buff), "%lld.%06d",
1199                (long long)diff.tv_sec, (int)diff.tv_usec);
1200       xmlSetProp(node, (xmlChar *)"session_duration", (xmlChar *)buff);
1201  
1202       if(jctx->header.tv_sec) {
1203         last.tv_sec = jctx->header.tv_sec;
1204         last.tv_usec = jctx->header.tv_usec;
1205         snprintf(buff, sizeof(buff), "%llu.%06d",
1206                  (unsigned long long)last.tv_sec, (int)last.tv_usec);
1207         xmlSetProp(node, (xmlChar *)"last_event", (xmlChar *)buff);
1208         sub_timeval(now, last, &diff);
1209         snprintf(buff, sizeof(buff), "%lld.%06d",
1210                  (long long)diff.tv_sec, (int)diff.tv_usec);
1211         xmlSetProp(node, (xmlChar *)"last_event_age", (xmlChar *)buff);
1212       }
1213     }
1214
1215     xmlAddChild(root, node);
1216     noit_connection_ctx_deref(ctx);
1217   }
1218   free(ctxs);
1219
1220   if(!type || !strcmp(type, "configured")) {
1221     snprintf(path, sizeof(path), "//noits//noit");
1222     noit_configs = noit_conf_get_sections(NULL, path, &cnt);
1223     for(di=0; di<cnt; di++) {
1224       char address[64], port_str[32], remote_str[98];
1225       char expected_cn_buff[256], *expected_cn = NULL;
1226       if(noit_conf_get_stringbuf(noit_configs[di], "self::node()/config/cn",
1227                                  expected_cn_buff, sizeof(expected_cn_buff)))
1228         expected_cn = expected_cn_buff;
1229       if(want_cn && (!expected_cn || strcmp(want_cn, expected_cn))) continue;
1230       if(noit_conf_get_stringbuf(noit_configs[di], "self::node()/@address",
1231                                  address, sizeof(address))) {
1232         void *v;
1233         if(!noit_conf_get_stringbuf(noit_configs[di], "self::node()/@port",
1234                                    port_str, sizeof(port_str)))
1235           strlcpy(port_str, "43191", sizeof(port_str));
1236
1237         /* If the user wants a specific CN... limit to that. */
1238           if(want_cn && (!expected_cn || strcmp(want_cn, expected_cn)))
1239             continue;
1240
1241         snprintf(remote_str, sizeof(remote_str), "%s:%s", address, port_str);
1242         if(!noit_hash_retrieve(&seen, remote_str, strlen(remote_str), &v)) {
1243           node = xmlNewNode(NULL, (xmlChar *)"noit");
1244           xmlSetProp(node, (xmlChar *)"remote", (xmlChar *)remote_str);
1245           xmlSetProp(node, (xmlChar *)"type", (xmlChar *)"configured");
1246           if(expected_cn)
1247             xmlSetProp(node, (xmlChar *)"cn", (xmlChar *)expected_cn);
1248           xmlAddChild(root, node);
1249         }
1250       }
1251     }
1252     free(noit_configs);
1253   }
1254   noit_hash_destroy(&seen, free, NULL);
1255
1256   noit_http_response_ok(restc->http_ctx, "text/xml");
1257   noit_http_response_xml(restc->http_ctx, doc);
1258   noit_http_response_end(restc->http_ctx);
1259   xmlFreeDoc(doc);
1260   return 0;
1261 }
1262 static int
1263 stratcon_add_noit(const char *target, unsigned short port,
1264                   const char *cn) {
1265   int cnt;
1266   char path[256];
1267   char port_str[6];
1268   noit_conf_section_t *noit_configs, parent;
1269   xmlNodePtr newnoit, config, cnnode;
1270
1271   snprintf(path, sizeof(path),
1272            "//noits//noit[@address=\"%s\" and @port=\"%d\"]", target, port);
1273   noit_configs = noit_conf_get_sections(NULL, path, &cnt);
1274   free(noit_configs);
1275   if(cnt != 0) return -1;
1276
1277   parent = noit_conf_get_section(NULL, "//noits");
1278   if(!parent) return -1;
1279   snprintf(port_str, sizeof(port_str), "%d", port);
1280   newnoit = xmlNewNode(NULL, (xmlChar *)"noit");
1281   xmlSetProp(newnoit, (xmlChar *)"address", (xmlChar *)target);
1282   xmlSetProp(newnoit, (xmlChar *)"port", (xmlChar *)port_str);
1283   xmlAddChild(parent, newnoit);
1284   if(cn) {
1285     config = xmlNewNode(NULL, (xmlChar *)"config");
1286     cnnode = xmlNewNode(NULL, (xmlChar *)"cn");
1287     xmlNodeAddContent(cnnode, (xmlChar *)cn);
1288     xmlAddChild(config, cnnode);
1289     xmlAddChild(newnoit, config);
1290     pthread_mutex_lock(&noit_ip_by_cn_lock);
1291     noit_hash_replace(&noit_ip_by_cn, strdup(cn), strlen(cn),
1292                       strdup(target), free, free);
1293     pthread_mutex_unlock(&noit_ip_by_cn_lock);
1294   }
1295   if(stratcon_datastore_get_enabled())
1296     stratcon_streamer_connection(NULL, target,
1297                                  stratcon_jlog_recv_handler,
1298                                  (void *(*)())stratcon_jlog_streamer_datastore_ctx_alloc,
1299                                  NULL,
1300                                  jlog_streamer_ctx_free);
1301   if(stratcon_iep_get_enabled())
1302     stratcon_streamer_connection(NULL, target,
1303                                  stratcon_jlog_recv_handler,
1304                                  (void *(*)())stratcon_jlog_streamer_iep_ctx_alloc,
1305                                  NULL,
1306                                  jlog_streamer_ctx_free);
1307   return 1;
1308 }
1309 static int
1310 stratcon_remove_noit(const char *target, unsigned short port) {
1311   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
1312   const char *key_id;
1313   int klen, n = -1, i, cnt = 0;
1314   void *vconn;
1315   noit_connection_ctx_t **ctx;
1316   noit_conf_section_t *noit_configs;
1317   char path[256];
1318   char remote_str[256];
1319
1320   snprintf(remote_str, sizeof(remote_str), "%s:%d", target, port);
1321
1322   snprintf(path, sizeof(path),
1323            "//noits//noit[@address=\"%s\" and @port=\"%d\"]", target, port);
1324   noit_configs = noit_conf_get_sections(NULL, path, &cnt);
1325   for(i=0; i<cnt; i++) {
1326     char expected_cn[256];
1327     if(noit_conf_get_stringbuf(noit_configs[i], "self::node()/config/cn",
1328                                expected_cn, sizeof(expected_cn))) {
1329       pthread_mutex_lock(&noit_ip_by_cn_lock);
1330       noit_hash_delete(&noit_ip_by_cn, expected_cn, strlen(expected_cn),
1331                        free, free);
1332       pthread_mutex_unlock(&noit_ip_by_cn_lock);
1333     }
1334     xmlUnlinkNode(noit_configs[i]);
1335     xmlFreeNode(noit_configs[i]);
1336     n = 0;
1337   }
1338   free(noit_configs);
1339
1340   pthread_mutex_lock(&noits_lock);
1341   ctx = malloc(sizeof(*ctx) * noits.size);
1342   while(noit_hash_next(&noits, &iter, &key_id, &klen,
1343                        &vconn)) {
1344     if(!strcmp(((noit_connection_ctx_t *)vconn)->remote_str, remote_str)) {
1345       ctx[n] = (noit_connection_ctx_t *)vconn;
1346       noit_atomic_inc32(&ctx[n]->refcnt);
1347       n++;
1348     }
1349   }
1350   pthread_mutex_unlock(&noits_lock);
1351   for(i=0; i<n; i++) {
1352     noit_connection_ctx_dealloc(ctx[i]); /* once for the record */
1353     noit_connection_ctx_deref(ctx[i]);   /* once for the aboce inc32 */
1354   }
1355   free(ctx);
1356   return n;
1357 }
1358 static int
1359 rest_set_noit(noit_http_rest_closure_t *restc,
1360               int npats, char **pats) {
1361   const char *cn = NULL;
1362   noit_http_session_ctx *ctx = restc->http_ctx;
1363   noit_http_request *req = noit_http_session_request(ctx);
1364   unsigned short port = 43191;
1365   if(npats < 1 || npats > 2)
1366     noit_http_response_server_error(ctx, "text/xml");
1367   if(npats == 2) port = atoi(pats[1]);
1368   noit_http_process_querystring(req);
1369   cn = noit_http_request_querystring(req, "cn");
1370   if(stratcon_add_noit(pats[0], port, cn) >= 0)
1371     noit_http_response_ok(ctx, "text/xml");
1372   else
1373     noit_http_response_standard(ctx, 409, "EXISTS", "text/xml");
1374   if(noit_conf_write_file(NULL) != 0)
1375     noitL(noit_error, "local config write failed\n");
1376   noit_conf_mark_changed();
1377   noit_http_response_end(ctx);
1378   return 0;
1379 }
1380 static int
1381 rest_delete_noit(noit_http_rest_closure_t *restc,
1382                  int npats, char **pats) {
1383   noit_http_session_ctx *ctx = restc->http_ctx;
1384   unsigned short port = 43191;
1385   if(npats < 1 || npats > 2)
1386     noit_http_response_server_error(ctx, "text/xml");
1387   if(npats == 2) port = atoi(pats[1]);
1388   if(stratcon_remove_noit(pats[0], port) >= 0)
1389     noit_http_response_ok(ctx, "text/xml");
1390   else
1391     noit_http_response_not_found(ctx, "text/xml");
1392   if(noit_conf_write_file(NULL) != 0)
1393     noitL(noit_error, "local config write failed\n");
1394   noit_conf_mark_changed();
1395   noit_http_response_end(ctx);
1396   return 0;
1397 }
1398 static int
1399 stratcon_console_conf_noits(noit_console_closure_t ncct,
1400                             int argc, char **argv,
1401                             noit_console_state_t *dstate,
1402                             void *closure) {
1403   char *cp, target[128];
1404   unsigned short port = 43191;
1405   int adding = (int)(vpsized_int)closure;
1406   if(argc != 1)
1407     return -1;
1408
1409   cp = strchr(argv[0], ':');
1410   if(cp) {
1411     strlcpy(target, argv[0], MIN(sizeof(target), cp-argv[0]+1));
1412     port = atoi(cp+1);
1413   }
1414   else strlcpy(target, argv[0], sizeof(target));
1415   if(adding) {
1416     if(stratcon_add_noit(target, port, NULL) >= 0) {
1417       nc_printf(ncct, "Added noit at %s:%d\n", target, port);
1418     }
1419     else {
1420       nc_printf(ncct, "Failed to add noit at %s:%d\n", target, port);
1421     }
1422   }
1423   else {
1424     if(stratcon_remove_noit(target, port) >= 0) {
1425       nc_printf(ncct, "Removed noit at %s:%d\n", target, port);
1426     }
1427     else {
1428       nc_printf(ncct, "Failed to remove noit at %s:%d\n", target, port);
1429     }
1430   }
1431   return 0;
1432 }
1433
1434 static void
1435 register_console_streamer_commands() {
1436   noit_console_state_t *tl;
1437   cmd_info_t *showcmd, *confcmd, *conftcmd, *conftnocmd;
1438
1439   tl = noit_console_state_initial();
1440   showcmd = noit_console_state_get_cmd(tl, "show");
1441   assert(showcmd && showcmd->dstate);
1442   confcmd = noit_console_state_get_cmd(tl, "configure");
1443   conftcmd = noit_console_state_get_cmd(confcmd->dstate, "terminal");
1444   conftnocmd = noit_console_state_get_cmd(conftcmd->dstate, "no");
1445
1446   noit_console_state_add_cmd(conftcmd->dstate,
1447     NCSCMD("noit", stratcon_console_conf_noits, NULL, NULL, (void *)1));
1448   noit_console_state_add_cmd(conftnocmd->dstate,
1449     NCSCMD("noit", stratcon_console_conf_noits, NULL, NULL, (void *)0));
1450
1451   noit_console_state_add_cmd(showcmd->dstate,
1452     NCSCMD("noits", stratcon_console_show_noits, NULL, NULL, NULL));
1453 }
1454
1455 void
1456 stratcon_jlog_streamer_init(const char *toplevel) {
1457   struct timeval whence = DEFAULT_NOIT_PERIOD_TV;
1458   struct in_addr remote;
1459   char uuid_str[UUID_STR_LEN + 1];
1460
1461   pthread_mutex_init(&noits_lock, NULL);
1462   pthread_mutex_init(&noit_ip_by_cn_lock, NULL);
1463   eventer_name_callback("noit_connection_reinitiate",
1464                         noit_connection_reinitiate);
1465   eventer_name_callback("stratcon_jlog_recv_handler",
1466                         stratcon_jlog_recv_handler);
1467   eventer_name_callback("noit_connection_ssl_upgrade",
1468                         noit_connection_ssl_upgrade);
1469   eventer_name_callback("noit_connection_complete_connect",
1470                         noit_connection_complete_connect);
1471   eventer_name_callback("noit_connection_session_timeout",
1472                         noit_connection_session_timeout);
1473   register_console_streamer_commands();
1474   stratcon_jlog_streamer_reload(toplevel);
1475   stratcon_streamer_connection(toplevel, "", NULL, NULL, NULL, NULL);
1476   assert(noit_http_rest_register_auth(
1477     "GET", "/noits/", "^show$", rest_show_noits,
1478              noit_http_rest_client_cert_auth
1479   ) == 0);
1480   assert(noit_http_rest_register_auth(
1481     "PUT", "/noits/", "^set/([^/:]+)$", rest_set_noit,
1482              noit_http_rest_client_cert_auth
1483   ) == 0);
1484   assert(noit_http_rest_register_auth(
1485     "PUT", "/noits/", "^set/([^/:]+):(\\d+)$", rest_set_noit,
1486              noit_http_rest_client_cert_auth
1487   ) == 0);
1488   assert(noit_http_rest_register_auth(
1489     "DELETE", "/noits/", "^delete/([^/:]+)$", rest_delete_noit,
1490              noit_http_rest_client_cert_auth
1491   ) == 0);
1492   assert(noit_http_rest_register_auth(
1493     "DELETE", "/noits/", "^delete/([^/:]+):(\\d+)$", rest_delete_noit,
1494              noit_http_rest_client_cert_auth
1495   ) == 0);
1496
1497   uuid_clear(self_stratcon_id);
1498
1499   if(noit_conf_get_stringbuf(NULL, "/stratcon/@id",
1500                              uuid_str, sizeof(uuid_str)) &&
1501      uuid_parse(uuid_str, self_stratcon_id) == 0) {
1502     int period;
1503     /* If a UUID was provided for stratcon itself, we will report metrics
1504      * on a large variety of things (including all noits).
1505      */
1506     if(noit_conf_get_int(NULL, "/stratcon/@metric_period", &period) &&
1507        period > 0) {
1508       DEFAULT_NOIT_PERIOD_TV.tv_sec = period / 1000;
1509       DEFAULT_NOIT_PERIOD_TV.tv_usec = (period % 1000) * 1000;
1510     }
1511     self_stratcon_ip.sin_family = AF_INET;
1512     remote.s_addr = 0xffffffff;
1513     noit_getip_ipv4(remote, &self_stratcon_ip.sin_addr);
1514     gethostname(self_stratcon_hostname, sizeof(self_stratcon_hostname));
1515     eventer_add_in(periodic_noit_metrics, NULL, whence);
1516   }
1517 }
1518
Note: See TracBrowser for help on using the browser.