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 #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 amqp_connection_state_t connection;
100 typedef struct camqp_config_s camqp_config_t;
105 static const char *def_host = "localhost";
106 static const char *def_vhost = "/";
107 static const char *def_user = "guest";
108 static const char *def_password = "guest";
109 static const char *def_exchange = "amq.fanout";
111 static pthread_t *subscriber_threads = NULL;
112 static size_t subscriber_threads_num = 0;
113 static _Bool subscriber_threads_running = 1;
115 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
120 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
124 if ((conf == NULL) || (conf->connection == NULL))
127 sockfd = amqp_get_sockfd (conf->connection);
128 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
129 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
130 amqp_destroy_connection (conf->connection);
132 conf->connection = NULL;
133 } /* }}} void camqp_close_connection */
135 static void camqp_config_free (void *ptr) /* {{{ */
137 camqp_config_t *conf = ptr;
142 camqp_close_connection (conf);
148 sfree (conf->password);
149 sfree (conf->exchange);
150 sfree (conf->exchange_type);
152 sfree (conf->routing_key);
153 sfree (conf->prefix);
154 sfree (conf->postfix);
158 } /* }}} void camqp_config_free */
160 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
164 if ((in == NULL) || (in->bytes == NULL))
167 ret = malloc (in->len + 1);
171 memcpy (ret, in->bytes, in->len);
175 } /* }}} char *camqp_bytes_cstring */
177 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
181 r = amqp_get_rpc_reply (conf->connection);
182 if (r.reply_type == AMQP_RESPONSE_NORMAL)
186 } /* }}} _Bool camqp_is_error */
188 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
189 char *buffer, size_t buffer_size)
193 r = amqp_get_rpc_reply (conf->connection);
194 switch (r.reply_type)
196 case AMQP_RESPONSE_NORMAL:
197 sstrncpy (buffer, "Success", sizeof (buffer));
200 case AMQP_RESPONSE_NONE:
201 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
204 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
205 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
207 return (sstrerror (r.library_errno, buffer, buffer_size));
210 return (sstrerror (r.library_error, buffer, buffer_size));
213 sstrncpy (buffer, "End of stream", sizeof (buffer));
216 case AMQP_RESPONSE_SERVER_EXCEPTION:
217 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
219 amqp_connection_close_t *m = r.reply.decoded;
220 char *tmp = camqp_bytes_cstring (&m->reply_text);
221 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
225 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
227 amqp_channel_close_t *m = r.reply.decoded;
228 char *tmp = camqp_bytes_cstring (&m->reply_text);
229 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
235 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
241 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
246 } /* }}} char *camqp_strerror */
248 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
249 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
251 amqp_exchange_declare_ok_t *ed_ret;
253 if (conf->exchange_type == NULL)
256 ed_ret = amqp_exchange_declare (conf->connection,
257 /* channel = */ CAMQP_CHANNEL,
258 /* exchange = */ amqp_cstring_bytes (conf->exchange),
259 /* type = */ amqp_cstring_bytes (conf->exchange_type),
262 /* auto_delete = */ 1,
263 /* arguments = */ AMQP_EMPTY_TABLE);
264 if ((ed_ret == NULL) && camqp_is_error (conf))
267 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
268 camqp_strerror (conf, errbuf, sizeof (errbuf)));
269 camqp_close_connection (conf);
273 INFO ("amqp plugin: Successfully created exchange \"%s\" "
275 conf->exchange, conf->exchange_type);
278 } /* }}} int camqp_create_exchange */
280 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
282 amqp_exchange_declare_ok_t *ed_ret;
283 amqp_table_t argument_table;
284 struct amqp_table_entry_t_ argument_table_entries[1];
286 if (conf->exchange_type == NULL)
289 /* Valid arguments: "auto_delete", "internal" */
290 argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
291 argument_table.entries = argument_table_entries;
292 argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
293 argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
294 argument_table_entries[0].value.value.boolean = 1;
296 ed_ret = amqp_exchange_declare (conf->connection,
297 /* channel = */ CAMQP_CHANNEL,
298 /* exchange = */ amqp_cstring_bytes (conf->exchange),
299 /* type = */ amqp_cstring_bytes (conf->exchange_type),
302 /* arguments = */ argument_table);
303 if ((ed_ret == NULL) && camqp_is_error (conf))
306 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
307 camqp_strerror (conf, errbuf, sizeof (errbuf)));
308 camqp_close_connection (conf);
312 INFO ("amqp plugin: Successfully created exchange \"%s\" "
314 conf->exchange, conf->exchange_type);
317 } /* }}} int camqp_create_exchange */
320 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
322 amqp_queue_declare_ok_t *qd_ret;
323 amqp_basic_consume_ok_t *cm_ret;
325 qd_ret = amqp_queue_declare (conf->connection,
326 /* channel = */ CAMQP_CHANNEL,
327 /* queue = */ (conf->queue != NULL)
328 ? amqp_cstring_bytes (conf->queue)
333 /* auto_delete = */ 1,
334 /* arguments = */ AMQP_EMPTY_TABLE);
337 ERROR ("amqp plugin: amqp_queue_declare failed.");
338 camqp_close_connection (conf);
342 if (conf->queue == NULL)
344 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
345 if (conf->queue == NULL)
347 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
348 camqp_close_connection (conf);
352 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
354 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
356 /* bind to an exchange */
357 if (conf->exchange != NULL)
359 amqp_queue_bind_ok_t *qb_ret;
361 assert (conf->queue != NULL);
362 qb_ret = amqp_queue_bind (conf->connection,
363 /* channel = */ CAMQP_CHANNEL,
364 /* queue = */ amqp_cstring_bytes (conf->queue),
365 /* exchange = */ amqp_cstring_bytes (conf->exchange),
366 /* routing_key = */ (conf->routing_key != NULL)
367 ? amqp_cstring_bytes (conf->routing_key)
369 /* arguments = */ AMQP_EMPTY_TABLE);
370 if ((qb_ret == NULL) && camqp_is_error (conf))
373 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
374 camqp_strerror (conf, errbuf, sizeof (errbuf)));
375 camqp_close_connection (conf);
379 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
380 conf->queue, conf->exchange);
381 } /* if (conf->exchange != NULL) */
383 cm_ret = amqp_basic_consume (conf->connection,
384 /* channel = */ CAMQP_CHANNEL,
385 /* queue = */ amqp_cstring_bytes (conf->queue),
386 /* consumer_tag = */ AMQP_EMPTY_BYTES,
390 /* arguments = */ AMQP_EMPTY_TABLE
392 if ((cm_ret == NULL) && camqp_is_error (conf))
395 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
396 camqp_strerror (conf, errbuf, sizeof (errbuf)));
397 camqp_close_connection (conf);
402 } /* }}} int camqp_setup_queue */
404 static int camqp_connect (camqp_config_t *conf) /* {{{ */
406 amqp_rpc_reply_t reply;
408 #ifdef HAVE_AMQP_TCP_SOCKET
409 amqp_socket_t *socket;
414 if (conf->connection != NULL)
417 conf->connection = amqp_new_connection ();
418 if (conf->connection == NULL)
420 ERROR ("amqp plugin: amqp_new_connection failed.");
424 #ifdef HAVE_AMQP_TCP_SOCKET
425 # define CLOSE_SOCKET() amqp_socket_close (socket)
426 /* TODO: add support for SSL using amqp_ssl_socket_new
427 * and related functions */
428 socket = amqp_tcp_socket_new (conf->connection);
431 ERROR ("amqp plugin: amqp_tcp_socket_new failed.");
432 amqp_destroy_connection (conf->connection);
433 conf->connection = NULL;
437 status = amqp_socket_open (socket, CONF(conf, host), conf->port);
442 ERROR ("amqp plugin: amqp_socket_open failed: %s",
443 sstrerror (status, errbuf, sizeof (errbuf)));
445 amqp_destroy_connection (conf->connection);
446 conf->connection = NULL;
449 #else /* HAVE_AMQP_TCP_SOCKET */
450 # define CLOSE_SOCKET() close(sockfd)
451 /* this interface is deprecated as of rabbitmq-c 0.4 */
452 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
456 status = (-1) * sockfd;
457 ERROR ("amqp plugin: amqp_open_socket failed: %s",
458 sstrerror (status, errbuf, sizeof (errbuf)));
459 amqp_destroy_connection (conf->connection);
460 conf->connection = NULL;
463 amqp_set_sockfd (conf->connection, sockfd);
466 reply = amqp_login (conf->connection, CONF(conf, vhost),
467 /* channel max = */ 0,
468 /* frame max = */ 131072,
470 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
471 CONF(conf, user), CONF(conf, password));
472 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
474 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
475 CONF(conf, vhost), CONF(conf, user));
476 amqp_destroy_connection (conf->connection);
478 conf->connection = NULL;
482 amqp_channel_open (conf->connection, /* channel = */ 1);
483 /* FIXME: Is checking "reply.reply_type" really correct here? How does
484 * it get set? --octo */
485 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
487 ERROR ("amqp plugin: amqp_channel_open failed.");
488 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
489 amqp_destroy_connection (conf->connection);
491 conf->connection = NULL;
495 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
496 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
498 status = camqp_create_exchange (conf);
503 return (camqp_setup_queue (conf));
505 } /* }}} int camqp_connect */
507 static int camqp_shutdown (void) /* {{{ */
511 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
512 subscriber_threads_num);
514 subscriber_threads_running = 0;
515 for (i = 0; i < subscriber_threads_num; i++)
517 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
518 * to use a timeout in the thread and check for the variable in regular
520 pthread_kill (subscriber_threads[i], SIGTERM);
521 pthread_join (subscriber_threads[i], /* retval = */ NULL);
524 subscriber_threads_num = 0;
525 sfree (subscriber_threads);
527 DEBUG ("amqp plugin: All subscriber threads exited.");
530 } /* }}} int camqp_shutdown */
535 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
536 size_t body_size, const char *content_type)
538 char body[body_size + 1];
544 memset (body, 0, sizeof (body));
548 while (received < body_size)
550 status = amqp_simple_wait_frame (conf->connection, &frame);
554 status = (-1) * status;
555 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
556 sstrerror (status, errbuf, sizeof (errbuf)));
557 camqp_close_connection (conf);
561 if (frame.frame_type != AMQP_FRAME_BODY)
563 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
568 if ((body_size - received) < frame.payload.body_fragment.len)
570 WARNING ("amqp plugin: Body is larger than indicated by header.");
574 memcpy (body_ptr, frame.payload.body_fragment.bytes,
575 frame.payload.body_fragment.len);
576 body_ptr += frame.payload.body_fragment.len;
577 received += frame.payload.body_fragment.len;
578 } /* while (received < body_size) */
580 if (strcasecmp ("text/collectd", content_type) == 0)
582 status = handle_putval (stderr, body);
584 ERROR ("amqp plugin: handle_putval failed with status %i.",
588 else if (strcasecmp ("application/json", content_type) == 0)
590 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
591 "been implemented yet. FIXME!");
596 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
603 } /* }}} int camqp_read_body */
605 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
609 amqp_basic_properties_t *properties;
612 status = amqp_simple_wait_frame (conf->connection, &frame);
616 status = (-1) * status;
617 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
618 sstrerror (status, errbuf, sizeof (errbuf)));
619 camqp_close_connection (conf);
623 if (frame.frame_type != AMQP_FRAME_HEADER)
625 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
630 properties = frame.payload.properties.decoded;
631 content_type = camqp_bytes_cstring (&properties->content_type);
632 if (content_type == NULL)
634 ERROR ("amqp plugin: Unable to determine content type.");
638 status = camqp_read_body (conf,
639 (size_t) frame.payload.properties.body_size,
642 sfree (content_type);
644 } /* }}} int camqp_read_header */
646 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
648 camqp_config_t *conf = user_data;
651 cdtime_t interval = plugin_get_interval ();
653 while (subscriber_threads_running)
657 status = camqp_connect (conf);
660 struct timespec ts_interval;
661 ERROR ("amqp plugin: camqp_connect failed. "
662 "Will sleep for %.3f seconds.",
663 CDTIME_T_TO_DOUBLE (interval));
664 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
665 nanosleep (&ts_interval, /* remaining = */ NULL);
669 status = amqp_simple_wait_frame (conf->connection, &frame);
672 struct timespec ts_interval;
673 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
674 "Will sleep for %.3f seconds.",
675 CDTIME_T_TO_DOUBLE (interval));
676 camqp_close_connection (conf);
677 CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
678 nanosleep (&ts_interval, /* remaining = */ NULL);
682 if (frame.frame_type != AMQP_FRAME_METHOD)
684 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
689 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
691 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
692 frame.payload.method.id);
696 status = camqp_read_header (conf);
698 amqp_maybe_release_buffers (conf->connection);
699 } /* while (subscriber_threads_running) */
701 camqp_config_free (conf);
704 } /* }}} void *camqp_subscribe_thread */
706 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
711 tmp = realloc (subscriber_threads,
712 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
715 ERROR ("amqp plugin: realloc failed.");
716 camqp_config_free (conf);
719 subscriber_threads = tmp;
720 tmp = subscriber_threads + subscriber_threads_num;
721 memset (tmp, 0, sizeof (*tmp));
723 status = plugin_thread_create (tmp, /* attr = */ NULL,
724 camqp_subscribe_thread, conf);
728 ERROR ("amqp plugin: pthread_create failed: %s",
729 sstrerror (status, errbuf, sizeof (errbuf)));
730 camqp_config_free (conf);
734 subscriber_threads_num++;
737 } /* }}} int camqp_subscribe_init */
742 /* XXX: You must hold "conf->lock" when calling this function! */
743 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
744 const char *buffer, const char *routing_key)
746 amqp_basic_properties_t props;
749 status = camqp_connect (conf);
753 memset (&props, 0, sizeof (props));
754 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
755 | AMQP_BASIC_DELIVERY_MODE_FLAG
756 | AMQP_BASIC_APP_ID_FLAG;
757 if (conf->format == CAMQP_FORMAT_COMMAND)
758 props.content_type = amqp_cstring_bytes("text/collectd");
759 else if (conf->format == CAMQP_FORMAT_JSON)
760 props.content_type = amqp_cstring_bytes("application/json");
761 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
762 props.content_type = amqp_cstring_bytes("text/graphite");
765 props.delivery_mode = conf->delivery_mode;
766 props.app_id = amqp_cstring_bytes("collectd");
768 status = amqp_basic_publish(conf->connection,
770 amqp_cstring_bytes(CONF(conf, exchange)),
771 amqp_cstring_bytes (routing_key),
775 amqp_cstring_bytes(buffer));
778 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
780 camqp_close_connection (conf);
784 } /* }}} int camqp_write_locked */
786 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
787 user_data_t *user_data)
789 camqp_config_t *conf = user_data->data;
790 char routing_key[6 * DATA_MAX_NAME_LEN];
794 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
797 memset (buffer, 0, sizeof (buffer));
799 if (conf->routing_key != NULL)
801 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
806 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
808 vl->plugin, vl->plugin_instance,
809 vl->type, vl->type_instance);
811 /* Switch slashes (the only character forbidden by collectd) and dots
812 * (the separation character used by AMQP). */
813 for (i = 0; routing_key[i] != 0; i++)
815 if (routing_key[i] == '.')
816 routing_key[i] = '/';
817 else if (routing_key[i] == '/')
818 routing_key[i] = '.';
822 if (conf->format == CAMQP_FORMAT_COMMAND)
824 status = create_putval (buffer, sizeof (buffer), ds, vl);
827 ERROR ("amqp plugin: create_putval failed with status %i.",
832 else if (conf->format == CAMQP_FORMAT_JSON)
834 size_t bfree = sizeof (buffer);
837 format_json_initialize (buffer, &bfill, &bfree);
838 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
839 format_json_finalize (buffer, &bfill, &bfree);
841 else if (conf->format == CAMQP_FORMAT_GRAPHITE)
843 status = format_graphite (buffer, sizeof (buffer), ds, vl,
844 conf->prefix, conf->postfix, conf->escape_char,
845 conf->graphite_flags);
848 ERROR ("amqp plugin: format_graphite failed with status %i.",
855 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
859 pthread_mutex_lock (&conf->lock);
860 status = camqp_write_locked (conf, buffer, routing_key);
861 pthread_mutex_unlock (&conf->lock);
864 } /* }}} int camqp_write */
869 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
870 camqp_config_t *conf)
876 status = cf_util_get_string (ci, &string);
880 assert (string != NULL);
881 if (strcasecmp ("Command", string) == 0)
882 conf->format = CAMQP_FORMAT_COMMAND;
883 else if (strcasecmp ("JSON", string) == 0)
884 conf->format = CAMQP_FORMAT_JSON;
885 else if (strcasecmp ("Graphite", string) == 0)
886 conf->format = CAMQP_FORMAT_GRAPHITE;
889 WARNING ("amqp plugin: Invalid format string: %s",
896 } /* }}} int config_set_string */
898 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
901 camqp_config_t *conf;
905 conf = malloc (sizeof (*conf));
908 ERROR ("amqp plugin: malloc failed.");
912 /* Initialize "conf" {{{ */
913 memset (conf, 0, sizeof (*conf));
914 conf->publish = publish;
916 conf->format = CAMQP_FORMAT_COMMAND;
921 conf->password = NULL;
922 conf->exchange = NULL;
923 conf->routing_key = NULL;
925 conf->delivery_mode = CAMQP_DM_VOLATILE;
926 conf->store_rates = 0;
927 /* publish & graphite only */
929 conf->postfix = NULL;
930 conf->escape_char = '_';
932 conf->exchange_type = NULL;
935 conf->connection = NULL;
936 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
939 status = cf_util_get_string (ci, &conf->name);
946 for (i = 0; i < ci->children_num; i++)
948 oconfig_item_t *child = ci->children + i;
950 if (strcasecmp ("Host", child->key) == 0)
951 status = cf_util_get_string (child, &conf->host);
952 else if (strcasecmp ("Port", child->key) == 0)
954 status = cf_util_get_port_number (child);
961 else if (strcasecmp ("VHost", child->key) == 0)
962 status = cf_util_get_string (child, &conf->vhost);
963 else if (strcasecmp ("User", child->key) == 0)
964 status = cf_util_get_string (child, &conf->user);
965 else if (strcasecmp ("Password", child->key) == 0)
966 status = cf_util_get_string (child, &conf->password);
967 else if (strcasecmp ("Exchange", child->key) == 0)
968 status = cf_util_get_string (child, &conf->exchange);
969 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
970 status = cf_util_get_string (child, &conf->exchange_type);
971 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
972 status = cf_util_get_string (child, &conf->queue);
973 else if (strcasecmp ("RoutingKey", child->key) == 0)
974 status = cf_util_get_string (child, &conf->routing_key);
975 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
978 status = cf_util_get_boolean (child, &tmp);
980 conf->delivery_mode = CAMQP_DM_PERSISTENT;
982 conf->delivery_mode = CAMQP_DM_VOLATILE;
984 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
986 status = cf_util_get_boolean (child, &conf->store_rates);
987 (void) cf_util_get_flag (child, &conf->graphite_flags,
988 GRAPHITE_STORE_RATES);
990 else if ((strcasecmp ("Format", child->key) == 0) && publish)
991 status = camqp_config_set_format (child, conf);
992 else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
993 status = cf_util_get_string (child, &conf->prefix);
994 else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
995 status = cf_util_get_string (child, &conf->postfix);
996 else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
998 char *tmp_buff = NULL;
999 status = cf_util_get_string (child, &tmp_buff);
1000 if (strlen (tmp_buff) > 1)
1001 WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
1002 "only one character. Others will be ignored.");
1003 conf->escape_char = tmp_buff[0];
1007 WARNING ("amqp plugin: Ignoring unknown "
1008 "configuration option \"%s\".", child->key);
1012 } /* for (i = 0; i < ci->children_num; i++) */
1014 if ((status == 0) && (conf->exchange == NULL))
1016 if (conf->exchange_type != NULL)
1017 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
1018 "without the \"Exchange\" option. It will be ignored.");
1020 if (!publish && (conf->routing_key != NULL))
1021 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
1022 "without the \"Exchange\" option. It will be ignored.");
1028 camqp_config_free (conf);
1032 if (conf->exchange != NULL)
1034 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
1041 user_data_t ud = { conf, camqp_config_free };
1043 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
1045 status = plugin_register_write (cbname, camqp_write, &ud);
1048 camqp_config_free (conf);
1054 status = camqp_subscribe_init (conf);
1057 camqp_config_free (conf);
1063 } /* }}} int camqp_config_connection */
1065 static int camqp_config (oconfig_item_t *ci) /* {{{ */
1069 for (i = 0; i < ci->children_num; i++)
1071 oconfig_item_t *child = ci->children + i;
1073 if (strcasecmp ("Publish", child->key) == 0)
1074 camqp_config_connection (child, /* publish = */ 1);
1075 else if (strcasecmp ("Subscribe", child->key) == 0)
1076 camqp_config_connection (child, /* publish = */ 0);
1078 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
1080 } /* for (ci->children_num) */
1083 } /* }}} int camqp_config */
1085 void module_register (void)
1087 plugin_register_complex_config ("amqp", camqp_config);
1088 plugin_register_shutdown ("amqp", camqp_shutdown);
1089 } /* void module_register */
1091 /* vim: set sw=4 sts=4 et fdm=marker : */