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
83 /* Number of seconds to wait before connection is retried */
84 int connection_retry_delay;
87 uint8_t delivery_mode;
90 /* publish & graphite format only */
94 unsigned int graphite_flags;
100 _Bool queue_auto_delete;
102 amqp_connection_state_t connection;
103 pthread_mutex_t lock;
105 typedef struct camqp_config_s camqp_config_t;
110 static const char *def_host = "localhost";
111 static const char *def_vhost = "/";
112 static const char *def_user = "guest";
113 static const char *def_password = "guest";
114 static const char *def_exchange = "amq.fanout";
116 static pthread_t *subscriber_threads = NULL;
117 static size_t subscriber_threads_num = 0;
118 static _Bool subscriber_threads_running = 1;
120 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
125 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
129 if ((conf == NULL) || (conf->connection == NULL))
132 sockfd = amqp_get_sockfd (conf->connection);
133 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
134 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
135 amqp_destroy_connection (conf->connection);
137 conf->connection = NULL;
138 } /* }}} void camqp_close_connection */
140 static void camqp_config_free (void *ptr) /* {{{ */
142 camqp_config_t *conf = ptr;
147 camqp_close_connection (conf);
153 sfree (conf->password);
154 sfree (conf->exchange);
155 sfree (conf->exchange_type);
157 sfree (conf->routing_key);
158 sfree (conf->prefix);
159 sfree (conf->postfix);
163 } /* }}} void camqp_config_free */
165 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
169 if ((in == NULL) || (in->bytes == NULL))
172 ret = malloc (in->len + 1);
176 memcpy (ret, in->bytes, in->len);
180 } /* }}} char *camqp_bytes_cstring */
182 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
186 r = amqp_get_rpc_reply (conf->connection);
187 if (r.reply_type == AMQP_RESPONSE_NORMAL)
191 } /* }}} _Bool camqp_is_error */
193 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
194 char *buffer, size_t buffer_size)
198 r = amqp_get_rpc_reply (conf->connection);
199 switch (r.reply_type)
201 case AMQP_RESPONSE_NORMAL:
202 sstrncpy (buffer, "Success", buffer_size);
205 case AMQP_RESPONSE_NONE:
206 sstrncpy (buffer, "Missing RPC reply type", buffer_size);
209 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
210 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
212 return (sstrerror (r.library_errno, buffer, buffer_size));
215 return (sstrerror (r.library_error, buffer, buffer_size));
218 sstrncpy (buffer, "End of stream", buffer_size);
221 case AMQP_RESPONSE_SERVER_EXCEPTION:
222 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
224 amqp_connection_close_t *m = r.reply.decoded;
225 char *tmp = camqp_bytes_cstring (&m->reply_text);
226 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
230 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
232 amqp_channel_close_t *m = r.reply.decoded;
233 char *tmp = camqp_bytes_cstring (&m->reply_text);
234 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
240 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
246 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
251 } /* }}} char *camqp_strerror */
253 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
254 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
256 amqp_exchange_declare_ok_t *ed_ret;
258 if (conf->exchange_type == NULL)
261 ed_ret = amqp_exchange_declare (conf->connection,
262 /* channel = */ CAMQP_CHANNEL,
263 /* exchange = */ amqp_cstring_bytes (conf->exchange),
264 /* type = */ amqp_cstring_bytes (conf->exchange_type),
267 /* auto_delete = */ 1,
268 /* arguments = */ AMQP_EMPTY_TABLE);
269 if ((ed_ret == NULL) && camqp_is_error (conf))
272 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
273 camqp_strerror (conf, errbuf, sizeof (errbuf)));
274 camqp_close_connection (conf);
278 INFO ("amqp plugin: Successfully created exchange \"%s\" "
280 conf->exchange, conf->exchange_type);
283 } /* }}} int camqp_create_exchange */
285 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
287 amqp_exchange_declare_ok_t *ed_ret;
288 amqp_table_t argument_table;
289 struct amqp_table_entry_t_ argument_table_entries[1];
291 if (conf->exchange_type == NULL)
294 /* Valid arguments: "auto_delete", "internal" */
295 argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
296 argument_table.entries = argument_table_entries;
297 argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
298 argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
299 argument_table_entries[0].value.value.boolean = 1;
301 ed_ret = amqp_exchange_declare (conf->connection,
302 /* channel = */ CAMQP_CHANNEL,
303 /* exchange = */ amqp_cstring_bytes (conf->exchange),
304 /* type = */ amqp_cstring_bytes (conf->exchange_type),
307 #if defined(AMQP_VERSION) && AMQP_VERSION >= 0x00060000
308 /* auto delete = */ 0,
311 /* arguments = */ argument_table);
312 if ((ed_ret == NULL) && camqp_is_error (conf))
315 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
316 camqp_strerror (conf, errbuf, sizeof (errbuf)));
317 camqp_close_connection (conf);
321 INFO ("amqp plugin: Successfully created exchange \"%s\" "
323 conf->exchange, conf->exchange_type);
326 } /* }}} int camqp_create_exchange */
329 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
331 amqp_queue_declare_ok_t *qd_ret;
332 amqp_basic_consume_ok_t *cm_ret;
334 qd_ret = amqp_queue_declare (conf->connection,
335 /* channel = */ CAMQP_CHANNEL,
336 /* queue = */ (conf->queue != NULL)
337 ? amqp_cstring_bytes (conf->queue)
340 /* durable = */ conf->queue_durable,
342 /* auto_delete = */ conf->queue_auto_delete,
343 /* arguments = */ AMQP_EMPTY_TABLE);
346 ERROR ("amqp plugin: amqp_queue_declare failed.");
347 camqp_close_connection (conf);
351 if (conf->queue == NULL)
353 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
354 if (conf->queue == NULL)
356 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
357 camqp_close_connection (conf);
361 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
363 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
365 /* bind to an exchange */
366 if (conf->exchange != NULL)
368 amqp_queue_bind_ok_t *qb_ret;
370 assert (conf->queue != NULL);
371 qb_ret = amqp_queue_bind (conf->connection,
372 /* channel = */ CAMQP_CHANNEL,
373 /* queue = */ amqp_cstring_bytes (conf->queue),
374 /* exchange = */ amqp_cstring_bytes (conf->exchange),
375 /* routing_key = */ (conf->routing_key != NULL)
376 ? amqp_cstring_bytes (conf->routing_key)
378 /* arguments = */ AMQP_EMPTY_TABLE);
379 if ((qb_ret == NULL) && camqp_is_error (conf))
382 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
383 camqp_strerror (conf, errbuf, sizeof (errbuf)));
384 camqp_close_connection (conf);
388 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
389 conf->queue, conf->exchange);
390 } /* if (conf->exchange != NULL) */
392 cm_ret = amqp_basic_consume (conf->connection,
393 /* channel = */ CAMQP_CHANNEL,
394 /* queue = */ amqp_cstring_bytes (conf->queue),
395 /* consumer_tag = */ AMQP_EMPTY_BYTES,
399 /* arguments = */ AMQP_EMPTY_TABLE
401 if ((cm_ret == NULL) && camqp_is_error (conf))
404 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
405 camqp_strerror (conf, errbuf, sizeof (errbuf)));
406 camqp_close_connection (conf);
411 } /* }}} int camqp_setup_queue */
413 static int camqp_connect (camqp_config_t *conf) /* {{{ */
415 static time_t last_connect_time = 0;
417 amqp_rpc_reply_t reply;
419 #ifdef HAVE_AMQP_TCP_SOCKET
420 amqp_socket_t *socket;
425 if (conf->connection != NULL)
428 time_t now = time(NULL);
429 if (now < (last_connect_time + conf->connection_retry_delay))
431 DEBUG("amqp plugin: skipping connection retry, "
432 "ConnectionRetryDelay: %d", conf->connection_retry_delay);
437 DEBUG ("amqp plugin: retrying connection");
438 last_connect_time = now;
441 conf->connection = amqp_new_connection ();
442 if (conf->connection == NULL)
444 ERROR ("amqp plugin: amqp_new_connection failed.");
448 #ifdef HAVE_AMQP_TCP_SOCKET
449 # define CLOSE_SOCKET() /* amqp_destroy_connection() closes the socket for us */
450 /* TODO: add support for SSL using amqp_ssl_socket_new
451 * and related functions */
452 socket = amqp_tcp_socket_new (conf->connection);
455 ERROR ("amqp plugin: amqp_tcp_socket_new failed.");
456 amqp_destroy_connection (conf->connection);
457 conf->connection = NULL;
461 status = amqp_socket_open (socket, CONF(conf, host), conf->port);
466 ERROR ("amqp plugin: amqp_socket_open failed: %s",
467 sstrerror (status, errbuf, sizeof (errbuf)));
468 amqp_destroy_connection (conf->connection);
469 conf->connection = NULL;
472 #else /* HAVE_AMQP_TCP_SOCKET */
473 # define CLOSE_SOCKET() close(sockfd)
474 /* this interface is deprecated as of rabbitmq-c 0.4 */
475 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
479 status = (-1) * sockfd;
480 ERROR ("amqp plugin: amqp_open_socket failed: %s",
481 sstrerror (status, errbuf, sizeof (errbuf)));
482 amqp_destroy_connection (conf->connection);
483 conf->connection = NULL;
486 amqp_set_sockfd (conf->connection, sockfd);
489 reply = amqp_login (conf->connection, CONF(conf, vhost),
490 /* channel max = */ 0,
491 /* frame max = */ 131072,
493 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
494 CONF(conf, user), CONF(conf, password));
495 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
497 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
498 CONF(conf, vhost), CONF(conf, user));
499 amqp_destroy_connection (conf->connection);
501 conf->connection = NULL;
505 amqp_channel_open (conf->connection, /* channel = */ 1);
506 /* FIXME: Is checking "reply.reply_type" really correct here? How does
507 * it get set? --octo */
508 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
510 ERROR ("amqp plugin: amqp_channel_open failed.");
511 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
512 amqp_destroy_connection (conf->connection);
514 conf->connection = NULL;
518 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
519 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
521 status = camqp_create_exchange (conf);
526 return (camqp_setup_queue (conf));
528 } /* }}} int camqp_connect */
530 static int camqp_shutdown (void) /* {{{ */
534 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
535 subscriber_threads_num);
537 subscriber_threads_running = 0;
538 for (i = 0; i < subscriber_threads_num; i++)
540 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
541 * to use a timeout in the thread and check for the variable in regular
543 pthread_kill (subscriber_threads[i], SIGTERM);
544 pthread_join (subscriber_threads[i], /* retval = */ NULL);
547 subscriber_threads_num = 0;
548 sfree (subscriber_threads);
550 DEBUG ("amqp plugin: All subscriber threads exited.");
553 } /* }}} int camqp_shutdown */
558 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
559 size_t body_size, const char *content_type)
561 char body[body_size + 1];
567 memset (body, 0, sizeof (body));
571 while (received < body_size)
573 status = amqp_simple_wait_frame (conf->connection, &frame);
577 status = (-1) * status;
578 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
579 sstrerror (status, errbuf, sizeof (errbuf)));
580 camqp_close_connection (conf);
584 if (frame.frame_type != AMQP_FRAME_BODY)
586 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
591 if ((body_size - received) < frame.payload.body_fragment.len)
593 WARNING ("amqp plugin: Body is larger than indicated by header.");
597 memcpy (body_ptr, frame.payload.body_fragment.bytes,
598 frame.payload.body_fragment.len);
599 body_ptr += frame.payload.body_fragment.len;
600 received += frame.payload.body_fragment.len;
601 } /* while (received < body_size) */
603 if (strcasecmp ("text/collectd", content_type) == 0)
605 status = handle_putval (stderr, body);
607 ERROR ("amqp plugin: handle_putval failed with status %i.",
611 else if (strcasecmp ("application/json", content_type) == 0)
613 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
614 "been implemented yet. FIXME!");
619 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
626 } /* }}} int camqp_read_body */
628 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
632 amqp_basic_properties_t *properties;
635 status = amqp_simple_wait_frame (conf->connection, &frame);
639 status = (-1) * status;
640 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
641 sstrerror (status, errbuf, sizeof (errbuf)));
642 camqp_close_connection (conf);
646 if (frame.frame_type != AMQP_FRAME_HEADER)
648 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
653 properties = frame.payload.properties.decoded;
654 content_type = camqp_bytes_cstring (&properties->content_type);
655 if (content_type == NULL)
657 ERROR ("amqp plugin: Unable to determine content type.");
661 status = camqp_read_body (conf,
662 (size_t) frame.payload.properties.body_size,
665 sfree (content_type);
667 } /* }}} int camqp_read_header */
669 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
671 camqp_config_t *conf = user_data;
674 cdtime_t interval = plugin_get_interval ();
676 while (subscriber_threads_running)
680 status = camqp_connect (conf);
683 struct timespec ts_interval;
684 ERROR ("amqp plugin: camqp_connect failed. "
685 "Will sleep for %.3f seconds.",
686 CDTIME_T_TO_DOUBLE (interval));
687 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
688 nanosleep (&ts_interval, /* remaining = */ NULL);
692 status = amqp_simple_wait_frame (conf->connection, &frame);
695 struct timespec ts_interval;
696 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
697 "Will sleep for %.3f seconds.",
698 CDTIME_T_TO_DOUBLE (interval));
699 camqp_close_connection (conf);
700 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
701 nanosleep (&ts_interval, /* remaining = */ NULL);
705 if (frame.frame_type != AMQP_FRAME_METHOD)
707 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
712 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
714 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
715 frame.payload.method.id);
719 camqp_read_header (conf);
721 amqp_maybe_release_buffers (conf->connection);
722 } /* while (subscriber_threads_running) */
724 camqp_config_free (conf);
727 } /* }}} void *camqp_subscribe_thread */
729 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
734 tmp = realloc (subscriber_threads,
735 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
738 ERROR ("amqp plugin: realloc failed.");
739 sfree (subscriber_threads);
742 subscriber_threads = tmp;
743 tmp = subscriber_threads + subscriber_threads_num;
744 memset (tmp, 0, sizeof (*tmp));
746 status = plugin_thread_create (tmp, /* attr = */ NULL,
747 camqp_subscribe_thread, conf);
751 ERROR ("amqp plugin: pthread_create failed: %s",
752 sstrerror (status, errbuf, sizeof (errbuf)));
756 subscriber_threads_num++;
759 } /* }}} int camqp_subscribe_init */
764 /* XXX: You must hold "conf->lock" when calling this function! */
765 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
766 const char *buffer, const char *routing_key)
768 amqp_basic_properties_t props;
771 status = camqp_connect (conf);
775 memset (&props, 0, sizeof (props));
776 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
777 | AMQP_BASIC_DELIVERY_MODE_FLAG
778 | AMQP_BASIC_APP_ID_FLAG;
779 if (conf->format == CAMQP_FORMAT_COMMAND)
780 props.content_type = amqp_cstring_bytes("text/collectd");
781 else if (conf->format == CAMQP_FORMAT_JSON)
782 props.content_type = amqp_cstring_bytes("application/json");
783 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
784 props.content_type = amqp_cstring_bytes("text/graphite");
787 props.delivery_mode = conf->delivery_mode;
788 props.app_id = amqp_cstring_bytes("collectd");
790 status = amqp_basic_publish(conf->connection,
792 amqp_cstring_bytes(CONF(conf, exchange)),
793 amqp_cstring_bytes (routing_key),
797 amqp_cstring_bytes(buffer));
800 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
802 camqp_close_connection (conf);
806 } /* }}} int camqp_write_locked */
808 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
809 user_data_t *user_data)
811 camqp_config_t *conf = user_data->data;
812 char routing_key[6 * DATA_MAX_NAME_LEN];
816 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
819 memset (buffer, 0, sizeof (buffer));
821 if (conf->routing_key != NULL)
823 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
828 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
830 vl->plugin, vl->plugin_instance,
831 vl->type, vl->type_instance);
833 /* Switch slashes (the only character forbidden by collectd) and dots
834 * (the separation character used by AMQP). */
835 for (i = 0; routing_key[i] != 0; i++)
837 if (routing_key[i] == '.')
838 routing_key[i] = '/';
839 else if (routing_key[i] == '/')
840 routing_key[i] = '.';
844 if (conf->format == CAMQP_FORMAT_COMMAND)
846 status = create_putval (buffer, sizeof (buffer), ds, vl);
849 ERROR ("amqp plugin: create_putval failed with status %i.",
854 else if (conf->format == CAMQP_FORMAT_JSON)
856 size_t bfree = sizeof (buffer);
859 format_json_initialize (buffer, &bfill, &bfree);
860 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
861 format_json_finalize (buffer, &bfill, &bfree);
863 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
865 status = format_graphite (buffer, sizeof (buffer), ds, vl,
866 conf->prefix, conf->postfix, conf->escape_char,
867 conf->graphite_flags);
870 ERROR ("amqp plugin: format_graphite failed with status %i.",
877 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
881 pthread_mutex_lock (&conf->lock);
882 status = camqp_write_locked (conf, buffer, routing_key);
883 pthread_mutex_unlock (&conf->lock);
886 } /* }}} int camqp_write */
891 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
892 camqp_config_t *conf)
898 status = cf_util_get_string (ci, &string);
902 assert (string != NULL);
903 if (strcasecmp ("Command", string) == 0)
904 conf->format = CAMQP_FORMAT_COMMAND;
905 else if (strcasecmp ("JSON", string) == 0)
906 conf->format = CAMQP_FORMAT_JSON;
907 else if (strcasecmp ("Graphite", string) == 0)
908 conf->format = CAMQP_FORMAT_GRAPHITE;
911 WARNING ("amqp plugin: Invalid format string: %s",
918 } /* }}} int config_set_string */
920 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
923 camqp_config_t *conf;
927 conf = calloc (1, sizeof (*conf));
930 ERROR ("amqp plugin: calloc failed.");
934 /* Initialize "conf" {{{ */
935 conf->publish = publish;
937 conf->format = CAMQP_FORMAT_COMMAND;
942 conf->password = NULL;
943 conf->exchange = NULL;
944 conf->routing_key = NULL;
945 conf->connection_retry_delay = 0;
948 conf->delivery_mode = CAMQP_DM_VOLATILE;
949 conf->store_rates = 0;
950 conf->graphite_flags = 0;
951 /* publish & graphite only */
953 conf->postfix = NULL;
954 conf->escape_char = '_';
956 conf->exchange_type = NULL;
958 conf->queue_durable = 0;
959 conf->queue_auto_delete = 1;
961 conf->connection = NULL;
962 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
965 status = cf_util_get_string (ci, &conf->name);
972 for (i = 0; i < ci->children_num; i++)
974 oconfig_item_t *child = ci->children + i;
976 if (strcasecmp ("Host", child->key) == 0)
977 status = cf_util_get_string (child, &conf->host);
978 else if (strcasecmp ("Port", child->key) == 0)
980 status = cf_util_get_port_number (child);
987 else if (strcasecmp ("VHost", child->key) == 0)
988 status = cf_util_get_string (child, &conf->vhost);
989 else if (strcasecmp ("User", child->key) == 0)
990 status = cf_util_get_string (child, &conf->user);
991 else if (strcasecmp ("Password", child->key) == 0)
992 status = cf_util_get_string (child, &conf->password);
993 else if (strcasecmp ("Exchange", child->key) == 0)
994 status = cf_util_get_string (child, &conf->exchange);
995 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
996 status = cf_util_get_string (child, &conf->exchange_type);
997 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
998 status = cf_util_get_string (child, &conf->queue);
999 else if ((strcasecmp ("QueueDurable", child->key) == 0) && !publish)
1000 status = cf_util_get_boolean (child, &conf->queue_durable);
1001 else if ((strcasecmp ("QueueAutoDelete", child->key) == 0) && !publish)
1002 status = cf_util_get_boolean (child, &conf->queue_auto_delete);
1003 else if (strcasecmp ("RoutingKey", child->key) == 0)
1004 status = cf_util_get_string (child, &conf->routing_key);
1005 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
1008 status = cf_util_get_boolean (child, &tmp);
1010 conf->delivery_mode = CAMQP_DM_PERSISTENT;
1012 conf->delivery_mode = CAMQP_DM_VOLATILE;
1014 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
1016 status = cf_util_get_boolean (child, &conf->store_rates);
1017 (void) cf_util_get_flag (child, &conf->graphite_flags,
1018 GRAPHITE_STORE_RATES);
1020 else if ((strcasecmp ("Format", child->key) == 0) && publish)
1021 status = camqp_config_set_format (child, conf);
1022 else if ((strcasecmp ("GraphiteSeparateInstances", child->key) == 0) && publish)
1023 status = cf_util_get_flag (child, &conf->graphite_flags,
1024 GRAPHITE_SEPARATE_INSTANCES);
1025 else if ((strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) && publish)
1026 status = cf_util_get_flag (child, &conf->graphite_flags,
1027 GRAPHITE_ALWAYS_APPEND_DS);
1028 else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
1029 status = cf_util_get_string (child, &conf->prefix);
1030 else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
1031 status = cf_util_get_string (child, &conf->postfix);
1032 else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
1034 char *tmp_buff = NULL;
1035 status = cf_util_get_string (child, &tmp_buff);
1036 if (strlen (tmp_buff) > 1)
1037 WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
1038 "only one character. Others will be ignored.");
1039 conf->escape_char = tmp_buff[0];
1042 else if (strcasecmp ("ConnectionRetryDelay", child->key) == 0)
1043 status = cf_util_get_int (child, &conf->connection_retry_delay);
1045 WARNING ("amqp plugin: Ignoring unknown "
1046 "configuration option \"%s\".", child->key);
1050 } /* for (i = 0; i < ci->children_num; i++) */
1052 if ((status == 0) && (conf->exchange == NULL))
1054 if (conf->exchange_type != NULL)
1055 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
1056 "without the \"Exchange\" option. It will be ignored.");
1058 if (!publish && (conf->routing_key != NULL))
1059 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
1060 "without the \"Exchange\" option. It will be ignored.");
1066 camqp_config_free (conf);
1070 if (conf->exchange != NULL)
1072 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
1079 user_data_t ud = { conf, camqp_config_free };
1081 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
1083 status = plugin_register_write (cbname, camqp_write, &ud);
1086 camqp_config_free (conf);
1092 status = camqp_subscribe_init (conf);
1095 camqp_config_free (conf);
1101 } /* }}} int camqp_config_connection */
1103 static int camqp_config (oconfig_item_t *ci) /* {{{ */
1107 for (i = 0; i < ci->children_num; i++)
1109 oconfig_item_t *child = ci->children + i;
1111 if (strcasecmp ("Publish", child->key) == 0)
1112 camqp_config_connection (child, /* publish = */ 1);
1113 else if (strcasecmp ("Subscribe", child->key) == 0)
1114 camqp_config_connection (child, /* publish = */ 0);
1116 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
1118 } /* for (ci->children_num) */
1121 } /* }}} int camqp_config */
1123 void module_register (void)
1125 plugin_register_complex_config ("amqp", camqp_config);
1126 plugin_register_shutdown ("amqp", camqp_shutdown);
1127 } /* void module_register */
1129 /* vim: set sw=4 sts=4 et fdm=marker : */