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_cmd_putval.h"
39 #include "utils_format_json.h"
42 #include <amqp_framing.h>
44 /* Defines for the delivery mode. I have no idea why they're not defined by the
46 #define CAMQP_DM_VOLATILE 1
47 #define CAMQP_DM_PERSISTENT 2
49 #define CAMQP_FORMAT_COMMAND 1
50 #define CAMQP_FORMAT_JSON 2
52 #define CAMQP_CHANNEL 1
73 uint8_t delivery_mode;
80 amqp_connection_state_t connection;
83 typedef struct camqp_config_s camqp_config_t;
88 static const char *def_host = "localhost";
89 static const char *def_vhost = "/";
90 static const char *def_user = "guest";
91 static const char *def_password = "guest";
92 static const char *def_exchange = "amq.fanout";
93 static const char *def_routingkey = "collectd";
95 static pthread_t *subscriber_threads = NULL;
96 static size_t subscriber_threads_num = 0;
97 static _Bool subscriber_threads_running = 1;
99 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
104 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
108 if ((conf == NULL) || (conf->connection == NULL))
111 sockfd = amqp_get_sockfd (conf->connection);
112 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
113 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
114 amqp_destroy_connection (conf->connection);
116 conf->connection = NULL;
117 } /* }}} void camqp_close_connection */
119 static void camqp_config_free (void *ptr) /* {{{ */
121 camqp_config_t *conf = ptr;
126 camqp_close_connection (conf);
132 sfree (conf->password);
133 sfree (conf->exchange);
134 sfree (conf->exchange_type);
136 sfree (conf->routingkey);
139 } /* }}} void camqp_config_free */
141 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
145 if ((in == NULL) || (in->bytes == NULL))
148 ret = malloc (in->len + 1);
152 memcpy (ret, in->bytes, in->len);
156 } /* }}} char *camqp_bytes_cstring */
158 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
162 r = amqp_get_rpc_reply (conf->connection);
163 if (r.reply_type == AMQP_RESPONSE_NORMAL)
167 } /* }}} _Bool camqp_is_error */
169 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
170 char *buffer, size_t buffer_size)
174 r = amqp_get_rpc_reply (conf->connection);
175 switch (r.reply_type)
177 case AMQP_RESPONSE_NORMAL:
178 sstrncpy (buffer, "Success", sizeof (buffer));
181 case AMQP_RESPONSE_NONE:
182 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
185 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
187 return (sstrerror (r.library_errno, buffer, buffer_size));
189 sstrncpy (buffer, "End of stream", sizeof (buffer));
192 case AMQP_RESPONSE_SERVER_EXCEPTION:
193 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
195 amqp_connection_close_t *m = r.reply.decoded;
196 char *tmp = camqp_bytes_cstring (&m->reply_text);
197 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
201 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
203 amqp_channel_close_t *m = r.reply.decoded;
204 char *tmp = camqp_bytes_cstring (&m->reply_text);
205 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
211 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
217 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
222 } /* }}} char *camqp_strerror */
224 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
226 amqp_queue_declare_ok_t *qd_ret;
227 amqp_basic_consume_ok_t *cm_ret;
229 qd_ret = amqp_queue_declare (conf->connection,
230 /* channel = */ CAMQP_CHANNEL,
231 /* queue = */ (conf->queue != NULL)
232 ? amqp_cstring_bytes (conf->queue)
237 /* auto_delete = */ 1,
238 /* arguments = */ AMQP_EMPTY_TABLE);
241 ERROR ("amqp plugin: amqp_queue_declare failed.");
242 camqp_close_connection (conf);
246 if (conf->queue == NULL)
248 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
249 if (conf->queue == NULL)
251 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
252 camqp_close_connection (conf);
256 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
258 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
260 /* bind to an exchange */
261 if (conf->exchange != NULL)
263 amqp_queue_bind_ok_t *qb_ret;
265 /* create the exchange */
266 if (conf->exchange_type != NULL)
268 amqp_exchange_declare_ok_t *ed_ret;
270 ed_ret = amqp_exchange_declare (conf->connection,
271 /* channel = */ CAMQP_CHANNEL,
272 /* exchange = */ amqp_cstring_bytes (conf->exchange),
273 /* type = */ amqp_cstring_bytes (conf->exchange_type),
276 /* auto_delete = */ 1,
277 /* arguments = */ AMQP_EMPTY_TABLE);
278 if ((ed_ret == NULL) && camqp_is_error (conf))
281 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
282 camqp_strerror (conf, errbuf, sizeof (errbuf)));
283 camqp_close_connection (conf);
288 DEBUG ("amqp plugin: queue = %s; exchange = %s; routing_key = %s;",
289 conf->queue, conf->exchange, CONF (conf, routingkey));
291 assert (conf->queue != NULL);
292 qb_ret = amqp_queue_bind (conf->connection,
293 /* channel = */ CAMQP_CHANNEL,
294 /* queue = */ amqp_cstring_bytes (conf->queue),
295 /* exchange = */ amqp_cstring_bytes (conf->exchange),
297 /* routing_key = */ amqp_cstring_bytes (CONF (conf, routingkey)),
299 /* routing_key = */ AMQP_EMPTY_BYTES,
301 /* arguments = */ AMQP_EMPTY_TABLE);
302 if ((qb_ret == NULL) && camqp_is_error (conf))
305 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
306 camqp_strerror (conf, errbuf, sizeof (errbuf)));
307 camqp_close_connection (conf);
311 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
312 conf->queue, conf->exchange);
313 } /* if (conf->exchange != NULL) */
315 cm_ret = amqp_basic_consume (conf->connection,
316 /* channel = */ CAMQP_CHANNEL,
317 /* queue = */ amqp_cstring_bytes (conf->queue),
318 /* consumer_tag = */ AMQP_EMPTY_BYTES,
321 /* exclusive = */ 0);
322 if ((cm_ret == NULL) && camqp_is_error (conf))
325 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
326 camqp_strerror (conf, errbuf, sizeof (errbuf)));
327 camqp_close_connection (conf);
332 } /* }}} int camqp_setup_queue */
334 static int camqp_connect (camqp_config_t *conf) /* {{{ */
336 amqp_rpc_reply_t reply;
340 if (conf->connection != NULL)
343 conf->connection = amqp_new_connection ();
344 if (conf->connection == NULL)
346 ERROR ("amqp plugin: amqp_new_connection failed.");
350 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
354 status = (-1) * sockfd;
355 ERROR ("amqp plugin: amqp_open_socket failed: %s",
356 sstrerror (status, errbuf, sizeof (errbuf)));
357 amqp_destroy_connection (conf->connection);
358 conf->connection = NULL;
361 amqp_set_sockfd (conf->connection, sockfd);
363 reply = amqp_login (conf->connection, CONF(conf, vhost),
364 /* channel max = */ 0,
365 /* frame max = */ 131072,
367 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
368 CONF(conf, user), CONF(conf, password));
369 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
371 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
372 CONF(conf, vhost), CONF(conf, user));
373 amqp_destroy_connection (conf->connection);
375 conf->connection = NULL;
379 amqp_channel_open (conf->connection, /* channel = */ 1);
380 /* FIXME: Is checking "reply.reply_type" really correct here? How does
381 * it get set? --octo */
382 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
384 ERROR ("amqp plugin: amqp_channel_open failed.");
385 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
386 amqp_destroy_connection (conf->connection);
388 conf->connection = NULL;
392 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
393 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
396 return (camqp_setup_queue (conf));
398 } /* }}} int camqp_connect */
400 static int shutdown (void) /* {{{ */
404 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
405 subscriber_threads_num);
407 subscriber_threads_running = 0;
408 for (i = 0; i < subscriber_threads_num; i++)
410 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
411 * to use a timeout in the thread and check for the variable in regular
413 pthread_kill (subscriber_threads[i], SIGTERM);
414 pthread_join (subscriber_threads[i], /* retval = */ NULL);
417 subscriber_threads_num = 0;
418 sfree (subscriber_threads);
420 DEBUG ("amqp plugin: All subscriber threads exited.");
423 } /* }}} int shutdown */
428 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
431 char body[body_size + 1];
437 memset (body, 0, sizeof (body));
441 while (received < body_size)
443 status = amqp_simple_wait_frame (conf->connection, &frame);
447 status = (-1) * status;
448 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
449 sstrerror (status, errbuf, sizeof (errbuf)));
450 camqp_close_connection (conf);
454 if (frame.frame_type != AMQP_FRAME_BODY)
456 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
461 if ((body_size - received) < frame.payload.body_fragment.len)
463 WARNING ("amqp plugin: Body is larger than indicated by header.");
467 memcpy (body_ptr, frame.payload.body_fragment.bytes,
468 frame.payload.body_fragment.len);
469 body_ptr += frame.payload.body_fragment.len;
470 received += frame.payload.body_fragment.len;
471 } /* while (received < body_size) */
473 DEBUG ("amqp plugin: camqp_read_body: body = %s", body);
476 } /* }}} int camqp_read_body */
478 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
483 status = amqp_simple_wait_frame (conf->connection, &frame);
487 status = (-1) * status;
488 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
489 sstrerror (status, errbuf, sizeof (errbuf)));
490 camqp_close_connection (conf);
494 if (frame.frame_type != AMQP_FRAME_HEADER)
496 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
501 return (camqp_read_body (conf, frame.payload.properties.body_size));
502 } /* }}} int camqp_read_header */
504 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
506 camqp_config_t *conf = user_data;
509 while (subscriber_threads_running)
513 status = camqp_connect (conf);
516 ERROR ("amqp plugin: camqp_connect failed. "
517 "Will sleep for %i seconds.", interval_g);
522 status = amqp_simple_wait_frame (conf->connection, &frame);
525 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
526 "Will sleep for %i seconds.", interval_g);
527 camqp_close_connection (conf);
532 if (frame.frame_type != AMQP_FRAME_METHOD)
534 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
539 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
541 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
542 frame.payload.method.id);
546 status = camqp_read_header (conf);
548 amqp_maybe_release_buffers (conf->connection);
549 } /* while (subscriber_threads_running) */
551 camqp_config_free (conf);
553 } /* }}} void *camqp_subscribe_thread */
555 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
560 tmp = realloc (subscriber_threads,
561 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
564 ERROR ("amqp plugin: realloc failed.");
565 camqp_config_free (conf);
568 subscriber_threads = tmp;
569 tmp = subscriber_threads + subscriber_threads_num;
570 memset (tmp, 0, sizeof (*tmp));
572 status = pthread_create (tmp, /* attr = */ NULL,
573 camqp_subscribe_thread, conf);
577 ERROR ("amqp plugin: pthread_create failed: %s",
578 sstrerror (status, errbuf, sizeof (errbuf)));
579 camqp_config_free (conf);
583 subscriber_threads_num++;
586 } /* }}} int camqp_subscribe_init */
591 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
594 amqp_basic_properties_t props;
597 status = camqp_connect (conf);
601 memset (&props, 0, sizeof (props));
602 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
603 | AMQP_BASIC_DELIVERY_MODE_FLAG
604 | AMQP_BASIC_APP_ID_FLAG;
605 props.content_type = amqp_cstring_bytes("application/json");
606 props.delivery_mode = conf->delivery_mode;
607 props.app_id = amqp_cstring_bytes("collectd");
609 status = amqp_basic_publish(conf->connection,
611 amqp_cstring_bytes(CONF(conf, exchange)),
612 amqp_cstring_bytes(CONF(conf, routingkey)),
616 amqp_cstring_bytes(buffer));
619 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
621 camqp_close_connection (conf);
625 } /* }}} int camqp_write_locked */
627 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
628 user_data_t *user_data)
630 camqp_config_t *conf = user_data->data;
636 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
639 memset (buffer, 0, sizeof (buffer));
640 bfree = sizeof (buffer);
643 if (conf->format == CAMQP_FORMAT_COMMAND)
645 status = create_putval (buffer, sizeof (buffer), ds, vl);
648 ERROR ("amqp plugin: create_putval failed with status %i.",
653 else if (conf->format == CAMQP_FORMAT_JSON)
655 format_json_initialize (buffer, &bfill, &bfree);
656 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
657 format_json_finalize (buffer, &bfill, &bfree);
661 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
665 pthread_mutex_lock (&conf->lock);
666 status = camqp_write_locked (conf, buffer);
667 pthread_mutex_unlock (&conf->lock);
670 } /* }}} int camqp_write */
675 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
676 camqp_config_t *conf)
682 status = cf_util_get_string (ci, &string);
686 assert (string != NULL);
687 if (strcasecmp ("Command", string) == 0)
688 conf->format = CAMQP_FORMAT_COMMAND;
689 else if (strcasecmp ("JSON", string) == 0)
690 conf->format = CAMQP_FORMAT_JSON;
693 WARNING ("amqp plugin: Invalid format string: %s",
700 } /* }}} int config_set_string */
702 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
705 camqp_config_t *conf;
709 conf = malloc (sizeof (*conf));
712 ERROR ("amqp plugin: malloc failed.");
716 /* Initialize "conf" {{{ */
717 memset (conf, 0, sizeof (*conf));
718 conf->publish = publish;
720 conf->format = CAMQP_FORMAT_COMMAND;
725 conf->password = NULL;
726 conf->exchange = NULL;
727 conf->routingkey = NULL;
729 conf->delivery_mode = CAMQP_DM_VOLATILE;
730 conf->store_rates = 0;
732 conf->exchange_type = NULL;
735 conf->connection = NULL;
736 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
739 status = cf_util_get_string (ci, &conf->name);
746 for (i = 0; i < ci->children_num; i++)
748 oconfig_item_t *child = ci->children + i;
750 if (strcasecmp ("Format", child->key) == 0)
751 status = camqp_config_set_format (child, conf);
752 else if (strcasecmp ("Host", child->key) == 0)
753 status = cf_util_get_string (child, &conf->host);
754 else if (strcasecmp ("Port", child->key) == 0)
756 status = cf_util_get_port_number (child);
763 else if (strcasecmp ("VHost", child->key) == 0)
764 status = cf_util_get_string (child, &conf->vhost);
765 else if (strcasecmp ("User", child->key) == 0)
766 status = cf_util_get_string (child, &conf->user);
767 else if (strcasecmp ("Password", child->key) == 0)
768 status = cf_util_get_string (child, &conf->password);
769 else if (strcasecmp ("Exchange", child->key) == 0)
770 status = cf_util_get_string (child, &conf->exchange);
771 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
772 status = cf_util_get_string (child, &conf->exchange_type);
773 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
774 status = cf_util_get_string (child, &conf->queue);
775 else if (strcasecmp ("RoutingKey", child->key) == 0)
776 status = cf_util_get_string (child, &conf->routingkey);
777 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
780 status = cf_util_get_boolean (child, &tmp);
782 conf->delivery_mode = CAMQP_DM_PERSISTENT;
784 conf->delivery_mode = CAMQP_DM_VOLATILE;
786 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
787 status = cf_util_get_boolean (child, &conf->store_rates);
789 WARNING ("amqp plugin: Ignoring unknown "
790 "configuration option \"%s\".", child->key);
794 } /* for (i = 0; i < ci->children_num; i++) */
796 if ((status == 0) && !publish && (conf->exchange == NULL))
798 if (conf->routingkey != NULL)
799 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
800 "without the \"Exchange\" option. It will be ignored.");
802 if (conf->exchange_type != NULL)
803 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
804 "without the \"Exchange\" option. It will be ignored.");
809 camqp_config_free (conf);
813 if (conf->exchange != NULL)
815 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
822 user_data_t ud = { conf, camqp_config_free };
824 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
826 status = plugin_register_write (cbname, camqp_write, &ud);
829 camqp_config_free (conf);
835 status = camqp_subscribe_init (conf);
838 camqp_config_free (conf);
844 } /* }}} int camqp_config_connection */
846 static int camqp_config (oconfig_item_t *ci) /* {{{ */
850 for (i = 0; i < ci->children_num; i++)
852 oconfig_item_t *child = ci->children + i;
854 if (strcasecmp ("Publish", child->key) == 0)
855 camqp_config_connection (child, /* publish = */ 1);
856 else if (strcasecmp ("Subscribe", child->key) == 0)
857 camqp_config_connection (child, /* publish = */ 0);
859 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
861 } /* for (ci->children_num) */
864 } /* }}} int camqp_config */
866 void module_register (void)
868 plugin_register_complex_config ("amqp", camqp_config);
869 plugin_register_shutdown ("amqp", shutdown);
870 } /* void module_register */
872 /* vim: set sw=4 sts=4 et fdm=marker : */