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 #ifdef HAVE_AMQP_TCP_SOCKET_H
42 # include <amqp_tcp_socket.h>
44 #ifdef HAVE_AMQP_SOCKET_H
45 # include <amqp_socket.h>
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 *);
53 /* Defines for the delivery mode. I have no idea why they're not defined by the
55 #define CAMQP_DM_VOLATILE 1
56 #define CAMQP_DM_PERSISTENT 2
58 #define CAMQP_FORMAT_COMMAND 1
59 #define CAMQP_FORMAT_JSON 2
60 #define CAMQP_FORMAT_GRAPHITE 3
62 #define CAMQP_CHANNEL 1
82 uint8_t delivery_mode;
85 /* publish & graphite format only */
89 unsigned int graphite_flags;
95 amqp_connection_state_t connection;
98 typedef struct camqp_config_s camqp_config_t;
103 static const char *def_host = "localhost";
104 static const char *def_vhost = "/";
105 static const char *def_user = "guest";
106 static const char *def_password = "guest";
107 static const char *def_exchange = "amq.fanout";
109 static pthread_t *subscriber_threads = NULL;
110 static size_t subscriber_threads_num = 0;
111 static _Bool subscriber_threads_running = 1;
113 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
118 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
122 if ((conf == NULL) || (conf->connection == NULL))
125 sockfd = amqp_get_sockfd (conf->connection);
126 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
127 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
128 amqp_destroy_connection (conf->connection);
130 conf->connection = NULL;
131 } /* }}} void camqp_close_connection */
133 static void camqp_config_free (void *ptr) /* {{{ */
135 camqp_config_t *conf = ptr;
140 camqp_close_connection (conf);
146 sfree (conf->password);
147 sfree (conf->exchange);
148 sfree (conf->exchange_type);
150 sfree (conf->routing_key);
151 sfree (conf->prefix);
152 sfree (conf->postfix);
156 } /* }}} void camqp_config_free */
158 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
162 if ((in == NULL) || (in->bytes == NULL))
165 ret = malloc (in->len + 1);
169 memcpy (ret, in->bytes, in->len);
173 } /* }}} char *camqp_bytes_cstring */
175 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
179 r = amqp_get_rpc_reply (conf->connection);
180 if (r.reply_type == AMQP_RESPONSE_NORMAL)
184 } /* }}} _Bool camqp_is_error */
186 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
187 char *buffer, size_t buffer_size)
191 r = amqp_get_rpc_reply (conf->connection);
192 switch (r.reply_type)
194 case AMQP_RESPONSE_NORMAL:
195 sstrncpy (buffer, "Success", sizeof (buffer));
198 case AMQP_RESPONSE_NONE:
199 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
202 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
203 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
205 return (sstrerror (r.library_errno, buffer, buffer_size));
208 return (sstrerror (r.library_error, buffer, buffer_size));
211 sstrncpy (buffer, "End of stream", sizeof (buffer));
214 case AMQP_RESPONSE_SERVER_EXCEPTION:
215 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
217 amqp_connection_close_t *m = r.reply.decoded;
218 char *tmp = camqp_bytes_cstring (&m->reply_text);
219 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
223 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
225 amqp_channel_close_t *m = r.reply.decoded;
226 char *tmp = camqp_bytes_cstring (&m->reply_text);
227 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
233 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
239 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
244 } /* }}} char *camqp_strerror */
246 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
247 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
249 amqp_exchange_declare_ok_t *ed_ret;
251 if (conf->exchange_type == NULL)
254 ed_ret = amqp_exchange_declare (conf->connection,
255 /* channel = */ CAMQP_CHANNEL,
256 /* exchange = */ amqp_cstring_bytes (conf->exchange),
257 /* type = */ amqp_cstring_bytes (conf->exchange_type),
260 /* auto_delete = */ 1,
261 /* arguments = */ AMQP_EMPTY_TABLE);
262 if ((ed_ret == NULL) && camqp_is_error (conf))
265 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
266 camqp_strerror (conf, errbuf, sizeof (errbuf)));
267 camqp_close_connection (conf);
271 INFO ("amqp plugin: Successfully created exchange \"%s\" "
273 conf->exchange, conf->exchange_type);
276 } /* }}} int camqp_create_exchange */
278 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
280 amqp_exchange_declare_ok_t *ed_ret;
281 amqp_table_t argument_table;
282 struct amqp_table_entry_t_ argument_table_entries[1];
284 if (conf->exchange_type == NULL)
287 /* Valid arguments: "auto_delete", "internal" */
288 argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
289 argument_table.entries = argument_table_entries;
290 argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
291 argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
292 argument_table_entries[0].value.value.boolean = 1;
294 ed_ret = amqp_exchange_declare (conf->connection,
295 /* channel = */ CAMQP_CHANNEL,
296 /* exchange = */ amqp_cstring_bytes (conf->exchange),
297 /* type = */ amqp_cstring_bytes (conf->exchange_type),
300 /* arguments = */ argument_table);
301 if ((ed_ret == NULL) && camqp_is_error (conf))
304 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
305 camqp_strerror (conf, errbuf, sizeof (errbuf)));
306 camqp_close_connection (conf);
310 INFO ("amqp plugin: Successfully created exchange \"%s\" "
312 conf->exchange, conf->exchange_type);
315 } /* }}} int camqp_create_exchange */
318 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
320 amqp_queue_declare_ok_t *qd_ret;
321 amqp_basic_consume_ok_t *cm_ret;
323 qd_ret = amqp_queue_declare (conf->connection,
324 /* channel = */ CAMQP_CHANNEL,
325 /* queue = */ (conf->queue != NULL)
326 ? amqp_cstring_bytes (conf->queue)
331 /* auto_delete = */ 1,
332 /* arguments = */ AMQP_EMPTY_TABLE);
335 ERROR ("amqp plugin: amqp_queue_declare failed.");
336 camqp_close_connection (conf);
340 if (conf->queue == NULL)
342 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
343 if (conf->queue == NULL)
345 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
346 camqp_close_connection (conf);
350 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
352 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
354 /* bind to an exchange */
355 if (conf->exchange != NULL)
357 amqp_queue_bind_ok_t *qb_ret;
359 assert (conf->queue != NULL);
360 qb_ret = amqp_queue_bind (conf->connection,
361 /* channel = */ CAMQP_CHANNEL,
362 /* queue = */ amqp_cstring_bytes (conf->queue),
363 /* exchange = */ amqp_cstring_bytes (conf->exchange),
364 /* routing_key = */ (conf->routing_key != NULL)
365 ? amqp_cstring_bytes (conf->routing_key)
367 /* arguments = */ AMQP_EMPTY_TABLE);
368 if ((qb_ret == NULL) && camqp_is_error (conf))
371 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
372 camqp_strerror (conf, errbuf, sizeof (errbuf)));
373 camqp_close_connection (conf);
377 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
378 conf->queue, conf->exchange);
379 } /* if (conf->exchange != NULL) */
381 cm_ret = amqp_basic_consume (conf->connection,
382 /* channel = */ CAMQP_CHANNEL,
383 /* queue = */ amqp_cstring_bytes (conf->queue),
384 /* consumer_tag = */ AMQP_EMPTY_BYTES,
388 /* arguments = */ AMQP_EMPTY_TABLE
390 if ((cm_ret == NULL) && camqp_is_error (conf))
393 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
394 camqp_strerror (conf, errbuf, sizeof (errbuf)));
395 camqp_close_connection (conf);
400 } /* }}} int camqp_setup_queue */
402 static int camqp_connect (camqp_config_t *conf) /* {{{ */
404 amqp_rpc_reply_t reply;
406 #ifdef HAVE_AMQP_TCP_SOCKET
407 amqp_socket_t *socket;
412 if (conf->connection != NULL)
415 conf->connection = amqp_new_connection ();
416 if (conf->connection == NULL)
418 ERROR ("amqp plugin: amqp_new_connection failed.");
422 #ifdef HAVE_AMQP_TCP_SOCKET
423 # define CLOSE_SOCKET() amqp_socket_close (socket)
424 /* TODO: add support for SSL using amqp_ssl_socket_new
425 * and related functions */
426 socket = amqp_tcp_socket_new (conf->connection);
429 ERROR ("amqp plugin: amqp_tcp_socket_new failed.");
430 amqp_destroy_connection (conf->connection);
431 conf->connection = NULL;
435 status = amqp_socket_open (socket, CONF(conf, host), conf->port);
440 ERROR ("amqp plugin: amqp_socket_open failed: %s",
441 sstrerror (status, errbuf, sizeof (errbuf)));
443 amqp_destroy_connection (conf->connection);
444 conf->connection = NULL;
447 #else /* HAVE_AMQP_TCP_SOCKET */
448 # define CLOSE_SOCKET close(sockfd)
449 /* this interface is deprecated as of rabbitmq-c 0.4 */
450 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
454 status = (-1) * sockfd;
455 ERROR ("amqp plugin: amqp_open_socket failed: %s",
456 sstrerror (status, errbuf, sizeof (errbuf)));
457 amqp_destroy_connection (conf->connection);
458 conf->connection = NULL;
461 amqp_set_sockfd (conf->connection, sockfd);
464 reply = amqp_login (conf->connection, CONF(conf, vhost),
465 /* channel max = */ 0,
466 /* frame max = */ 131072,
468 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
469 CONF(conf, user), CONF(conf, password));
470 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
472 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
473 CONF(conf, vhost), CONF(conf, user));
474 amqp_destroy_connection (conf->connection);
476 conf->connection = NULL;
480 amqp_channel_open (conf->connection, /* channel = */ 1);
481 /* FIXME: Is checking "reply.reply_type" really correct here? How does
482 * it get set? --octo */
483 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
485 ERROR ("amqp plugin: amqp_channel_open failed.");
486 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
487 amqp_destroy_connection (conf->connection);
489 conf->connection = NULL;
493 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
494 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
496 status = camqp_create_exchange (conf);
501 return (camqp_setup_queue (conf));
503 } /* }}} int camqp_connect */
505 static int camqp_shutdown (void) /* {{{ */
509 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
510 subscriber_threads_num);
512 subscriber_threads_running = 0;
513 for (i = 0; i < subscriber_threads_num; i++)
515 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
516 * to use a timeout in the thread and check for the variable in regular
518 pthread_kill (subscriber_threads[i], SIGTERM);
519 pthread_join (subscriber_threads[i], /* retval = */ NULL);
522 subscriber_threads_num = 0;
523 sfree (subscriber_threads);
525 DEBUG ("amqp plugin: All subscriber threads exited.");
528 } /* }}} int camqp_shutdown */
533 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
534 size_t body_size, const char *content_type)
536 char body[body_size + 1];
542 memset (body, 0, sizeof (body));
546 while (received < body_size)
548 status = amqp_simple_wait_frame (conf->connection, &frame);
552 status = (-1) * status;
553 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
554 sstrerror (status, errbuf, sizeof (errbuf)));
555 camqp_close_connection (conf);
559 if (frame.frame_type != AMQP_FRAME_BODY)
561 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
566 if ((body_size - received) < frame.payload.body_fragment.len)
568 WARNING ("amqp plugin: Body is larger than indicated by header.");
572 memcpy (body_ptr, frame.payload.body_fragment.bytes,
573 frame.payload.body_fragment.len);
574 body_ptr += frame.payload.body_fragment.len;
575 received += frame.payload.body_fragment.len;
576 } /* while (received < body_size) */
578 if (strcasecmp ("text/collectd", content_type) == 0)
580 status = handle_putval (stderr, body);
582 ERROR ("amqp plugin: handle_putval failed with status %i.",
586 else if (strcasecmp ("application/json", content_type) == 0)
588 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
589 "been implemented yet. FIXME!");
594 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
601 } /* }}} int camqp_read_body */
603 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
607 amqp_basic_properties_t *properties;
610 status = amqp_simple_wait_frame (conf->connection, &frame);
614 status = (-1) * status;
615 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
616 sstrerror (status, errbuf, sizeof (errbuf)));
617 camqp_close_connection (conf);
621 if (frame.frame_type != AMQP_FRAME_HEADER)
623 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
628 properties = frame.payload.properties.decoded;
629 content_type = camqp_bytes_cstring (&properties->content_type);
630 if (content_type == NULL)
632 ERROR ("amqp plugin: Unable to determine content type.");
636 status = camqp_read_body (conf,
637 (size_t) frame.payload.properties.body_size,
640 sfree (content_type);
642 } /* }}} int camqp_read_header */
644 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
646 camqp_config_t *conf = user_data;
649 cdtime_t interval = plugin_get_interval ();
651 while (subscriber_threads_running)
655 status = camqp_connect (conf);
658 struct timespec ts_interval;
659 ERROR ("amqp plugin: camqp_connect failed. "
660 "Will sleep for %.3f seconds.",
661 CDTIME_T_TO_DOUBLE (interval));
662 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
663 nanosleep (&ts_interval, /* remaining = */ NULL);
667 status = amqp_simple_wait_frame (conf->connection, &frame);
670 struct timespec ts_interval;
671 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
672 "Will sleep for %.3f seconds.",
673 CDTIME_T_TO_DOUBLE (interval));
674 camqp_close_connection (conf);
675 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
676 nanosleep (&ts_interval, /* remaining = */ NULL);
680 if (frame.frame_type != AMQP_FRAME_METHOD)
682 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
687 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
689 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
690 frame.payload.method.id);
694 status = camqp_read_header (conf);
696 amqp_maybe_release_buffers (conf->connection);
697 } /* while (subscriber_threads_running) */
699 camqp_config_free (conf);
702 } /* }}} void *camqp_subscribe_thread */
704 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
709 tmp = realloc (subscriber_threads,
710 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
713 ERROR ("amqp plugin: realloc failed.");
714 camqp_config_free (conf);
717 subscriber_threads = tmp;
718 tmp = subscriber_threads + subscriber_threads_num;
719 memset (tmp, 0, sizeof (*tmp));
721 status = plugin_thread_create (tmp, /* attr = */ NULL,
722 camqp_subscribe_thread, conf);
726 ERROR ("amqp plugin: pthread_create failed: %s",
727 sstrerror (status, errbuf, sizeof (errbuf)));
728 camqp_config_free (conf);
732 subscriber_threads_num++;
735 } /* }}} int camqp_subscribe_init */
740 /* XXX: You must hold "conf->lock" when calling this function! */
741 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
742 const char *buffer, const char *routing_key)
744 amqp_basic_properties_t props;
747 status = camqp_connect (conf);
751 memset (&props, 0, sizeof (props));
752 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
753 | AMQP_BASIC_DELIVERY_MODE_FLAG
754 | AMQP_BASIC_APP_ID_FLAG;
755 if (conf->format == CAMQP_FORMAT_COMMAND)
756 props.content_type = amqp_cstring_bytes("text/collectd");
757 else if (conf->format == CAMQP_FORMAT_JSON)
758 props.content_type = amqp_cstring_bytes("application/json");
759 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
760 props.content_type = amqp_cstring_bytes("text/graphite");
763 props.delivery_mode = conf->delivery_mode;
764 props.app_id = amqp_cstring_bytes("collectd");
766 status = amqp_basic_publish(conf->connection,
768 amqp_cstring_bytes(CONF(conf, exchange)),
769 amqp_cstring_bytes (routing_key),
773 amqp_cstring_bytes(buffer));
776 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
778 camqp_close_connection (conf);
782 } /* }}} int camqp_write_locked */
784 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
785 user_data_t *user_data)
787 camqp_config_t *conf = user_data->data;
788 char routing_key[6 * DATA_MAX_NAME_LEN];
792 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
795 memset (buffer, 0, sizeof (buffer));
797 if (conf->routing_key != NULL)
799 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
804 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
806 vl->plugin, vl->plugin_instance,
807 vl->type, vl->type_instance);
809 /* Switch slashes (the only character forbidden by collectd) and dots
810 * (the separation character used by AMQP). */
811 for (i = 0; routing_key[i] != 0; i++)
813 if (routing_key[i] == '.')
814 routing_key[i] = '/';
815 else if (routing_key[i] == '/')
816 routing_key[i] = '.';
820 if (conf->format == CAMQP_FORMAT_COMMAND)
822 status = create_putval (buffer, sizeof (buffer), ds, vl);
825 ERROR ("amqp plugin: create_putval failed with status %i.",
830 else if (conf->format == CAMQP_FORMAT_JSON)
832 size_t bfree = sizeof (buffer);
835 format_json_initialize (buffer, &bfill, &bfree);
836 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
837 format_json_finalize (buffer, &bfill, &bfree);
839 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
841 status = format_graphite (buffer, sizeof (buffer), ds, vl,
842 conf->prefix, conf->postfix, conf->escape_char,
843 conf->graphite_flags);
846 ERROR ("amqp plugin: format_graphite failed with status %i.",
853 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
857 pthread_mutex_lock (&conf->lock);
858 status = camqp_write_locked (conf, buffer, routing_key);
859 pthread_mutex_unlock (&conf->lock);
862 } /* }}} int camqp_write */
867 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
868 camqp_config_t *conf)
874 status = cf_util_get_string (ci, &string);
878 assert (string != NULL);
879 if (strcasecmp ("Command", string) == 0)
880 conf->format = CAMQP_FORMAT_COMMAND;
881 else if (strcasecmp ("JSON", string) == 0)
882 conf->format = CAMQP_FORMAT_JSON;
883 else if (strcasecmp ("Graphite", string) == 0)
884 conf->format = CAMQP_FORMAT_GRAPHITE;
887 WARNING ("amqp plugin: Invalid format string: %s",
894 } /* }}} int config_set_string */
896 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
899 camqp_config_t *conf;
903 conf = malloc (sizeof (*conf));
906 ERROR ("amqp plugin: malloc failed.");
910 /* Initialize "conf" {{{ */
911 memset (conf, 0, sizeof (*conf));
912 conf->publish = publish;
914 conf->format = CAMQP_FORMAT_COMMAND;
919 conf->password = NULL;
920 conf->exchange = NULL;
921 conf->routing_key = NULL;
923 conf->delivery_mode = CAMQP_DM_VOLATILE;
924 conf->store_rates = 0;
925 /* publish & graphite only */
927 conf->postfix = NULL;
928 conf->escape_char = '_';
930 conf->exchange_type = NULL;
933 conf->connection = NULL;
934 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
937 status = cf_util_get_string (ci, &conf->name);
944 for (i = 0; i < ci->children_num; i++)
946 oconfig_item_t *child = ci->children + i;
948 if (strcasecmp ("Host", child->key) == 0)
949 status = cf_util_get_string (child, &conf->host);
950 else if (strcasecmp ("Port", child->key) == 0)
952 status = cf_util_get_port_number (child);
959 else if (strcasecmp ("VHost", child->key) == 0)
960 status = cf_util_get_string (child, &conf->vhost);
961 else if (strcasecmp ("User", child->key) == 0)
962 status = cf_util_get_string (child, &conf->user);
963 else if (strcasecmp ("Password", child->key) == 0)
964 status = cf_util_get_string (child, &conf->password);
965 else if (strcasecmp ("Exchange", child->key) == 0)
966 status = cf_util_get_string (child, &conf->exchange);
967 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
968 status = cf_util_get_string (child, &conf->exchange_type);
969 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
970 status = cf_util_get_string (child, &conf->queue);
971 else if (strcasecmp ("RoutingKey", child->key) == 0)
972 status = cf_util_get_string (child, &conf->routing_key);
973 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
976 status = cf_util_get_boolean (child, &tmp);
978 conf->delivery_mode = CAMQP_DM_PERSISTENT;
980 conf->delivery_mode = CAMQP_DM_VOLATILE;
982 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
984 status = cf_util_get_boolean (child, &conf->store_rates);
985 (void) cf_util_get_flag (child, &conf->graphite_flags,
986 GRAPHITE_STORE_RATES);
988 else if ((strcasecmp ("Format", child->key) == 0) && publish)
989 status = camqp_config_set_format (child, conf);
990 else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
991 status = cf_util_get_string (child, &conf->prefix);
992 else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
993 status = cf_util_get_string (child, &conf->postfix);
994 else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
996 char *tmp_buff = NULL;
997 status = cf_util_get_string (child, &tmp_buff);
998 if (strlen (tmp_buff) > 1)
999 WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
1000 "only one character. Others will be ignored.");
1001 conf->escape_char = tmp_buff[0];
1005 WARNING ("amqp plugin: Ignoring unknown "
1006 "configuration option \"%s\".", child->key);
1010 } /* for (i = 0; i < ci->children_num; i++) */
1012 if ((status == 0) && (conf->exchange == NULL))
1014 if (conf->exchange_type != NULL)
1015 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
1016 "without the \"Exchange\" option. It will be ignored.");
1018 if (!publish && (conf->routing_key != NULL))
1019 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
1020 "without the \"Exchange\" option. It will be ignored.");
1026 camqp_config_free (conf);
1030 if (conf->exchange != NULL)
1032 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
1039 user_data_t ud = { conf, camqp_config_free };
1041 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
1043 status = plugin_register_write (cbname, camqp_write, &ud);
1046 camqp_config_free (conf);
1052 status = camqp_subscribe_init (conf);
1055 camqp_config_free (conf);
1061 } /* }}} int camqp_config_connection */
1063 static int camqp_config (oconfig_item_t *ci) /* {{{ */
1067 for (i = 0; i < ci->children_num; i++)
1069 oconfig_item_t *child = ci->children + i;
1071 if (strcasecmp ("Publish", child->key) == 0)
1072 camqp_config_connection (child, /* publish = */ 1);
1073 else if (strcasecmp ("Subscribe", child->key) == 0)
1074 camqp_config_connection (child, /* publish = */ 0);
1076 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
1078 } /* for (ci->children_num) */
1081 } /* }}} int camqp_config */
1083 void module_register (void)
1085 plugin_register_complex_config ("amqp", camqp_config);
1086 plugin_register_shutdown ("amqp", camqp_shutdown);
1087 } /* void module_register */
1089 /* vim: set sw=4 sts=4 et fdm=marker : */