root/src/stratcon_jlog_streamer.c

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

updates... yay! for test suites... all passes, refs #334

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