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 _Bool queue_auto_delete;
85 amqp_connection_state_t connection;
88 typedef struct camqp_config_s camqp_config_t;
93 static const char *def_host = "localhost";
94 static const char *def_vhost = "/";
95 static const char *def_user = "guest";
96 static const char *def_password = "guest";
97 static const char *def_exchange = "amq.fanout";
99 static pthread_t *subscriber_threads = NULL;
100 static size_t subscriber_threads_num = 0;
101 static _Bool subscriber_threads_running = 1;
103 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
108 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
112 if ((conf == NULL) || (conf->connection == NULL))
115 sockfd = amqp_get_sockfd (conf->connection);
116 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
117 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
118 amqp_destroy_connection (conf->connection);
120 conf->connection = NULL;
121 } /* }}} void camqp_close_connection */
123 static void camqp_config_free (void *ptr) /* {{{ */
125 camqp_config_t *conf = ptr;
130 camqp_close_connection (conf);
136 sfree (conf->password);
137 sfree (conf->exchange);
138 sfree (conf->exchange_type);
140 sfree (conf->routing_key);
141 sfree (conf->prefix);
142 sfree (conf->postfix);
146 } /* }}} void camqp_config_free */
148 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
152 if ((in == NULL) || (in->bytes == NULL))
155 ret = malloc (in->len + 1);
159 memcpy (ret, in->bytes, in->len);
163 } /* }}} char *camqp_bytes_cstring */
165 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
169 r = amqp_get_rpc_reply (conf->connection);
170 if (r.reply_type == AMQP_RESPONSE_NORMAL)
174 } /* }}} _Bool camqp_is_error */
176 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
177 char *buffer, size_t buffer_size)
181 r = amqp_get_rpc_reply (conf->connection);
182 switch (r.reply_type)
184 case AMQP_RESPONSE_NORMAL:
185 sstrncpy (buffer, "Success", sizeof (buffer));
188 case AMQP_RESPONSE_NONE:
189 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
192 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
193 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
195 return (sstrerror (r.library_errno, buffer, buffer_size));
198 return (sstrerror (r.library_error, buffer, buffer_size));
201 sstrncpy (buffer, "End of stream", sizeof (buffer));
204 case AMQP_RESPONSE_SERVER_EXCEPTION:
205 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
207 amqp_connection_close_t *m = r.reply.decoded;
208 char *tmp = camqp_bytes_cstring (&m->reply_text);
209 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
213 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
215 amqp_channel_close_t *m = r.reply.decoded;
216 char *tmp = camqp_bytes_cstring (&m->reply_text);
217 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
223 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
229 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
234 } /* }}} char *camqp_strerror */
236 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
237 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
239 amqp_exchange_declare_ok_t *ed_ret;
241 if (conf->exchange_type == NULL)
244 ed_ret = amqp_exchange_declare (conf->connection,
245 /* channel = */ CAMQP_CHANNEL,
246 /* exchange = */ amqp_cstring_bytes (conf->exchange),
247 /* type = */ amqp_cstring_bytes (conf->exchange_type),
250 /* auto_delete = */ 1,
251 /* arguments = */ AMQP_EMPTY_TABLE);
252 if ((ed_ret == NULL) && camqp_is_error (conf))
255 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
256 camqp_strerror (conf, errbuf, sizeof (errbuf)));
257 camqp_close_connection (conf);
261 INFO ("amqp plugin: Successfully created exchange \"%s\" "
263 conf->exchange, conf->exchange_type);
266 } /* }}} int camqp_create_exchange */
268 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
270 amqp_exchange_declare_ok_t *ed_ret;
271 amqp_table_t argument_table;
272 struct amqp_table_entry_t_ argument_table_entries[1];
274 if (conf->exchange_type == NULL)
277 /* Valid arguments: "auto_delete", "internal" */
278 argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
279 argument_table.entries = argument_table_entries;
280 argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
281 argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
282 argument_table_entries[0].value.value.boolean = 1;
284 ed_ret = amqp_exchange_declare (conf->connection,
285 /* channel = */ CAMQP_CHANNEL,
286 /* exchange = */ amqp_cstring_bytes (conf->exchange),
287 /* type = */ amqp_cstring_bytes (conf->exchange_type),
290 /* arguments = */ argument_table);
291 if ((ed_ret == NULL) && camqp_is_error (conf))
294 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
295 camqp_strerror (conf, errbuf, sizeof (errbuf)));
296 camqp_close_connection (conf);
300 INFO ("amqp plugin: Successfully created exchange \"%s\" "
302 conf->exchange, conf->exchange_type);
305 } /* }}} int camqp_create_exchange */
308 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
310 amqp_queue_declare_ok_t *qd_ret;
311 amqp_basic_consume_ok_t *cm_ret;
313 qd_ret = amqp_queue_declare (conf->connection,
314 /* channel = */ CAMQP_CHANNEL,
315 /* queue = */ (conf->queue != NULL)
316 ? amqp_cstring_bytes (conf->queue)
319 /* durable = */ conf->queue_durable,
321 /* auto_delete = */ conf->queue_auto_delete,
322 /* arguments = */ AMQP_EMPTY_TABLE);
325 ERROR ("amqp plugin: amqp_queue_declare failed.");
326 camqp_close_connection (conf);
330 if (conf->queue == NULL)
332 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
333 if (conf->queue == NULL)
335 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
336 camqp_close_connection (conf);
340 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
342 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
344 /* bind to an exchange */
345 if (conf->exchange != NULL)
347 amqp_queue_bind_ok_t *qb_ret;
349 assert (conf->queue != NULL);
350 qb_ret = amqp_queue_bind (conf->connection,
351 /* channel = */ CAMQP_CHANNEL,
352 /* queue = */ amqp_cstring_bytes (conf->queue),
353 /* exchange = */ amqp_cstring_bytes (conf->exchange),
354 /* routing_key = */ (conf->routing_key != NULL)
355 ? amqp_cstring_bytes (conf->routing_key)
357 /* arguments = */ AMQP_EMPTY_TABLE);
358 if ((qb_ret == NULL) && camqp_is_error (conf))
361 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
362 camqp_strerror (conf, errbuf, sizeof (errbuf)));
363 camqp_close_connection (conf);
367 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
368 conf->queue, conf->exchange);
369 } /* if (conf->exchange != NULL) */
371 cm_ret = amqp_basic_consume (conf->connection,
372 /* channel = */ CAMQP_CHANNEL,
373 /* queue = */ amqp_cstring_bytes (conf->queue),
374 /* consumer_tag = */ AMQP_EMPTY_BYTES,
378 /* arguments = */ AMQP_EMPTY_TABLE
380 if ((cm_ret == NULL) && camqp_is_error (conf))
383 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
384 camqp_strerror (conf, errbuf, sizeof (errbuf)));
385 camqp_close_connection (conf);
390 } /* }}} int camqp_setup_queue */
392 static int camqp_connect (camqp_config_t *conf) /* {{{ */
394 amqp_rpc_reply_t reply;
398 if (conf->connection != NULL)
401 conf->connection = amqp_new_connection ();
402 if (conf->connection == NULL)
404 ERROR ("amqp plugin: amqp_new_connection failed.");
408 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
412 status = (-1) * sockfd;
413 ERROR ("amqp plugin: amqp_open_socket failed: %s",
414 sstrerror (status, errbuf, sizeof (errbuf)));
415 amqp_destroy_connection (conf->connection);
416 conf->connection = NULL;
419 amqp_set_sockfd (conf->connection, sockfd);
421 reply = amqp_login (conf->connection, CONF(conf, vhost),
422 /* channel max = */ 0,
423 /* frame max = */ 131072,
425 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
426 CONF(conf, user), CONF(conf, password));
427 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
429 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
430 CONF(conf, vhost), CONF(conf, user));
431 amqp_destroy_connection (conf->connection);
433 conf->connection = NULL;
437 amqp_channel_open (conf->connection, /* channel = */ 1);
438 /* FIXME: Is checking "reply.reply_type" really correct here? How does
439 * it get set? --octo */
440 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
442 ERROR ("amqp plugin: amqp_channel_open failed.");
443 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
444 amqp_destroy_connection (conf->connection);
446 conf->connection = NULL;
450 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
451 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
453 status = camqp_create_exchange (conf);
458 return (camqp_setup_queue (conf));
460 } /* }}} int camqp_connect */
462 static int camqp_shutdown (void) /* {{{ */
466 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
467 subscriber_threads_num);
469 subscriber_threads_running = 0;
470 for (i = 0; i < subscriber_threads_num; i++)
472 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
473 * to use a timeout in the thread and check for the variable in regular
475 pthread_kill (subscriber_threads[i], SIGTERM);
476 pthread_join (subscriber_threads[i], /* retval = */ NULL);
479 subscriber_threads_num = 0;
480 sfree (subscriber_threads);
482 DEBUG ("amqp plugin: All subscriber threads exited.");
485 } /* }}} int camqp_shutdown */
490 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
491 size_t body_size, const char *content_type)
493 char body[body_size + 1];
499 memset (body, 0, sizeof (body));
503 while (received < body_size)
505 status = amqp_simple_wait_frame (conf->connection, &frame);
509 status = (-1) * status;
510 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
511 sstrerror (status, errbuf, sizeof (errbuf)));
512 camqp_close_connection (conf);
516 if (frame.frame_type != AMQP_FRAME_BODY)
518 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
523 if ((body_size - received) < frame.payload.body_fragment.len)
525 WARNING ("amqp plugin: Body is larger than indicated by header.");
529 memcpy (body_ptr, frame.payload.body_fragment.bytes,
530 frame.payload.body_fragment.len);
531 body_ptr += frame.payload.body_fragment.len;
532 received += frame.payload.body_fragment.len;
533 } /* while (received < body_size) */
535 if (strcasecmp ("text/collectd", content_type) == 0)
537 status = handle_putval (stderr, body);
539 ERROR ("amqp plugin: handle_putval failed with status %i.",
543 else if (strcasecmp ("application/json", content_type) == 0)
545 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
546 "been implemented yet. FIXME!");
551 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
558 } /* }}} int camqp_read_body */
560 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
564 amqp_basic_properties_t *properties;
567 status = amqp_simple_wait_frame (conf->connection, &frame);
571 status = (-1) * status;
572 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
573 sstrerror (status, errbuf, sizeof (errbuf)));
574 camqp_close_connection (conf);
578 if (frame.frame_type != AMQP_FRAME_HEADER)
580 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
585 properties = frame.payload.properties.decoded;
586 content_type = camqp_bytes_cstring (&properties->content_type);
587 if (content_type == NULL)
589 ERROR ("amqp plugin: Unable to determine content type.");
593 status = camqp_read_body (conf,
594 (size_t) frame.payload.properties.body_size,
597 sfree (content_type);
599 } /* }}} int camqp_read_header */
601 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
603 camqp_config_t *conf = user_data;
606 cdtime_t interval = plugin_get_interval ();
608 while (subscriber_threads_running)
612 status = camqp_connect (conf);
615 struct timespec ts_interval;
616 ERROR ("amqp plugin: camqp_connect failed. "
617 "Will sleep for %.3f seconds.",
618 CDTIME_T_TO_DOUBLE (interval));
619 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
620 nanosleep (&ts_interval, /* remaining = */ NULL);
624 status = amqp_simple_wait_frame (conf->connection, &frame);
627 struct timespec ts_interval;
628 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
629 "Will sleep for %.3f seconds.",
630 CDTIME_T_TO_DOUBLE (interval));
631 camqp_close_connection (conf);
632 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
633 nanosleep (&ts_interval, /* remaining = */ NULL);
637 if (frame.frame_type != AMQP_FRAME_METHOD)
639 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
644 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
646 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
647 frame.payload.method.id);
651 status = camqp_read_header (conf);
653 amqp_maybe_release_buffers (conf->connection);
654 } /* while (subscriber_threads_running) */
656 camqp_config_free (conf);
659 } /* }}} void *camqp_subscribe_thread */
661 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
666 tmp = realloc (subscriber_threads,
667 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
670 ERROR ("amqp plugin: realloc failed.");
671 camqp_config_free (conf);
674 subscriber_threads = tmp;
675 tmp = subscriber_threads + subscriber_threads_num;
676 memset (tmp, 0, sizeof (*tmp));
678 status = plugin_thread_create (tmp, /* attr = */ NULL,
679 camqp_subscribe_thread, conf);
683 ERROR ("amqp plugin: pthread_create failed: %s",
684 sstrerror (status, errbuf, sizeof (errbuf)));
685 camqp_config_free (conf);
689 subscriber_threads_num++;
692 } /* }}} int camqp_subscribe_init */
697 /* XXX: You must hold "conf->lock" when calling this function! */
698 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
699 const char *buffer, const char *routing_key)
701 amqp_basic_properties_t props;
704 status = camqp_connect (conf);
708 memset (&props, 0, sizeof (props));
709 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
710 | AMQP_BASIC_DELIVERY_MODE_FLAG
711 | AMQP_BASIC_APP_ID_FLAG;
712 if (conf->format == CAMQP_FORMAT_COMMAND)
713 props.content_type = amqp_cstring_bytes("text/collectd");
714 else if (conf->format == CAMQP_FORMAT_JSON)
715 props.content_type = amqp_cstring_bytes("application/json");
716 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
717 props.content_type = amqp_cstring_bytes("text/graphite");
720 props.delivery_mode = conf->delivery_mode;
721 props.app_id = amqp_cstring_bytes("collectd");
723 status = amqp_basic_publish(conf->connection,
725 amqp_cstring_bytes(CONF(conf, exchange)),
726 amqp_cstring_bytes (routing_key),
730 amqp_cstring_bytes(buffer));
733 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
735 camqp_close_connection (conf);
739 } /* }}} int camqp_write_locked */
741 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
742 user_data_t *user_data)
744 camqp_config_t *conf = user_data->data;
745 char routing_key[6 * DATA_MAX_NAME_LEN];
749 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
752 memset (buffer, 0, sizeof (buffer));
754 if (conf->routing_key != NULL)
756 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
761 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
763 vl->plugin, vl->plugin_instance,
764 vl->type, vl->type_instance);
766 /* Switch slashes (the only character forbidden by collectd) and dots
767 * (the separation character used by AMQP). */
768 for (i = 0; routing_key[i] != 0; i++)
770 if (routing_key[i] == '.')
771 routing_key[i] = '/';
772 else if (routing_key[i] == '/')
773 routing_key[i] = '.';
777 if (conf->format == CAMQP_FORMAT_COMMAND)
779 status = create_putval (buffer, sizeof (buffer), ds, vl);
782 ERROR ("amqp plugin: create_putval failed with status %i.",
787 else if (conf->format == CAMQP_FORMAT_JSON)
789 size_t bfree = sizeof (buffer);
792 format_json_initialize (buffer, &bfill, &bfree);
793 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
794 format_json_finalize (buffer, &bfill, &bfree);
796 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
798 status = format_graphite (buffer, sizeof (buffer), ds, vl,
799 conf->prefix, conf->postfix, conf->escape_char,
800 conf->graphite_flags);
803 ERROR ("amqp plugin: format_graphite failed with status %i.",
810 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
814 pthread_mutex_lock (&conf->lock);
815 status = camqp_write_locked (conf, buffer, routing_key);
816 pthread_mutex_unlock (&conf->lock);
819 } /* }}} int camqp_write */
824 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
825 camqp_config_t *conf)
831 status = cf_util_get_string (ci, &string);
835 assert (string != NULL);
836 if (strcasecmp ("Command", string) == 0)
837 conf->format = CAMQP_FORMAT_COMMAND;
838 else if (strcasecmp ("JSON", string) == 0)
839 conf->format = CAMQP_FORMAT_JSON;
840 else if (strcasecmp ("Graphite", string) == 0)
841 conf->format = CAMQP_FORMAT_GRAPHITE;
844 WARNING ("amqp plugin: Invalid format string: %s",
851 } /* }}} int config_set_string */
853 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
856 camqp_config_t *conf;
860 conf = malloc (sizeof (*conf));
863 ERROR ("amqp plugin: malloc failed.");
867 /* Initialize "conf" {{{ */
868 memset (conf, 0, sizeof (*conf));
869 conf->publish = publish;
871 conf->format = CAMQP_FORMAT_COMMAND;
876 conf->password = NULL;
877 conf->exchange = NULL;
878 conf->routing_key = NULL;
880 conf->delivery_mode = CAMQP_DM_VOLATILE;
881 conf->store_rates = 0;
882 conf->graphite_flags = 0;
883 /* publish & graphite only */
885 conf->postfix = NULL;
886 conf->escape_char = '_';
888 conf->exchange_type = NULL;
890 conf->queue_durable = 0;
891 conf->queue_auto_delete = 1;
893 conf->connection = NULL;
894 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
897 status = cf_util_get_string (ci, &conf->name);
904 for (i = 0; i < ci->children_num; i++)
906 oconfig_item_t *child = ci->children + i;
908 if (strcasecmp ("Host", child->key) == 0)
909 status = cf_util_get_string (child, &conf->host);
910 else if (strcasecmp ("Port", child->key) == 0)
912 status = cf_util_get_port_number (child);
919 else if (strcasecmp ("VHost", child->key) == 0)
920 status = cf_util_get_string (child, &conf->vhost);
921 else if (strcasecmp ("User", child->key) == 0)
922 status = cf_util_get_string (child, &conf->user);
923 else if (strcasecmp ("Password", child->key) == 0)
924 status = cf_util_get_string (child, &conf->password);
925 else if (strcasecmp ("Exchange", child->key) == 0)
926 status = cf_util_get_string (child, &conf->exchange);
927 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
928 status = cf_util_get_string (child, &conf->exchange_type);
929 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
930 status = cf_util_get_string (child, &conf->queue);
931 else if (strcasecmp ("QueueDurable", child->key) == 0)
932 status = cf_util_get_boolean (child, &conf->queue_durable);
933 else if (strcasecmp ("QueueAutoDelete", child->key) == 0)
934 status = cf_util_get_boolean (child, &conf->queue_auto_delete);
935 else if (strcasecmp ("RoutingKey", child->key) == 0)
936 status = cf_util_get_string (child, &conf->routing_key);
937 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
940 status = cf_util_get_boolean (child, &tmp);
942 conf->delivery_mode = CAMQP_DM_PERSISTENT;
944 conf->delivery_mode = CAMQP_DM_VOLATILE;
946 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
948 status = cf_util_get_boolean (child, &conf->store_rates);
949 (void) cf_util_get_flag (child, &conf->graphite_flags,
950 GRAPHITE_STORE_RATES);
952 else if ((strcasecmp ("Format", child->key) == 0) && publish)
953 status = camqp_config_set_format (child, conf);
954 else if ((strcasecmp ("GraphiteSeparateInstances", child->key) == 0) && publish)
955 status = cf_util_get_flag (child, &conf->graphite_flags,
956 GRAPHITE_SEPARATE_INSTANCES);
957 else if ((strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) && publish)
958 status = cf_util_get_flag (child, &conf->graphite_flags,
959 GRAPHITE_ALWAYS_APPEND_DS);
960 else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
961 status = cf_util_get_string (child, &conf->prefix);
962 else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
963 status = cf_util_get_string (child, &conf->postfix);
964 else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
966 char *tmp_buff = NULL;
967 status = cf_util_get_string (child, &tmp_buff);
968 if (strlen (tmp_buff) > 1)
969 WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
970 "only one character. Others will be ignored.");
971 conf->escape_char = tmp_buff[0];
975 WARNING ("amqp plugin: Ignoring unknown "
976 "configuration option \"%s\".", child->key);
980 } /* for (i = 0; i < ci->children_num; i++) */
982 if ((status == 0) && (conf->exchange == NULL))
984 if (conf->exchange_type != NULL)
985 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
986 "without the \"Exchange\" option. It will be ignored.");
988 if (!publish && (conf->routing_key != NULL))
989 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
990 "without the \"Exchange\" option. It will be ignored.");
996 camqp_config_free (conf);
1000 if (conf->exchange != NULL)
1002 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
1009 user_data_t ud = { conf, camqp_config_free };
1011 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
1013 status = plugin_register_write (cbname, camqp_write, &ud);
1016 camqp_config_free (conf);
1022 status = camqp_subscribe_init (conf);
1025 camqp_config_free (conf);
1031 } /* }}} int camqp_config_connection */
1033 static int camqp_config (oconfig_item_t *ci) /* {{{ */
1037 for (i = 0; i < ci->children_num; i++)
1039 oconfig_item_t *child = ci->children + i;
1041 if (strcasecmp ("Publish", child->key) == 0)
1042 camqp_config_connection (child, /* publish = */ 1);
1043 else if (strcasecmp ("Subscribe", child->key) == 0)
1044 camqp_config_connection (child, /* publish = */ 0);
1046 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
1048 } /* for (ci->children_num) */
1051 } /* }}} int camqp_config */
1053 void module_register (void)
1055 plugin_register_complex_config ("amqp", camqp_config);
1056 plugin_register_shutdown ("amqp", camqp_shutdown);
1057 } /* void module_register */
1059 /* vim: set sw=4 sts=4 et fdm=marker : */