root/src/modules/rabbitmq_driver.c

Revision 70c30ef22f6d1312375e0392164d755db5824cb5, 16.1 kB (checked in by Phil Maddox <philip.maddox@circonus.com>, 6 months ago)

Use mtevAssert and mtevFatal Instead Of assert() and abort()

Use libmtev calls to safely flush logs and abort rather than calling
the assert and abort calls directly.

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