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 */
77 unsigned int graphite_flags;
83 amqp_connection_state_t connection;
86 typedef struct camqp_config_s camqp_config_t;
91 static const char *def_host = "localhost";
92 static const char *def_vhost = "/";
93 static const char *def_user = "guest";
94 static const char *def_password = "guest";
95 static const char *def_exchange = "amq.fanout";
97 static pthread_t *subscriber_threads = NULL;
98 static size_t subscriber_threads_num = 0;
99 static _Bool subscriber_threads_running = 1;
101 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
106 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
110 if ((conf == NULL) || (conf->connection == NULL))
113 sockfd = amqp_get_sockfd (conf->connection);
114 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
115 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
116 amqp_destroy_connection (conf->connection);
118 conf->connection = NULL;
119 } /* }}} void camqp_close_connection */
121 static void camqp_config_free (void *ptr) /* {{{ */
123 camqp_config_t *conf = ptr;
128 camqp_close_connection (conf);
134 sfree (conf->password);
135 sfree (conf->exchange);
136 sfree (conf->exchange_type);
138 sfree (conf->routing_key);
139 sfree (conf->prefix);
140 sfree (conf->postfix);
144 } /* }}} void camqp_config_free */
146 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
150 if ((in == NULL) || (in->bytes == NULL))
153 ret = malloc (in->len + 1);
157 memcpy (ret, in->bytes, in->len);
161 } /* }}} char *camqp_bytes_cstring */
163 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
167 r = amqp_get_rpc_reply (conf->connection);
168 if (r.reply_type == AMQP_RESPONSE_NORMAL)
172 } /* }}} _Bool camqp_is_error */
174 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
175 char *buffer, size_t buffer_size)
179 r = amqp_get_rpc_reply (conf->connection);
180 switch (r.reply_type)
182 case AMQP_RESPONSE_NORMAL:
183 sstrncpy (buffer, "Success", sizeof (buffer));
186 case AMQP_RESPONSE_NONE:
187 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
190 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
191 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
193 return (sstrerror (r.library_errno, buffer, buffer_size));
196 return (sstrerror (r.library_error, buffer, buffer_size));
199 sstrncpy (buffer, "End of stream", sizeof (buffer));
202 case AMQP_RESPONSE_SERVER_EXCEPTION:
203 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
205 amqp_connection_close_t *m = r.reply.decoded;
206 char *tmp = camqp_bytes_cstring (&m->reply_text);
207 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
211 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
213 amqp_channel_close_t *m = r.reply.decoded;
214 char *tmp = camqp_bytes_cstring (&m->reply_text);
215 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
221 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
227 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
232 } /* }}} char *camqp_strerror */
234 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
235 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
237 amqp_exchange_declare_ok_t *ed_ret;
239 if (conf->exchange_type == NULL)
242 ed_ret = amqp_exchange_declare (conf->connection,
243 /* channel = */ CAMQP_CHANNEL,
244 /* exchange = */ amqp_cstring_bytes (conf->exchange),
245 /* type = */ amqp_cstring_bytes (conf->exchange_type),
248 /* auto_delete = */ 1,
249 /* arguments = */ AMQP_EMPTY_TABLE);
250 if ((ed_ret == NULL) && camqp_is_error (conf))
253 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
254 camqp_strerror (conf, errbuf, sizeof (errbuf)));
255 camqp_close_connection (conf);
259 INFO ("amqp plugin: Successfully created exchange \"%s\" "
261 conf->exchange, conf->exchange_type);
264 } /* }}} int camqp_create_exchange */
266 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
268 amqp_exchange_declare_ok_t *ed_ret;
269 amqp_table_t argument_table;
270 struct amqp_table_entry_t_ argument_table_entries[1];
272 if (conf->exchange_type == NULL)
275 /* Valid arguments: "auto_delete", "internal" */
276 argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
277 argument_table.entries = argument_table_entries;
278 argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
279 argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
280 argument_table_entries[0].value.value.boolean = 1;
282 ed_ret = amqp_exchange_declare (conf->connection,
283 /* channel = */ CAMQP_CHANNEL,
284 /* exchange = */ amqp_cstring_bytes (conf->exchange),
285 /* type = */ amqp_cstring_bytes (conf->exchange_type),
288 /* arguments = */ argument_table);
289 if ((ed_ret == NULL) && camqp_is_error (conf))
292 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
293 camqp_strerror (conf, errbuf, sizeof (errbuf)));
294 camqp_close_connection (conf);
298 INFO ("amqp plugin: Successfully created exchange \"%s\" "
300 conf->exchange, conf->exchange_type);
303 } /* }}} int camqp_create_exchange */
306 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
308 amqp_queue_declare_ok_t *qd_ret;
309 amqp_basic_consume_ok_t *cm_ret;
311 qd_ret = amqp_queue_declare (conf->connection,
312 /* channel = */ CAMQP_CHANNEL,
313 /* queue = */ (conf->queue != NULL)
314 ? amqp_cstring_bytes (conf->queue)
319 /* auto_delete = */ 1,
320 /* arguments = */ AMQP_EMPTY_TABLE);
323 ERROR ("amqp plugin: amqp_queue_declare failed.");
324 camqp_close_connection (conf);
328 if (conf->queue == NULL)
330 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
331 if (conf->queue == NULL)
333 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
334 camqp_close_connection (conf);
338 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
340 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
342 /* bind to an exchange */
343 if (conf->exchange != NULL)
345 amqp_queue_bind_ok_t *qb_ret;
347 assert (conf->queue != NULL);
348 qb_ret = amqp_queue_bind (conf->connection,
349 /* channel = */ CAMQP_CHANNEL,
350 /* queue = */ amqp_cstring_bytes (conf->queue),
351 /* exchange = */ amqp_cstring_bytes (conf->exchange),
352 /* routing_key = */ (conf->routing_key != NULL)
353 ? amqp_cstring_bytes (conf->routing_key)
355 /* arguments = */ AMQP_EMPTY_TABLE);
356 if ((qb_ret == NULL) && camqp_is_error (conf))
359 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
360 camqp_strerror (conf, errbuf, sizeof (errbuf)));
361 camqp_close_connection (conf);
365 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
366 conf->queue, conf->exchange);
367 } /* if (conf->exchange != NULL) */
369 cm_ret = amqp_basic_consume (conf->connection,
370 /* channel = */ CAMQP_CHANNEL,
371 /* queue = */ amqp_cstring_bytes (conf->queue),
372 /* consumer_tag = */ AMQP_EMPTY_BYTES,
376 /* arguments = */ AMQP_EMPTY_TABLE
378 if ((cm_ret == NULL) && camqp_is_error (conf))
381 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
382 camqp_strerror (conf, errbuf, sizeof (errbuf)));
383 camqp_close_connection (conf);
388 } /* }}} int camqp_setup_queue */
390 static int camqp_connect (camqp_config_t *conf) /* {{{ */
392 amqp_rpc_reply_t reply;
396 if (conf->connection != NULL)
399 conf->connection = amqp_new_connection ();
400 if (conf->connection == NULL)
402 ERROR ("amqp plugin: amqp_new_connection failed.");
406 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
410 status = (-1) * sockfd;
411 ERROR ("amqp plugin: amqp_open_socket failed: %s",
412 sstrerror (status, errbuf, sizeof (errbuf)));
413 amqp_destroy_connection (conf->connection);
414 conf->connection = NULL;
417 amqp_set_sockfd (conf->connection, sockfd);
419 reply = amqp_login (conf->connection, CONF(conf, vhost),
420 /* channel max = */ 0,
421 /* frame max = */ 131072,
423 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
424 CONF(conf, user), CONF(conf, password));
425 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
427 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
428 CONF(conf, vhost), CONF(conf, user));
429 amqp_destroy_connection (conf->connection);
431 conf->connection = NULL;
435 amqp_channel_open (conf->connection, /* channel = */ 1);
436 /* FIXME: Is checking "reply.reply_type" really correct here? How does
437 * it get set? --octo */
438 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
440 ERROR ("amqp plugin: amqp_channel_open failed.");
441 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
442 amqp_destroy_connection (conf->connection);
444 conf->connection = NULL;
448 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
449 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
451 status = camqp_create_exchange (conf);
456 return (camqp_setup_queue (conf));
458 } /* }}} int camqp_connect */
460 static int camqp_shutdown (void) /* {{{ */
464 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
465 subscriber_threads_num);
467 subscriber_threads_running = 0;
468 for (i = 0; i < subscriber_threads_num; i++)
470 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
471 * to use a timeout in the thread and check for the variable in regular
473 pthread_kill (subscriber_threads[i], SIGTERM);
474 pthread_join (subscriber_threads[i], /* retval = */ NULL);
477 subscriber_threads_num = 0;
478 sfree (subscriber_threads);
480 DEBUG ("amqp plugin: All subscriber threads exited.");
483 } /* }}} int camqp_shutdown */
488 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
489 size_t body_size, const char *content_type)
491 char body[body_size + 1];
497 memset (body, 0, sizeof (body));
501 while (received < body_size)
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_BODY)
516 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
521 if ((body_size - received) < frame.payload.body_fragment.len)
523 WARNING ("amqp plugin: Body is larger than indicated by header.");
527 memcpy (body_ptr, frame.payload.body_fragment.bytes,
528 frame.payload.body_fragment.len);
529 body_ptr += frame.payload.body_fragment.len;
530 received += frame.payload.body_fragment.len;
531 } /* while (received < body_size) */
533 if (strcasecmp ("text/collectd", content_type) == 0)
535 status = handle_putval (stderr, body);
537 ERROR ("amqp plugin: handle_putval failed with status %i.",
541 else if (strcasecmp ("application/json", content_type) == 0)
543 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
544 "been implemented yet. FIXME!");
549 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
556 } /* }}} int camqp_read_body */
558 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
562 amqp_basic_properties_t *properties;
565 status = amqp_simple_wait_frame (conf->connection, &frame);
569 status = (-1) * status;
570 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
571 sstrerror (status, errbuf, sizeof (errbuf)));
572 camqp_close_connection (conf);
576 if (frame.frame_type != AMQP_FRAME_HEADER)
578 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
583 properties = frame.payload.properties.decoded;
584 content_type = camqp_bytes_cstring (&properties->content_type);
585 if (content_type == NULL)
587 ERROR ("amqp plugin: Unable to determine content type.");
591 status = camqp_read_body (conf,
592 (size_t) frame.payload.properties.body_size,
595 sfree (content_type);
597 } /* }}} int camqp_read_header */
599 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
601 camqp_config_t *conf = user_data;
604 cdtime_t interval = plugin_get_interval ();
606 while (subscriber_threads_running)
610 status = camqp_connect (conf);
613 struct timespec ts_interval;
614 ERROR ("amqp plugin: camqp_connect failed. "
615 "Will sleep for %.3f seconds.",
616 CDTIME_T_TO_DOUBLE (interval));
617 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
618 nanosleep (&ts_interval, /* remaining = */ NULL);
622 status = amqp_simple_wait_frame (conf->connection, &frame);
625 struct timespec ts_interval;
626 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
627 "Will sleep for %.3f seconds.",
628 CDTIME_T_TO_DOUBLE (interval));
629 camqp_close_connection (conf);
630 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
631 nanosleep (&ts_interval, /* remaining = */ NULL);
635 if (frame.frame_type != AMQP_FRAME_METHOD)
637 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
642 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
644 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
645 frame.payload.method.id);
649 status = camqp_read_header (conf);
651 amqp_maybe_release_buffers (conf->connection);
652 } /* while (subscriber_threads_running) */
654 camqp_config_free (conf);
657 } /* }}} void *camqp_subscribe_thread */
659 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
664 tmp = realloc (subscriber_threads,
665 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
668 ERROR ("amqp plugin: realloc failed.");
669 camqp_config_free (conf);
672 subscriber_threads = tmp;
673 tmp = subscriber_threads + subscriber_threads_num;
674 memset (tmp, 0, sizeof (*tmp));
676 status = plugin_thread_create (tmp, /* attr = */ NULL,
677 camqp_subscribe_thread, conf);
681 ERROR ("amqp plugin: pthread_create failed: %s",
682 sstrerror (status, errbuf, sizeof (errbuf)));
683 camqp_config_free (conf);
687 subscriber_threads_num++;
690 } /* }}} int camqp_subscribe_init */
695 /* XXX: You must hold "conf->lock" when calling this function! */
696 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
697 const char *buffer, const char *routing_key)
699 amqp_basic_properties_t props;
702 status = camqp_connect (conf);
706 memset (&props, 0, sizeof (props));
707 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
708 | AMQP_BASIC_DELIVERY_MODE_FLAG
709 | AMQP_BASIC_APP_ID_FLAG;
710 if (conf->format == CAMQP_FORMAT_COMMAND)
711 props.content_type = amqp_cstring_bytes("text/collectd");
712 else if (conf->format == CAMQP_FORMAT_JSON)
713 props.content_type = amqp_cstring_bytes("application/json");
714 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
715 props.content_type = amqp_cstring_bytes("text/graphite");
718 props.delivery_mode = conf->delivery_mode;
719 props.app_id = amqp_cstring_bytes("collectd");
721 status = amqp_basic_publish(conf->connection,
723 amqp_cstring_bytes(CONF(conf, exchange)),
724 amqp_cstring_bytes (routing_key),
728 amqp_cstring_bytes(buffer));
731 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
733 camqp_close_connection (conf);
737 } /* }}} int camqp_write_locked */
739 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
740 user_data_t *user_data)
742 camqp_config_t *conf = user_data->data;
743 char routing_key[6 * DATA_MAX_NAME_LEN];
747 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
750 memset (buffer, 0, sizeof (buffer));
752 if (conf->routing_key != NULL)
754 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
759 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
761 vl->plugin, vl->plugin_instance,
762 vl->type, vl->type_instance);
764 /* Switch slashes (the only character forbidden by collectd) and dots
765 * (the separation character used by AMQP). */
766 for (i = 0; routing_key[i] != 0; i++)
768 if (routing_key[i] == '.')
769 routing_key[i] = '/';
770 else if (routing_key[i] == '/')
771 routing_key[i] = '.';
775 if (conf->format == CAMQP_FORMAT_COMMAND)
777 status = create_putval (buffer, sizeof (buffer), ds, vl);
780 ERROR ("amqp plugin: create_putval failed with status %i.",
785 else if (conf->format == CAMQP_FORMAT_JSON)
787 size_t bfree = sizeof (buffer);
790 format_json_initialize (buffer, &bfill, &bfree);
791 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
792 format_json_finalize (buffer, &bfill, &bfree);
794 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
796 status = format_graphite (buffer, sizeof (buffer), ds, vl,
797 conf->prefix, conf->postfix, conf->escape_char,
798 conf->graphite_flags);
801 ERROR ("amqp plugin: format_graphite failed with status %i.",
808 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
812 pthread_mutex_lock (&conf->lock);
813 status = camqp_write_locked (conf, buffer, routing_key);
814 pthread_mutex_unlock (&conf->lock);
817 } /* }}} int camqp_write */
822 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
823 camqp_config_t *conf)
829 status = cf_util_get_string (ci, &string);
833 assert (string != NULL);
834 if (strcasecmp ("Command", string) == 0)
835 conf->format = CAMQP_FORMAT_COMMAND;
836 else if (strcasecmp ("JSON", string) == 0)
837 conf->format = CAMQP_FORMAT_JSON;
838 else if (strcasecmp ("Graphite", string) == 0)
839 conf->format = CAMQP_FORMAT_GRAPHITE;
842 WARNING ("amqp plugin: Invalid format string: %s",
849 } /* }}} int config_set_string */
851 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
854 camqp_config_t *conf;
858 conf = malloc (sizeof (*conf));
861 ERROR ("amqp plugin: malloc failed.");
865 /* Initialize "conf" {{{ */
866 memset (conf, 0, sizeof (*conf));
867 conf->publish = publish;
869 conf->format = CAMQP_FORMAT_COMMAND;
874 conf->password = NULL;
875 conf->exchange = NULL;
876 conf->routing_key = NULL;
878 conf->delivery_mode = CAMQP_DM_VOLATILE;
879 conf->store_rates = 0;
880 conf->graphite_flags = 0;
881 /* publish & graphite only */
883 conf->postfix = NULL;
884 conf->escape_char = '_';
886 conf->exchange_type = NULL;
889 conf->connection = NULL;
890 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
893 status = cf_util_get_string (ci, &conf->name);
900 for (i = 0; i < ci->children_num; i++)
902 oconfig_item_t *child = ci->children + i;
904 if (strcasecmp ("Host", child->key) == 0)
905 status = cf_util_get_string (child, &conf->host);
906 else if (strcasecmp ("Port", child->key) == 0)
908 status = cf_util_get_port_number (child);
915 else if (strcasecmp ("VHost", child->key) == 0)
916 status = cf_util_get_string (child, &conf->vhost);
917 else if (strcasecmp ("User", child->key) == 0)
918 status = cf_util_get_string (child, &conf->user);
919 else if (strcasecmp ("Password", child->key) == 0)
920 status = cf_util_get_string (child, &conf->password);
921 else if (strcasecmp ("Exchange", child->key) == 0)
922 status = cf_util_get_string (child, &conf->exchange);
923 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
924 status = cf_util_get_string (child, &conf->exchange_type);
925 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
926 status = cf_util_get_string (child, &conf->queue);
927 else if (strcasecmp ("RoutingKey", child->key) == 0)
928 status = cf_util_get_string (child, &conf->routing_key);
929 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
932 status = cf_util_get_boolean (child, &tmp);
934 conf->delivery_mode = CAMQP_DM_PERSISTENT;
936 conf->delivery_mode = CAMQP_DM_VOLATILE;
938 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
940 status = cf_util_get_boolean (child, &conf->store_rates);
941 (void) cf_util_get_flag (child, &conf->graphite_flags,
942 GRAPHITE_STORE_RATES);
944 else if ((strcasecmp ("Format", child->key) == 0) && publish)
945 status = camqp_config_set_format (child, conf);
946 else if ((strcasecmp ("GraphiteSeparateInstances", child->key) == 0) && publish)
947 status = cf_util_get_flag (child, &conf->graphite_flags,
948 GRAPHITE_SEPARATE_INSTANCES);
949 else if ((strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) && publish)
950 status = cf_util_get_flag (child, &conf->graphite_flags,
951 GRAPHITE_ALWAYS_APPEND_DS);
952 else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
953 status = cf_util_get_string (child, &conf->prefix);
954 else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
955 status = cf_util_get_string (child, &conf->postfix);
956 else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
958 char *tmp_buff = NULL;
959 status = cf_util_get_string (child, &tmp_buff);
960 if (strlen (tmp_buff) > 1)
961 WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
962 "only one character. Others will be ignored.");
963 conf->escape_char = tmp_buff[0];
967 WARNING ("amqp plugin: Ignoring unknown "
968 "configuration option \"%s\".", child->key);
972 } /* for (i = 0; i < ci->children_num; i++) */
974 if ((status == 0) && (conf->exchange == NULL))
976 if (conf->exchange_type != NULL)
977 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
978 "without the \"Exchange\" option. It will be ignored.");
980 if (!publish && (conf->routing_key != NULL))
981 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
982 "without the \"Exchange\" option. It will be ignored.");
988 camqp_config_free (conf);
992 if (conf->exchange != NULL)
994 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
1001 user_data_t ud = { conf, camqp_config_free };
1003 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
1005 status = plugin_register_write (cbname, camqp_write, &ud);
1008 camqp_config_free (conf);
1014 status = camqp_subscribe_init (conf);
1017 camqp_config_free (conf);
1023 } /* }}} int camqp_config_connection */
1025 static int camqp_config (oconfig_item_t *ci) /* {{{ */
1029 for (i = 0; i < ci->children_num; i++)
1031 oconfig_item_t *child = ci->children + i;
1033 if (strcasecmp ("Publish", child->key) == 0)
1034 camqp_config_connection (child, /* publish = */ 1);
1035 else if (strcasecmp ("Subscribe", child->key) == 0)
1036 camqp_config_connection (child, /* publish = */ 0);
1038 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
1040 } /* for (ci->children_num) */
1043 } /* }}} int camqp_config */
1045 void module_register (void)
1047 plugin_register_complex_config ("amqp", camqp_config);
1048 plugin_register_shutdown ("amqp", camqp_shutdown);
1049 } /* void module_register */
1051 /* vim: set sw=4 sts=4 et fdm=marker : */