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>
32 #include "utils_cmd_putval.h"
33 #include "utils_format_json.h"
38 #include <amqp_framing.h>
40 /* Defines for the delivery mode. I have no idea why they're not defined by the
42 #define CAMQP_DM_VOLATILE 1
43 #define CAMQP_DM_PERSISTENT 2
45 #define CAMQP_FORMAT_COMMAND 1
46 #define CAMQP_FORMAT_JSON 2
48 #define CAMQP_CHANNEL 1
68 uint8_t delivery_mode;
76 amqp_connection_state_t connection;
79 typedef struct camqp_config_s camqp_config_t;
84 static const char *def_host = "localhost";
85 static const char *def_vhost = "/";
86 static const char *def_user = "guest";
87 static const char *def_password = "guest";
88 static const char *def_exchange = "amq.fanout";
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->routing_key);
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_create_exchange (camqp_config_t *conf) /* {{{ */
221 amqp_exchange_declare_ok_t *ed_ret;
223 if (conf->exchange_type == NULL)
226 ed_ret = amqp_exchange_declare (conf->connection,
227 /* channel = */ CAMQP_CHANNEL,
228 /* exchange = */ amqp_cstring_bytes (conf->exchange),
229 /* type = */ amqp_cstring_bytes (conf->exchange_type),
232 /* auto_delete = */ 1,
233 /* arguments = */ AMQP_EMPTY_TABLE);
234 if ((ed_ret == NULL) && camqp_is_error (conf))
237 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
238 camqp_strerror (conf, errbuf, sizeof (errbuf)));
239 camqp_close_connection (conf);
243 INFO ("amqp plugin: Successfully created exchange \"%s\" "
245 conf->exchange, conf->exchange_type);
248 } /* }}} int camqp_create_exchange */
250 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
252 amqp_queue_declare_ok_t *qd_ret;
253 amqp_basic_consume_ok_t *cm_ret;
255 qd_ret = amqp_queue_declare (conf->connection,
256 /* channel = */ CAMQP_CHANNEL,
257 /* queue = */ (conf->queue != NULL)
258 ? amqp_cstring_bytes (conf->queue)
263 /* auto_delete = */ 1,
264 /* arguments = */ AMQP_EMPTY_TABLE);
267 ERROR ("amqp plugin: amqp_queue_declare failed.");
268 camqp_close_connection (conf);
272 if (conf->queue == NULL)
274 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
275 if (conf->queue == NULL)
277 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
278 camqp_close_connection (conf);
282 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
284 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
286 /* bind to an exchange */
287 if (conf->exchange != NULL)
289 amqp_queue_bind_ok_t *qb_ret;
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),
296 /* routing_key = */ (conf->routing_key != NULL)
297 ? amqp_cstring_bytes (conf->routing_key)
299 /* arguments = */ AMQP_EMPTY_TABLE);
300 if ((qb_ret == NULL) && camqp_is_error (conf))
303 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
304 camqp_strerror (conf, errbuf, sizeof (errbuf)));
305 camqp_close_connection (conf);
309 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
310 conf->queue, conf->exchange);
311 } /* if (conf->exchange != NULL) */
313 cm_ret = amqp_basic_consume (conf->connection,
314 /* channel = */ CAMQP_CHANNEL,
315 /* queue = */ amqp_cstring_bytes (conf->queue),
316 /* consumer_tag = */ AMQP_EMPTY_BYTES,
319 /* exclusive = */ 0);
320 if ((cm_ret == NULL) && camqp_is_error (conf))
323 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
324 camqp_strerror (conf, errbuf, sizeof (errbuf)));
325 camqp_close_connection (conf);
330 } /* }}} int camqp_setup_queue */
332 static int camqp_connect (camqp_config_t *conf) /* {{{ */
334 amqp_rpc_reply_t reply;
338 if (conf->connection != NULL)
341 conf->connection = amqp_new_connection ();
342 if (conf->connection == NULL)
344 ERROR ("amqp plugin: amqp_new_connection failed.");
348 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
352 status = (-1) * sockfd;
353 ERROR ("amqp plugin: amqp_open_socket failed: %s",
354 sstrerror (status, errbuf, sizeof (errbuf)));
355 amqp_destroy_connection (conf->connection);
356 conf->connection = NULL;
359 amqp_set_sockfd (conf->connection, sockfd);
361 reply = amqp_login (conf->connection, CONF(conf, vhost),
362 /* channel max = */ 0,
363 /* frame max = */ 131072,
365 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
366 CONF(conf, user), CONF(conf, password));
367 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
369 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
370 CONF(conf, vhost), CONF(conf, user));
371 amqp_destroy_connection (conf->connection);
373 conf->connection = NULL;
377 amqp_channel_open (conf->connection, /* channel = */ 1);
378 /* FIXME: Is checking "reply.reply_type" really correct here? How does
379 * it get set? --octo */
380 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
382 ERROR ("amqp plugin: amqp_channel_open failed.");
383 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
384 amqp_destroy_connection (conf->connection);
386 conf->connection = NULL;
390 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
391 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
393 status = camqp_create_exchange (conf);
398 return (camqp_setup_queue (conf));
400 } /* }}} int camqp_connect */
402 static int camqp_shutdown (void) /* {{{ */
406 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
407 subscriber_threads_num);
409 subscriber_threads_running = 0;
410 for (i = 0; i < subscriber_threads_num; i++)
412 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
413 * to use a timeout in the thread and check for the variable in regular
415 pthread_kill (subscriber_threads[i], SIGTERM);
416 pthread_join (subscriber_threads[i], /* retval = */ NULL);
419 subscriber_threads_num = 0;
420 sfree (subscriber_threads);
422 DEBUG ("amqp plugin: All subscriber threads exited.");
425 } /* }}} int camqp_shutdown */
430 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
431 size_t body_size, const char *content_type)
433 char body[body_size + 1];
439 memset (body, 0, sizeof (body));
443 while (received < body_size)
445 status = amqp_simple_wait_frame (conf->connection, &frame);
449 status = (-1) * status;
450 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
451 sstrerror (status, errbuf, sizeof (errbuf)));
452 camqp_close_connection (conf);
456 if (frame.frame_type != AMQP_FRAME_BODY)
458 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
463 if ((body_size - received) < frame.payload.body_fragment.len)
465 WARNING ("amqp plugin: Body is larger than indicated by header.");
469 memcpy (body_ptr, frame.payload.body_fragment.bytes,
470 frame.payload.body_fragment.len);
471 body_ptr += frame.payload.body_fragment.len;
472 received += frame.payload.body_fragment.len;
473 } /* while (received < body_size) */
475 if (strcasecmp ("text/collectd", content_type) == 0)
477 status = handle_putval (stderr, body);
479 ERROR ("amqp plugin: handle_putval failed with status %i.",
483 else if (strcasecmp ("application/json", content_type) == 0)
485 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
486 "been implemented yet. FIXME!");
491 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
498 } /* }}} int camqp_read_body */
500 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
504 amqp_basic_properties_t *properties;
507 status = amqp_simple_wait_frame (conf->connection, &frame);
511 status = (-1) * status;
512 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
513 sstrerror (status, errbuf, sizeof (errbuf)));
514 camqp_close_connection (conf);
518 if (frame.frame_type != AMQP_FRAME_HEADER)
520 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
525 properties = frame.payload.properties.decoded;
526 content_type = camqp_bytes_cstring (&properties->content_type);
527 if (content_type == NULL)
529 ERROR ("amqp plugin: Unable to determine content type.");
533 status = camqp_read_body (conf,
534 (size_t) frame.payload.properties.body_size,
537 sfree (content_type);
539 } /* }}} int camqp_read_header */
541 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
543 camqp_config_t *conf = user_data;
546 while (subscriber_threads_running)
550 status = camqp_connect (conf);
553 ERROR ("amqp plugin: camqp_connect failed. "
554 "Will sleep for %i seconds.", interval_g);
559 status = amqp_simple_wait_frame (conf->connection, &frame);
562 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
563 "Will sleep for %i seconds.", interval_g);
564 camqp_close_connection (conf);
569 if (frame.frame_type != AMQP_FRAME_METHOD)
571 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
576 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
578 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
579 frame.payload.method.id);
583 status = camqp_read_header (conf);
585 amqp_maybe_release_buffers (conf->connection);
586 } /* while (subscriber_threads_running) */
588 camqp_config_free (conf);
590 } /* }}} void *camqp_subscribe_thread */
592 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
597 tmp = realloc (subscriber_threads,
598 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
601 ERROR ("amqp plugin: realloc failed.");
602 camqp_config_free (conf);
605 subscriber_threads = tmp;
606 tmp = subscriber_threads + subscriber_threads_num;
607 memset (tmp, 0, sizeof (*tmp));
609 status = pthread_create (tmp, /* attr = */ NULL,
610 camqp_subscribe_thread, conf);
614 ERROR ("amqp plugin: pthread_create failed: %s",
615 sstrerror (status, errbuf, sizeof (errbuf)));
616 camqp_config_free (conf);
620 subscriber_threads_num++;
623 } /* }}} int camqp_subscribe_init */
628 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
629 const char *buffer, const char *routing_key)
631 amqp_basic_properties_t props;
634 status = camqp_connect (conf);
638 memset (&props, 0, sizeof (props));
639 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
640 | AMQP_BASIC_DELIVERY_MODE_FLAG
641 | AMQP_BASIC_APP_ID_FLAG;
642 if (conf->format == CAMQP_FORMAT_COMMAND)
643 props.content_type = amqp_cstring_bytes("text/collectd");
644 else if (conf->format == CAMQP_FORMAT_JSON)
645 props.content_type = amqp_cstring_bytes("application/json");
648 props.delivery_mode = conf->delivery_mode;
649 props.app_id = amqp_cstring_bytes("collectd");
651 status = amqp_basic_publish(conf->connection,
653 amqp_cstring_bytes(CONF(conf, exchange)),
654 amqp_cstring_bytes (routing_key),
658 amqp_cstring_bytes(buffer));
661 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
663 camqp_close_connection (conf);
667 } /* }}} int camqp_write_locked */
669 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
670 user_data_t *user_data)
672 camqp_config_t *conf = user_data->data;
673 char routing_key[6 * DATA_MAX_NAME_LEN];
677 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
680 memset (buffer, 0, sizeof (buffer));
682 if (conf->routing_key != NULL)
684 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
689 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
691 vl->plugin, vl->plugin_instance,
692 vl->type, vl->type_instance);
694 /* Switch slashes (the only character forbidden by collectd) and dots
695 * (the separation character used by AMQP). */
696 for (i = 0; routing_key[i] != 0; i++)
698 if (routing_key[i] == '.')
699 routing_key[i] = '/';
700 else if (routing_key[i] == '/')
701 routing_key[i] = '.';
705 if (conf->format == CAMQP_FORMAT_COMMAND)
707 status = create_putval (buffer, sizeof (buffer), ds, vl);
710 ERROR ("amqp plugin: create_putval failed with status %i.",
715 else if (conf->format == CAMQP_FORMAT_JSON)
717 size_t bfree = sizeof (buffer);
720 format_json_initialize (buffer, &bfill, &bfree);
721 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
722 format_json_finalize (buffer, &bfill, &bfree);
726 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
730 pthread_mutex_lock (&conf->lock);
731 status = camqp_write_locked (conf, buffer, routing_key);
732 pthread_mutex_unlock (&conf->lock);
735 } /* }}} int camqp_write */
740 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
741 camqp_config_t *conf)
747 status = cf_util_get_string (ci, &string);
751 assert (string != NULL);
752 if (strcasecmp ("Command", string) == 0)
753 conf->format = CAMQP_FORMAT_COMMAND;
754 else if (strcasecmp ("JSON", string) == 0)
755 conf->format = CAMQP_FORMAT_JSON;
758 WARNING ("amqp plugin: Invalid format string: %s",
765 } /* }}} int config_set_string */
767 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
770 camqp_config_t *conf;
774 conf = malloc (sizeof (*conf));
777 ERROR ("amqp plugin: malloc failed.");
781 /* Initialize "conf" {{{ */
782 memset (conf, 0, sizeof (*conf));
783 conf->publish = publish;
785 conf->format = CAMQP_FORMAT_COMMAND;
790 conf->password = NULL;
791 conf->exchange = NULL;
792 conf->routing_key = NULL;
794 conf->delivery_mode = CAMQP_DM_VOLATILE;
795 conf->store_rates = 0;
797 conf->exchange_type = NULL;
800 conf->connection = NULL;
801 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
804 status = cf_util_get_string (ci, &conf->name);
811 for (i = 0; i < ci->children_num; i++)
813 oconfig_item_t *child = ci->children + i;
815 if (strcasecmp ("Host", child->key) == 0)
816 status = cf_util_get_string (child, &conf->host);
817 else if (strcasecmp ("Port", child->key) == 0)
819 status = cf_util_get_port_number (child);
826 else if (strcasecmp ("VHost", child->key) == 0)
827 status = cf_util_get_string (child, &conf->vhost);
828 else if (strcasecmp ("User", child->key) == 0)
829 status = cf_util_get_string (child, &conf->user);
830 else if (strcasecmp ("Password", child->key) == 0)
831 status = cf_util_get_string (child, &conf->password);
832 else if (strcasecmp ("Exchange", child->key) == 0)
833 status = cf_util_get_string (child, &conf->exchange);
834 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
835 status = cf_util_get_string (child, &conf->exchange_type);
836 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
837 status = cf_util_get_string (child, &conf->queue);
838 else if (strcasecmp ("RoutingKey", child->key) == 0)
839 status = cf_util_get_string (child, &conf->routing_key);
840 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
843 status = cf_util_get_boolean (child, &tmp);
845 conf->delivery_mode = CAMQP_DM_PERSISTENT;
847 conf->delivery_mode = CAMQP_DM_VOLATILE;
849 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
850 status = cf_util_get_boolean (child, &conf->store_rates);
851 else if ((strcasecmp ("Format", child->key) == 0) && publish)
852 status = camqp_config_set_format (child, conf);
854 WARNING ("amqp plugin: Ignoring unknown "
855 "configuration option \"%s\".", child->key);
859 } /* for (i = 0; i < ci->children_num; i++) */
861 if ((status == 0) && (conf->exchange == NULL))
863 if (conf->exchange_type != NULL)
864 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
865 "without the \"Exchange\" option. It will be ignored.");
867 if (!publish && (conf->routing_key != NULL))
868 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
869 "without the \"Exchange\" option. It will be ignored.");
875 camqp_config_free (conf);
879 if (conf->exchange != NULL)
881 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
888 user_data_t ud = { conf, camqp_config_free };
890 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
892 status = plugin_register_write (cbname, camqp_write, &ud);
895 camqp_config_free (conf);
901 status = camqp_subscribe_init (conf);
904 camqp_config_free (conf);
910 } /* }}} int camqp_config_connection */
912 static int camqp_config (oconfig_item_t *ci) /* {{{ */
916 for (i = 0; i < ci->children_num; i++)
918 oconfig_item_t *child = ci->children + i;
920 if (strcasecmp ("Publish", child->key) == 0)
921 camqp_config_connection (child, /* publish = */ 1);
922 else if (strcasecmp ("Subscribe", child->key) == 0)
923 camqp_config_connection (child, /* publish = */ 0);
925 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
927 } /* for (ci->children_num) */
930 } /* }}} int camqp_config */
932 void module_register (void)
934 plugin_register_complex_config ("amqp", camqp_config);
935 plugin_register_shutdown ("amqp", camqp_shutdown);
936 } /* void module_register */
938 /* vim: set sw=4 sts=4 et fdm=marker : */