root/src/modules/rabbitmq_driver.c

Revision 304ec80b8cf842fc0abe5f9029790908b6455957, 16.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 1 month ago)

Convert to libmtev.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2011, OmniTI Computer Consulting, Inc.
3  * Copyright (c) 2015, Circonus, Inc. 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 #include <mtev_defines.h>
33
34 #include <poll.h>
35 #include <unistd.h>
36 #include <assert.h>
37 #include <errno.h>
38
39 #include <mtev_dso.h>
40 #include <eventer/eventer.h>
41 #include <mtev_log.h>
42 #include <stratcon_iep.h>
43 #include <mtev_conf.h>
44
45 #include "librabbitmq/amqp.h"
46 #include "librabbitmq/amqp_framing.h"
47 #include "rabbitmq_driver.xmlh"
48
49 #define MAX_CONCURRENCY 16
50 #define MAX_HOSTS 10
51 #define DEFAULT_SNDBUF (1 << 20)
52 #define DEFAULT_RCVBUF (1 << 20)
53
54 static socklen_t desired_sndbuf = DEFAULT_SNDBUF;
55 static socklen_t desired_rcvbuf = DEFAULT_RCVBUF;
56
57 static pthread_mutex_t driver_lock;
58 struct amqp_driver {
59   pthread_t owner;
60   amqp_connection_state_t connection;
61   char exchange[128];
62   char routingkey[256];
63   char username[80];
64   char password[80];
65   char vhost[256];
66   int sockfd;
67   int heartbeat;
68   int nhosts;
69   int nconnects;
70   int hostidx;
71   char hostname[10][256];
72   int port;
73   struct timeval last_hb;
74   int has_error; /* out of band */
75 };
76
77 static struct {
78   mtev_atomic64_t basic_returns;
79   mtev_atomic64_t connects;
80   mtev_atomic64_t inbound_methods;
81   mtev_atomic64_t inbound_heartbeats;
82   mtev_atomic64_t publications;
83   mtev_atomic64_t concurrency;
84   struct amqp_driver thread_states[MAX_CONCURRENCY];
85 } stats;
86 #define BUMPSTAT(a) mtev_atomic_inc64(&stats.a)
87
88 static iep_thread_driver_t *noit_rabbimq_allocate(mtev_conf_section_t conf) {
89   char *hostname = NULL, *cp, *brk;
90   struct amqp_driver *dr = NULL;
91   int i;
92
93   pthread_mutex_lock(&driver_lock);
94   for(i=0; i<MAX_HOSTS; i++) {
95     if(stats.thread_states[i].owner == (pthread_t)(vpsized_int)NULL) {
96       stats.thread_states[i].owner = pthread_self();
97       dr = &stats.thread_states[i];
98       break;
99     }
100   }
101   pthread_mutex_unlock(&driver_lock);
102   if(!dr) return NULL;
103   dr->nconnects = rand();
104 #define GETCONFSTR(w) mtev_conf_get_stringbuf(conf, #w, dr->w, sizeof(dr->w))
105   GETCONFSTR(exchange);
106   if(!GETCONFSTR(routingkey))
107     dr->routingkey[0] = '\0';
108   GETCONFSTR(username);
109   GETCONFSTR(password);
110   if(!GETCONFSTR(vhost)) { dr->vhost[0] = '/'; dr->vhost[1] = '\0'; }
111   if(!mtev_conf_get_int(conf, "heartbeat", &dr->heartbeat))
112     dr->heartbeat = 5000;
113   dr->heartbeat = (dr->heartbeat + 999) / 1000;
114
115   (void)mtev_conf_get_string(conf, "hostname", &hostname);
116   if(!hostname) hostname = strdup("127.0.0.1");
117   for(cp = hostname; cp; cp = strchr(cp+1, ',')) dr->nhosts++;
118   if(dr->nhosts > MAX_HOSTS) dr->nhosts = MAX_HOSTS;
119   for(i = 0, cp = strtok_r(hostname, ",", &brk);
120       cp; cp = strtok_r(NULL, ",", &brk), i++)
121     strlcpy(dr->hostname[i], cp, sizeof(dr->hostname[i]));
122   free(hostname);
123
124   if(!mtev_conf_get_int(conf, "port", &dr->port))
125     dr->port = 5672;
126   mtev_atomic_inc64(&stats.concurrency);
127   return (iep_thread_driver_t *)dr;
128 }
129 static int noit_rabbimq_disconnect(iep_thread_driver_t *d) {
130   struct amqp_driver *dr = (struct amqp_driver *)d;
131   if(dr->connection) {
132     amqp_destroy_connection(dr->connection);
133     if(dr->sockfd >= 0) close(dr->sockfd);
134     dr->sockfd = -1;
135     dr->connection = NULL;
136     return 0;
137   }
138   return -1;
139 }
140 static void noit_rabbimq_deallocate(iep_thread_driver_t *d) {
141   struct amqp_driver *dr = (struct amqp_driver *)d;
142   noit_rabbimq_disconnect(d);
143   pthread_mutex_lock(&driver_lock);
144   memset(dr, 0, sizeof(*dr));
145   pthread_mutex_unlock(&driver_lock);
146   mtev_atomic_dec64(&stats.concurrency);
147   free(dr);
148 }
149 static void noit_rabbitmq_read_frame(struct amqp_driver *dr) {
150   struct pollfd p;
151   if(!dr->connection) return;
152   while(1) {
153     memset(&p, 0, sizeof(p));
154     p.fd = dr->sockfd;
155     p.events = POLLIN;
156     if(poll(&p, 1, 0)) {
157       int rv;
158       amqp_frame_t f;
159       rv = amqp_simple_wait_frame(dr->connection, &f);
160       if(rv > 0) {
161         if(f.frame_type == AMQP_FRAME_HEARTBEAT) {
162           BUMPSTAT(inbound_heartbeats);
163           mtevL(mtev_debug, "amqp <- hearbeat\n");
164         }
165         else if(f.frame_type == AMQP_FRAME_METHOD) {
166           BUMPSTAT(inbound_methods);
167           mtevL(mtev_error, "amqp <- method [%s]\n", amqp_method_name(f.payload.method.id));
168           dr->has_error = 1;
169           switch(f.payload.method.id) {
170             case AMQP_CHANNEL_CLOSE_METHOD: {
171                 amqp_channel_close_t *m = (amqp_channel_close_t *) f.payload.method.decoded;
172                 mtevL(mtev_error, "AMQP channel close error %d: %s\n",
173                       m->reply_code, (char *)m->reply_text.bytes);
174               }
175               break;
176             case AMQP_CONNECTION_CLOSE_METHOD: {
177                 amqp_connection_close_t *m = (amqp_connection_close_t *) f.payload.method.decoded;
178                 mtevL(mtev_error, "AMQP connection close error %d: %s\n",
179                       m->reply_code, (char *)m->reply_text.bytes);
180               }
181               break;
182           }
183         }
184         else {
185           mtevL(mtev_error, "amqp <- frame [%d]\n", f.frame_type);
186         }
187       }
188       else break;
189     }
190     else break;
191   }
192 }
193 static void noit_rabbitmq_heartbeat(struct amqp_driver *dr) {
194   struct timeval n, d;
195   if(!dr->connection) return;
196   gettimeofday(&n, NULL);
197   sub_timeval(n, dr->last_hb, &d);
198   if(d.tv_sec >= dr->heartbeat) {
199     amqp_frame_t f;
200     f.frame_type = AMQP_FRAME_HEARTBEAT;
201     f.channel = 0;
202     amqp_send_frame(dr->connection, &f);
203     mtevL(mtev_debug, "amqp -> hearbeat\n");
204     memcpy(&dr->last_hb, &n, sizeof(n));
205   }
206 }
207 static void
208 noit_rabbitmq_brcb(amqp_channel_t channel, amqp_basic_return_t *m, void *closure) {
209   BUMPSTAT(basic_returns);
210   mtevL(mtev_debug, "AMQP return [%d:%.*s]\n", m->reply_code,
211         (int)m->reply_text.len, (char *)m->reply_text.bytes);
212 }
213 static int noit_rabbimq_connect(iep_thread_driver_t *dr) {
214   struct amqp_driver *driver = (struct amqp_driver *)dr;
215
216   if(!driver->connection) {
217     int sidx = driver->nconnects++ % driver->nhosts;
218     struct timeval timeout;
219     amqp_rpc_reply_t r, *rptr;
220
221     mtevL(mtev_error, "AMQP connect: %s:%d\n",
222           driver->hostname[sidx], driver->port);
223     BUMPSTAT(connects);
224     driver->hostidx = sidx;
225     timeout.tv_sec = driver->heartbeat;
226     timeout.tv_usec = 0;
227     driver->sockfd = amqp_open_socket(driver->hostname[sidx], driver->port, &timeout);
228     if(driver->sockfd < 0) {
229       mtevL(mtev_error, "AMQP connect failed: %s:%d\n",
230             driver->hostname[sidx], driver->port);
231       return -1;
232     }
233     if(setsockopt(driver->sockfd, SOL_SOCKET, SO_SNDBUF, &desired_sndbuf, sizeof(desired_sndbuf)) < 0)
234       mtevL(mtev_debug, "rabbitmq: setsockopt(SO_SNDBUF, %ld) -> %s\n", desired_sndbuf, strerror(errno));
235     if(setsockopt(driver->sockfd, SOL_SOCKET, SO_RCVBUF, &desired_rcvbuf, sizeof(desired_rcvbuf)) < 0)
236       mtevL(mtev_debug, "rabbitmq: setsockopt(SO_RCVBUF, %ld) -> %s\n", desired_rcvbuf, strerror(errno));
237     driver->has_error = 0;
238     driver->connection = amqp_new_connection();
239     amqp_set_basic_return_cb(driver->connection, noit_rabbitmq_brcb, driver);
240     amqp_set_sockfd(driver->connection, driver->sockfd);
241     r = amqp_login(driver->connection,
242                    driver->vhost, 0, 131072, driver->heartbeat,
243                    AMQP_SASL_METHOD_PLAIN,
244                    driver->username, driver->password);
245     if(r.reply_type != AMQP_RESPONSE_NORMAL) {
246       mtevL(mtev_error, "AMQP login failed\n");
247       amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
248       amqp_destroy_connection(driver->connection);
249       if(driver->sockfd >= 0) close(driver->sockfd);
250       driver->sockfd = -1;
251       driver->connection = NULL;
252       return -1;
253     }
254
255     amqp_channel_open(driver->connection, 1);
256     rptr = amqp_get_rpc_reply();
257     if(rptr->reply_type != AMQP_RESPONSE_NORMAL) {
258       mtevL(mtev_error, "AMQP channe_open failed\n");
259       amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
260       amqp_destroy_connection(driver->connection);
261       if(driver->sockfd >= 0) close(driver->sockfd);
262       driver->sockfd = -1;
263       driver->connection = NULL;
264       return -1;
265     }
266     gettimeofday(&driver->last_hb, NULL);
267     return 0;
268   }
269   /* 1 means already connected */
270   return 1;
271 }
272
273 /* This is very specific to an internal implementation somewhere...
274  * and thus unlikely to be useful unless people name their checks:
275  * c_<accountid>_<checknumber>::<rest of name>
276  * This code should likley be made generic, perhaps with named
277  * pcre captures.  However, I'm worried about performance.
278  * For now, leave it and understand it is limited usefulness.
279  */
280 static int extract_uuid_from_jlog(const char *payload, size_t payloadlen,
281                                   int *account_id, int *check_id, char *dst) {
282   int i = 0;
283   const char *atab = payload, *u = NULL;
284
285   if(account_id) *account_id = 0;
286   if(check_id) *check_id = 0;
287
288 #define advance_past_tab do { \
289   atab = memchr(atab, '\t', payloadlen - (atab - payload)); \
290   if(!atab) return 0; \
291   atab++; \
292 } while(0)
293
294   /* Tab -> M|S|C */
295   advance_past_tab;
296   /* Tab -> noit IP */
297   advance_past_tab;
298   /* Tab -> timestamp */
299   advance_past_tab;
300   /* Tab -> uuid */
301   u = atab;
302   advance_past_tab;
303   /* Tab -> metric_name */
304   atab--;
305   if(atab - u < UUID_STR_LEN) return 0;
306   if(atab - u > UUID_STR_LEN) {
307     const char *f;
308     f = memchr(u, '`', payloadlen - (u - payload));
309     if(f) {
310       f = memchr(f+1, '`', payloadlen - (f + 1 - payload));
311       if(f) {
312         f++;
313         if(memcmp(f, "c_", 2) == 0) {
314           f += 2;
315           if(account_id) *account_id = atoi(f);
316           f = memchr(f, '_', payloadlen - (f - payload));
317           if(f) {
318             f++;
319             if(check_id) *check_id = atoi(f);
320           }
321         }
322       }
323     }
324   }
325   u = atab - UUID_STR_LEN;
326   while(i<32 && u < atab) {
327     if((*u >= 'a' && *u <= 'f') ||
328        (*u >= '0' && *u <= '9')) {
329       dst[i*2] = '.';
330       dst[i*2 + 1] = *u;
331       i++;
332     }
333     else if(*u != '-') return 0;
334     u++;
335   }
336   dst[i*2] = '\0';
337   return 1;
338 }
339 static int
340 noit_rabbimq_submit(iep_thread_driver_t *dr,
341                     const char *payload, size_t payloadlen) {
342   int rv;
343   amqp_bytes_t body;
344   struct amqp_driver *driver = (struct amqp_driver *)dr;
345   const char *routingkey = driver->routingkey;
346
347   body.len = payloadlen;
348   body.bytes = (char *)payload;
349   if(*payload == 'M' ||
350      *payload == 'S' ||
351      *payload == 'C' ||
352      (*payload == 'F' && payload[1] == '1') ||
353      (*payload == 'B' && (payload[1] == '1' || payload[1] == '2'))) {
354     char uuid_str[32 * 2 + 1];
355     int account_id, check_id;
356     if(extract_uuid_from_jlog(payload, payloadlen,
357                               &account_id, &check_id, uuid_str)) {
358       if(*routingkey) {
359         char *replace;
360         int newlen = strlen(driver->routingkey) + 1 + sizeof(uuid_str) + 2 * 32;
361         replace = alloca(newlen);
362         snprintf(replace, newlen, "%s.%x.%x.%d.%d%s", driver->routingkey,
363                  account_id%16, (account_id/16)%16, account_id,
364                  check_id, uuid_str);
365         routingkey = replace;
366       }
367     }
368   }
369   rv = amqp_basic_publish(driver->connection, 1,
370                           amqp_cstring_bytes(driver->exchange),
371                           amqp_cstring_bytes(routingkey),
372                           1, 0, NULL, body);
373   if(rv < 0) {
374     mtevL(mtev_error, "AMQP publish failed, disconnecting\n");
375     amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
376     amqp_destroy_connection(driver->connection);
377     if(driver->sockfd >= 0) close(driver->sockfd);
378     driver->sockfd = -1;
379     driver->connection = NULL;
380     return -1;
381   }
382   BUMPSTAT(publications);
383   noit_rabbitmq_heartbeat(driver);
384   noit_rabbitmq_read_frame(driver);
385   amqp_maybe_release_buffers(driver->connection);
386   if(driver->has_error) {
387     amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
388     amqp_destroy_connection(driver->connection);
389     if(driver->sockfd >= 0) close(driver->sockfd);
390     driver->sockfd = -1;
391     driver->connection = NULL;
392     return -1;
393   }
394   return 0;
395 }
396
397 mq_driver_t mq_driver_rabbitmq = {
398   noit_rabbimq_allocate,
399   noit_rabbimq_connect,
400   noit_rabbimq_submit,
401   noit_rabbimq_disconnect,
402   noit_rabbimq_deallocate
403 };
404
405 static int noit_rabbimq_driver_config(mtev_dso_generic_t *self, mtev_hash_table *o) {
406   const char *intstr;
407   if(mtev_hash_retr_str(o, "sndbuf", strlen("sndbuf"), &intstr))
408     desired_sndbuf = atoi(intstr);
409   if(mtev_hash_retr_str(o, "rcvbuf", strlen("rcvbuf"), &intstr))
410     desired_rcvbuf = atoi(intstr);
411   return 0;
412 }
413 static int noit_rabbimq_driver_onload(mtev_image_t *self) {
414   return 0;
415 }
416
417 static int
418 noit_console_show_rabbitmq(mtev_console_closure_t ncct,
419                            int argc, char **argv,
420                            mtev_console_state_t *dstate,
421                            void *closure) {
422   int i;
423   nc_printf(ncct, " == RabbitMQ ==\n");
424   nc_printf(ncct, " Concurrency:           %llu\n", stats.concurrency);
425   nc_printf(ncct, " Connects:              %llu\n", stats.connects);
426   nc_printf(ncct, " AMQP basic returns:    %llu\n", stats.basic_returns);
427   nc_printf(ncct, " AMQP methods (in):     %llu\n", stats.inbound_methods);
428   nc_printf(ncct, " AMQP heartbeats (in):  %llu\n", stats.inbound_heartbeats);
429   nc_printf(ncct, " AMQP basic publish:    %llu\n", stats.publications);
430   pthread_mutex_lock(&driver_lock);
431   for(i=0;i<MAX_HOSTS;i++) {
432     struct amqp_driver *dr;
433     if(!stats.thread_states[i].owner) continue;
434     dr = &stats.thread_states[i];
435     nc_printf(ncct, "   == connection: %p ==\n", (void *)(vpsized_int)dr->owner);
436     if(dr->connection)
437       nc_printf(ncct, "     %s@%s:%d (vhost: %s, exchange: %s)\n",
438                 dr->username, dr->hostname[dr->hostidx], dr->port, dr->vhost,
439                 dr->exchange);
440     else
441       nc_printf(ncct, "     not connected\n");
442   }
443   pthread_mutex_unlock(&driver_lock);
444   return 1;
445 }
446
447 static void
448 register_console_rabbitmq_commands() {
449   mtev_console_state_t *tl;
450   cmd_info_t *showcmd;
451
452   tl = mtev_console_state_initial();
453   showcmd = mtev_console_state_get_cmd(tl, "show");
454   assert(showcmd && showcmd->dstate);
455   mtev_console_state_add_cmd(showcmd->dstate,
456     NCSCMD("rabbitmq", noit_console_show_rabbitmq, NULL, NULL, NULL));
457 }
458
459 static int noit_rabbimq_driver_init(mtev_dso_generic_t *self) {
460   pthread_mutex_init(&driver_lock, NULL);
461   memset(&stats, 0, sizeof(stats));
462   stratcon_iep_mq_driver_register("rabbitmq", &mq_driver_rabbitmq);
463   register_console_rabbitmq_commands();
464   return 0;
465 }
466
467 mtev_dso_generic_t rabbitmq_driver = {
468   {
469     .magic = MTEV_GENERIC_MAGIC,
470     .version = MTEV_GENERIC_ABI_VERSION,
471     .name = "rabbitmq_driver",
472     .description = "AMQP driver for IEP MQ submission",
473     .xml_description = rabbitmq_driver_xml_description,
474     .onload = noit_rabbimq_driver_onload
475   },
476   noit_rabbimq_driver_config,
477   noit_rabbimq_driver_init
478 };
479
Note: See TracBrowser for help on using the browser.