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);
302 } /* if (conf->exchange != NULL) */
304 cm_ret = amqp_basic_consume (conf->connection,
305 /* channel = */ CAMQP_CHANNEL,
306 /* queue = */ amqp_cstring_bytes (conf->queue),
307 /* consumer_tag = */ AMQP_EMPTY_BYTES,
310 /* exclusive = */ 0);
311 if ((cm_ret == NULL) && camqp_is_error (conf))
314 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
315 camqp_strerror (conf, errbuf, sizeof (errbuf)));
316 camqp_close_connection (conf);
321 } /* }}} int camqp_setup_queue */
323 static int camqp_connect (camqp_config_t *conf) /* {{{ */
325 amqp_rpc_reply_t reply;
329 if (conf->connection != NULL)
332 conf->connection = amqp_new_connection ();
333 if (conf->connection == NULL)
335 ERROR ("amqp plugin: amqp_new_connection failed.");
339 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
343 status = (-1) * sockfd;
344 ERROR ("amqp plugin: amqp_open_socket failed: %s",
345 sstrerror (status, errbuf, sizeof (errbuf)));
346 amqp_destroy_connection (conf->connection);
347 conf->connection = NULL;
350 amqp_set_sockfd (conf->connection, sockfd);
352 reply = amqp_login (conf->connection, CONF(conf, vhost),
353 /* channel max = */ 0,
354 /* frame max = */ 131072,
356 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
357 CONF(conf, user), CONF(conf, password));
358 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
360 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
361 CONF(conf, vhost), CONF(conf, user));
362 amqp_destroy_connection (conf->connection);
364 conf->connection = NULL;
368 amqp_channel_open (conf->connection, /* channel = */ 1);
369 /* FIXME: Is checking "reply.reply_type" really correct here? How does
370 * it get set? --octo */
371 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
373 ERROR ("amqp plugin: amqp_channel_open failed.");
374 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
375 amqp_destroy_connection (conf->connection);
377 conf->connection = NULL;
381 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
382 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
385 return (camqp_setup_queue (conf));
387 } /* }}} int camqp_connect */
389 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
392 char body[body_size + 1];
398 memset (body, 0, sizeof (body));
402 while (received < body_size)
404 status = amqp_simple_wait_frame (conf->connection, &frame);
408 status = (-1) * status;
409 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
410 sstrerror (status, errbuf, sizeof (errbuf)));
411 camqp_close_connection (conf);
415 if (frame.frame_type != AMQP_FRAME_BODY)
417 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
422 if ((body_size - received) < frame.payload.body_fragment.len)
424 WARNING ("amqp plugin: Body is larger than indicated by header.");
428 memcpy (body_ptr, frame.payload.body_fragment.bytes,
429 frame.payload.body_fragment.len);
430 body_ptr += frame.payload.body_fragment.len;
431 received += frame.payload.body_fragment.len;
432 } /* while (received < body_size) */
434 DEBUG ("amqp plugin: camqp_read_body: body = %s", body);
437 } /* }}} int camqp_read_body */
439 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
444 status = amqp_simple_wait_frame (conf->connection, &frame);
448 status = (-1) * status;
449 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
450 sstrerror (status, errbuf, sizeof (errbuf)));
451 camqp_close_connection (conf);
455 if (frame.frame_type != AMQP_FRAME_HEADER)
457 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
462 return (camqp_read_body (conf, frame.payload.properties.body_size));
463 } /* }}} int camqp_read_header */
465 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
467 camqp_config_t *conf = user_data;
470 while (subscriber_threads_running)
474 status = camqp_connect (conf);
477 ERROR ("amqp plugin: camqp_connect failed. "
478 "Will sleep for %i seconds.", interval_g);
483 status = amqp_simple_wait_frame (conf->connection, &frame);
486 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
487 "Will sleep for %i seconds.", interval_g);
488 camqp_close_connection (conf);
493 if (frame.frame_type != AMQP_FRAME_METHOD)
495 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
500 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
502 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
503 frame.payload.method.id);
507 status = camqp_read_header (conf);
509 amqp_maybe_release_buffers (conf->connection);
510 } /* while (subscriber_threads_running) */
512 camqp_config_free (conf);
514 } /* }}} void *camqp_subscribe_thread */
516 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
521 tmp = realloc (subscriber_threads,
522 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
525 ERROR ("amqp plugin: realloc failed.");
526 camqp_config_free (conf);
529 subscriber_threads = tmp;
530 tmp = subscriber_threads + subscriber_threads_num;
531 memset (tmp, 0, sizeof (*tmp));
533 status = pthread_create (tmp, /* attr = */ NULL,
534 camqp_subscribe_thread, conf);
538 ERROR ("amqp plugin: pthread_create failed: %s",
539 sstrerror (status, errbuf, sizeof (errbuf)));
540 camqp_config_free (conf);
544 subscriber_threads_num++;
547 } /* }}} int camqp_subscribe_init */
549 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
552 amqp_basic_properties_t props;
555 status = camqp_connect (conf);
559 memset (&props, 0, sizeof (props));
560 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
561 | AMQP_BASIC_DELIVERY_MODE_FLAG
562 | AMQP_BASIC_APP_ID_FLAG;
563 props.content_type = amqp_cstring_bytes("application/json");
564 props.delivery_mode = conf->delivery_mode;
565 props.app_id = amqp_cstring_bytes("collectd");
567 status = amqp_basic_publish(conf->connection,
569 amqp_cstring_bytes(CONF(conf, exchange)),
570 amqp_cstring_bytes(CONF(conf, routingkey)),
574 amqp_cstring_bytes(buffer));
577 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
579 camqp_close_connection (conf);
583 } /* }}} int camqp_write_locked */
585 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
586 user_data_t *user_data)
588 camqp_config_t *conf = user_data->data;
594 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
597 memset (buffer, 0, sizeof (buffer));
598 bfree = sizeof (buffer);
601 format_json_initialize (buffer, &bfill, &bfree);
602 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
603 format_json_finalize (buffer, &bfill, &bfree);
605 pthread_mutex_lock (&conf->lock);
606 status = camqp_write_locked (conf, buffer);
607 pthread_mutex_unlock (&conf->lock);
610 } /* }}} int camqp_write */
612 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
615 camqp_config_t *conf;
619 conf = malloc (sizeof (*conf));
622 ERROR ("amqp plugin: malloc failed.");
626 /* Initialize "conf" {{{ */
627 memset (conf, 0, sizeof (*conf));
628 conf->publish = publish;
634 conf->password = NULL;
635 conf->exchange = NULL;
636 conf->exchange_type = NULL;
638 conf->routingkey = NULL;
639 conf->delivery_mode = CAMQP_DM_VOLATILE;
640 conf->store_rates = 0;
641 conf->connection = NULL;
642 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
645 status = cf_util_get_string (ci, &conf->name);
652 for (i = 0; i < ci->children_num; i++)
654 oconfig_item_t *child = ci->children + i;
656 if (strcasecmp ("Host", child->key) == 0)
657 status = cf_util_get_string (child, &conf->host);
658 else if (strcasecmp ("Port", child->key) == 0)
660 status = cf_util_get_port_number (child);
667 else if (strcasecmp ("VHost", child->key) == 0)
668 status = cf_util_get_string (child, &conf->vhost);
669 else if (strcasecmp ("User", child->key) == 0)
670 status = cf_util_get_string (child, &conf->user);
671 else if (strcasecmp ("Password", child->key) == 0)
672 status = cf_util_get_string (child, &conf->password);
673 else if (strcasecmp ("Exchange", child->key) == 0)
674 status = cf_util_get_string (child, &conf->exchange);
675 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
676 status = cf_util_get_string (child, &conf->exchange_type);
677 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
678 status = cf_util_get_string (child, &conf->queue);
679 else if (strcasecmp ("RoutingKey", child->key) == 0)
680 status = cf_util_get_string (child, &conf->routingkey);
681 else if (strcasecmp ("Persistent", child->key) == 0)
684 status = cf_util_get_boolean (child, &tmp);
686 conf->delivery_mode = CAMQP_DM_PERSISTENT;
688 conf->delivery_mode = CAMQP_DM_VOLATILE;
690 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
691 status = cf_util_get_boolean (child, &conf->store_rates);
693 WARNING ("amqp plugin: Ignoring unknown "
694 "configuration option \"%s\".", child->key);
698 } /* for (i = 0; i < ci->children_num; i++) */
700 if ((status == 0) && !publish && (conf->exchange == NULL))
702 if (conf->routingkey != NULL)
703 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
704 "without the \"Exchange\" option. It will be ignored.");
706 if (conf->exchange_type != NULL)
707 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
708 "without the \"Exchange\" option. It will be ignored.");
713 camqp_config_free (conf);
717 if (conf->exchange != NULL)
719 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
726 user_data_t ud = { conf, camqp_config_free };
728 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
730 status = plugin_register_write (cbname, camqp_write, &ud);
733 camqp_config_free (conf);
739 status = camqp_subscribe_init (conf);
742 camqp_config_free (conf);
748 } /* }}} int camqp_config_connection */
750 static int camqp_config (oconfig_item_t *ci) /* {{{ */
754 for (i = 0; i < ci->children_num; i++)
756 oconfig_item_t *child = ci->children + i;
758 if (strcasecmp ("Publish", child->key) == 0)
759 camqp_config_connection (child, /* publish = */ 1);
760 else if (strcasecmp ("Subscribe", child->key) == 0)
761 camqp_config_connection (child, /* publish = */ 0);
763 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
765 } /* for (ci->children_num) */
768 } /* }}} int camqp_config */
770 static int shutdown (void) /* {{{ */
774 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
775 subscriber_threads_num);
777 subscriber_threads_running = 0;
778 for (i = 0; i < subscriber_threads_num; i++)
780 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
781 * to use a timeout in the thread and check for the variable in regular
783 pthread_kill (subscriber_threads[i], SIGTERM);
784 pthread_join (subscriber_threads[i], /* retval = */ NULL);
787 subscriber_threads_num = 0;
788 sfree (subscriber_threads);
790 DEBUG ("amqp plugin: All subscriber threads exited.");
793 } /* }}} int shutdown */
795 void module_register (void)
797 plugin_register_complex_config ("amqp", camqp_config);
798 plugin_register_shutdown ("amqp", shutdown);
799 } /* void module_register */
801 /* vim: set sw=4 sts=4 et fdm=marker : */