1 /**
2 * collectd - src/amqp.c
3 * Copyright (C) 2009 Sebastien Pahl
4 * Copyright (C) 2010 Florian Forster
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors:
25 * Sebastien Pahl <sebastien.pahl at dotcloud.com>
26 * Florian Forster <octo at verplant.org>
27 **/
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <strings.h>
33 #include <pthread.h>
35 #include "collectd.h"
36 #include "common.h"
37 #include "plugin.h"
38 #include "utils_cmd_putval.h"
39 #include "utils_format_json.h"
41 #include <amqp.h>
42 #include <amqp_framing.h>
44 /* Defines for the delivery mode. I have no idea why they're not defined by the
45 * library.. */
46 #define CAMQP_DM_VOLATILE 1
47 #define CAMQP_DM_PERSISTENT 2
49 #define CAMQP_FORMAT_COMMAND 1
50 #define CAMQP_FORMAT_JSON 2
52 #define CAMQP_CHANNEL 1
54 /*
55 * Data types
56 */
57 struct camqp_config_s
58 {
59 _Bool publish;
60 char *name;
61 int format;
63 char *host;
64 int port;
65 char *vhost;
66 char *user;
67 char *password;
69 char *exchange;
70 char *routingkey;
72 /* publish only */
73 uint8_t delivery_mode;
74 _Bool store_rates;
76 /* subscribe only */
77 char *exchange_type;
78 char *queue;
80 amqp_connection_state_t connection;
81 pthread_mutex_t lock;
82 };
83 typedef struct camqp_config_s camqp_config_t;
85 /*
86 * Global variables
87 */
88 static const char *def_host = "localhost";
89 static const char *def_vhost = "/";
90 static const char *def_user = "guest";
91 static const char *def_password = "guest";
92 static const char *def_exchange = "amq.fanout";
93 static const char *def_routingkey = "collectd";
95 static pthread_t *subscriber_threads = NULL;
96 static size_t subscriber_threads_num = 0;
97 static _Bool subscriber_threads_running = 1;
99 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
101 /*
102 * Functions
103 */
104 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
105 {
106 int sockfd;
108 if ((conf == NULL) || (conf->connection == NULL))
109 return;
111 sockfd = amqp_get_sockfd (conf->connection);
112 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
113 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
114 amqp_destroy_connection (conf->connection);
115 close (sockfd);
116 conf->connection = NULL;
117 } /* }}} void camqp_close_connection */
119 static void camqp_config_free (void *ptr) /* {{{ */
120 {
121 camqp_config_t *conf = ptr;
123 if (conf == NULL)
124 return;
126 camqp_close_connection (conf);
128 sfree (conf->name);
129 sfree (conf->host);
130 sfree (conf->vhost);
131 sfree (conf->user);
132 sfree (conf->password);
133 sfree (conf->exchange);
134 sfree (conf->exchange_type);
135 sfree (conf->queue);
136 sfree (conf->routingkey);
138 sfree (conf);
139 } /* }}} void camqp_config_free */
141 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
142 {
143 char *ret;
145 if ((in == NULL) || (in->bytes == NULL))
146 return (NULL);
148 ret = malloc (in->len + 1);
149 if (ret == NULL)
150 return (NULL);
152 memcpy (ret, in->bytes, in->len);
153 ret[in->len] = 0;
155 return (ret);
156 } /* }}} char *camqp_bytes_cstring */
158 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
159 {
160 amqp_rpc_reply_t r;
162 r = amqp_get_rpc_reply (conf->connection);
163 if (r.reply_type == AMQP_RESPONSE_NORMAL)
164 return (0);
166 return (1);
167 } /* }}} _Bool camqp_is_error */
169 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
170 char *buffer, size_t buffer_size)
171 {
172 amqp_rpc_reply_t r;
174 r = amqp_get_rpc_reply (conf->connection);
175 switch (r.reply_type)
176 {
177 case AMQP_RESPONSE_NORMAL:
178 sstrncpy (buffer, "Success", sizeof (buffer));
179 break;
181 case AMQP_RESPONSE_NONE:
182 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
183 break;
185 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
186 if (r.library_errno)
187 return (sstrerror (r.library_errno, buffer, buffer_size));
188 else
189 sstrncpy (buffer, "End of stream", sizeof (buffer));
190 break;
192 case AMQP_RESPONSE_SERVER_EXCEPTION:
193 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
194 {
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",
198 m->reply_code, tmp);
199 sfree (tmp);
200 }
201 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
202 {
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",
206 m->reply_code, tmp);
207 sfree (tmp);
208 }
209 else
210 {
211 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
212 r.reply.id);
213 }
214 break;
216 default:
217 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
218 (int) r.reply_type);
219 }
221 return (buffer);
222 } /* }}} char *camqp_strerror */
224 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
225 {
226 amqp_queue_declare_ok_t *qd_ret;
227 amqp_basic_consume_ok_t *cm_ret;
229 qd_ret = amqp_queue_declare (conf->connection,
230 /* channel = */ CAMQP_CHANNEL,
231 /* queue = */ (conf->queue != NULL)
232 ? amqp_cstring_bytes (conf->queue)
233 : AMQP_EMPTY_BYTES,
234 /* passive = */ 0,
235 /* durable = */ 0,
236 /* exclusive = */ 0,
237 /* auto_delete = */ 1,
238 /* arguments = */ AMQP_EMPTY_TABLE);
239 if (qd_ret == NULL)
240 {
241 ERROR ("amqp plugin: amqp_queue_declare failed.");
242 camqp_close_connection (conf);
243 return (-1);
244 }
246 if (conf->queue == NULL)
247 {
248 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
249 if (conf->queue == NULL)
250 {
251 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
252 camqp_close_connection (conf);
253 return (-1);
254 }
256 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
257 }
258 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
260 /* bind to an exchange */
261 if (conf->exchange != NULL)
262 {
263 amqp_queue_bind_ok_t *qb_ret;
265 /* create the exchange */
266 if (conf->exchange_type != NULL)
267 {
268 amqp_exchange_declare_ok_t *ed_ret;
270 ed_ret = amqp_exchange_declare (conf->connection,
271 /* channel = */ CAMQP_CHANNEL,
272 /* exchange = */ amqp_cstring_bytes (conf->exchange),
273 /* type = */ amqp_cstring_bytes (conf->exchange_type),
274 /* passive = */ 0,
275 /* durable = */ 0,
276 /* auto_delete = */ 1,
277 /* arguments = */ AMQP_EMPTY_TABLE);
278 if ((ed_ret == NULL) && camqp_is_error (conf))
279 {
280 char errbuf[1024];
281 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
282 camqp_strerror (conf, errbuf, sizeof (errbuf)));
283 camqp_close_connection (conf);
284 return (-1);
285 }
286 }
288 DEBUG ("amqp plugin: queue = %s; exchange = %s; routing_key = %s;",
289 conf->queue, conf->exchange, CONF (conf, routingkey));
291 assert (conf->queue != NULL);
292 qb_ret = amqp_queue_bind (conf->connection,
293 /* channel = */ CAMQP_CHANNEL,
294 /* queue = */ amqp_cstring_bytes (conf->queue),
295 /* exchange = */ amqp_cstring_bytes (conf->exchange),
296 #if 1
297 /* routing_key = */ amqp_cstring_bytes (CONF (conf, routingkey)),
298 #else
299 /* routing_key = */ AMQP_EMPTY_BYTES,
300 #endif
301 /* arguments = */ AMQP_EMPTY_TABLE);
302 if ((qb_ret == NULL) && camqp_is_error (conf))
303 {
304 char errbuf[1024];
305 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
306 camqp_strerror (conf, errbuf, sizeof (errbuf)));
307 camqp_close_connection (conf);
308 return (-1);
309 }
311 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
312 conf->queue, conf->exchange);
313 } /* if (conf->exchange != NULL) */
315 cm_ret = amqp_basic_consume (conf->connection,
316 /* channel = */ CAMQP_CHANNEL,
317 /* queue = */ amqp_cstring_bytes (conf->queue),
318 /* consumer_tag = */ AMQP_EMPTY_BYTES,
319 /* no_local = */ 0,
320 /* no_ack = */ 1,
321 /* exclusive = */ 0);
322 if ((cm_ret == NULL) && camqp_is_error (conf))
323 {
324 char errbuf[1024];
325 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
326 camqp_strerror (conf, errbuf, sizeof (errbuf)));
327 camqp_close_connection (conf);
328 return (-1);
329 }
331 return (0);
332 } /* }}} int camqp_setup_queue */
334 static int camqp_connect (camqp_config_t *conf) /* {{{ */
335 {
336 amqp_rpc_reply_t reply;
337 int sockfd;
338 int status;
340 if (conf->connection != NULL)
341 return (0);
343 conf->connection = amqp_new_connection ();
344 if (conf->connection == NULL)
345 {
346 ERROR ("amqp plugin: amqp_new_connection failed.");
347 return (ENOMEM);
348 }
350 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
351 if (sockfd < 0)
352 {
353 char errbuf[1024];
354 status = (-1) * sockfd;
355 ERROR ("amqp plugin: amqp_open_socket failed: %s",
356 sstrerror (status, errbuf, sizeof (errbuf)));
357 amqp_destroy_connection (conf->connection);
358 conf->connection = NULL;
359 return (status);
360 }
361 amqp_set_sockfd (conf->connection, sockfd);
363 reply = amqp_login (conf->connection, CONF(conf, vhost),
364 /* channel max = */ 0,
365 /* frame max = */ 131072,
366 /* heartbeat = */ 0,
367 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
368 CONF(conf, user), CONF(conf, password));
369 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
370 {
371 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
372 CONF(conf, vhost), CONF(conf, user));
373 amqp_destroy_connection (conf->connection);
374 close (sockfd);
375 conf->connection = NULL;
376 return (1);
377 }
379 amqp_channel_open (conf->connection, /* channel = */ 1);
380 /* FIXME: Is checking "reply.reply_type" really correct here? How does
381 * it get set? --octo */
382 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
383 {
384 ERROR ("amqp plugin: amqp_channel_open failed.");
385 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
386 amqp_destroy_connection (conf->connection);
387 close(sockfd);
388 conf->connection = NULL;
389 return (1);
390 }
392 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
393 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
395 if (!conf->publish)
396 return (camqp_setup_queue (conf));
397 return (0);
398 } /* }}} int camqp_connect */
400 static int shutdown (void) /* {{{ */
401 {
402 size_t i;
404 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
405 subscriber_threads_num);
407 subscriber_threads_running = 0;
408 for (i = 0; i < subscriber_threads_num; i++)
409 {
410 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
411 * to use a timeout in the thread and check for the variable in regular
412 * intervals. */
413 pthread_kill (subscriber_threads[i], SIGTERM);
414 pthread_join (subscriber_threads[i], /* retval = */ NULL);
415 }
417 subscriber_threads_num = 0;
418 sfree (subscriber_threads);
420 DEBUG ("amqp plugin: All subscriber threads exited.");
422 return (0);
423 } /* }}} int shutdown */
425 /*
426 * Subscribing code
427 */
428 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
429 size_t body_size)
430 {
431 char body[body_size + 1];
432 char *body_ptr;
433 size_t received;
434 amqp_frame_t frame;
435 int status;
437 memset (body, 0, sizeof (body));
438 body_ptr = &body[0];
439 received = 0;
441 while (received < body_size)
442 {
443 status = amqp_simple_wait_frame (conf->connection, &frame);
444 if (status < 0)
445 {
446 char errbuf[1024];
447 status = (-1) * status;
448 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
449 sstrerror (status, errbuf, sizeof (errbuf)));
450 camqp_close_connection (conf);
451 return (status);
452 }
454 if (frame.frame_type != AMQP_FRAME_BODY)
455 {
456 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
457 frame.frame_type);
458 return (-1);
459 }
461 if ((body_size - received) < frame.payload.body_fragment.len)
462 {
463 WARNING ("amqp plugin: Body is larger than indicated by header.");
464 return (-1);
465 }
467 memcpy (body_ptr, frame.payload.body_fragment.bytes,
468 frame.payload.body_fragment.len);
469 body_ptr += frame.payload.body_fragment.len;
470 received += frame.payload.body_fragment.len;
471 } /* while (received < body_size) */
473 if (conf->format == CAMQP_FORMAT_COMMAND)
474 {
475 status = handle_putval (stderr, body);
476 if (status != 0)
477 ERROR ("amqp plugin: handle_putval failed with status %i.",
478 status);
479 return (status);
480 }
481 else if (conf->format == CAMQP_FORMAT_JSON)
482 {
483 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
484 "been implemented yet. FIXME!");
485 return (0);
486 }
487 else
488 {
489 ERROR ("amqp plugin: camqp_read_body: Unknown format option (%i).",
490 conf->format);
491 return (EINVAL);
492 }
494 /* not reached */
495 return (0);
496 } /* }}} int camqp_read_body */
498 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
499 {
500 int status;
501 amqp_frame_t frame;
503 status = amqp_simple_wait_frame (conf->connection, &frame);
504 if (status < 0)
505 {
506 char errbuf[1024];
507 status = (-1) * status;
508 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
509 sstrerror (status, errbuf, sizeof (errbuf)));
510 camqp_close_connection (conf);
511 return (status);
512 }
514 if (frame.frame_type != AMQP_FRAME_HEADER)
515 {
516 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
517 frame.frame_type);
518 return (-1);
519 }
521 return (camqp_read_body (conf, frame.payload.properties.body_size));
522 } /* }}} int camqp_read_header */
524 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
525 {
526 camqp_config_t *conf = user_data;
527 int status;
529 while (subscriber_threads_running)
530 {
531 amqp_frame_t frame;
533 status = camqp_connect (conf);
534 if (status != 0)
535 {
536 ERROR ("amqp plugin: camqp_connect failed. "
537 "Will sleep for %i seconds.", interval_g);
538 sleep (interval_g);
539 continue;
540 }
542 status = amqp_simple_wait_frame (conf->connection, &frame);
543 if (status < 0)
544 {
545 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
546 "Will sleep for %i seconds.", interval_g);
547 camqp_close_connection (conf);
548 sleep (interval_g);
549 continue;
550 }
552 if (frame.frame_type != AMQP_FRAME_METHOD)
553 {
554 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
555 frame.frame_type);
556 continue;
557 }
559 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
560 {
561 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
562 frame.payload.method.id);
563 continue;
564 }
566 status = camqp_read_header (conf);
568 amqp_maybe_release_buffers (conf->connection);
569 } /* while (subscriber_threads_running) */
571 camqp_config_free (conf);
572 pthread_exit (NULL);
573 } /* }}} void *camqp_subscribe_thread */
575 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
576 {
577 int status;
578 pthread_t *tmp;
580 tmp = realloc (subscriber_threads,
581 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
582 if (tmp == NULL)
583 {
584 ERROR ("amqp plugin: realloc failed.");
585 camqp_config_free (conf);
586 return (ENOMEM);
587 }
588 subscriber_threads = tmp;
589 tmp = subscriber_threads + subscriber_threads_num;
590 memset (tmp, 0, sizeof (*tmp));
592 status = pthread_create (tmp, /* attr = */ NULL,
593 camqp_subscribe_thread, conf);
594 if (status != 0)
595 {
596 char errbuf[1024];
597 ERROR ("amqp plugin: pthread_create failed: %s",
598 sstrerror (status, errbuf, sizeof (errbuf)));
599 camqp_config_free (conf);
600 return (status);
601 }
603 subscriber_threads_num++;
605 return (0);
606 } /* }}} int camqp_subscribe_init */
608 /*
609 * Publishing code
610 */
611 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
612 const char *buffer)
613 {
614 amqp_basic_properties_t props;
615 int status;
617 status = camqp_connect (conf);
618 if (status != 0)
619 return (status);
621 memset (&props, 0, sizeof (props));
622 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
623 | AMQP_BASIC_DELIVERY_MODE_FLAG
624 | AMQP_BASIC_APP_ID_FLAG;
625 props.content_type = amqp_cstring_bytes("application/json");
626 props.delivery_mode = conf->delivery_mode;
627 props.app_id = amqp_cstring_bytes("collectd");
629 status = amqp_basic_publish(conf->connection,
630 /* channel = */ 1,
631 amqp_cstring_bytes(CONF(conf, exchange)),
632 amqp_cstring_bytes(CONF(conf, routingkey)),
633 /* mandatory = */ 0,
634 /* immediate = */ 0,
635 &props,
636 amqp_cstring_bytes(buffer));
637 if (status != 0)
638 {
639 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
640 status);
641 camqp_close_connection (conf);
642 }
644 return (status);
645 } /* }}} int camqp_write_locked */
647 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
648 user_data_t *user_data)
649 {
650 camqp_config_t *conf = user_data->data;
651 char buffer[4096];
652 size_t bfree;
653 size_t bfill;
654 int status;
656 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
657 return (EINVAL);
659 memset (buffer, 0, sizeof (buffer));
660 bfree = sizeof (buffer);
661 bfill = 0;
663 if (conf->format == CAMQP_FORMAT_COMMAND)
664 {
665 status = create_putval (buffer, sizeof (buffer), ds, vl);
666 if (status != 0)
667 {
668 ERROR ("amqp plugin: create_putval failed with status %i.",
669 status);
670 return (status);
671 }
672 }
673 else if (conf->format == CAMQP_FORMAT_JSON)
674 {
675 format_json_initialize (buffer, &bfill, &bfree);
676 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
677 format_json_finalize (buffer, &bfill, &bfree);
678 }
679 else
680 {
681 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
682 return (-1);
683 }
685 pthread_mutex_lock (&conf->lock);
686 status = camqp_write_locked (conf, buffer);
687 pthread_mutex_unlock (&conf->lock);
689 return (status);
690 } /* }}} int camqp_write */
692 /*
693 * Config handling
694 */
695 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
696 camqp_config_t *conf)
697 {
698 char *string;
699 int status;
701 string = NULL;
702 status = cf_util_get_string (ci, &string);
703 if (status != 0)
704 return (status);
706 assert (string != NULL);
707 if (strcasecmp ("Command", string) == 0)
708 conf->format = CAMQP_FORMAT_COMMAND;
709 else if (strcasecmp ("JSON", string) == 0)
710 conf->format = CAMQP_FORMAT_JSON;
711 else
712 {
713 WARNING ("amqp plugin: Invalid format string: %s",
714 string);
715 }
717 free (string);
719 return (0);
720 } /* }}} int config_set_string */
722 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
723 _Bool publish)
724 {
725 camqp_config_t *conf;
726 int status;
727 int i;
729 conf = malloc (sizeof (*conf));
730 if (conf == NULL)
731 {
732 ERROR ("amqp plugin: malloc failed.");
733 return (ENOMEM);
734 }
736 /* Initialize "conf" {{{ */
737 memset (conf, 0, sizeof (*conf));
738 conf->publish = publish;
739 conf->name = NULL;
740 conf->format = CAMQP_FORMAT_COMMAND;
741 conf->host = NULL;
742 conf->port = 5672;
743 conf->vhost = NULL;
744 conf->user = NULL;
745 conf->password = NULL;
746 conf->exchange = NULL;
747 conf->routingkey = NULL;
748 /* publish only */
749 conf->delivery_mode = CAMQP_DM_VOLATILE;
750 conf->store_rates = 0;
751 /* subscribe only */
752 conf->exchange_type = NULL;
753 conf->queue = NULL;
754 /* general */
755 conf->connection = NULL;
756 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
757 /* }}} */
759 status = cf_util_get_string (ci, &conf->name);
760 if (status != 0)
761 {
762 sfree (conf);
763 return (status);
764 }
766 for (i = 0; i < ci->children_num; i++)
767 {
768 oconfig_item_t *child = ci->children + i;
770 if (strcasecmp ("Format", child->key) == 0)
771 status = camqp_config_set_format (child, conf);
772 else if (strcasecmp ("Host", child->key) == 0)
773 status = cf_util_get_string (child, &conf->host);
774 else if (strcasecmp ("Port", child->key) == 0)
775 {
776 status = cf_util_get_port_number (child);
777 if (status > 0)
778 {
779 conf->port = status;
780 status = 0;
781 }
782 }
783 else if (strcasecmp ("VHost", child->key) == 0)
784 status = cf_util_get_string (child, &conf->vhost);
785 else if (strcasecmp ("User", child->key) == 0)
786 status = cf_util_get_string (child, &conf->user);
787 else if (strcasecmp ("Password", child->key) == 0)
788 status = cf_util_get_string (child, &conf->password);
789 else if (strcasecmp ("Exchange", child->key) == 0)
790 status = cf_util_get_string (child, &conf->exchange);
791 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
792 status = cf_util_get_string (child, &conf->exchange_type);
793 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
794 status = cf_util_get_string (child, &conf->queue);
795 else if (strcasecmp ("RoutingKey", child->key) == 0)
796 status = cf_util_get_string (child, &conf->routingkey);
797 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
798 {
799 _Bool tmp = 0;
800 status = cf_util_get_boolean (child, &tmp);
801 if (tmp)
802 conf->delivery_mode = CAMQP_DM_PERSISTENT;
803 else
804 conf->delivery_mode = CAMQP_DM_VOLATILE;
805 }
806 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
807 status = cf_util_get_boolean (child, &conf->store_rates);
808 else
809 WARNING ("amqp plugin: Ignoring unknown "
810 "configuration option \"%s\".", child->key);
812 if (status != 0)
813 break;
814 } /* for (i = 0; i < ci->children_num; i++) */
816 if ((status == 0) && !publish && (conf->exchange == NULL))
817 {
818 if (conf->routingkey != NULL)
819 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
820 "without the \"Exchange\" option. It will be ignored.");
822 if (conf->exchange_type != NULL)
823 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
824 "without the \"Exchange\" option. It will be ignored.");
825 }
827 if (status != 0)
828 {
829 camqp_config_free (conf);
830 return (status);
831 }
833 if (conf->exchange != NULL)
834 {
835 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
836 conf->exchange);
837 }
839 if (publish)
840 {
841 char cbname[128];
842 user_data_t ud = { conf, camqp_config_free };
844 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
846 status = plugin_register_write (cbname, camqp_write, &ud);
847 if (status != 0)
848 {
849 camqp_config_free (conf);
850 return (status);
851 }
852 }
853 else
854 {
855 status = camqp_subscribe_init (conf);
856 if (status != 0)
857 {
858 camqp_config_free (conf);
859 return (status);
860 }
861 }
863 return (0);
864 } /* }}} int camqp_config_connection */
866 static int camqp_config (oconfig_item_t *ci) /* {{{ */
867 {
868 int i;
870 for (i = 0; i < ci->children_num; i++)
871 {
872 oconfig_item_t *child = ci->children + i;
874 if (strcasecmp ("Publish", child->key) == 0)
875 camqp_config_connection (child, /* publish = */ 1);
876 else if (strcasecmp ("Subscribe", child->key) == 0)
877 camqp_config_connection (child, /* publish = */ 0);
878 else
879 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
880 child->key);
881 } /* for (ci->children_num) */
883 return (0);
884 } /* }}} int camqp_config */
886 void module_register (void)
887 {
888 plugin_register_complex_config ("amqp", camqp_config);
889 plugin_register_shutdown ("amqp", shutdown);
890 } /* void module_register */
892 /* vim: set sw=4 sts=4 et fdm=marker : */