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 if (conf->format == CAMQP_FORMAT_COMMAND)
475 status = handle_putval (stderr, body);
477 ERROR ("amqp plugin: handle_putval failed with status %i.",
481 else if (conf->format == CAMQP_FORMAT_JSON)
483 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
484 "been implemented yet. FIXME!");
489 ERROR ("amqp plugin: camqp_read_body: Unknown format option (%i).",
496 } /* }}} int camqp_read_body */
498 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
503 status = amqp_simple_wait_frame (conf->connection, &frame);
507 status = (-1) * status;
508 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
509 sstrerror (status, errbuf, sizeof (errbuf)));
510 camqp_close_connection (conf);
514 if (frame.frame_type != AMQP_FRAME_HEADER)
516 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
521 return (camqp_read_body (conf, frame.payload.properties.body_size));
522 } /* }}} int camqp_read_header */
524 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
526 camqp_config_t *conf = user_data;
529 while (subscriber_threads_running)
533 status = camqp_connect (conf);
536 ERROR ("amqp plugin: camqp_connect failed. "
537 "Will sleep for %i seconds.", interval_g);
542 status = amqp_simple_wait_frame (conf->connection, &frame);
545 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
546 "Will sleep for %i seconds.", interval_g);
547 camqp_close_connection (conf);
552 if (frame.frame_type != AMQP_FRAME_METHOD)
554 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
559 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
561 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
562 frame.payload.method.id);
566 status = camqp_read_header (conf);
568 amqp_maybe_release_buffers (conf->connection);
569 } /* while (subscriber_threads_running) */
571 camqp_config_free (conf);
573 } /* }}} void *camqp_subscribe_thread */
575 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
580 tmp = realloc (subscriber_threads,
581 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
584 ERROR ("amqp plugin: realloc failed.");
585 camqp_config_free (conf);
588 subscriber_threads = tmp;
589 tmp = subscriber_threads + subscriber_threads_num;
590 memset (tmp, 0, sizeof (*tmp));
592 status = pthread_create (tmp, /* attr = */ NULL,
593 camqp_subscribe_thread, conf);
597 ERROR ("amqp plugin: pthread_create failed: %s",
598 sstrerror (status, errbuf, sizeof (errbuf)));
599 camqp_config_free (conf);
603 subscriber_threads_num++;
606 } /* }}} int camqp_subscribe_init */
611 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
614 amqp_basic_properties_t props;
617 status = camqp_connect (conf);
621 memset (&props, 0, sizeof (props));
622 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
623 | AMQP_BASIC_DELIVERY_MODE_FLAG
624 | AMQP_BASIC_APP_ID_FLAG;
625 props.content_type = amqp_cstring_bytes("application/json");
626 props.delivery_mode = conf->delivery_mode;
627 props.app_id = amqp_cstring_bytes("collectd");
629 status = amqp_basic_publish(conf->connection,
631 amqp_cstring_bytes(CONF(conf, exchange)),
632 amqp_cstring_bytes(CONF(conf, routingkey)),
636 amqp_cstring_bytes(buffer));
639 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
641 camqp_close_connection (conf);
645 } /* }}} int camqp_write_locked */
647 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
648 user_data_t *user_data)
650 camqp_config_t *conf = user_data->data;
656 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
659 memset (buffer, 0, sizeof (buffer));
660 bfree = sizeof (buffer);
663 if (conf->format == CAMQP_FORMAT_COMMAND)
665 status = create_putval (buffer, sizeof (buffer), ds, vl);
668 ERROR ("amqp plugin: create_putval failed with status %i.",
673 else if (conf->format == CAMQP_FORMAT_JSON)
675 format_json_initialize (buffer, &bfill, &bfree);
676 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
677 format_json_finalize (buffer, &bfill, &bfree);
681 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
685 pthread_mutex_lock (&conf->lock);
686 status = camqp_write_locked (conf, buffer);
687 pthread_mutex_unlock (&conf->lock);
690 } /* }}} int camqp_write */
695 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
696 camqp_config_t *conf)
702 status = cf_util_get_string (ci, &string);
706 assert (string != NULL);
707 if (strcasecmp ("Command", string) == 0)
708 conf->format = CAMQP_FORMAT_COMMAND;
709 else if (strcasecmp ("JSON", string) == 0)
710 conf->format = CAMQP_FORMAT_JSON;
713 WARNING ("amqp plugin: Invalid format string: %s",
720 } /* }}} int config_set_string */
722 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
725 camqp_config_t *conf;
729 conf = malloc (sizeof (*conf));
732 ERROR ("amqp plugin: malloc failed.");
736 /* Initialize "conf" {{{ */
737 memset (conf, 0, sizeof (*conf));
738 conf->publish = publish;
740 conf->format = CAMQP_FORMAT_COMMAND;
745 conf->password = NULL;
746 conf->exchange = NULL;
747 conf->routingkey = NULL;
749 conf->delivery_mode = CAMQP_DM_VOLATILE;
750 conf->store_rates = 0;
752 conf->exchange_type = NULL;
755 conf->connection = NULL;
756 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
759 status = cf_util_get_string (ci, &conf->name);
766 for (i = 0; i < ci->children_num; i++)
768 oconfig_item_t *child = ci->children + i;
770 if (strcasecmp ("Format", child->key) == 0)
771 status = camqp_config_set_format (child, conf);
772 else if (strcasecmp ("Host", child->key) == 0)
773 status = cf_util_get_string (child, &conf->host);
774 else if (strcasecmp ("Port", child->key) == 0)
776 status = cf_util_get_port_number (child);
783 else if (strcasecmp ("VHost", child->key) == 0)
784 status = cf_util_get_string (child, &conf->vhost);
785 else if (strcasecmp ("User", child->key) == 0)
786 status = cf_util_get_string (child, &conf->user);
787 else if (strcasecmp ("Password", child->key) == 0)
788 status = cf_util_get_string (child, &conf->password);
789 else if (strcasecmp ("Exchange", child->key) == 0)
790 status = cf_util_get_string (child, &conf->exchange);
791 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
792 status = cf_util_get_string (child, &conf->exchange_type);
793 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
794 status = cf_util_get_string (child, &conf->queue);
795 else if (strcasecmp ("RoutingKey", child->key) == 0)
796 status = cf_util_get_string (child, &conf->routingkey);
797 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
800 status = cf_util_get_boolean (child, &tmp);
802 conf->delivery_mode = CAMQP_DM_PERSISTENT;
804 conf->delivery_mode = CAMQP_DM_VOLATILE;
806 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
807 status = cf_util_get_boolean (child, &conf->store_rates);
809 WARNING ("amqp plugin: Ignoring unknown "
810 "configuration option \"%s\".", child->key);
814 } /* for (i = 0; i < ci->children_num; i++) */
816 if ((status == 0) && !publish && (conf->exchange == NULL))
818 if (conf->routingkey != NULL)
819 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
820 "without the \"Exchange\" option. It will be ignored.");
822 if (conf->exchange_type != NULL)
823 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
824 "without the \"Exchange\" option. It will be ignored.");
829 camqp_config_free (conf);
833 if (conf->exchange != NULL)
835 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
842 user_data_t ud = { conf, camqp_config_free };
844 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
846 status = plugin_register_write (cbname, camqp_write, &ud);
849 camqp_config_free (conf);
855 status = camqp_subscribe_init (conf);
858 camqp_config_free (conf);
864 } /* }}} int camqp_config_connection */
866 static int camqp_config (oconfig_item_t *ci) /* {{{ */
870 for (i = 0; i < ci->children_num; i++)
872 oconfig_item_t *child = ci->children + i;
874 if (strcasecmp ("Publish", child->key) == 0)
875 camqp_config_connection (child, /* publish = */ 1);
876 else if (strcasecmp ("Subscribe", child->key) == 0)
877 camqp_config_connection (child, /* publish = */ 0);
879 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
881 } /* for (ci->children_num) */
884 } /* }}} int camqp_config */
886 void module_register (void)
888 plugin_register_complex_config ("amqp", camqp_config);
889 plugin_register_shutdown ("amqp", shutdown);
890 } /* void module_register */
892 /* vim: set sw=4 sts=4 et fdm=marker : */