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;
75 amqp_connection_state_t connection;
78 typedef struct camqp_config_s camqp_config_t;
83 static const char *def_host = "localhost";
84 static const char *def_vhost = "/";
85 static const char *def_user = "guest";
86 static const char *def_password = "guest";
87 static const char *def_exchange = "amq.fanout";
88 static const char *def_routingkey = "collectd";
90 static pthread_t *subscriber_threads = NULL;
91 static size_t subscriber_threads_num = 0;
92 static _Bool subscriber_threads_running = 1;
94 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
99 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
103 if ((conf == NULL) || (conf->connection == NULL))
106 sockfd = amqp_get_sockfd (conf->connection);
107 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
108 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
109 amqp_destroy_connection (conf->connection);
111 conf->connection = NULL;
112 } /* }}} void camqp_close_connection */
114 static void camqp_config_free (void *ptr) /* {{{ */
116 camqp_config_t *conf = ptr;
121 camqp_close_connection (conf);
127 sfree (conf->password);
128 sfree (conf->exchange);
129 sfree (conf->exchange_type);
131 sfree (conf->routingkey);
134 } /* }}} void camqp_config_free */
136 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
140 if ((in == NULL) || (in->bytes == NULL))
143 ret = malloc (in->len + 1);
147 memcpy (ret, in->bytes, in->len);
151 } /* }}} char *camqp_bytes_cstring */
153 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
157 r = amqp_get_rpc_reply (conf->connection);
158 if (r.reply_type == AMQP_RESPONSE_NORMAL)
162 } /* }}} _Bool camqp_is_error */
164 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
165 char *buffer, size_t buffer_size)
169 r = amqp_get_rpc_reply (conf->connection);
170 switch (r.reply_type)
172 case AMQP_RESPONSE_NORMAL:
173 sstrncpy (buffer, "Success", sizeof (buffer));
176 case AMQP_RESPONSE_NONE:
177 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
180 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
182 return (sstrerror (r.library_errno, buffer, buffer_size));
184 sstrncpy (buffer, "End of stream", sizeof (buffer));
187 case AMQP_RESPONSE_SERVER_EXCEPTION:
188 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
190 amqp_connection_close_t *m = r.reply.decoded;
191 char *tmp = camqp_bytes_cstring (&m->reply_text);
192 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
196 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
198 amqp_channel_close_t *m = r.reply.decoded;
199 char *tmp = camqp_bytes_cstring (&m->reply_text);
200 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
206 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
212 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
217 } /* }}} char *camqp_strerror */
219 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
221 amqp_queue_declare_ok_t *qd_ret;
222 amqp_basic_consume_ok_t *cm_ret;
224 qd_ret = amqp_queue_declare (conf->connection,
225 /* channel = */ CAMQP_CHANNEL,
226 /* queue = */ (conf->queue != NULL)
227 ? amqp_cstring_bytes (conf->queue)
232 /* auto_delete = */ 1,
233 /* arguments = */ AMQP_EMPTY_TABLE);
236 ERROR ("amqp plugin: amqp_queue_declare failed.");
237 camqp_close_connection (conf);
241 if (conf->queue == NULL)
243 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
244 if (conf->queue == NULL)
246 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
247 camqp_close_connection (conf);
251 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
253 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
255 /* bind to an exchange */
256 if (conf->exchange != NULL)
258 amqp_queue_bind_ok_t *qb_ret;
260 /* create the exchange */
261 if (conf->exchange_type != NULL)
263 amqp_exchange_declare_ok_t *ed_ret;
265 ed_ret = amqp_exchange_declare (conf->connection,
266 /* channel = */ CAMQP_CHANNEL,
267 /* exchange = */ amqp_cstring_bytes (conf->exchange),
268 /* type = */ amqp_cstring_bytes (conf->exchange_type),
271 /* auto_delete = */ 1,
272 /* arguments = */ AMQP_EMPTY_TABLE);
273 if ((ed_ret == NULL) && camqp_is_error (conf))
276 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
277 camqp_strerror (conf, errbuf, sizeof (errbuf)));
278 camqp_close_connection (conf);
283 DEBUG ("amqp plugin: queue = %s; exchange = %s; routing_key = %s;",
284 conf->queue, conf->exchange, CONF (conf, routingkey));
286 assert (conf->queue != NULL);
287 qb_ret = amqp_queue_bind (conf->connection,
288 /* channel = */ CAMQP_CHANNEL,
289 /* queue = */ amqp_cstring_bytes (conf->queue),
290 /* exchange = */ amqp_cstring_bytes (conf->exchange),
292 /* routing_key = */ amqp_cstring_bytes (CONF (conf, routingkey)),
294 /* routing_key = */ AMQP_EMPTY_BYTES,
296 /* arguments = */ AMQP_EMPTY_TABLE);
297 if ((qb_ret == NULL) && camqp_is_error (conf))
300 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
301 camqp_strerror (conf, errbuf, sizeof (errbuf)));
302 camqp_close_connection (conf);
306 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
307 conf->queue, conf->exchange);
308 } /* if (conf->exchange != NULL) */
310 cm_ret = amqp_basic_consume (conf->connection,
311 /* channel = */ CAMQP_CHANNEL,
312 /* queue = */ amqp_cstring_bytes (conf->queue),
313 /* consumer_tag = */ AMQP_EMPTY_BYTES,
316 /* exclusive = */ 0);
317 if ((cm_ret == NULL) && camqp_is_error (conf))
320 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
321 camqp_strerror (conf, errbuf, sizeof (errbuf)));
322 camqp_close_connection (conf);
327 } /* }}} int camqp_setup_queue */
329 static int camqp_connect (camqp_config_t *conf) /* {{{ */
331 amqp_rpc_reply_t reply;
335 if (conf->connection != NULL)
338 conf->connection = amqp_new_connection ();
339 if (conf->connection == NULL)
341 ERROR ("amqp plugin: amqp_new_connection failed.");
345 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
349 status = (-1) * sockfd;
350 ERROR ("amqp plugin: amqp_open_socket failed: %s",
351 sstrerror (status, errbuf, sizeof (errbuf)));
352 amqp_destroy_connection (conf->connection);
353 conf->connection = NULL;
356 amqp_set_sockfd (conf->connection, sockfd);
358 reply = amqp_login (conf->connection, CONF(conf, vhost),
359 /* channel max = */ 0,
360 /* frame max = */ 131072,
362 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
363 CONF(conf, user), CONF(conf, password));
364 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
366 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
367 CONF(conf, vhost), CONF(conf, user));
368 amqp_destroy_connection (conf->connection);
370 conf->connection = NULL;
374 amqp_channel_open (conf->connection, /* channel = */ 1);
375 /* FIXME: Is checking "reply.reply_type" really correct here? How does
376 * it get set? --octo */
377 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
379 ERROR ("amqp plugin: amqp_channel_open failed.");
380 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
381 amqp_destroy_connection (conf->connection);
383 conf->connection = NULL;
387 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
388 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
391 return (camqp_setup_queue (conf));
393 } /* }}} int camqp_connect */
395 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
398 char body[body_size + 1];
404 memset (body, 0, sizeof (body));
408 while (received < body_size)
410 status = amqp_simple_wait_frame (conf->connection, &frame);
414 status = (-1) * status;
415 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
416 sstrerror (status, errbuf, sizeof (errbuf)));
417 camqp_close_connection (conf);
421 if (frame.frame_type != AMQP_FRAME_BODY)
423 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
428 if ((body_size - received) < frame.payload.body_fragment.len)
430 WARNING ("amqp plugin: Body is larger than indicated by header.");
434 memcpy (body_ptr, frame.payload.body_fragment.bytes,
435 frame.payload.body_fragment.len);
436 body_ptr += frame.payload.body_fragment.len;
437 received += frame.payload.body_fragment.len;
438 } /* while (received < body_size) */
440 DEBUG ("amqp plugin: camqp_read_body: body = %s", body);
443 } /* }}} int camqp_read_body */
445 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
450 status = amqp_simple_wait_frame (conf->connection, &frame);
454 status = (-1) * status;
455 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
456 sstrerror (status, errbuf, sizeof (errbuf)));
457 camqp_close_connection (conf);
461 if (frame.frame_type != AMQP_FRAME_HEADER)
463 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
468 return (camqp_read_body (conf, frame.payload.properties.body_size));
469 } /* }}} int camqp_read_header */
471 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
473 camqp_config_t *conf = user_data;
476 while (subscriber_threads_running)
480 status = camqp_connect (conf);
483 ERROR ("amqp plugin: camqp_connect failed. "
484 "Will sleep for %i seconds.", interval_g);
489 status = amqp_simple_wait_frame (conf->connection, &frame);
492 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
493 "Will sleep for %i seconds.", interval_g);
494 camqp_close_connection (conf);
499 if (frame.frame_type != AMQP_FRAME_METHOD)
501 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
506 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
508 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
509 frame.payload.method.id);
513 status = camqp_read_header (conf);
515 amqp_maybe_release_buffers (conf->connection);
516 } /* while (subscriber_threads_running) */
518 camqp_config_free (conf);
520 } /* }}} void *camqp_subscribe_thread */
522 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
527 tmp = realloc (subscriber_threads,
528 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
531 ERROR ("amqp plugin: realloc failed.");
532 camqp_config_free (conf);
535 subscriber_threads = tmp;
536 tmp = subscriber_threads + subscriber_threads_num;
537 memset (tmp, 0, sizeof (*tmp));
539 status = pthread_create (tmp, /* attr = */ NULL,
540 camqp_subscribe_thread, conf);
544 ERROR ("amqp plugin: pthread_create failed: %s",
545 sstrerror (status, errbuf, sizeof (errbuf)));
546 camqp_config_free (conf);
550 subscriber_threads_num++;
553 } /* }}} int camqp_subscribe_init */
555 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
558 amqp_basic_properties_t props;
561 status = camqp_connect (conf);
565 memset (&props, 0, sizeof (props));
566 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
567 | AMQP_BASIC_DELIVERY_MODE_FLAG
568 | AMQP_BASIC_APP_ID_FLAG;
569 props.content_type = amqp_cstring_bytes("application/json");
570 props.delivery_mode = conf->delivery_mode;
571 props.app_id = amqp_cstring_bytes("collectd");
573 status = amqp_basic_publish(conf->connection,
575 amqp_cstring_bytes(CONF(conf, exchange)),
576 amqp_cstring_bytes(CONF(conf, routingkey)),
580 amqp_cstring_bytes(buffer));
583 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
585 camqp_close_connection (conf);
589 } /* }}} int camqp_write_locked */
591 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
592 user_data_t *user_data)
594 camqp_config_t *conf = user_data->data;
600 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
603 memset (buffer, 0, sizeof (buffer));
604 bfree = sizeof (buffer);
607 format_json_initialize (buffer, &bfill, &bfree);
608 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
609 format_json_finalize (buffer, &bfill, &bfree);
611 pthread_mutex_lock (&conf->lock);
612 status = camqp_write_locked (conf, buffer);
613 pthread_mutex_unlock (&conf->lock);
616 } /* }}} int camqp_write */
618 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
621 camqp_config_t *conf;
625 conf = malloc (sizeof (*conf));
628 ERROR ("amqp plugin: malloc failed.");
632 /* Initialize "conf" {{{ */
633 memset (conf, 0, sizeof (*conf));
634 conf->publish = publish;
640 conf->password = NULL;
641 conf->exchange = NULL;
642 conf->routingkey = NULL;
644 conf->delivery_mode = CAMQP_DM_VOLATILE;
645 conf->store_rates = 0;
647 conf->exchange_type = NULL;
650 conf->connection = NULL;
651 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
654 status = cf_util_get_string (ci, &conf->name);
661 for (i = 0; i < ci->children_num; i++)
663 oconfig_item_t *child = ci->children + i;
665 if (strcasecmp ("Host", child->key) == 0)
666 status = cf_util_get_string (child, &conf->host);
667 else if (strcasecmp ("Port", child->key) == 0)
669 status = cf_util_get_port_number (child);
676 else if (strcasecmp ("VHost", child->key) == 0)
677 status = cf_util_get_string (child, &conf->vhost);
678 else if (strcasecmp ("User", child->key) == 0)
679 status = cf_util_get_string (child, &conf->user);
680 else if (strcasecmp ("Password", child->key) == 0)
681 status = cf_util_get_string (child, &conf->password);
682 else if (strcasecmp ("Exchange", child->key) == 0)
683 status = cf_util_get_string (child, &conf->exchange);
684 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
685 status = cf_util_get_string (child, &conf->exchange_type);
686 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
687 status = cf_util_get_string (child, &conf->queue);
688 else if (strcasecmp ("RoutingKey", child->key) == 0)
689 status = cf_util_get_string (child, &conf->routingkey);
690 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
693 status = cf_util_get_boolean (child, &tmp);
695 conf->delivery_mode = CAMQP_DM_PERSISTENT;
697 conf->delivery_mode = CAMQP_DM_VOLATILE;
699 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
700 status = cf_util_get_boolean (child, &conf->store_rates);
702 WARNING ("amqp plugin: Ignoring unknown "
703 "configuration option \"%s\".", child->key);
707 } /* for (i = 0; i < ci->children_num; i++) */
709 if ((status == 0) && !publish && (conf->exchange == NULL))
711 if (conf->routingkey != NULL)
712 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
713 "without the \"Exchange\" option. It will be ignored.");
715 if (conf->exchange_type != NULL)
716 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
717 "without the \"Exchange\" option. It will be ignored.");
722 camqp_config_free (conf);
726 if (conf->exchange != NULL)
728 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
735 user_data_t ud = { conf, camqp_config_free };
737 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
739 status = plugin_register_write (cbname, camqp_write, &ud);
742 camqp_config_free (conf);
748 status = camqp_subscribe_init (conf);
751 camqp_config_free (conf);
757 } /* }}} int camqp_config_connection */
759 static int camqp_config (oconfig_item_t *ci) /* {{{ */
763 for (i = 0; i < ci->children_num; i++)
765 oconfig_item_t *child = ci->children + i;
767 if (strcasecmp ("Publish", child->key) == 0)
768 camqp_config_connection (child, /* publish = */ 1);
769 else if (strcasecmp ("Subscribe", child->key) == 0)
770 camqp_config_connection (child, /* publish = */ 0);
772 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
774 } /* for (ci->children_num) */
777 } /* }}} int camqp_config */
779 static int shutdown (void) /* {{{ */
783 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
784 subscriber_threads_num);
786 subscriber_threads_running = 0;
787 for (i = 0; i < subscriber_threads_num; i++)
789 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
790 * to use a timeout in the thread and check for the variable in regular
792 pthread_kill (subscriber_threads[i], SIGTERM);
793 pthread_join (subscriber_threads[i], /* retval = */ NULL);
796 subscriber_threads_num = 0;
797 sfree (subscriber_threads);
799 DEBUG ("amqp plugin: All subscriber threads exited.");
802 } /* }}} int shutdown */
804 void module_register (void)
806 plugin_register_complex_config ("amqp", camqp_config);
807 plugin_register_shutdown ("amqp", shutdown);
808 } /* void module_register */
810 /* vim: set sw=4 sts=4 et fdm=marker : */