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";
94 static pthread_t *subscriber_threads = NULL;
95 static size_t subscriber_threads_num = 0;
96 static _Bool subscriber_threads_running = 1;
98 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
103 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
107 if ((conf == NULL) || (conf->connection == NULL))
110 sockfd = amqp_get_sockfd (conf->connection);
111 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
112 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
113 amqp_destroy_connection (conf->connection);
115 conf->connection = NULL;
116 } /* }}} void camqp_close_connection */
118 static void camqp_config_free (void *ptr) /* {{{ */
120 camqp_config_t *conf = ptr;
125 camqp_close_connection (conf);
131 sfree (conf->password);
132 sfree (conf->exchange);
133 sfree (conf->exchange_type);
135 sfree (conf->routing_key);
138 } /* }}} void camqp_config_free */
140 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
144 if ((in == NULL) || (in->bytes == NULL))
147 ret = malloc (in->len + 1);
151 memcpy (ret, in->bytes, in->len);
155 } /* }}} char *camqp_bytes_cstring */
157 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
161 r = amqp_get_rpc_reply (conf->connection);
162 if (r.reply_type == AMQP_RESPONSE_NORMAL)
166 } /* }}} _Bool camqp_is_error */
168 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
169 char *buffer, size_t buffer_size)
173 r = amqp_get_rpc_reply (conf->connection);
174 switch (r.reply_type)
176 case AMQP_RESPONSE_NORMAL:
177 sstrncpy (buffer, "Success", sizeof (buffer));
180 case AMQP_RESPONSE_NONE:
181 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
184 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
186 return (sstrerror (r.library_errno, buffer, buffer_size));
188 sstrncpy (buffer, "End of stream", sizeof (buffer));
191 case AMQP_RESPONSE_SERVER_EXCEPTION:
192 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
194 amqp_connection_close_t *m = r.reply.decoded;
195 char *tmp = camqp_bytes_cstring (&m->reply_text);
196 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
200 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
202 amqp_channel_close_t *m = r.reply.decoded;
203 char *tmp = camqp_bytes_cstring (&m->reply_text);
204 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
210 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
216 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
221 } /* }}} char *camqp_strerror */
223 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
225 amqp_queue_declare_ok_t *qd_ret;
226 amqp_basic_consume_ok_t *cm_ret;
228 qd_ret = amqp_queue_declare (conf->connection,
229 /* channel = */ CAMQP_CHANNEL,
230 /* queue = */ (conf->queue != NULL)
231 ? amqp_cstring_bytes (conf->queue)
236 /* auto_delete = */ 1,
237 /* arguments = */ AMQP_EMPTY_TABLE);
240 ERROR ("amqp plugin: amqp_queue_declare failed.");
241 camqp_close_connection (conf);
245 if (conf->queue == NULL)
247 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
248 if (conf->queue == NULL)
250 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
251 camqp_close_connection (conf);
255 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
257 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
259 /* bind to an exchange */
260 if (conf->exchange != NULL)
262 amqp_queue_bind_ok_t *qb_ret;
264 /* create the exchange */
265 if (conf->exchange_type != NULL)
267 amqp_exchange_declare_ok_t *ed_ret;
269 ed_ret = amqp_exchange_declare (conf->connection,
270 /* channel = */ CAMQP_CHANNEL,
271 /* exchange = */ amqp_cstring_bytes (conf->exchange),
272 /* type = */ amqp_cstring_bytes (conf->exchange_type),
275 /* auto_delete = */ 1,
276 /* arguments = */ AMQP_EMPTY_TABLE);
277 if ((ed_ret == NULL) && camqp_is_error (conf))
280 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
281 camqp_strerror (conf, errbuf, sizeof (errbuf)));
282 camqp_close_connection (conf);
287 assert (conf->queue != NULL);
288 qb_ret = amqp_queue_bind (conf->connection,
289 /* channel = */ CAMQP_CHANNEL,
290 /* queue = */ amqp_cstring_bytes (conf->queue),
291 /* exchange = */ amqp_cstring_bytes (conf->exchange),
292 /* routing_key = */ (conf->routing_key != NULL)
293 ? amqp_cstring_bytes (conf->routing_key)
295 /* arguments = */ AMQP_EMPTY_TABLE);
296 if ((qb_ret == NULL) && camqp_is_error (conf))
299 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
300 camqp_strerror (conf, errbuf, sizeof (errbuf)));
301 camqp_close_connection (conf);
305 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
306 conf->queue, conf->exchange);
307 } /* if (conf->exchange != NULL) */
309 cm_ret = amqp_basic_consume (conf->connection,
310 /* channel = */ CAMQP_CHANNEL,
311 /* queue = */ amqp_cstring_bytes (conf->queue),
312 /* consumer_tag = */ AMQP_EMPTY_BYTES,
315 /* exclusive = */ 0);
316 if ((cm_ret == NULL) && camqp_is_error (conf))
319 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
320 camqp_strerror (conf, errbuf, sizeof (errbuf)));
321 camqp_close_connection (conf);
326 } /* }}} int camqp_setup_queue */
328 static int camqp_connect (camqp_config_t *conf) /* {{{ */
330 amqp_rpc_reply_t reply;
334 if (conf->connection != NULL)
337 conf->connection = amqp_new_connection ();
338 if (conf->connection == NULL)
340 ERROR ("amqp plugin: amqp_new_connection failed.");
344 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
348 status = (-1) * sockfd;
349 ERROR ("amqp plugin: amqp_open_socket failed: %s",
350 sstrerror (status, errbuf, sizeof (errbuf)));
351 amqp_destroy_connection (conf->connection);
352 conf->connection = NULL;
355 amqp_set_sockfd (conf->connection, sockfd);
357 reply = amqp_login (conf->connection, CONF(conf, vhost),
358 /* channel max = */ 0,
359 /* frame max = */ 131072,
361 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
362 CONF(conf, user), CONF(conf, password));
363 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
365 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
366 CONF(conf, vhost), CONF(conf, user));
367 amqp_destroy_connection (conf->connection);
369 conf->connection = NULL;
373 amqp_channel_open (conf->connection, /* channel = */ 1);
374 /* FIXME: Is checking "reply.reply_type" really correct here? How does
375 * it get set? --octo */
376 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
378 ERROR ("amqp plugin: amqp_channel_open failed.");
379 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
380 amqp_destroy_connection (conf->connection);
382 conf->connection = NULL;
386 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
387 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
390 return (camqp_setup_queue (conf));
392 } /* }}} int camqp_connect */
394 static int shutdown (void) /* {{{ */
398 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
399 subscriber_threads_num);
401 subscriber_threads_running = 0;
402 for (i = 0; i < subscriber_threads_num; i++)
404 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
405 * to use a timeout in the thread and check for the variable in regular
407 pthread_kill (subscriber_threads[i], SIGTERM);
408 pthread_join (subscriber_threads[i], /* retval = */ NULL);
411 subscriber_threads_num = 0;
412 sfree (subscriber_threads);
414 DEBUG ("amqp plugin: All subscriber threads exited.");
417 } /* }}} int shutdown */
422 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
425 char body[body_size + 1];
431 memset (body, 0, sizeof (body));
435 while (received < body_size)
437 status = amqp_simple_wait_frame (conf->connection, &frame);
441 status = (-1) * status;
442 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
443 sstrerror (status, errbuf, sizeof (errbuf)));
444 camqp_close_connection (conf);
448 if (frame.frame_type != AMQP_FRAME_BODY)
450 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
455 if ((body_size - received) < frame.payload.body_fragment.len)
457 WARNING ("amqp plugin: Body is larger than indicated by header.");
461 memcpy (body_ptr, frame.payload.body_fragment.bytes,
462 frame.payload.body_fragment.len);
463 body_ptr += frame.payload.body_fragment.len;
464 received += frame.payload.body_fragment.len;
465 } /* while (received < body_size) */
467 if (conf->format == CAMQP_FORMAT_COMMAND)
469 status = handle_putval (stderr, body);
471 ERROR ("amqp plugin: handle_putval failed with status %i.",
475 else if (conf->format == CAMQP_FORMAT_JSON)
477 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
478 "been implemented yet. FIXME!");
483 ERROR ("amqp plugin: camqp_read_body: Unknown format option (%i).",
490 } /* }}} int camqp_read_body */
492 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
497 status = amqp_simple_wait_frame (conf->connection, &frame);
501 status = (-1) * status;
502 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
503 sstrerror (status, errbuf, sizeof (errbuf)));
504 camqp_close_connection (conf);
508 if (frame.frame_type != AMQP_FRAME_HEADER)
510 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
515 return (camqp_read_body (conf, frame.payload.properties.body_size));
516 } /* }}} int camqp_read_header */
518 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
520 camqp_config_t *conf = user_data;
523 while (subscriber_threads_running)
527 status = camqp_connect (conf);
530 ERROR ("amqp plugin: camqp_connect failed. "
531 "Will sleep for %i seconds.", interval_g);
536 status = amqp_simple_wait_frame (conf->connection, &frame);
539 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
540 "Will sleep for %i seconds.", interval_g);
541 camqp_close_connection (conf);
546 if (frame.frame_type != AMQP_FRAME_METHOD)
548 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
553 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
555 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
556 frame.payload.method.id);
560 status = camqp_read_header (conf);
562 amqp_maybe_release_buffers (conf->connection);
563 } /* while (subscriber_threads_running) */
565 camqp_config_free (conf);
567 } /* }}} void *camqp_subscribe_thread */
569 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
574 tmp = realloc (subscriber_threads,
575 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
578 ERROR ("amqp plugin: realloc failed.");
579 camqp_config_free (conf);
582 subscriber_threads = tmp;
583 tmp = subscriber_threads + subscriber_threads_num;
584 memset (tmp, 0, sizeof (*tmp));
586 status = pthread_create (tmp, /* attr = */ NULL,
587 camqp_subscribe_thread, conf);
591 ERROR ("amqp plugin: pthread_create failed: %s",
592 sstrerror (status, errbuf, sizeof (errbuf)));
593 camqp_config_free (conf);
597 subscriber_threads_num++;
600 } /* }}} int camqp_subscribe_init */
605 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
606 const char *buffer, const char *routing_key)
608 amqp_basic_properties_t props;
611 status = camqp_connect (conf);
615 memset (&props, 0, sizeof (props));
616 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
617 | AMQP_BASIC_DELIVERY_MODE_FLAG
618 | AMQP_BASIC_APP_ID_FLAG;
619 props.content_type = amqp_cstring_bytes("application/json");
620 props.delivery_mode = conf->delivery_mode;
621 props.app_id = amqp_cstring_bytes("collectd");
623 status = amqp_basic_publish(conf->connection,
625 amqp_cstring_bytes(CONF(conf, exchange)),
626 amqp_cstring_bytes (routing_key),
630 amqp_cstring_bytes(buffer));
633 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
635 camqp_close_connection (conf);
639 } /* }}} int camqp_write_locked */
641 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
642 user_data_t *user_data)
644 camqp_config_t *conf = user_data->data;
645 char routing_key[6 * DATA_MAX_NAME_LEN];
649 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
652 memset (buffer, 0, sizeof (buffer));
654 if (conf->routing_key != NULL)
656 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
661 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
663 vl->plugin, vl->plugin_instance,
664 vl->type, vl->type_instance);
666 /* Switch slashes (the only character forbidden by collectd) and dots
667 * (the separation character used by AMQP). */
668 for (i = 0; routing_key[i] != 0; i++)
670 if (routing_key[i] == '.')
671 routing_key[i] = '/';
672 else if (routing_key[i] == '/')
673 routing_key[i] = '.';
677 if (conf->format == CAMQP_FORMAT_COMMAND)
679 status = create_putval (buffer, sizeof (buffer), ds, vl);
682 ERROR ("amqp plugin: create_putval failed with status %i.",
687 else if (conf->format == CAMQP_FORMAT_JSON)
689 size_t bfree = sizeof (buffer);
692 format_json_initialize (buffer, &bfill, &bfree);
693 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
694 format_json_finalize (buffer, &bfill, &bfree);
698 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
702 pthread_mutex_lock (&conf->lock);
703 status = camqp_write_locked (conf, buffer, routing_key);
704 pthread_mutex_unlock (&conf->lock);
707 } /* }}} int camqp_write */
712 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
713 camqp_config_t *conf)
719 status = cf_util_get_string (ci, &string);
723 assert (string != NULL);
724 if (strcasecmp ("Command", string) == 0)
725 conf->format = CAMQP_FORMAT_COMMAND;
726 else if (strcasecmp ("JSON", string) == 0)
727 conf->format = CAMQP_FORMAT_JSON;
730 WARNING ("amqp plugin: Invalid format string: %s",
737 } /* }}} int config_set_string */
739 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
742 camqp_config_t *conf;
746 conf = malloc (sizeof (*conf));
749 ERROR ("amqp plugin: malloc failed.");
753 /* Initialize "conf" {{{ */
754 memset (conf, 0, sizeof (*conf));
755 conf->publish = publish;
757 conf->format = CAMQP_FORMAT_COMMAND;
762 conf->password = NULL;
763 conf->exchange = NULL;
764 conf->routing_key = NULL;
766 conf->delivery_mode = CAMQP_DM_VOLATILE;
767 conf->store_rates = 0;
769 conf->exchange_type = NULL;
772 conf->connection = NULL;
773 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
776 status = cf_util_get_string (ci, &conf->name);
783 for (i = 0; i < ci->children_num; i++)
785 oconfig_item_t *child = ci->children + i;
787 if (strcasecmp ("Format", child->key) == 0)
788 status = camqp_config_set_format (child, conf);
789 else if (strcasecmp ("Host", child->key) == 0)
790 status = cf_util_get_string (child, &conf->host);
791 else if (strcasecmp ("Port", child->key) == 0)
793 status = cf_util_get_port_number (child);
800 else if (strcasecmp ("VHost", child->key) == 0)
801 status = cf_util_get_string (child, &conf->vhost);
802 else if (strcasecmp ("User", child->key) == 0)
803 status = cf_util_get_string (child, &conf->user);
804 else if (strcasecmp ("Password", child->key) == 0)
805 status = cf_util_get_string (child, &conf->password);
806 else if (strcasecmp ("Exchange", child->key) == 0)
807 status = cf_util_get_string (child, &conf->exchange);
808 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
809 status = cf_util_get_string (child, &conf->exchange_type);
810 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
811 status = cf_util_get_string (child, &conf->queue);
812 else if (strcasecmp ("RoutingKey", child->key) == 0)
813 status = cf_util_get_string (child, &conf->routing_key);
814 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
817 status = cf_util_get_boolean (child, &tmp);
819 conf->delivery_mode = CAMQP_DM_PERSISTENT;
821 conf->delivery_mode = CAMQP_DM_VOLATILE;
823 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
824 status = cf_util_get_boolean (child, &conf->store_rates);
826 WARNING ("amqp plugin: Ignoring unknown "
827 "configuration option \"%s\".", child->key);
831 } /* for (i = 0; i < ci->children_num; i++) */
833 if ((status == 0) && !publish && (conf->exchange == NULL))
835 if (conf->routing_key != NULL)
836 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
837 "without the \"Exchange\" option. It will be ignored.");
839 if (conf->exchange_type != NULL)
840 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
841 "without the \"Exchange\" option. It will be ignored.");
846 camqp_config_free (conf);
850 if (conf->exchange != NULL)
852 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
859 user_data_t ud = { conf, camqp_config_free };
861 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
863 status = plugin_register_write (cbname, camqp_write, &ud);
866 camqp_config_free (conf);
872 status = camqp_subscribe_init (conf);
875 camqp_config_free (conf);
881 } /* }}} int camqp_config_connection */
883 static int camqp_config (oconfig_item_t *ci) /* {{{ */
887 for (i = 0; i < ci->children_num; i++)
889 oconfig_item_t *child = ci->children + i;
891 if (strcasecmp ("Publish", child->key) == 0)
892 camqp_config_connection (child, /* publish = */ 1);
893 else if (strcasecmp ("Subscribe", child->key) == 0)
894 camqp_config_connection (child, /* publish = */ 0);
896 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
898 } /* for (ci->children_num) */
901 } /* }}} int camqp_config */
903 void module_register (void)
905 plugin_register_complex_config ("amqp", camqp_config);
906 plugin_register_shutdown ("amqp", shutdown);
907 } /* void module_register */
909 /* vim: set sw=4 sts=4 et fdm=marker : */