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>
33 #include "utils_cmd_putval.h"
34 #include "utils_format_json.h"
35 #include "utils_format_graphite.h"
38 #include <amqp_framing.h>
40 #ifdef HAVE_AMQP_TCP_SOCKET_H
41 # include <amqp_tcp_socket.h>
43 #ifdef HAVE_AMQP_SOCKET_H
44 # include <amqp_socket.h>
46 #ifdef HAVE_AMQP_TCP_SOCKET
47 #if defined HAVE_DECL_AMQP_SOCKET_CLOSE && !HAVE_DECL_AMQP_SOCKET_CLOSE
48 /* rabbitmq-c does not currently ship amqp_socket.h
49 * and, thus, does not define this function. */
50 int amqp_socket_close(amqp_socket_t *);
54 /* Defines for the delivery mode. I have no idea why they're not defined by the
56 #define CAMQP_DM_VOLATILE 1
57 #define CAMQP_DM_PERSISTENT 2
59 #define CAMQP_FORMAT_COMMAND 1
60 #define CAMQP_FORMAT_JSON 2
61 #define CAMQP_FORMAT_GRAPHITE 3
63 #define CAMQP_CHANNEL 1
82 /* Number of seconds to wait before connection is retried */
83 int connection_retry_delay;
86 uint8_t delivery_mode;
89 /* publish & graphite format only */
93 unsigned int graphite_flags;
99 _Bool queue_auto_delete;
101 amqp_connection_state_t connection;
102 pthread_mutex_t lock;
104 typedef struct camqp_config_s camqp_config_t;
109 static const char *def_host = "localhost";
110 static const char *def_vhost = "/";
111 static const char *def_user = "guest";
112 static const char *def_password = "guest";
113 static const char *def_exchange = "amq.fanout";
115 static pthread_t *subscriber_threads = NULL;
116 static size_t subscriber_threads_num = 0;
117 static _Bool subscriber_threads_running = 1;
119 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
124 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
128 if ((conf == NULL) || (conf->connection == NULL))
131 sockfd = amqp_get_sockfd (conf->connection);
132 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
133 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
134 amqp_destroy_connection (conf->connection);
136 conf->connection = NULL;
137 } /* }}} void camqp_close_connection */
139 static void camqp_config_free (void *ptr) /* {{{ */
141 camqp_config_t *conf = ptr;
146 camqp_close_connection (conf);
152 sfree (conf->password);
153 sfree (conf->exchange);
154 sfree (conf->exchange_type);
156 sfree (conf->routing_key);
157 sfree (conf->prefix);
158 sfree (conf->postfix);
162 } /* }}} void camqp_config_free */
164 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
168 if ((in == NULL) || (in->bytes == NULL))
171 ret = malloc (in->len + 1);
175 memcpy (ret, in->bytes, in->len);
179 } /* }}} char *camqp_bytes_cstring */
181 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
185 r = amqp_get_rpc_reply (conf->connection);
186 if (r.reply_type == AMQP_RESPONSE_NORMAL)
190 } /* }}} _Bool camqp_is_error */
192 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
193 char *buffer, size_t buffer_size)
197 r = amqp_get_rpc_reply (conf->connection);
198 switch (r.reply_type)
200 case AMQP_RESPONSE_NORMAL:
201 sstrncpy (buffer, "Success", buffer_size);
204 case AMQP_RESPONSE_NONE:
205 sstrncpy (buffer, "Missing RPC reply type", buffer_size);
208 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
209 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
211 return (sstrerror (r.library_errno, buffer, buffer_size));
214 return (sstrerror (r.library_error, buffer, buffer_size));
217 sstrncpy (buffer, "End of stream", buffer_size);
220 case AMQP_RESPONSE_SERVER_EXCEPTION:
221 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
223 amqp_connection_close_t *m = r.reply.decoded;
224 char *tmp = camqp_bytes_cstring (&m->reply_text);
225 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
229 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
231 amqp_channel_close_t *m = r.reply.decoded;
232 char *tmp = camqp_bytes_cstring (&m->reply_text);
233 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
239 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
245 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
250 } /* }}} char *camqp_strerror */
252 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
253 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
255 amqp_exchange_declare_ok_t *ed_ret;
257 if (conf->exchange_type == NULL)
260 ed_ret = amqp_exchange_declare (conf->connection,
261 /* channel = */ CAMQP_CHANNEL,
262 /* exchange = */ amqp_cstring_bytes (conf->exchange),
263 /* type = */ amqp_cstring_bytes (conf->exchange_type),
266 /* auto_delete = */ 1,
267 /* arguments = */ AMQP_EMPTY_TABLE);
268 if ((ed_ret == NULL) && camqp_is_error (conf))
271 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
272 camqp_strerror (conf, errbuf, sizeof (errbuf)));
273 camqp_close_connection (conf);
277 INFO ("amqp plugin: Successfully created exchange \"%s\" "
279 conf->exchange, conf->exchange_type);
282 } /* }}} int camqp_create_exchange */
284 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
286 amqp_exchange_declare_ok_t *ed_ret;
287 amqp_table_t argument_table;
288 struct amqp_table_entry_t_ argument_table_entries[1];
290 if (conf->exchange_type == NULL)
293 /* Valid arguments: "auto_delete", "internal" */
294 argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
295 argument_table.entries = argument_table_entries;
296 argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
297 argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
298 argument_table_entries[0].value.value.boolean = 1;
300 ed_ret = amqp_exchange_declare (conf->connection,
301 /* channel = */ CAMQP_CHANNEL,
302 /* exchange = */ amqp_cstring_bytes (conf->exchange),
303 /* type = */ amqp_cstring_bytes (conf->exchange_type),
306 #if defined(AMQP_VERSION) && AMQP_VERSION >= 0x00060000
307 /* auto delete = */ 0,
310 /* arguments = */ argument_table);
311 if ((ed_ret == NULL) && camqp_is_error (conf))
314 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
315 camqp_strerror (conf, errbuf, sizeof (errbuf)));
316 camqp_close_connection (conf);
320 INFO ("amqp plugin: Successfully created exchange \"%s\" "
322 conf->exchange, conf->exchange_type);
325 } /* }}} int camqp_create_exchange */
328 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
330 amqp_queue_declare_ok_t *qd_ret;
331 amqp_basic_consume_ok_t *cm_ret;
333 qd_ret = amqp_queue_declare (conf->connection,
334 /* channel = */ CAMQP_CHANNEL,
335 /* queue = */ (conf->queue != NULL)
336 ? amqp_cstring_bytes (conf->queue)
339 /* durable = */ conf->queue_durable,
341 /* auto_delete = */ conf->queue_auto_delete,
342 /* arguments = */ AMQP_EMPTY_TABLE);
345 ERROR ("amqp plugin: amqp_queue_declare failed.");
346 camqp_close_connection (conf);
350 if (conf->queue == NULL)
352 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
353 if (conf->queue == NULL)
355 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
356 camqp_close_connection (conf);
360 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
362 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
364 /* bind to an exchange */
365 if (conf->exchange != NULL)
367 amqp_queue_bind_ok_t *qb_ret;
369 assert (conf->queue != NULL);
370 qb_ret = amqp_queue_bind (conf->connection,
371 /* channel = */ CAMQP_CHANNEL,
372 /* queue = */ amqp_cstring_bytes (conf->queue),
373 /* exchange = */ amqp_cstring_bytes (conf->exchange),
374 /* routing_key = */ (conf->routing_key != NULL)
375 ? amqp_cstring_bytes (conf->routing_key)
377 /* arguments = */ AMQP_EMPTY_TABLE);
378 if ((qb_ret == NULL) && camqp_is_error (conf))
381 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
382 camqp_strerror (conf, errbuf, sizeof (errbuf)));
383 camqp_close_connection (conf);
387 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
388 conf->queue, conf->exchange);
389 } /* if (conf->exchange != NULL) */
391 cm_ret = amqp_basic_consume (conf->connection,
392 /* channel = */ CAMQP_CHANNEL,
393 /* queue = */ amqp_cstring_bytes (conf->queue),
394 /* consumer_tag = */ AMQP_EMPTY_BYTES,
398 /* arguments = */ AMQP_EMPTY_TABLE
400 if ((cm_ret == NULL) && camqp_is_error (conf))
403 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
404 camqp_strerror (conf, errbuf, sizeof (errbuf)));
405 camqp_close_connection (conf);
410 } /* }}} int camqp_setup_queue */
412 static int camqp_connect (camqp_config_t *conf) /* {{{ */
414 static time_t last_connect_time = 0;
416 amqp_rpc_reply_t reply;
418 #ifdef HAVE_AMQP_TCP_SOCKET
419 amqp_socket_t *socket;
424 if (conf->connection != NULL)
427 time_t now = time(NULL);
428 if (now < (last_connect_time + conf->connection_retry_delay))
430 DEBUG("amqp plugin: skipping connection retry, "
431 "ConnectionRetryDelay: %d", conf->connection_retry_delay);
436 DEBUG ("amqp plugin: retrying connection");
437 last_connect_time = now;
440 conf->connection = amqp_new_connection ();
441 if (conf->connection == NULL)
443 ERROR ("amqp plugin: amqp_new_connection failed.");
447 #ifdef HAVE_AMQP_TCP_SOCKET
448 # define CLOSE_SOCKET() /* amqp_destroy_connection() closes the socket for us */
449 /* TODO: add support for SSL using amqp_ssl_socket_new
450 * and related functions */
451 socket = amqp_tcp_socket_new (conf->connection);
454 ERROR ("amqp plugin: amqp_tcp_socket_new failed.");
455 amqp_destroy_connection (conf->connection);
456 conf->connection = NULL;
460 status = amqp_socket_open (socket, CONF(conf, host), conf->port);
465 ERROR ("amqp plugin: amqp_socket_open failed: %s",
466 sstrerror (status, errbuf, sizeof (errbuf)));
467 amqp_destroy_connection (conf->connection);
468 conf->connection = NULL;
471 #else /* HAVE_AMQP_TCP_SOCKET */
472 # define CLOSE_SOCKET() close(sockfd)
473 /* this interface is deprecated as of rabbitmq-c 0.4 */
474 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
478 status = (-1) * sockfd;
479 ERROR ("amqp plugin: amqp_open_socket failed: %s",
480 sstrerror (status, errbuf, sizeof (errbuf)));
481 amqp_destroy_connection (conf->connection);
482 conf->connection = NULL;
485 amqp_set_sockfd (conf->connection, sockfd);
488 reply = amqp_login (conf->connection, CONF(conf, vhost),
489 /* channel max = */ 0,
490 /* frame max = */ 131072,
492 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
493 CONF(conf, user), CONF(conf, password));
494 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
496 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
497 CONF(conf, vhost), CONF(conf, user));
498 amqp_destroy_connection (conf->connection);
500 conf->connection = NULL;
504 amqp_channel_open (conf->connection, /* channel = */ 1);
505 /* FIXME: Is checking "reply.reply_type" really correct here? How does
506 * it get set? --octo */
507 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
509 ERROR ("amqp plugin: amqp_channel_open failed.");
510 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
511 amqp_destroy_connection (conf->connection);
513 conf->connection = NULL;
517 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
518 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
520 status = camqp_create_exchange (conf);
525 return (camqp_setup_queue (conf));
527 } /* }}} int camqp_connect */
529 static int camqp_shutdown (void) /* {{{ */
531 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
532 subscriber_threads_num);
534 subscriber_threads_running = 0;
535 for (size_t i = 0; i < subscriber_threads_num; i++)
537 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
538 * to use a timeout in the thread and check for the variable in regular
540 pthread_kill (subscriber_threads[i], SIGTERM);
541 pthread_join (subscriber_threads[i], /* retval = */ NULL);
544 subscriber_threads_num = 0;
545 sfree (subscriber_threads);
547 DEBUG ("amqp plugin: All subscriber threads exited.");
550 } /* }}} int camqp_shutdown */
555 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
556 size_t body_size, const char *content_type)
558 char body[body_size + 1];
564 memset (body, 0, sizeof (body));
568 while (received < body_size)
570 status = amqp_simple_wait_frame (conf->connection, &frame);
574 status = (-1) * status;
575 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
576 sstrerror (status, errbuf, sizeof (errbuf)));
577 camqp_close_connection (conf);
581 if (frame.frame_type != AMQP_FRAME_BODY)
583 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
588 if ((body_size - received) < frame.payload.body_fragment.len)
590 WARNING ("amqp plugin: Body is larger than indicated by header.");
594 memcpy (body_ptr, frame.payload.body_fragment.bytes,
595 frame.payload.body_fragment.len);
596 body_ptr += frame.payload.body_fragment.len;
597 received += frame.payload.body_fragment.len;
598 } /* while (received < body_size) */
600 if (strcasecmp ("text/collectd", content_type) == 0)
602 status = cmd_handle_putval (stderr, body);
604 ERROR ("amqp plugin: cmd_handle_putval failed with status %i.",
608 else if (strcasecmp ("application/json", content_type) == 0)
610 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
611 "been implemented yet. FIXME!");
616 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
623 } /* }}} int camqp_read_body */
625 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
629 amqp_basic_properties_t *properties;
632 status = amqp_simple_wait_frame (conf->connection, &frame);
636 status = (-1) * status;
637 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
638 sstrerror (status, errbuf, sizeof (errbuf)));
639 camqp_close_connection (conf);
643 if (frame.frame_type != AMQP_FRAME_HEADER)
645 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
650 properties = frame.payload.properties.decoded;
651 content_type = camqp_bytes_cstring (&properties->content_type);
652 if (content_type == NULL)
654 ERROR ("amqp plugin: Unable to determine content type.");
658 status = camqp_read_body (conf,
659 (size_t) frame.payload.properties.body_size,
662 sfree (content_type);
664 } /* }}} int camqp_read_header */
666 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
668 camqp_config_t *conf = user_data;
671 cdtime_t interval = plugin_get_interval ();
673 while (subscriber_threads_running)
677 status = camqp_connect (conf);
680 ERROR ("amqp plugin: camqp_connect failed. "
681 "Will sleep for %.3f seconds.",
682 CDTIME_T_TO_DOUBLE (interval));
683 nanosleep (&CDTIME_T_TO_TIMESPEC (interval), /* remaining = */ NULL);
687 status = amqp_simple_wait_frame (conf->connection, &frame);
690 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
691 "Will sleep for %.3f seconds.",
692 CDTIME_T_TO_DOUBLE (interval));
693 camqp_close_connection (conf);
694 nanosleep (&CDTIME_T_TO_TIMESPEC (interval), /* remaining = */ NULL);
698 if (frame.frame_type != AMQP_FRAME_METHOD)
700 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
705 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
707 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
708 frame.payload.method.id);
712 camqp_read_header (conf);
714 amqp_maybe_release_buffers (conf->connection);
715 } /* while (subscriber_threads_running) */
717 camqp_config_free (conf);
720 } /* }}} void *camqp_subscribe_thread */
722 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
727 tmp = realloc (subscriber_threads,
728 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
731 ERROR ("amqp plugin: realloc failed.");
732 sfree (subscriber_threads);
735 subscriber_threads = tmp;
736 tmp = subscriber_threads + subscriber_threads_num;
737 memset (tmp, 0, sizeof (*tmp));
739 status = plugin_thread_create (tmp, /* attr = */ NULL,
740 camqp_subscribe_thread, conf, "amqp subscribe");
744 ERROR ("amqp plugin: pthread_create failed: %s",
745 sstrerror (status, errbuf, sizeof (errbuf)));
749 subscriber_threads_num++;
752 } /* }}} int camqp_subscribe_init */
757 /* XXX: You must hold "conf->lock" when calling this function! */
758 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
759 const char *buffer, const char *routing_key)
763 status = camqp_connect (conf);
767 amqp_basic_properties_t props = {
768 ._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
769 | AMQP_BASIC_DELIVERY_MODE_FLAG
770 | AMQP_BASIC_APP_ID_FLAG,
771 .delivery_mode = conf->delivery_mode,
772 .app_id = amqp_cstring_bytes("collectd")
775 if (conf->format == CAMQP_FORMAT_COMMAND)
776 props.content_type = amqp_cstring_bytes("text/collectd");
777 else if (conf->format == CAMQP_FORMAT_JSON)
778 props.content_type = amqp_cstring_bytes("application/json");
779 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
780 props.content_type = amqp_cstring_bytes("text/graphite");
784 status = amqp_basic_publish(conf->connection,
786 amqp_cstring_bytes(CONF(conf, exchange)),
787 amqp_cstring_bytes (routing_key),
791 amqp_cstring_bytes(buffer));
794 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
796 camqp_close_connection (conf);
800 } /* }}} int camqp_write_locked */
802 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
803 user_data_t *user_data)
805 camqp_config_t *conf = user_data->data;
806 char routing_key[6 * DATA_MAX_NAME_LEN];
810 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
813 if (conf->routing_key != NULL)
815 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
819 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
821 vl->plugin, vl->plugin_instance,
822 vl->type, vl->type_instance);
824 /* Switch slashes (the only character forbidden by collectd) and dots
825 * (the separation character used by AMQP). */
826 for (size_t i = 0; routing_key[i] != 0; i++)
828 if (routing_key[i] == '.')
829 routing_key[i] = '/';
830 else if (routing_key[i] == '/')
831 routing_key[i] = '.';
835 if (conf->format == CAMQP_FORMAT_COMMAND)
837 status = cmd_create_putval (buffer, sizeof (buffer), ds, vl);
840 ERROR ("amqp plugin: cmd_create_putval failed with status %i.",
845 else if (conf->format == CAMQP_FORMAT_JSON)
847 size_t bfree = sizeof (buffer);
850 format_json_initialize (buffer, &bfill, &bfree);
851 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
852 format_json_finalize (buffer, &bfill, &bfree);
854 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
856 status = format_graphite (buffer, sizeof (buffer), ds, vl,
857 conf->prefix, conf->postfix, conf->escape_char,
858 conf->graphite_flags);
861 ERROR ("amqp plugin: format_graphite failed with status %i.",
868 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
872 pthread_mutex_lock (&conf->lock);
873 status = camqp_write_locked (conf, buffer, routing_key);
874 pthread_mutex_unlock (&conf->lock);
877 } /* }}} int camqp_write */
882 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
883 camqp_config_t *conf)
889 status = cf_util_get_string (ci, &string);
893 assert (string != NULL);
894 if (strcasecmp ("Command", string) == 0)
895 conf->format = CAMQP_FORMAT_COMMAND;
896 else if (strcasecmp ("JSON", string) == 0)
897 conf->format = CAMQP_FORMAT_JSON;
898 else if (strcasecmp ("Graphite", string) == 0)
899 conf->format = CAMQP_FORMAT_GRAPHITE;
902 WARNING ("amqp plugin: Invalid format string: %s",
909 } /* }}} int config_set_string */
911 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
914 camqp_config_t *conf;
917 conf = calloc (1, sizeof (*conf));
920 ERROR ("amqp plugin: calloc failed.");
924 /* Initialize "conf" {{{ */
925 conf->publish = publish;
927 conf->format = CAMQP_FORMAT_COMMAND;
932 conf->password = NULL;
933 conf->exchange = NULL;
934 conf->routing_key = NULL;
935 conf->connection_retry_delay = 0;
938 conf->delivery_mode = CAMQP_DM_VOLATILE;
939 conf->store_rates = 0;
940 conf->graphite_flags = 0;
941 /* publish & graphite only */
943 conf->postfix = NULL;
944 conf->escape_char = '_';
946 conf->exchange_type = NULL;
948 conf->queue_durable = 0;
949 conf->queue_auto_delete = 1;
951 conf->connection = NULL;
952 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
955 status = cf_util_get_string (ci, &conf->name);
962 for (int i = 0; i < ci->children_num; i++)
964 oconfig_item_t *child = ci->children + i;
966 if (strcasecmp ("Host", child->key) == 0)
967 status = cf_util_get_string (child, &conf->host);
968 else if (strcasecmp ("Port", child->key) == 0)
970 status = cf_util_get_port_number (child);
977 else if (strcasecmp ("VHost", child->key) == 0)
978 status = cf_util_get_string (child, &conf->vhost);
979 else if (strcasecmp ("User", child->key) == 0)
980 status = cf_util_get_string (child, &conf->user);
981 else if (strcasecmp ("Password", child->key) == 0)
982 status = cf_util_get_string (child, &conf->password);
983 else if (strcasecmp ("Exchange", child->key) == 0)
984 status = cf_util_get_string (child, &conf->exchange);
985 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
986 status = cf_util_get_string (child, &conf->exchange_type);
987 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
988 status = cf_util_get_string (child, &conf->queue);
989 else if ((strcasecmp ("QueueDurable", child->key) == 0) && !publish)
990 status = cf_util_get_boolean (child, &conf->queue_durable);
991 else if ((strcasecmp ("QueueAutoDelete", child->key) == 0) && !publish)
992 status = cf_util_get_boolean (child, &conf->queue_auto_delete);
993 else if (strcasecmp ("RoutingKey", child->key) == 0)
994 status = cf_util_get_string (child, &conf->routing_key);
995 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
998 status = cf_util_get_boolean (child, &tmp);
1000 conf->delivery_mode = CAMQP_DM_PERSISTENT;
1002 conf->delivery_mode = CAMQP_DM_VOLATILE;
1004 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
1006 status = cf_util_get_boolean (child, &conf->store_rates);
1007 (void) cf_util_get_flag (child, &conf->graphite_flags,
1008 GRAPHITE_STORE_RATES);
1010 else if ((strcasecmp ("Format", child->key) == 0) && publish)
1011 status = camqp_config_set_format (child, conf);
1012 else if ((strcasecmp ("GraphiteSeparateInstances", child->key) == 0) && publish)
1013 status = cf_util_get_flag (child, &conf->graphite_flags,
1014 GRAPHITE_SEPARATE_INSTANCES);
1015 else if ((strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) && publish)
1016 status = cf_util_get_flag (child, &conf->graphite_flags,
1017 GRAPHITE_ALWAYS_APPEND_DS);
1018 else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
1019 status = cf_util_get_string (child, &conf->prefix);
1020 else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
1021 status = cf_util_get_string (child, &conf->postfix);
1022 else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
1024 char *tmp_buff = NULL;
1025 status = cf_util_get_string (child, &tmp_buff);
1026 if (strlen (tmp_buff) > 1)
1027 WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
1028 "only one character. Others will be ignored.");
1029 conf->escape_char = tmp_buff[0];
1032 else if (strcasecmp ("ConnectionRetryDelay", child->key) == 0)
1033 status = cf_util_get_int (child, &conf->connection_retry_delay);
1035 WARNING ("amqp plugin: Ignoring unknown "
1036 "configuration option \"%s\".", child->key);
1040 } /* for (i = 0; i < ci->children_num; i++) */
1042 if ((status == 0) && (conf->exchange == NULL))
1044 if (conf->exchange_type != NULL)
1045 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
1046 "without the \"Exchange\" option. It will be ignored.");
1048 if (!publish && (conf->routing_key != NULL))
1049 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
1050 "without the \"Exchange\" option. It will be ignored.");
1056 camqp_config_free (conf);
1060 if (conf->exchange != NULL)
1062 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
1069 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
1071 status = plugin_register_write (cbname, camqp_write,
1074 .free_func = camqp_config_free,
1078 camqp_config_free (conf);
1084 status = camqp_subscribe_init (conf);
1087 camqp_config_free (conf);
1093 } /* }}} int camqp_config_connection */
1095 static int camqp_config (oconfig_item_t *ci) /* {{{ */
1097 for (int i = 0; i < ci->children_num; i++)
1099 oconfig_item_t *child = ci->children + i;
1101 if (strcasecmp ("Publish", child->key) == 0)
1102 camqp_config_connection (child, /* publish = */ 1);
1103 else if (strcasecmp ("Subscribe", child->key) == 0)
1104 camqp_config_connection (child, /* publish = */ 0);
1106 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
1108 } /* for (ci->children_num) */
1111 } /* }}} int camqp_config */
1113 void module_register (void)
1115 plugin_register_complex_config ("amqp", camqp_config);
1116 plugin_register_shutdown ("amqp", camqp_shutdown);
1117 } /* void module_register */
1119 /* vim: set sw=4 sts=4 et fdm=marker : */