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 collectd.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 #ifdef HAVE_AMQP_TCP_SOCKET_H
42 # include <amqp_tcp_socket.h>
44 #ifdef HAVE_AMQP_SOCKET_H
45 # include <amqp_socket.h>
47 #ifdef HAVE_AMQP_TCP_SOCKET
48 #if defined HAVE_DECL_AMQP_SOCKET_CLOSE && !HAVE_DECL_AMQP_SOCKET_CLOSE
49 /* rabbitmq-c does not currently ship amqp_socket.h
50 * and, thus, does not define this function. */
51 int amqp_socket_close(amqp_socket_t *);
55 /* Defines for the delivery mode. I have no idea why they're not defined by the
57 #define CAMQP_DM_VOLATILE 1
58 #define CAMQP_DM_PERSISTENT 2
60 #define CAMQP_FORMAT_COMMAND 1
61 #define CAMQP_FORMAT_JSON 2
62 #define CAMQP_FORMAT_GRAPHITE 3
64 #define CAMQP_CHANNEL 1
84 uint8_t delivery_mode;
87 /* publish & graphite format only */
91 unsigned int graphite_flags;
97 _Bool queue_auto_delete;
99 amqp_connection_state_t connection;
100 pthread_mutex_t lock;
102 typedef struct camqp_config_s camqp_config_t;
107 static const char *def_host = "localhost";
108 static const char *def_vhost = "/";
109 static const char *def_user = "guest";
110 static const char *def_password = "guest";
111 static const char *def_exchange = "amq.fanout";
113 static pthread_t *subscriber_threads = NULL;
114 static size_t subscriber_threads_num = 0;
115 static _Bool subscriber_threads_running = 1;
117 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
122 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
126 if ((conf == NULL) || (conf->connection == NULL))
129 sockfd = amqp_get_sockfd (conf->connection);
130 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
131 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
132 amqp_destroy_connection (conf->connection);
134 conf->connection = NULL;
135 } /* }}} void camqp_close_connection */
137 static void camqp_config_free (void *ptr) /* {{{ */
139 camqp_config_t *conf = ptr;
144 camqp_close_connection (conf);
150 sfree (conf->password);
151 sfree (conf->exchange);
152 sfree (conf->exchange_type);
154 sfree (conf->routing_key);
155 sfree (conf->prefix);
156 sfree (conf->postfix);
160 } /* }}} void camqp_config_free */
162 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
166 if ((in == NULL) || (in->bytes == NULL))
169 ret = malloc (in->len + 1);
173 memcpy (ret, in->bytes, in->len);
177 } /* }}} char *camqp_bytes_cstring */
179 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
183 r = amqp_get_rpc_reply (conf->connection);
184 if (r.reply_type == AMQP_RESPONSE_NORMAL)
188 } /* }}} _Bool camqp_is_error */
190 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
191 char *buffer, size_t buffer_size)
195 r = amqp_get_rpc_reply (conf->connection);
196 switch (r.reply_type)
198 case AMQP_RESPONSE_NORMAL:
199 sstrncpy (buffer, "Success", sizeof (buffer));
202 case AMQP_RESPONSE_NONE:
203 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
206 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
207 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
209 return (sstrerror (r.library_errno, buffer, buffer_size));
212 return (sstrerror (r.library_error, buffer, buffer_size));
215 sstrncpy (buffer, "End of stream", sizeof (buffer));
218 case AMQP_RESPONSE_SERVER_EXCEPTION:
219 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
221 amqp_connection_close_t *m = r.reply.decoded;
222 char *tmp = camqp_bytes_cstring (&m->reply_text);
223 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
227 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
229 amqp_channel_close_t *m = r.reply.decoded;
230 char *tmp = camqp_bytes_cstring (&m->reply_text);
231 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
237 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
243 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
248 } /* }}} char *camqp_strerror */
250 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
251 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
253 amqp_exchange_declare_ok_t *ed_ret;
255 if (conf->exchange_type == NULL)
258 ed_ret = amqp_exchange_declare (conf->connection,
259 /* channel = */ CAMQP_CHANNEL,
260 /* exchange = */ amqp_cstring_bytes (conf->exchange),
261 /* type = */ amqp_cstring_bytes (conf->exchange_type),
264 /* auto_delete = */ 1,
265 /* arguments = */ AMQP_EMPTY_TABLE);
266 if ((ed_ret == NULL) && camqp_is_error (conf))
269 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
270 camqp_strerror (conf, errbuf, sizeof (errbuf)));
271 camqp_close_connection (conf);
275 INFO ("amqp plugin: Successfully created exchange \"%s\" "
277 conf->exchange, conf->exchange_type);
280 } /* }}} int camqp_create_exchange */
282 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
284 amqp_exchange_declare_ok_t *ed_ret;
285 amqp_table_t argument_table;
286 struct amqp_table_entry_t_ argument_table_entries[1];
288 if (conf->exchange_type == NULL)
291 /* Valid arguments: "auto_delete", "internal" */
292 argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
293 argument_table.entries = argument_table_entries;
294 argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
295 argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
296 argument_table_entries[0].value.value.boolean = 1;
298 ed_ret = amqp_exchange_declare (conf->connection,
299 /* channel = */ CAMQP_CHANNEL,
300 /* exchange = */ amqp_cstring_bytes (conf->exchange),
301 /* type = */ amqp_cstring_bytes (conf->exchange_type),
304 /* arguments = */ argument_table);
305 if ((ed_ret == NULL) && camqp_is_error (conf))
308 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
309 camqp_strerror (conf, errbuf, sizeof (errbuf)));
310 camqp_close_connection (conf);
314 INFO ("amqp plugin: Successfully created exchange \"%s\" "
316 conf->exchange, conf->exchange_type);
319 } /* }}} int camqp_create_exchange */
322 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
324 amqp_queue_declare_ok_t *qd_ret;
325 amqp_basic_consume_ok_t *cm_ret;
327 qd_ret = amqp_queue_declare (conf->connection,
328 /* channel = */ CAMQP_CHANNEL,
329 /* queue = */ (conf->queue != NULL)
330 ? amqp_cstring_bytes (conf->queue)
333 /* durable = */ conf->queue_durable,
335 /* auto_delete = */ conf->queue_auto_delete,
336 /* arguments = */ AMQP_EMPTY_TABLE);
339 ERROR ("amqp plugin: amqp_queue_declare failed.");
340 camqp_close_connection (conf);
344 if (conf->queue == NULL)
346 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
347 if (conf->queue == NULL)
349 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
350 camqp_close_connection (conf);
354 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
356 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
358 /* bind to an exchange */
359 if (conf->exchange != NULL)
361 amqp_queue_bind_ok_t *qb_ret;
363 assert (conf->queue != NULL);
364 qb_ret = amqp_queue_bind (conf->connection,
365 /* channel = */ CAMQP_CHANNEL,
366 /* queue = */ amqp_cstring_bytes (conf->queue),
367 /* exchange = */ amqp_cstring_bytes (conf->exchange),
368 /* routing_key = */ (conf->routing_key != NULL)
369 ? amqp_cstring_bytes (conf->routing_key)
371 /* arguments = */ AMQP_EMPTY_TABLE);
372 if ((qb_ret == NULL) && camqp_is_error (conf))
375 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
376 camqp_strerror (conf, errbuf, sizeof (errbuf)));
377 camqp_close_connection (conf);
381 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
382 conf->queue, conf->exchange);
383 } /* if (conf->exchange != NULL) */
385 cm_ret = amqp_basic_consume (conf->connection,
386 /* channel = */ CAMQP_CHANNEL,
387 /* queue = */ amqp_cstring_bytes (conf->queue),
388 /* consumer_tag = */ AMQP_EMPTY_BYTES,
392 /* arguments = */ AMQP_EMPTY_TABLE
394 if ((cm_ret == NULL) && camqp_is_error (conf))
397 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
398 camqp_strerror (conf, errbuf, sizeof (errbuf)));
399 camqp_close_connection (conf);
404 } /* }}} int camqp_setup_queue */
406 static int camqp_connect (camqp_config_t *conf) /* {{{ */
408 amqp_rpc_reply_t reply;
410 #ifdef HAVE_AMQP_TCP_SOCKET
411 amqp_socket_t *socket;
416 if (conf->connection != NULL)
419 conf->connection = amqp_new_connection ();
420 if (conf->connection == NULL)
422 ERROR ("amqp plugin: amqp_new_connection failed.");
426 #ifdef HAVE_AMQP_TCP_SOCKET
427 # define CLOSE_SOCKET() /* amqp_destroy_connection() closes the socket for us */
428 /* TODO: add support for SSL using amqp_ssl_socket_new
429 * and related functions */
430 socket = amqp_tcp_socket_new (conf->connection);
433 ERROR ("amqp plugin: amqp_tcp_socket_new failed.");
434 amqp_destroy_connection (conf->connection);
435 conf->connection = NULL;
439 status = amqp_socket_open (socket, CONF(conf, host), conf->port);
444 ERROR ("amqp plugin: amqp_socket_open failed: %s",
445 sstrerror (status, errbuf, sizeof (errbuf)));
446 amqp_destroy_connection (conf->connection);
447 conf->connection = NULL;
450 #else /* HAVE_AMQP_TCP_SOCKET */
451 # define CLOSE_SOCKET() close(sockfd)
452 /* this interface is deprecated as of rabbitmq-c 0.4 */
453 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
457 status = (-1) * sockfd;
458 ERROR ("amqp plugin: amqp_open_socket failed: %s",
459 sstrerror (status, errbuf, sizeof (errbuf)));
460 amqp_destroy_connection (conf->connection);
461 conf->connection = NULL;
464 amqp_set_sockfd (conf->connection, sockfd);
467 reply = amqp_login (conf->connection, CONF(conf, vhost),
468 /* channel max = */ 0,
469 /* frame max = */ 131072,
471 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
472 CONF(conf, user), CONF(conf, password));
473 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
475 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
476 CONF(conf, vhost), CONF(conf, user));
477 amqp_destroy_connection (conf->connection);
479 conf->connection = NULL;
483 amqp_channel_open (conf->connection, /* channel = */ 1);
484 /* FIXME: Is checking "reply.reply_type" really correct here? How does
485 * it get set? --octo */
486 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
488 ERROR ("amqp plugin: amqp_channel_open failed.");
489 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
490 amqp_destroy_connection (conf->connection);
492 conf->connection = NULL;
496 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
497 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
499 status = camqp_create_exchange (conf);
504 return (camqp_setup_queue (conf));
506 } /* }}} int camqp_connect */
508 static int camqp_shutdown (void) /* {{{ */
512 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
513 subscriber_threads_num);
515 subscriber_threads_running = 0;
516 for (i = 0; i < subscriber_threads_num; i++)
518 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
519 * to use a timeout in the thread and check for the variable in regular
521 pthread_kill (subscriber_threads[i], SIGTERM);
522 pthread_join (subscriber_threads[i], /* retval = */ NULL);
525 subscriber_threads_num = 0;
526 sfree (subscriber_threads);
528 DEBUG ("amqp plugin: All subscriber threads exited.");
531 } /* }}} int camqp_shutdown */
536 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
537 size_t body_size, const char *content_type)
539 char body[body_size + 1];
545 memset (body, 0, sizeof (body));
549 while (received < body_size)
551 status = amqp_simple_wait_frame (conf->connection, &frame);
555 status = (-1) * status;
556 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
557 sstrerror (status, errbuf, sizeof (errbuf)));
558 camqp_close_connection (conf);
562 if (frame.frame_type != AMQP_FRAME_BODY)
564 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
569 if ((body_size - received) < frame.payload.body_fragment.len)
571 WARNING ("amqp plugin: Body is larger than indicated by header.");
575 memcpy (body_ptr, frame.payload.body_fragment.bytes,
576 frame.payload.body_fragment.len);
577 body_ptr += frame.payload.body_fragment.len;
578 received += frame.payload.body_fragment.len;
579 } /* while (received < body_size) */
581 if (strcasecmp ("text/collectd", content_type) == 0)
583 status = handle_putval (stderr, body);
585 ERROR ("amqp plugin: handle_putval failed with status %i.",
589 else if (strcasecmp ("application/json", content_type) == 0)
591 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
592 "been implemented yet. FIXME!");
597 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
604 } /* }}} int camqp_read_body */
606 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
610 amqp_basic_properties_t *properties;
613 status = amqp_simple_wait_frame (conf->connection, &frame);
617 status = (-1) * status;
618 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
619 sstrerror (status, errbuf, sizeof (errbuf)));
620 camqp_close_connection (conf);
624 if (frame.frame_type != AMQP_FRAME_HEADER)
626 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
631 properties = frame.payload.properties.decoded;
632 content_type = camqp_bytes_cstring (&properties->content_type);
633 if (content_type == NULL)
635 ERROR ("amqp plugin: Unable to determine content type.");
639 status = camqp_read_body (conf,
640 (size_t) frame.payload.properties.body_size,
643 sfree (content_type);
645 } /* }}} int camqp_read_header */
647 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
649 camqp_config_t *conf = user_data;
652 cdtime_t interval = plugin_get_interval ();
654 while (subscriber_threads_running)
658 status = camqp_connect (conf);
661 struct timespec ts_interval;
662 ERROR ("amqp plugin: camqp_connect failed. "
663 "Will sleep for %.3f seconds.",
664 CDTIME_T_TO_DOUBLE (interval));
665 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
666 nanosleep (&ts_interval, /* remaining = */ NULL);
670 status = amqp_simple_wait_frame (conf->connection, &frame);
673 struct timespec ts_interval;
674 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
675 "Will sleep for %.3f seconds.",
676 CDTIME_T_TO_DOUBLE (interval));
677 camqp_close_connection (conf);
678 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
679 nanosleep (&ts_interval, /* remaining = */ NULL);
683 if (frame.frame_type != AMQP_FRAME_METHOD)
685 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
690 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
692 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
693 frame.payload.method.id);
697 status = camqp_read_header (conf);
699 amqp_maybe_release_buffers (conf->connection);
700 } /* while (subscriber_threads_running) */
702 camqp_config_free (conf);
705 } /* }}} void *camqp_subscribe_thread */
707 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
712 tmp = realloc (subscriber_threads,
713 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
716 ERROR ("amqp plugin: realloc failed.");
717 camqp_config_free (conf);
720 subscriber_threads = tmp;
721 tmp = subscriber_threads + subscriber_threads_num;
722 memset (tmp, 0, sizeof (*tmp));
724 status = plugin_thread_create (tmp, /* attr = */ NULL,
725 camqp_subscribe_thread, conf);
729 ERROR ("amqp plugin: pthread_create failed: %s",
730 sstrerror (status, errbuf, sizeof (errbuf)));
731 camqp_config_free (conf);
735 subscriber_threads_num++;
738 } /* }}} int camqp_subscribe_init */
743 /* XXX: You must hold "conf->lock" when calling this function! */
744 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
745 const char *buffer, const char *routing_key)
747 amqp_basic_properties_t props;
750 status = camqp_connect (conf);
754 memset (&props, 0, sizeof (props));
755 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
756 | AMQP_BASIC_DELIVERY_MODE_FLAG
757 | AMQP_BASIC_APP_ID_FLAG;
758 if (conf->format == CAMQP_FORMAT_COMMAND)
759 props.content_type = amqp_cstring_bytes("text/collectd");
760 else if (conf->format == CAMQP_FORMAT_JSON)
761 props.content_type = amqp_cstring_bytes("application/json");
762 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
763 props.content_type = amqp_cstring_bytes("text/graphite");
766 props.delivery_mode = conf->delivery_mode;
767 props.app_id = amqp_cstring_bytes("collectd");
769 status = amqp_basic_publish(conf->connection,
771 amqp_cstring_bytes(CONF(conf, exchange)),
772 amqp_cstring_bytes (routing_key),
776 amqp_cstring_bytes(buffer));
779 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
781 camqp_close_connection (conf);
785 } /* }}} int camqp_write_locked */
787 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
788 user_data_t *user_data)
790 camqp_config_t *conf = user_data->data;
791 char routing_key[6 * DATA_MAX_NAME_LEN];
795 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
798 memset (buffer, 0, sizeof (buffer));
800 if (conf->routing_key != NULL)
802 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
807 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
809 vl->plugin, vl->plugin_instance,
810 vl->type, vl->type_instance);
812 /* Switch slashes (the only character forbidden by collectd) and dots
813 * (the separation character used by AMQP). */
814 for (i = 0; routing_key[i] != 0; i++)
816 if (routing_key[i] == '.')
817 routing_key[i] = '/';
818 else if (routing_key[i] == '/')
819 routing_key[i] = '.';
823 if (conf->format == CAMQP_FORMAT_COMMAND)
825 status = create_putval (buffer, sizeof (buffer), ds, vl);
828 ERROR ("amqp plugin: create_putval failed with status %i.",
833 else if (conf->format == CAMQP_FORMAT_JSON)
835 size_t bfree = sizeof (buffer);
838 format_json_initialize (buffer, &bfill, &bfree);
839 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
840 format_json_finalize (buffer, &bfill, &bfree);
842 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
844 status = format_graphite (buffer, sizeof (buffer), ds, vl,
845 conf->prefix, conf->postfix, conf->escape_char,
846 conf->graphite_flags);
849 ERROR ("amqp plugin: format_graphite failed with status %i.",
856 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
860 pthread_mutex_lock (&conf->lock);
861 status = camqp_write_locked (conf, buffer, routing_key);
862 pthread_mutex_unlock (&conf->lock);
865 } /* }}} int camqp_write */
870 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
871 camqp_config_t *conf)
877 status = cf_util_get_string (ci, &string);
881 assert (string != NULL);
882 if (strcasecmp ("Command", string) == 0)
883 conf->format = CAMQP_FORMAT_COMMAND;
884 else if (strcasecmp ("JSON", string) == 0)
885 conf->format = CAMQP_FORMAT_JSON;
886 else if (strcasecmp ("Graphite", string) == 0)
887 conf->format = CAMQP_FORMAT_GRAPHITE;
890 WARNING ("amqp plugin: Invalid format string: %s",
897 } /* }}} int config_set_string */
899 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
902 camqp_config_t *conf;
906 conf = malloc (sizeof (*conf));
909 ERROR ("amqp plugin: malloc failed.");
913 /* Initialize "conf" {{{ */
914 memset (conf, 0, sizeof (*conf));
915 conf->publish = publish;
917 conf->format = CAMQP_FORMAT_COMMAND;
922 conf->password = NULL;
923 conf->exchange = NULL;
924 conf->routing_key = NULL;
926 conf->delivery_mode = CAMQP_DM_VOLATILE;
927 conf->store_rates = 0;
928 conf->graphite_flags = 0;
929 /* publish & graphite only */
931 conf->postfix = NULL;
932 conf->escape_char = '_';
934 conf->exchange_type = NULL;
936 conf->queue_durable = 0;
937 conf->queue_auto_delete = 1;
939 conf->connection = NULL;
940 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
943 status = cf_util_get_string (ci, &conf->name);
950 for (i = 0; i < ci->children_num; i++)
952 oconfig_item_t *child = ci->children + i;
954 if (strcasecmp ("Host", child->key) == 0)
955 status = cf_util_get_string (child, &conf->host);
956 else if (strcasecmp ("Port", child->key) == 0)
958 status = cf_util_get_port_number (child);
965 else if (strcasecmp ("VHost", child->key) == 0)
966 status = cf_util_get_string (child, &conf->vhost);
967 else if (strcasecmp ("User", child->key) == 0)
968 status = cf_util_get_string (child, &conf->user);
969 else if (strcasecmp ("Password", child->key) == 0)
970 status = cf_util_get_string (child, &conf->password);
971 else if (strcasecmp ("Exchange", child->key) == 0)
972 status = cf_util_get_string (child, &conf->exchange);
973 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
974 status = cf_util_get_string (child, &conf->exchange_type);
975 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
976 status = cf_util_get_string (child, &conf->queue);
977 else if ((strcasecmp ("QueueDurable", child->key) == 0) && !publish)
978 status = cf_util_get_boolean (child, &conf->queue_durable);
979 else if ((strcasecmp ("QueueAutoDelete", child->key) == 0) && !publish)
980 status = cf_util_get_boolean (child, &conf->queue_auto_delete);
981 else if (strcasecmp ("RoutingKey", child->key) == 0)
982 status = cf_util_get_string (child, &conf->routing_key);
983 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
986 status = cf_util_get_boolean (child, &tmp);
988 conf->delivery_mode = CAMQP_DM_PERSISTENT;
990 conf->delivery_mode = CAMQP_DM_VOLATILE;
992 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
994 status = cf_util_get_boolean (child, &conf->store_rates);
995 (void) cf_util_get_flag (child, &conf->graphite_flags,
996 GRAPHITE_STORE_RATES);
998 else if ((strcasecmp ("Format", child->key) == 0) && publish)
999 status = camqp_config_set_format (child, conf);
1000 else if ((strcasecmp ("GraphiteSeparateInstances", child->key) == 0) && publish)
1001 status = cf_util_get_flag (child, &conf->graphite_flags,
1002 GRAPHITE_SEPARATE_INSTANCES);
1003 else if ((strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) && publish)
1004 status = cf_util_get_flag (child, &conf->graphite_flags,
1005 GRAPHITE_ALWAYS_APPEND_DS);
1006 else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
1007 status = cf_util_get_string (child, &conf->prefix);
1008 else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
1009 status = cf_util_get_string (child, &conf->postfix);
1010 else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
1012 char *tmp_buff = NULL;
1013 status = cf_util_get_string (child, &tmp_buff);
1014 if (strlen (tmp_buff) > 1)
1015 WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
1016 "only one character. Others will be ignored.");
1017 conf->escape_char = tmp_buff[0];
1021 WARNING ("amqp plugin: Ignoring unknown "
1022 "configuration option \"%s\".", child->key);
1026 } /* for (i = 0; i < ci->children_num; i++) */
1028 if ((status == 0) && (conf->exchange == NULL))
1030 if (conf->exchange_type != NULL)
1031 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
1032 "without the \"Exchange\" option. It will be ignored.");
1034 if (!publish && (conf->routing_key != NULL))
1035 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
1036 "without the \"Exchange\" option. It will be ignored.");
1042 camqp_config_free (conf);
1046 if (conf->exchange != NULL)
1048 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
1055 user_data_t ud = { conf, camqp_config_free };
1057 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
1059 status = plugin_register_write (cbname, camqp_write, &ud);
1062 camqp_config_free (conf);
1068 status = camqp_subscribe_init (conf);
1071 camqp_config_free (conf);
1077 } /* }}} int camqp_config_connection */
1079 static int camqp_config (oconfig_item_t *ci) /* {{{ */
1083 for (i = 0; i < ci->children_num; i++)
1085 oconfig_item_t *child = ci->children + i;
1087 if (strcasecmp ("Publish", child->key) == 0)
1088 camqp_config_connection (child, /* publish = */ 1);
1089 else if (strcasecmp ("Subscribe", child->key) == 0)
1090 camqp_config_connection (child, /* publish = */ 0);
1092 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
1094 } /* for (ci->children_num) */
1097 } /* }}} int camqp_config */
1099 void module_register (void)
1101 plugin_register_complex_config ("amqp", camqp_config);
1102 plugin_register_shutdown ("amqp", camqp_shutdown);
1103 } /* void module_register */
1105 /* vim: set sw=4 sts=4 et fdm=marker : */