2 * collectd - src/amqp.c
3 * Copyright (C) 2009 Sebastien Pahl
4 * Copyright (C) 2010 Florian Forster
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Sebastien Pahl <sebastien.pahl at dotcloud.com>
26 * Florian Forster <octo at verplant.org>
38 #include "utils_format_json.h"
41 #include <amqp_framing.h>
43 /* Defines for the delivery mode. I have no idea why they're not defined by the
45 #define CAMQP_DM_VOLATILE 1
46 #define CAMQP_DM_PERSISTENT 2
48 #define CAMQP_CHANNEL 1
68 uint8_t delivery_mode;
72 amqp_connection_state_t connection;
75 typedef struct camqp_config_s camqp_config_t;
80 static const char *def_host = "localhost";
81 static const char *def_vhost = "/";
82 static const char *def_user = "guest";
83 static const char *def_password = "guest";
84 static const char *def_exchange = "amq.fanout";
85 static const char *def_routingkey = "collectd";
87 static pthread_t *subscriber_threads = NULL;
88 static size_t subscriber_threads_num = 0;
89 static _Bool subscriber_threads_running = 1;
91 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
96 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
100 if ((conf == NULL) || (conf->connection == NULL))
103 sockfd = amqp_get_sockfd (conf->connection);
104 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
105 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
106 amqp_destroy_connection (conf->connection);
108 conf->connection = NULL;
109 } /* }}} void camqp_close_connection */
111 static void camqp_config_free (void *ptr) /* {{{ */
113 camqp_config_t *conf = ptr;
118 camqp_close_connection (conf);
124 sfree (conf->password);
125 sfree (conf->exchange);
126 sfree (conf->exchange_type);
128 sfree (conf->routingkey);
131 } /* }}} void camqp_config_free */
133 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
137 if ((in == NULL) || (in->bytes == NULL))
140 ret = malloc (in->len + 1);
144 memcpy (ret, in->bytes, in->len);
148 } /* }}} char *camqp_bytes_cstring */
150 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
154 r = amqp_get_rpc_reply (conf->connection);
155 if (r.reply_type == AMQP_RESPONSE_NORMAL)
159 } /* }}} _Bool camqp_is_error */
161 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
162 char *buffer, size_t buffer_size)
166 r = amqp_get_rpc_reply (conf->connection);
167 switch (r.reply_type)
169 case AMQP_RESPONSE_NORMAL:
170 sstrncpy (buffer, "Success", sizeof (buffer));
173 case AMQP_RESPONSE_NONE:
174 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
177 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
179 return (sstrerror (r.library_errno, buffer, buffer_size));
181 sstrncpy (buffer, "End of stream", sizeof (buffer));
184 case AMQP_RESPONSE_SERVER_EXCEPTION:
185 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
187 amqp_connection_close_t *m = r.reply.decoded;
188 char *tmp = camqp_bytes_cstring (&m->reply_text);
189 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
193 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
195 amqp_channel_close_t *m = r.reply.decoded;
196 char *tmp = camqp_bytes_cstring (&m->reply_text);
197 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
203 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
209 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
214 } /* }}} char *camqp_strerror */
216 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
218 amqp_queue_declare_ok_t *qd_ret;
219 amqp_basic_consume_ok_t *cm_ret;
221 qd_ret = amqp_queue_declare (conf->connection,
222 /* channel = */ CAMQP_CHANNEL,
223 /* queue = */ (conf->queue != NULL)
224 ? amqp_cstring_bytes (conf->queue)
229 /* auto_delete = */ 1,
230 /* arguments = */ AMQP_EMPTY_TABLE);
233 ERROR ("amqp plugin: amqp_queue_declare failed.");
234 camqp_close_connection (conf);
238 if (conf->queue == NULL)
240 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
241 if (conf->queue == NULL)
243 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
244 camqp_close_connection (conf);
248 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
250 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
252 /* bind to an exchange */
253 if (conf->exchange != NULL)
255 amqp_queue_bind_ok_t *qb_ret;
257 /* create the exchange */
258 if (conf->exchange_type != NULL)
260 amqp_exchange_declare_ok_t *ed_ret;
262 ed_ret = amqp_exchange_declare (conf->connection,
263 /* channel = */ CAMQP_CHANNEL,
264 /* exchange = */ amqp_cstring_bytes (conf->exchange),
265 /* type = */ amqp_cstring_bytes (conf->exchange_type),
268 /* auto_delete = */ 1,
269 /* arguments = */ AMQP_EMPTY_TABLE);
270 if ((ed_ret == NULL) && camqp_is_error (conf))
273 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
274 camqp_strerror (conf, errbuf, sizeof (errbuf)));
275 camqp_close_connection (conf);
280 DEBUG ("amqp plugin: queue = %s; exchange = %s; routing_key = %s;",
281 conf->queue, conf->exchange, CONF (conf, routingkey));
283 assert (conf->queue != NULL);
284 qb_ret = amqp_queue_bind (conf->connection,
285 /* channel = */ CAMQP_CHANNEL,
286 /* queue = */ amqp_cstring_bytes (conf->queue),
287 /* exchange = */ amqp_cstring_bytes (conf->exchange),
289 /* routing_key = */ amqp_cstring_bytes (CONF (conf, routingkey)),
291 /* routing_key = */ AMQP_EMPTY_BYTES,
293 /* arguments = */ AMQP_EMPTY_TABLE);
294 if ((qb_ret == NULL) && camqp_is_error (conf))
297 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
298 camqp_strerror (conf, errbuf, sizeof (errbuf)));
299 camqp_close_connection (conf);
303 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
304 conf->queue, conf->exchange);
305 } /* if (conf->exchange != NULL) */
307 cm_ret = amqp_basic_consume (conf->connection,
308 /* channel = */ CAMQP_CHANNEL,
309 /* queue = */ amqp_cstring_bytes (conf->queue),
310 /* consumer_tag = */ AMQP_EMPTY_BYTES,
313 /* exclusive = */ 0);
314 if ((cm_ret == NULL) && camqp_is_error (conf))
317 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
318 camqp_strerror (conf, errbuf, sizeof (errbuf)));
319 camqp_close_connection (conf);
324 } /* }}} int camqp_setup_queue */
326 static int camqp_connect (camqp_config_t *conf) /* {{{ */
328 amqp_rpc_reply_t reply;
332 if (conf->connection != NULL)
335 conf->connection = amqp_new_connection ();
336 if (conf->connection == NULL)
338 ERROR ("amqp plugin: amqp_new_connection failed.");
342 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
346 status = (-1) * sockfd;
347 ERROR ("amqp plugin: amqp_open_socket failed: %s",
348 sstrerror (status, errbuf, sizeof (errbuf)));
349 amqp_destroy_connection (conf->connection);
350 conf->connection = NULL;
353 amqp_set_sockfd (conf->connection, sockfd);
355 reply = amqp_login (conf->connection, CONF(conf, vhost),
356 /* channel max = */ 0,
357 /* frame max = */ 131072,
359 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
360 CONF(conf, user), CONF(conf, password));
361 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
363 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
364 CONF(conf, vhost), CONF(conf, user));
365 amqp_destroy_connection (conf->connection);
367 conf->connection = NULL;
371 amqp_channel_open (conf->connection, /* channel = */ 1);
372 /* FIXME: Is checking "reply.reply_type" really correct here? How does
373 * it get set? --octo */
374 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
376 ERROR ("amqp plugin: amqp_channel_open failed.");
377 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
378 amqp_destroy_connection (conf->connection);
380 conf->connection = NULL;
384 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
385 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
388 return (camqp_setup_queue (conf));
390 } /* }}} int camqp_connect */
392 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
395 char body[body_size + 1];
401 memset (body, 0, sizeof (body));
405 while (received < body_size)
407 status = amqp_simple_wait_frame (conf->connection, &frame);
411 status = (-1) * status;
412 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
413 sstrerror (status, errbuf, sizeof (errbuf)));
414 camqp_close_connection (conf);
418 if (frame.frame_type != AMQP_FRAME_BODY)
420 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
425 if ((body_size - received) < frame.payload.body_fragment.len)
427 WARNING ("amqp plugin: Body is larger than indicated by header.");
431 memcpy (body_ptr, frame.payload.body_fragment.bytes,
432 frame.payload.body_fragment.len);
433 body_ptr += frame.payload.body_fragment.len;
434 received += frame.payload.body_fragment.len;
435 } /* while (received < body_size) */
437 DEBUG ("amqp plugin: camqp_read_body: body = %s", body);
440 } /* }}} int camqp_read_body */
442 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
447 status = amqp_simple_wait_frame (conf->connection, &frame);
451 status = (-1) * status;
452 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
453 sstrerror (status, errbuf, sizeof (errbuf)));
454 camqp_close_connection (conf);
458 if (frame.frame_type != AMQP_FRAME_HEADER)
460 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
465 return (camqp_read_body (conf, frame.payload.properties.body_size));
466 } /* }}} int camqp_read_header */
468 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
470 camqp_config_t *conf = user_data;
473 while (subscriber_threads_running)
477 status = camqp_connect (conf);
480 ERROR ("amqp plugin: camqp_connect failed. "
481 "Will sleep for %i seconds.", interval_g);
486 status = amqp_simple_wait_frame (conf->connection, &frame);
489 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
490 "Will sleep for %i seconds.", interval_g);
491 camqp_close_connection (conf);
496 if (frame.frame_type != AMQP_FRAME_METHOD)
498 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
503 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
505 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
506 frame.payload.method.id);
510 status = camqp_read_header (conf);
512 amqp_maybe_release_buffers (conf->connection);
513 } /* while (subscriber_threads_running) */
515 camqp_config_free (conf);
517 } /* }}} void *camqp_subscribe_thread */
519 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
524 tmp = realloc (subscriber_threads,
525 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
528 ERROR ("amqp plugin: realloc failed.");
529 camqp_config_free (conf);
532 subscriber_threads = tmp;
533 tmp = subscriber_threads + subscriber_threads_num;
534 memset (tmp, 0, sizeof (*tmp));
536 status = pthread_create (tmp, /* attr = */ NULL,
537 camqp_subscribe_thread, conf);
541 ERROR ("amqp plugin: pthread_create failed: %s",
542 sstrerror (status, errbuf, sizeof (errbuf)));
543 camqp_config_free (conf);
547 subscriber_threads_num++;
550 } /* }}} int camqp_subscribe_init */
552 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
555 amqp_basic_properties_t props;
558 status = camqp_connect (conf);
562 memset (&props, 0, sizeof (props));
563 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
564 | AMQP_BASIC_DELIVERY_MODE_FLAG
565 | AMQP_BASIC_APP_ID_FLAG;
566 props.content_type = amqp_cstring_bytes("application/json");
567 props.delivery_mode = conf->delivery_mode;
568 props.app_id = amqp_cstring_bytes("collectd");
570 status = amqp_basic_publish(conf->connection,
572 amqp_cstring_bytes(CONF(conf, exchange)),
573 amqp_cstring_bytes(CONF(conf, routingkey)),
577 amqp_cstring_bytes(buffer));
580 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
582 camqp_close_connection (conf);
586 } /* }}} int camqp_write_locked */
588 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
589 user_data_t *user_data)
591 camqp_config_t *conf = user_data->data;
597 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
600 memset (buffer, 0, sizeof (buffer));
601 bfree = sizeof (buffer);
604 format_json_initialize (buffer, &bfill, &bfree);
605 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
606 format_json_finalize (buffer, &bfill, &bfree);
608 pthread_mutex_lock (&conf->lock);
609 status = camqp_write_locked (conf, buffer);
610 pthread_mutex_unlock (&conf->lock);
613 } /* }}} int camqp_write */
615 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
618 camqp_config_t *conf;
622 conf = malloc (sizeof (*conf));
625 ERROR ("amqp plugin: malloc failed.");
629 /* Initialize "conf" {{{ */
630 memset (conf, 0, sizeof (*conf));
631 conf->publish = publish;
637 conf->password = NULL;
638 conf->exchange = NULL;
639 conf->exchange_type = NULL;
641 conf->routingkey = NULL;
642 conf->delivery_mode = CAMQP_DM_VOLATILE;
643 conf->store_rates = 0;
644 conf->connection = NULL;
645 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
648 status = cf_util_get_string (ci, &conf->name);
655 for (i = 0; i < ci->children_num; i++)
657 oconfig_item_t *child = ci->children + i;
659 if (strcasecmp ("Host", child->key) == 0)
660 status = cf_util_get_string (child, &conf->host);
661 else if (strcasecmp ("Port", child->key) == 0)
663 status = cf_util_get_port_number (child);
670 else if (strcasecmp ("VHost", child->key) == 0)
671 status = cf_util_get_string (child, &conf->vhost);
672 else if (strcasecmp ("User", child->key) == 0)
673 status = cf_util_get_string (child, &conf->user);
674 else if (strcasecmp ("Password", child->key) == 0)
675 status = cf_util_get_string (child, &conf->password);
676 else if (strcasecmp ("Exchange", child->key) == 0)
677 status = cf_util_get_string (child, &conf->exchange);
678 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
679 status = cf_util_get_string (child, &conf->exchange_type);
680 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
681 status = cf_util_get_string (child, &conf->queue);
682 else if (strcasecmp ("RoutingKey", child->key) == 0)
683 status = cf_util_get_string (child, &conf->routingkey);
684 else if (strcasecmp ("Persistent", child->key) == 0)
687 status = cf_util_get_boolean (child, &tmp);
689 conf->delivery_mode = CAMQP_DM_PERSISTENT;
691 conf->delivery_mode = CAMQP_DM_VOLATILE;
693 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
694 status = cf_util_get_boolean (child, &conf->store_rates);
696 WARNING ("amqp plugin: Ignoring unknown "
697 "configuration option \"%s\".", child->key);
701 } /* for (i = 0; i < ci->children_num; i++) */
703 if ((status == 0) && !publish && (conf->exchange == NULL))
705 if (conf->routingkey != NULL)
706 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
707 "without the \"Exchange\" option. It will be ignored.");
709 if (conf->exchange_type != NULL)
710 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
711 "without the \"Exchange\" option. It will be ignored.");
716 camqp_config_free (conf);
720 if (conf->exchange != NULL)
722 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
729 user_data_t ud = { conf, camqp_config_free };
731 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
733 status = plugin_register_write (cbname, camqp_write, &ud);
736 camqp_config_free (conf);
742 status = camqp_subscribe_init (conf);
745 camqp_config_free (conf);
751 } /* }}} int camqp_config_connection */
753 static int camqp_config (oconfig_item_t *ci) /* {{{ */
757 for (i = 0; i < ci->children_num; i++)
759 oconfig_item_t *child = ci->children + i;
761 if (strcasecmp ("Publish", child->key) == 0)
762 camqp_config_connection (child, /* publish = */ 1);
763 else if (strcasecmp ("Subscribe", child->key) == 0)
764 camqp_config_connection (child, /* publish = */ 0);
766 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
768 } /* for (ci->children_num) */
771 } /* }}} int camqp_config */
773 static int shutdown (void) /* {{{ */
777 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
778 subscriber_threads_num);
780 subscriber_threads_running = 0;
781 for (i = 0; i < subscriber_threads_num; i++)
783 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
784 * to use a timeout in the thread and check for the variable in regular
786 pthread_kill (subscriber_threads[i], SIGTERM);
787 pthread_join (subscriber_threads[i], /* retval = */ NULL);
790 subscriber_threads_num = 0;
791 sfree (subscriber_threads);
793 DEBUG ("amqp plugin: All subscriber threads exited.");
796 } /* }}} int shutdown */
798 void module_register (void)
800 plugin_register_complex_config ("amqp", camqp_config);
801 plugin_register_shutdown ("amqp", shutdown);
802 } /* void module_register */
804 /* vim: set sw=4 sts=4 et fdm=marker : */