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"
38 #include <amqp_framing.h>
40 /* Defines for the delivery mode. I have no idea why they're not defined by the
42 #define CAMQP_DM_VOLATILE 1
43 #define CAMQP_DM_PERSISTENT 2
45 #define CAMQP_FORMAT_COMMAND 1
46 #define CAMQP_FORMAT_JSON 2
48 #define CAMQP_CHANNEL 1
68 uint8_t delivery_mode;
76 amqp_connection_state_t connection;
79 typedef struct camqp_config_s camqp_config_t;
84 static const char *def_host = "localhost";
85 static const char *def_vhost = "/";
86 static const char *def_user = "guest";
87 static const char *def_password = "guest";
88 static const char *def_exchange = "amq.fanout";
90 static pthread_t *subscriber_threads = NULL;
91 static size_t subscriber_threads_num = 0;
92 static _Bool subscriber_threads_running = 1;
94 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
99 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
103 if ((conf == NULL) || (conf->connection == NULL))
106 sockfd = amqp_get_sockfd (conf->connection);
107 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
108 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
109 amqp_destroy_connection (conf->connection);
111 conf->connection = NULL;
112 } /* }}} void camqp_close_connection */
114 static void camqp_config_free (void *ptr) /* {{{ */
116 camqp_config_t *conf = ptr;
121 camqp_close_connection (conf);
127 sfree (conf->password);
128 sfree (conf->exchange);
129 sfree (conf->exchange_type);
131 sfree (conf->routing_key);
134 } /* }}} void camqp_config_free */
136 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
140 if ((in == NULL) || (in->bytes == NULL))
143 ret = malloc (in->len + 1);
147 memcpy (ret, in->bytes, in->len);
151 } /* }}} char *camqp_bytes_cstring */
153 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
157 r = amqp_get_rpc_reply (conf->connection);
158 if (r.reply_type == AMQP_RESPONSE_NORMAL)
162 } /* }}} _Bool camqp_is_error */
164 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
165 char *buffer, size_t buffer_size)
169 r = amqp_get_rpc_reply (conf->connection);
170 switch (r.reply_type)
172 case AMQP_RESPONSE_NORMAL:
173 sstrncpy (buffer, "Success", sizeof (buffer));
176 case AMQP_RESPONSE_NONE:
177 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
180 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
181 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
183 return (sstrerror (r.library_errno, buffer, buffer_size));
186 return (sstrerror (r.library_error, buffer, buffer_size));
189 sstrncpy (buffer, "End of stream", sizeof (buffer));
192 case AMQP_RESPONSE_SERVER_EXCEPTION:
193 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
195 amqp_connection_close_t *m = r.reply.decoded;
196 char *tmp = camqp_bytes_cstring (&m->reply_text);
197 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
201 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
203 amqp_channel_close_t *m = r.reply.decoded;
204 char *tmp = camqp_bytes_cstring (&m->reply_text);
205 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
211 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
217 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
222 } /* }}} char *camqp_strerror */
224 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
225 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
227 amqp_exchange_declare_ok_t *ed_ret;
229 if (conf->exchange_type == NULL)
232 ed_ret = amqp_exchange_declare (conf->connection,
233 /* channel = */ CAMQP_CHANNEL,
234 /* exchange = */ amqp_cstring_bytes (conf->exchange),
235 /* type = */ amqp_cstring_bytes (conf->exchange_type),
238 /* auto_delete = */ 1,
239 /* arguments = */ AMQP_EMPTY_TABLE);
240 if ((ed_ret == NULL) && camqp_is_error (conf))
243 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
244 camqp_strerror (conf, errbuf, sizeof (errbuf)));
245 camqp_close_connection (conf);
249 INFO ("amqp plugin: Successfully created exchange \"%s\" "
251 conf->exchange, conf->exchange_type);
254 } /* }}} int camqp_create_exchange */
256 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
258 amqp_exchange_declare_ok_t *ed_ret;
259 amqp_table_t argument_table;
260 struct amqp_table_entry_t_ argument_table_entries[1];
262 if (conf->exchange_type == NULL)
265 /* Valid arguments: "auto_delete", "internal" */
266 argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
267 argument_table.entries = argument_table_entries;
268 argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
269 argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
270 argument_table_entries[0].value.value.boolean = 1;
272 ed_ret = amqp_exchange_declare (conf->connection,
273 /* channel = */ CAMQP_CHANNEL,
274 /* exchange = */ amqp_cstring_bytes (conf->exchange),
275 /* type = */ amqp_cstring_bytes (conf->exchange_type),
278 /* arguments = */ argument_table);
279 if ((ed_ret == NULL) && camqp_is_error (conf))
282 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
283 camqp_strerror (conf, errbuf, sizeof (errbuf)));
284 camqp_close_connection (conf);
288 INFO ("amqp plugin: Successfully created exchange \"%s\" "
290 conf->exchange, conf->exchange_type);
293 } /* }}} int camqp_create_exchange */
296 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
298 amqp_queue_declare_ok_t *qd_ret;
299 amqp_basic_consume_ok_t *cm_ret;
301 qd_ret = amqp_queue_declare (conf->connection,
302 /* channel = */ CAMQP_CHANNEL,
303 /* queue = */ (conf->queue != NULL)
304 ? amqp_cstring_bytes (conf->queue)
309 /* auto_delete = */ 1,
310 /* arguments = */ AMQP_EMPTY_TABLE);
313 ERROR ("amqp plugin: amqp_queue_declare failed.");
314 camqp_close_connection (conf);
318 if (conf->queue == NULL)
320 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
321 if (conf->queue == NULL)
323 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
324 camqp_close_connection (conf);
328 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
330 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
332 /* bind to an exchange */
333 if (conf->exchange != NULL)
335 amqp_queue_bind_ok_t *qb_ret;
337 assert (conf->queue != NULL);
338 qb_ret = amqp_queue_bind (conf->connection,
339 /* channel = */ CAMQP_CHANNEL,
340 /* queue = */ amqp_cstring_bytes (conf->queue),
341 /* exchange = */ amqp_cstring_bytes (conf->exchange),
342 /* routing_key = */ (conf->routing_key != NULL)
343 ? amqp_cstring_bytes (conf->routing_key)
345 /* arguments = */ AMQP_EMPTY_TABLE);
346 if ((qb_ret == NULL) && camqp_is_error (conf))
349 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
350 camqp_strerror (conf, errbuf, sizeof (errbuf)));
351 camqp_close_connection (conf);
355 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
356 conf->queue, conf->exchange);
357 } /* if (conf->exchange != NULL) */
359 cm_ret = amqp_basic_consume (conf->connection,
360 /* channel = */ CAMQP_CHANNEL,
361 /* queue = */ amqp_cstring_bytes (conf->queue),
362 /* consumer_tag = */ AMQP_EMPTY_BYTES,
366 /* arguments = */ AMQP_EMPTY_TABLE
368 if ((cm_ret == NULL) && camqp_is_error (conf))
371 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
372 camqp_strerror (conf, errbuf, sizeof (errbuf)));
373 camqp_close_connection (conf);
378 } /* }}} int camqp_setup_queue */
380 static int camqp_connect (camqp_config_t *conf) /* {{{ */
382 amqp_rpc_reply_t reply;
386 if (conf->connection != NULL)
389 conf->connection = amqp_new_connection ();
390 if (conf->connection == NULL)
392 ERROR ("amqp plugin: amqp_new_connection failed.");
396 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
400 status = (-1) * sockfd;
401 ERROR ("amqp plugin: amqp_open_socket failed: %s",
402 sstrerror (status, errbuf, sizeof (errbuf)));
403 amqp_destroy_connection (conf->connection);
404 conf->connection = NULL;
407 amqp_set_sockfd (conf->connection, sockfd);
409 reply = amqp_login (conf->connection, CONF(conf, vhost),
410 /* channel max = */ 0,
411 /* frame max = */ 131072,
413 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
414 CONF(conf, user), CONF(conf, password));
415 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
417 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
418 CONF(conf, vhost), CONF(conf, user));
419 amqp_destroy_connection (conf->connection);
421 conf->connection = NULL;
425 amqp_channel_open (conf->connection, /* channel = */ 1);
426 /* FIXME: Is checking "reply.reply_type" really correct here? How does
427 * it get set? --octo */
428 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
430 ERROR ("amqp plugin: amqp_channel_open failed.");
431 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
432 amqp_destroy_connection (conf->connection);
434 conf->connection = NULL;
438 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
439 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
441 status = camqp_create_exchange (conf);
446 return (camqp_setup_queue (conf));
448 } /* }}} int camqp_connect */
450 static int camqp_shutdown (void) /* {{{ */
454 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
455 subscriber_threads_num);
457 subscriber_threads_running = 0;
458 for (i = 0; i < subscriber_threads_num; i++)
460 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
461 * to use a timeout in the thread and check for the variable in regular
463 pthread_kill (subscriber_threads[i], SIGTERM);
464 pthread_join (subscriber_threads[i], /* retval = */ NULL);
467 subscriber_threads_num = 0;
468 sfree (subscriber_threads);
470 DEBUG ("amqp plugin: All subscriber threads exited.");
473 } /* }}} int camqp_shutdown */
478 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
479 size_t body_size, const char *content_type)
481 char body[body_size + 1];
487 memset (body, 0, sizeof (body));
491 while (received < body_size)
493 status = amqp_simple_wait_frame (conf->connection, &frame);
497 status = (-1) * status;
498 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
499 sstrerror (status, errbuf, sizeof (errbuf)));
500 camqp_close_connection (conf);
504 if (frame.frame_type != AMQP_FRAME_BODY)
506 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
511 if ((body_size - received) < frame.payload.body_fragment.len)
513 WARNING ("amqp plugin: Body is larger than indicated by header.");
517 memcpy (body_ptr, frame.payload.body_fragment.bytes,
518 frame.payload.body_fragment.len);
519 body_ptr += frame.payload.body_fragment.len;
520 received += frame.payload.body_fragment.len;
521 } /* while (received < body_size) */
523 if (strcasecmp ("text/collectd", content_type) == 0)
525 status = handle_putval (stderr, body);
527 ERROR ("amqp plugin: handle_putval failed with status %i.",
531 else if (strcasecmp ("application/json", content_type) == 0)
533 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
534 "been implemented yet. FIXME!");
539 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
546 } /* }}} int camqp_read_body */
548 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
552 amqp_basic_properties_t *properties;
555 status = amqp_simple_wait_frame (conf->connection, &frame);
559 status = (-1) * status;
560 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
561 sstrerror (status, errbuf, sizeof (errbuf)));
562 camqp_close_connection (conf);
566 if (frame.frame_type != AMQP_FRAME_HEADER)
568 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
573 properties = frame.payload.properties.decoded;
574 content_type = camqp_bytes_cstring (&properties->content_type);
575 if (content_type == NULL)
577 ERROR ("amqp plugin: Unable to determine content type.");
581 status = camqp_read_body (conf,
582 (size_t) frame.payload.properties.body_size,
585 sfree (content_type);
587 } /* }}} int camqp_read_header */
589 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
591 camqp_config_t *conf = user_data;
594 while (subscriber_threads_running)
598 status = camqp_connect (conf);
601 struct timespec ts_interval;
602 ERROR ("amqp plugin: camqp_connect failed. "
603 "Will sleep for %.3f seconds.",
604 CDTIME_T_TO_DOUBLE (interval_g));
605 CDTIME_T_TO_TIMESPEC (interval_g, &ts_interval);
606 nanosleep (&ts_interval, /* remaining = */ NULL);
610 status = amqp_simple_wait_frame (conf->connection, &frame);
613 struct timespec ts_interval;
614 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
615 "Will sleep for %.3f seconds.",
616 CDTIME_T_TO_DOUBLE (interval_g));
617 camqp_close_connection (conf);
618 CDTIME_T_TO_TIMESPEC (interval_g, &ts_interval);
619 nanosleep (&ts_interval, /* remaining = */ NULL);
623 if (frame.frame_type != AMQP_FRAME_METHOD)
625 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
630 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
632 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
633 frame.payload.method.id);
637 status = camqp_read_header (conf);
639 amqp_maybe_release_buffers (conf->connection);
640 } /* while (subscriber_threads_running) */
642 camqp_config_free (conf);
645 } /* }}} void *camqp_subscribe_thread */
647 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
652 tmp = realloc (subscriber_threads,
653 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
656 ERROR ("amqp plugin: realloc failed.");
657 camqp_config_free (conf);
660 subscriber_threads = tmp;
661 tmp = subscriber_threads + subscriber_threads_num;
662 memset (tmp, 0, sizeof (*tmp));
664 status = pthread_create (tmp, /* attr = */ NULL,
665 camqp_subscribe_thread, conf);
669 ERROR ("amqp plugin: pthread_create failed: %s",
670 sstrerror (status, errbuf, sizeof (errbuf)));
671 camqp_config_free (conf);
675 subscriber_threads_num++;
678 } /* }}} int camqp_subscribe_init */
683 /* XXX: You must hold "conf->lock" when calling this function! */
684 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
685 const char *buffer, const char *routing_key)
687 amqp_basic_properties_t props;
690 status = camqp_connect (conf);
694 memset (&props, 0, sizeof (props));
695 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
696 | AMQP_BASIC_DELIVERY_MODE_FLAG
697 | AMQP_BASIC_APP_ID_FLAG;
698 if (conf->format == CAMQP_FORMAT_COMMAND)
699 props.content_type = amqp_cstring_bytes("text/collectd");
700 else if (conf->format == CAMQP_FORMAT_JSON)
701 props.content_type = amqp_cstring_bytes("application/json");
704 props.delivery_mode = conf->delivery_mode;
705 props.app_id = amqp_cstring_bytes("collectd");
707 status = amqp_basic_publish(conf->connection,
709 amqp_cstring_bytes(CONF(conf, exchange)),
710 amqp_cstring_bytes (routing_key),
714 amqp_cstring_bytes(buffer));
717 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
719 camqp_close_connection (conf);
723 } /* }}} int camqp_write_locked */
725 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
726 user_data_t *user_data)
728 camqp_config_t *conf = user_data->data;
729 char routing_key[6 * DATA_MAX_NAME_LEN];
733 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
736 memset (buffer, 0, sizeof (buffer));
738 if (conf->routing_key != NULL)
740 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
745 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
747 vl->plugin, vl->plugin_instance,
748 vl->type, vl->type_instance);
750 /* Switch slashes (the only character forbidden by collectd) and dots
751 * (the separation character used by AMQP). */
752 for (i = 0; routing_key[i] != 0; i++)
754 if (routing_key[i] == '.')
755 routing_key[i] = '/';
756 else if (routing_key[i] == '/')
757 routing_key[i] = '.';
761 if (conf->format == CAMQP_FORMAT_COMMAND)
763 status = create_putval (buffer, sizeof (buffer), ds, vl);
766 ERROR ("amqp plugin: create_putval failed with status %i.",
771 else if (conf->format == CAMQP_FORMAT_JSON)
773 size_t bfree = sizeof (buffer);
776 format_json_initialize (buffer, &bfill, &bfree);
777 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
778 format_json_finalize (buffer, &bfill, &bfree);
782 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
786 pthread_mutex_lock (&conf->lock);
787 status = camqp_write_locked (conf, buffer, routing_key);
788 pthread_mutex_unlock (&conf->lock);
791 } /* }}} int camqp_write */
796 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
797 camqp_config_t *conf)
803 status = cf_util_get_string (ci, &string);
807 assert (string != NULL);
808 if (strcasecmp ("Command", string) == 0)
809 conf->format = CAMQP_FORMAT_COMMAND;
810 else if (strcasecmp ("JSON", string) == 0)
811 conf->format = CAMQP_FORMAT_JSON;
814 WARNING ("amqp plugin: Invalid format string: %s",
821 } /* }}} int config_set_string */
823 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
826 camqp_config_t *conf;
830 conf = malloc (sizeof (*conf));
833 ERROR ("amqp plugin: malloc failed.");
837 /* Initialize "conf" {{{ */
838 memset (conf, 0, sizeof (*conf));
839 conf->publish = publish;
841 conf->format = CAMQP_FORMAT_COMMAND;
846 conf->password = NULL;
847 conf->exchange = NULL;
848 conf->routing_key = NULL;
850 conf->delivery_mode = CAMQP_DM_VOLATILE;
851 conf->store_rates = 0;
853 conf->exchange_type = NULL;
856 conf->connection = NULL;
857 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
860 status = cf_util_get_string (ci, &conf->name);
867 for (i = 0; i < ci->children_num; i++)
869 oconfig_item_t *child = ci->children + i;
871 if (strcasecmp ("Host", child->key) == 0)
872 status = cf_util_get_string (child, &conf->host);
873 else if (strcasecmp ("Port", child->key) == 0)
875 status = cf_util_get_port_number (child);
882 else if (strcasecmp ("VHost", child->key) == 0)
883 status = cf_util_get_string (child, &conf->vhost);
884 else if (strcasecmp ("User", child->key) == 0)
885 status = cf_util_get_string (child, &conf->user);
886 else if (strcasecmp ("Password", child->key) == 0)
887 status = cf_util_get_string (child, &conf->password);
888 else if (strcasecmp ("Exchange", child->key) == 0)
889 status = cf_util_get_string (child, &conf->exchange);
890 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
891 status = cf_util_get_string (child, &conf->exchange_type);
892 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
893 status = cf_util_get_string (child, &conf->queue);
894 else if (strcasecmp ("RoutingKey", child->key) == 0)
895 status = cf_util_get_string (child, &conf->routing_key);
896 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
899 status = cf_util_get_boolean (child, &tmp);
901 conf->delivery_mode = CAMQP_DM_PERSISTENT;
903 conf->delivery_mode = CAMQP_DM_VOLATILE;
905 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
906 status = cf_util_get_boolean (child, &conf->store_rates);
907 else if ((strcasecmp ("Format", child->key) == 0) && publish)
908 status = camqp_config_set_format (child, conf);
910 WARNING ("amqp plugin: Ignoring unknown "
911 "configuration option \"%s\".", child->key);
915 } /* for (i = 0; i < ci->children_num; i++) */
917 if ((status == 0) && (conf->exchange == NULL))
919 if (conf->exchange_type != NULL)
920 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
921 "without the \"Exchange\" option. It will be ignored.");
923 if (!publish && (conf->routing_key != NULL))
924 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
925 "without the \"Exchange\" option. It will be ignored.");
931 camqp_config_free (conf);
935 if (conf->exchange != NULL)
937 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
944 user_data_t ud = { conf, camqp_config_free };
946 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
948 status = plugin_register_write (cbname, camqp_write, &ud);
951 camqp_config_free (conf);
957 status = camqp_subscribe_init (conf);
960 camqp_config_free (conf);
966 } /* }}} int camqp_config_connection */
968 static int camqp_config (oconfig_item_t *ci) /* {{{ */
972 for (i = 0; i < ci->children_num; i++)
974 oconfig_item_t *child = ci->children + i;
976 if (strcasecmp ("Publish", child->key) == 0)
977 camqp_config_connection (child, /* publish = */ 1);
978 else if (strcasecmp ("Subscribe", child->key) == 0)
979 camqp_config_connection (child, /* publish = */ 0);
981 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
983 } /* for (ci->children_num) */
986 } /* }}} int camqp_config */
988 void module_register (void)
990 plugin_register_complex_config ("amqp", camqp_config);
991 plugin_register_shutdown ("amqp", camqp_shutdown);
992 } /* void module_register */
994 /* vim: set sw=4 sts=4 et fdm=marker : */