2 * collectd - src/amqp.c
3 * Copyright (C) 2009 Sebastien Pahl
4 * Copyright (C) 2010-2012 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"
34 #include "utils_format_graphite.h"
39 #include <amqp_framing.h>
41 /* Defines for the delivery mode. I have no idea why they're not defined by the
43 #define CAMQP_DM_VOLATILE 1
44 #define CAMQP_DM_PERSISTENT 2
46 #define CAMQP_FORMAT_COMMAND 1
47 #define CAMQP_FORMAT_JSON 2
48 #define CAMQP_FORMAT_GRAPHITE 3
50 #define CAMQP_CHANNEL 1
70 uint8_t delivery_mode;
73 /* publish & graphite format only */
82 amqp_connection_state_t connection;
85 typedef struct camqp_config_s camqp_config_t;
90 static const char *def_host = "localhost";
91 static const char *def_vhost = "/";
92 static const char *def_user = "guest";
93 static const char *def_password = "guest";
94 static const char *def_exchange = "amq.fanout";
96 static pthread_t *subscriber_threads = NULL;
97 static size_t subscriber_threads_num = 0;
98 static _Bool subscriber_threads_running = 1;
100 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
105 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
109 if ((conf == NULL) || (conf->connection == NULL))
112 sockfd = amqp_get_sockfd (conf->connection);
113 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
114 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
115 amqp_destroy_connection (conf->connection);
117 conf->connection = NULL;
118 } /* }}} void camqp_close_connection */
120 static void camqp_config_free (void *ptr) /* {{{ */
122 camqp_config_t *conf = ptr;
127 camqp_close_connection (conf);
133 sfree (conf->password);
134 sfree (conf->exchange);
135 sfree (conf->exchange_type);
137 sfree (conf->routing_key);
138 sfree (conf->prefix);
139 sfree (conf->postfix);
143 } /* }}} void camqp_config_free */
145 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
149 if ((in == NULL) || (in->bytes == NULL))
152 ret = malloc (in->len + 1);
156 memcpy (ret, in->bytes, in->len);
160 } /* }}} char *camqp_bytes_cstring */
162 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
166 r = amqp_get_rpc_reply (conf->connection);
167 if (r.reply_type == AMQP_RESPONSE_NORMAL)
171 } /* }}} _Bool camqp_is_error */
173 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
174 char *buffer, size_t buffer_size)
178 r = amqp_get_rpc_reply (conf->connection);
179 switch (r.reply_type)
181 case AMQP_RESPONSE_NORMAL:
182 sstrncpy (buffer, "Success", sizeof (buffer));
185 case AMQP_RESPONSE_NONE:
186 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
189 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
190 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
192 return (sstrerror (r.library_errno, buffer, buffer_size));
195 return (sstrerror (r.library_error, buffer, buffer_size));
198 sstrncpy (buffer, "End of stream", sizeof (buffer));
201 case AMQP_RESPONSE_SERVER_EXCEPTION:
202 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
204 amqp_connection_close_t *m = r.reply.decoded;
205 char *tmp = camqp_bytes_cstring (&m->reply_text);
206 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
210 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
212 amqp_channel_close_t *m = r.reply.decoded;
213 char *tmp = camqp_bytes_cstring (&m->reply_text);
214 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
220 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
226 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
231 } /* }}} char *camqp_strerror */
233 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
234 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
236 amqp_exchange_declare_ok_t *ed_ret;
238 if (conf->exchange_type == NULL)
241 ed_ret = amqp_exchange_declare (conf->connection,
242 /* channel = */ CAMQP_CHANNEL,
243 /* exchange = */ amqp_cstring_bytes (conf->exchange),
244 /* type = */ amqp_cstring_bytes (conf->exchange_type),
247 /* auto_delete = */ 1,
248 /* arguments = */ AMQP_EMPTY_TABLE);
249 if ((ed_ret == NULL) && camqp_is_error (conf))
252 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
253 camqp_strerror (conf, errbuf, sizeof (errbuf)));
254 camqp_close_connection (conf);
258 INFO ("amqp plugin: Successfully created exchange \"%s\" "
260 conf->exchange, conf->exchange_type);
263 } /* }}} int camqp_create_exchange */
265 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
267 amqp_exchange_declare_ok_t *ed_ret;
268 amqp_table_t argument_table;
269 struct amqp_table_entry_t_ argument_table_entries[1];
271 if (conf->exchange_type == NULL)
274 /* Valid arguments: "auto_delete", "internal" */
275 argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
276 argument_table.entries = argument_table_entries;
277 argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
278 argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
279 argument_table_entries[0].value.value.boolean = 1;
281 ed_ret = amqp_exchange_declare (conf->connection,
282 /* channel = */ CAMQP_CHANNEL,
283 /* exchange = */ amqp_cstring_bytes (conf->exchange),
284 /* type = */ amqp_cstring_bytes (conf->exchange_type),
287 /* arguments = */ argument_table);
288 if ((ed_ret == NULL) && camqp_is_error (conf))
291 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
292 camqp_strerror (conf, errbuf, sizeof (errbuf)));
293 camqp_close_connection (conf);
297 INFO ("amqp plugin: Successfully created exchange \"%s\" "
299 conf->exchange, conf->exchange_type);
302 } /* }}} int camqp_create_exchange */
305 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
307 amqp_queue_declare_ok_t *qd_ret;
308 amqp_basic_consume_ok_t *cm_ret;
310 qd_ret = amqp_queue_declare (conf->connection,
311 /* channel = */ CAMQP_CHANNEL,
312 /* queue = */ (conf->queue != NULL)
313 ? amqp_cstring_bytes (conf->queue)
318 /* auto_delete = */ 1,
319 /* arguments = */ AMQP_EMPTY_TABLE);
322 ERROR ("amqp plugin: amqp_queue_declare failed.");
323 camqp_close_connection (conf);
327 if (conf->queue == NULL)
329 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
330 if (conf->queue == NULL)
332 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
333 camqp_close_connection (conf);
337 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
339 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
341 /* bind to an exchange */
342 if (conf->exchange != NULL)
344 amqp_queue_bind_ok_t *qb_ret;
346 assert (conf->queue != NULL);
347 qb_ret = amqp_queue_bind (conf->connection,
348 /* channel = */ CAMQP_CHANNEL,
349 /* queue = */ amqp_cstring_bytes (conf->queue),
350 /* exchange = */ amqp_cstring_bytes (conf->exchange),
351 /* routing_key = */ (conf->routing_key != NULL)
352 ? amqp_cstring_bytes (conf->routing_key)
354 /* arguments = */ AMQP_EMPTY_TABLE);
355 if ((qb_ret == NULL) && camqp_is_error (conf))
358 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
359 camqp_strerror (conf, errbuf, sizeof (errbuf)));
360 camqp_close_connection (conf);
364 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
365 conf->queue, conf->exchange);
366 } /* if (conf->exchange != NULL) */
368 cm_ret = amqp_basic_consume (conf->connection,
369 /* channel = */ CAMQP_CHANNEL,
370 /* queue = */ amqp_cstring_bytes (conf->queue),
371 /* consumer_tag = */ AMQP_EMPTY_BYTES,
375 /* arguments = */ AMQP_EMPTY_TABLE
377 if ((cm_ret == NULL) && camqp_is_error (conf))
380 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
381 camqp_strerror (conf, errbuf, sizeof (errbuf)));
382 camqp_close_connection (conf);
387 } /* }}} int camqp_setup_queue */
389 static int camqp_connect (camqp_config_t *conf) /* {{{ */
391 amqp_rpc_reply_t reply;
395 if (conf->connection != NULL)
398 conf->connection = amqp_new_connection ();
399 if (conf->connection == NULL)
401 ERROR ("amqp plugin: amqp_new_connection failed.");
405 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
409 status = (-1) * sockfd;
410 ERROR ("amqp plugin: amqp_open_socket failed: %s",
411 sstrerror (status, errbuf, sizeof (errbuf)));
412 amqp_destroy_connection (conf->connection);
413 conf->connection = NULL;
416 amqp_set_sockfd (conf->connection, sockfd);
418 reply = amqp_login (conf->connection, CONF(conf, vhost),
419 /* channel max = */ 0,
420 /* frame max = */ 131072,
422 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
423 CONF(conf, user), CONF(conf, password));
424 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
426 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
427 CONF(conf, vhost), CONF(conf, user));
428 amqp_destroy_connection (conf->connection);
430 conf->connection = NULL;
434 amqp_channel_open (conf->connection, /* channel = */ 1);
435 /* FIXME: Is checking "reply.reply_type" really correct here? How does
436 * it get set? --octo */
437 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
439 ERROR ("amqp plugin: amqp_channel_open failed.");
440 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
441 amqp_destroy_connection (conf->connection);
443 conf->connection = NULL;
447 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
448 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
450 status = camqp_create_exchange (conf);
455 return (camqp_setup_queue (conf));
457 } /* }}} int camqp_connect */
459 static int camqp_shutdown (void) /* {{{ */
463 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
464 subscriber_threads_num);
466 subscriber_threads_running = 0;
467 for (i = 0; i < subscriber_threads_num; i++)
469 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
470 * to use a timeout in the thread and check for the variable in regular
472 pthread_kill (subscriber_threads[i], SIGTERM);
473 pthread_join (subscriber_threads[i], /* retval = */ NULL);
476 subscriber_threads_num = 0;
477 sfree (subscriber_threads);
479 DEBUG ("amqp plugin: All subscriber threads exited.");
482 } /* }}} int camqp_shutdown */
487 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
488 size_t body_size, const char *content_type)
490 char body[body_size + 1];
496 memset (body, 0, sizeof (body));
500 while (received < body_size)
502 status = amqp_simple_wait_frame (conf->connection, &frame);
506 status = (-1) * status;
507 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
508 sstrerror (status, errbuf, sizeof (errbuf)));
509 camqp_close_connection (conf);
513 if (frame.frame_type != AMQP_FRAME_BODY)
515 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
520 if ((body_size - received) < frame.payload.body_fragment.len)
522 WARNING ("amqp plugin: Body is larger than indicated by header.");
526 memcpy (body_ptr, frame.payload.body_fragment.bytes,
527 frame.payload.body_fragment.len);
528 body_ptr += frame.payload.body_fragment.len;
529 received += frame.payload.body_fragment.len;
530 } /* while (received < body_size) */
532 if (strcasecmp ("text/collectd", content_type) == 0)
534 status = handle_putval (stderr, body);
536 ERROR ("amqp plugin: handle_putval failed with status %i.",
540 else if (strcasecmp ("application/json", content_type) == 0)
542 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
543 "been implemented yet. FIXME!");
548 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
555 } /* }}} int camqp_read_body */
557 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
561 amqp_basic_properties_t *properties;
564 status = amqp_simple_wait_frame (conf->connection, &frame);
568 status = (-1) * status;
569 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
570 sstrerror (status, errbuf, sizeof (errbuf)));
571 camqp_close_connection (conf);
575 if (frame.frame_type != AMQP_FRAME_HEADER)
577 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
582 properties = frame.payload.properties.decoded;
583 content_type = camqp_bytes_cstring (&properties->content_type);
584 if (content_type == NULL)
586 ERROR ("amqp plugin: Unable to determine content type.");
590 status = camqp_read_body (conf,
591 (size_t) frame.payload.properties.body_size,
594 sfree (content_type);
596 } /* }}} int camqp_read_header */
598 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
600 camqp_config_t *conf = user_data;
603 while (subscriber_threads_running)
607 status = camqp_connect (conf);
610 struct timespec ts_interval;
611 ERROR ("amqp plugin: camqp_connect failed. "
612 "Will sleep for %.3f seconds.",
613 CDTIME_T_TO_DOUBLE (interval_g));
614 CDTIME_T_TO_TIMESPEC (interval_g, &ts_interval);
615 nanosleep (&ts_interval, /* remaining = */ NULL);
619 status = amqp_simple_wait_frame (conf->connection, &frame);
622 struct timespec ts_interval;
623 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
624 "Will sleep for %.3f seconds.",
625 CDTIME_T_TO_DOUBLE (interval_g));
626 camqp_close_connection (conf);
627 CDTIME_T_TO_TIMESPEC (interval_g, &ts_interval);
628 nanosleep (&ts_interval, /* remaining = */ NULL);
632 if (frame.frame_type != AMQP_FRAME_METHOD)
634 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
639 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
641 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
642 frame.payload.method.id);
646 status = camqp_read_header (conf);
648 amqp_maybe_release_buffers (conf->connection);
649 } /* while (subscriber_threads_running) */
651 camqp_config_free (conf);
654 } /* }}} void *camqp_subscribe_thread */
656 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
661 tmp = realloc (subscriber_threads,
662 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
665 ERROR ("amqp plugin: realloc failed.");
666 camqp_config_free (conf);
669 subscriber_threads = tmp;
670 tmp = subscriber_threads + subscriber_threads_num;
671 memset (tmp, 0, sizeof (*tmp));
673 status = pthread_create (tmp, /* attr = */ NULL,
674 camqp_subscribe_thread, conf);
678 ERROR ("amqp plugin: pthread_create failed: %s",
679 sstrerror (status, errbuf, sizeof (errbuf)));
680 camqp_config_free (conf);
684 subscriber_threads_num++;
687 } /* }}} int camqp_subscribe_init */
692 /* XXX: You must hold "conf->lock" when calling this function! */
693 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
694 const char *buffer, const char *routing_key)
696 amqp_basic_properties_t props;
699 status = camqp_connect (conf);
703 memset (&props, 0, sizeof (props));
704 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
705 | AMQP_BASIC_DELIVERY_MODE_FLAG
706 | AMQP_BASIC_APP_ID_FLAG;
707 if (conf->format == CAMQP_FORMAT_COMMAND)
708 props.content_type = amqp_cstring_bytes("text/collectd");
709 else if (conf->format == CAMQP_FORMAT_JSON)
710 props.content_type = amqp_cstring_bytes("application/json");
711 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
712 props.content_type = amqp_cstring_bytes("text/graphite");
715 props.delivery_mode = conf->delivery_mode;
716 props.app_id = amqp_cstring_bytes("collectd");
718 status = amqp_basic_publish(conf->connection,
720 amqp_cstring_bytes(CONF(conf, exchange)),
721 amqp_cstring_bytes (routing_key),
725 amqp_cstring_bytes(buffer));
728 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
730 camqp_close_connection (conf);
734 } /* }}} int camqp_write_locked */
736 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
737 user_data_t *user_data)
739 camqp_config_t *conf = user_data->data;
740 char routing_key[6 * DATA_MAX_NAME_LEN];
744 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
747 memset (buffer, 0, sizeof (buffer));
749 if (conf->routing_key != NULL)
751 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
756 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
758 vl->plugin, vl->plugin_instance,
759 vl->type, vl->type_instance);
761 /* Switch slashes (the only character forbidden by collectd) and dots
762 * (the separation character used by AMQP). */
763 for (i = 0; routing_key[i] != 0; i++)
765 if (routing_key[i] == '.')
766 routing_key[i] = '/';
767 else if (routing_key[i] == '/')
768 routing_key[i] = '.';
772 if (conf->format == CAMQP_FORMAT_COMMAND)
774 status = create_putval (buffer, sizeof (buffer), ds, vl);
777 ERROR ("amqp plugin: create_putval failed with status %i.",
782 else if (conf->format == CAMQP_FORMAT_JSON)
784 size_t bfree = sizeof (buffer);
787 format_json_initialize (buffer, &bfill, &bfree);
788 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
789 format_json_finalize (buffer, &bfill, &bfree);
791 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
793 status = format_graphite (buffer, sizeof (buffer), ds, vl,
794 conf->prefix, conf->postfix, conf->escape_char);
797 ERROR ("amqp plugin: format_graphite failed with status %i.",
804 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
808 pthread_mutex_lock (&conf->lock);
809 status = camqp_write_locked (conf, buffer, routing_key);
810 pthread_mutex_unlock (&conf->lock);
813 } /* }}} int camqp_write */
818 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
819 camqp_config_t *conf)
825 status = cf_util_get_string (ci, &string);
829 assert (string != NULL);
830 if (strcasecmp ("Command", string) == 0)
831 conf->format = CAMQP_FORMAT_COMMAND;
832 else if (strcasecmp ("JSON", string) == 0)
833 conf->format = CAMQP_FORMAT_JSON;
834 else if (strcasecmp ("Graphite", string) == 0)
835 conf->format = CAMQP_FORMAT_GRAPHITE;
838 WARNING ("amqp plugin: Invalid format string: %s",
845 } /* }}} int config_set_string */
847 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
850 camqp_config_t *conf;
854 conf = malloc (sizeof (*conf));
857 ERROR ("amqp plugin: malloc failed.");
861 /* Initialize "conf" {{{ */
862 memset (conf, 0, sizeof (*conf));
863 conf->publish = publish;
865 conf->format = CAMQP_FORMAT_COMMAND;
870 conf->password = NULL;
871 conf->exchange = NULL;
872 conf->routing_key = NULL;
874 conf->delivery_mode = CAMQP_DM_VOLATILE;
875 conf->store_rates = 0;
876 /* publish & graphite only */
878 conf->postfix = NULL;
879 conf->escape_char = '_';
881 conf->exchange_type = NULL;
884 conf->connection = NULL;
885 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
888 status = cf_util_get_string (ci, &conf->name);
895 for (i = 0; i < ci->children_num; i++)
897 oconfig_item_t *child = ci->children + i;
899 if (strcasecmp ("Host", child->key) == 0)
900 status = cf_util_get_string (child, &conf->host);
901 else if (strcasecmp ("Port", child->key) == 0)
903 status = cf_util_get_port_number (child);
910 else if (strcasecmp ("VHost", child->key) == 0)
911 status = cf_util_get_string (child, &conf->vhost);
912 else if (strcasecmp ("User", child->key) == 0)
913 status = cf_util_get_string (child, &conf->user);
914 else if (strcasecmp ("Password", child->key) == 0)
915 status = cf_util_get_string (child, &conf->password);
916 else if (strcasecmp ("Exchange", child->key) == 0)
917 status = cf_util_get_string (child, &conf->exchange);
918 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
919 status = cf_util_get_string (child, &conf->exchange_type);
920 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
921 status = cf_util_get_string (child, &conf->queue);
922 else if (strcasecmp ("RoutingKey", child->key) == 0)
923 status = cf_util_get_string (child, &conf->routing_key);
924 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
927 status = cf_util_get_boolean (child, &tmp);
929 conf->delivery_mode = CAMQP_DM_PERSISTENT;
931 conf->delivery_mode = CAMQP_DM_VOLATILE;
933 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
934 status = cf_util_get_boolean (child, &conf->store_rates);
935 else if ((strcasecmp ("Format", child->key) == 0) && publish)
936 status = camqp_config_set_format (child, conf);
937 else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
938 status = cf_util_get_string (child, &conf->prefix);
939 else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
940 status = cf_util_get_string (child, &conf->postfix);
941 else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
943 char *tmp_buff = NULL;
944 status = cf_util_get_string (child, &tmp_buff);
945 if (strlen (tmp_buff) > 1)
946 WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
947 "only one character. Others will be ignored.");
948 conf->escape_char = tmp_buff[0];
952 WARNING ("amqp plugin: Ignoring unknown "
953 "configuration option \"%s\".", child->key);
957 } /* for (i = 0; i < ci->children_num; i++) */
959 if ((status == 0) && (conf->exchange == NULL))
961 if (conf->exchange_type != NULL)
962 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
963 "without the \"Exchange\" option. It will be ignored.");
965 if (!publish && (conf->routing_key != NULL))
966 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
967 "without the \"Exchange\" option. It will be ignored.");
973 camqp_config_free (conf);
977 if (conf->exchange != NULL)
979 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
986 user_data_t ud = { conf, camqp_config_free };
988 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
990 status = plugin_register_write (cbname, camqp_write, &ud);
993 camqp_config_free (conf);
999 status = camqp_subscribe_init (conf);
1002 camqp_config_free (conf);
1008 } /* }}} int camqp_config_connection */
1010 static int camqp_config (oconfig_item_t *ci) /* {{{ */
1014 for (i = 0; i < ci->children_num; i++)
1016 oconfig_item_t *child = ci->children + i;
1018 if (strcasecmp ("Publish", child->key) == 0)
1019 camqp_config_connection (child, /* publish = */ 1);
1020 else if (strcasecmp ("Subscribe", child->key) == 0)
1021 camqp_config_connection (child, /* publish = */ 0);
1023 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
1025 } /* for (ci->children_num) */
1028 } /* }}} int camqp_config */
1030 void module_register (void)
1032 plugin_register_complex_config ("amqp", camqp_config);
1033 plugin_register_shutdown ("amqp", camqp_shutdown);
1034 } /* void module_register */
1036 /* vim: set sw=4 sts=4 et fdm=marker : */