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;
62 char *host;
63 int port;
64 char *vhost;
65 char *user;
66 char *password;
68 char *exchange;
69 char *routing_key;
71 /* publish only */
72 uint8_t delivery_mode;
73 _Bool store_rates;
74 int format;
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";
94 static pthread_t *subscriber_threads = NULL;
95 static size_t subscriber_threads_num = 0;
96 static _Bool subscriber_threads_running = 1;
98 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
100 /*
101 * Functions
102 */
103 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
104 {
105 int sockfd;
107 if ((conf == NULL) || (conf->connection == NULL))
108 return;
110 sockfd = amqp_get_sockfd (conf->connection);
111 amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
112 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
113 amqp_destroy_connection (conf->connection);
114 close (sockfd);
115 conf->connection = NULL;
116 } /* }}} void camqp_close_connection */
118 static void camqp_config_free (void *ptr) /* {{{ */
119 {
120 camqp_config_t *conf = ptr;
122 if (conf == NULL)
123 return;
125 camqp_close_connection (conf);
127 sfree (conf->name);
128 sfree (conf->host);
129 sfree (conf->vhost);
130 sfree (conf->user);
131 sfree (conf->password);
132 sfree (conf->exchange);
133 sfree (conf->exchange_type);
134 sfree (conf->queue);
135 sfree (conf->routing_key);
137 sfree (conf);
138 } /* }}} void camqp_config_free */
140 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
141 {
142 char *ret;
144 if ((in == NULL) || (in->bytes == NULL))
145 return (NULL);
147 ret = malloc (in->len + 1);
148 if (ret == NULL)
149 return (NULL);
151 memcpy (ret, in->bytes, in->len);
152 ret[in->len] = 0;
154 return (ret);
155 } /* }}} char *camqp_bytes_cstring */
157 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
158 {
159 amqp_rpc_reply_t r;
161 r = amqp_get_rpc_reply (conf->connection);
162 if (r.reply_type == AMQP_RESPONSE_NORMAL)
163 return (0);
165 return (1);
166 } /* }}} _Bool camqp_is_error */
168 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
169 char *buffer, size_t buffer_size)
170 {
171 amqp_rpc_reply_t r;
173 r = amqp_get_rpc_reply (conf->connection);
174 switch (r.reply_type)
175 {
176 case AMQP_RESPONSE_NORMAL:
177 sstrncpy (buffer, "Success", sizeof (buffer));
178 break;
180 case AMQP_RESPONSE_NONE:
181 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
182 break;
184 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
185 if (r.library_errno)
186 return (sstrerror (r.library_errno, buffer, buffer_size));
187 else
188 sstrncpy (buffer, "End of stream", sizeof (buffer));
189 break;
191 case AMQP_RESPONSE_SERVER_EXCEPTION:
192 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
193 {
194 amqp_connection_close_t *m = r.reply.decoded;
195 char *tmp = camqp_bytes_cstring (&m->reply_text);
196 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
197 m->reply_code, tmp);
198 sfree (tmp);
199 }
200 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
201 {
202 amqp_channel_close_t *m = r.reply.decoded;
203 char *tmp = camqp_bytes_cstring (&m->reply_text);
204 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
205 m->reply_code, tmp);
206 sfree (tmp);
207 }
208 else
209 {
210 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
211 r.reply.id);
212 }
213 break;
215 default:
216 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
217 (int) r.reply_type);
218 }
220 return (buffer);
221 } /* }}} char *camqp_strerror */
223 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
224 {
225 amqp_queue_declare_ok_t *qd_ret;
226 amqp_basic_consume_ok_t *cm_ret;
228 qd_ret = amqp_queue_declare (conf->connection,
229 /* channel = */ CAMQP_CHANNEL,
230 /* queue = */ (conf->queue != NULL)
231 ? amqp_cstring_bytes (conf->queue)
232 : AMQP_EMPTY_BYTES,
233 /* passive = */ 0,
234 /* durable = */ 0,
235 /* exclusive = */ 0,
236 /* auto_delete = */ 1,
237 /* arguments = */ AMQP_EMPTY_TABLE);
238 if (qd_ret == NULL)
239 {
240 ERROR ("amqp plugin: amqp_queue_declare failed.");
241 camqp_close_connection (conf);
242 return (-1);
243 }
245 if (conf->queue == NULL)
246 {
247 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
248 if (conf->queue == NULL)
249 {
250 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
251 camqp_close_connection (conf);
252 return (-1);
253 }
255 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
256 }
257 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
259 /* bind to an exchange */
260 if (conf->exchange != NULL)
261 {
262 amqp_queue_bind_ok_t *qb_ret;
264 /* create the exchange */
265 if (conf->exchange_type != NULL)
266 {
267 amqp_exchange_declare_ok_t *ed_ret;
269 ed_ret = amqp_exchange_declare (conf->connection,
270 /* channel = */ CAMQP_CHANNEL,
271 /* exchange = */ amqp_cstring_bytes (conf->exchange),
272 /* type = */ amqp_cstring_bytes (conf->exchange_type),
273 /* passive = */ 0,
274 /* durable = */ 0,
275 /* auto_delete = */ 1,
276 /* arguments = */ AMQP_EMPTY_TABLE);
277 if ((ed_ret == NULL) && camqp_is_error (conf))
278 {
279 char errbuf[1024];
280 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
281 camqp_strerror (conf, errbuf, sizeof (errbuf)));
282 camqp_close_connection (conf);
283 return (-1);
284 }
285 }
287 assert (conf->queue != NULL);
288 qb_ret = amqp_queue_bind (conf->connection,
289 /* channel = */ CAMQP_CHANNEL,
290 /* queue = */ amqp_cstring_bytes (conf->queue),
291 /* exchange = */ amqp_cstring_bytes (conf->exchange),
292 /* routing_key = */ (conf->routing_key != NULL)
293 ? amqp_cstring_bytes (conf->routing_key)
294 : AMQP_EMPTY_BYTES,
295 /* arguments = */ AMQP_EMPTY_TABLE);
296 if ((qb_ret == NULL) && camqp_is_error (conf))
297 {
298 char errbuf[1024];
299 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
300 camqp_strerror (conf, errbuf, sizeof (errbuf)));
301 camqp_close_connection (conf);
302 return (-1);
303 }
305 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
306 conf->queue, conf->exchange);
307 } /* if (conf->exchange != NULL) */
309 cm_ret = amqp_basic_consume (conf->connection,
310 /* channel = */ CAMQP_CHANNEL,
311 /* queue = */ amqp_cstring_bytes (conf->queue),
312 /* consumer_tag = */ AMQP_EMPTY_BYTES,
313 /* no_local = */ 0,
314 /* no_ack = */ 1,
315 /* exclusive = */ 0);
316 if ((cm_ret == NULL) && camqp_is_error (conf))
317 {
318 char errbuf[1024];
319 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
320 camqp_strerror (conf, errbuf, sizeof (errbuf)));
321 camqp_close_connection (conf);
322 return (-1);
323 }
325 return (0);
326 } /* }}} int camqp_setup_queue */
328 static int camqp_connect (camqp_config_t *conf) /* {{{ */
329 {
330 amqp_rpc_reply_t reply;
331 int sockfd;
332 int status;
334 if (conf->connection != NULL)
335 return (0);
337 conf->connection = amqp_new_connection ();
338 if (conf->connection == NULL)
339 {
340 ERROR ("amqp plugin: amqp_new_connection failed.");
341 return (ENOMEM);
342 }
344 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
345 if (sockfd < 0)
346 {
347 char errbuf[1024];
348 status = (-1) * sockfd;
349 ERROR ("amqp plugin: amqp_open_socket failed: %s",
350 sstrerror (status, errbuf, sizeof (errbuf)));
351 amqp_destroy_connection (conf->connection);
352 conf->connection = NULL;
353 return (status);
354 }
355 amqp_set_sockfd (conf->connection, sockfd);
357 reply = amqp_login (conf->connection, CONF(conf, vhost),
358 /* channel max = */ 0,
359 /* frame max = */ 131072,
360 /* heartbeat = */ 0,
361 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
362 CONF(conf, user), CONF(conf, password));
363 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
364 {
365 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
366 CONF(conf, vhost), CONF(conf, user));
367 amqp_destroy_connection (conf->connection);
368 close (sockfd);
369 conf->connection = NULL;
370 return (1);
371 }
373 amqp_channel_open (conf->connection, /* channel = */ 1);
374 /* FIXME: Is checking "reply.reply_type" really correct here? How does
375 * it get set? --octo */
376 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
377 {
378 ERROR ("amqp plugin: amqp_channel_open failed.");
379 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
380 amqp_destroy_connection (conf->connection);
381 close(sockfd);
382 conf->connection = NULL;
383 return (1);
384 }
386 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
387 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
389 if (!conf->publish)
390 return (camqp_setup_queue (conf));
391 return (0);
392 } /* }}} int camqp_connect */
394 static int shutdown (void) /* {{{ */
395 {
396 size_t i;
398 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
399 subscriber_threads_num);
401 subscriber_threads_running = 0;
402 for (i = 0; i < subscriber_threads_num; i++)
403 {
404 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
405 * to use a timeout in the thread and check for the variable in regular
406 * intervals. */
407 pthread_kill (subscriber_threads[i], SIGTERM);
408 pthread_join (subscriber_threads[i], /* retval = */ NULL);
409 }
411 subscriber_threads_num = 0;
412 sfree (subscriber_threads);
414 DEBUG ("amqp plugin: All subscriber threads exited.");
416 return (0);
417 } /* }}} int shutdown */
419 /*
420 * Subscribing code
421 */
422 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
423 size_t body_size, const char *content_type)
424 {
425 char body[body_size + 1];
426 char *body_ptr;
427 size_t received;
428 amqp_frame_t frame;
429 int status;
431 memset (body, 0, sizeof (body));
432 body_ptr = &body[0];
433 received = 0;
435 while (received < body_size)
436 {
437 status = amqp_simple_wait_frame (conf->connection, &frame);
438 if (status < 0)
439 {
440 char errbuf[1024];
441 status = (-1) * status;
442 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
443 sstrerror (status, errbuf, sizeof (errbuf)));
444 camqp_close_connection (conf);
445 return (status);
446 }
448 if (frame.frame_type != AMQP_FRAME_BODY)
449 {
450 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
451 frame.frame_type);
452 return (-1);
453 }
455 if ((body_size - received) < frame.payload.body_fragment.len)
456 {
457 WARNING ("amqp plugin: Body is larger than indicated by header.");
458 return (-1);
459 }
461 memcpy (body_ptr, frame.payload.body_fragment.bytes,
462 frame.payload.body_fragment.len);
463 body_ptr += frame.payload.body_fragment.len;
464 received += frame.payload.body_fragment.len;
465 } /* while (received < body_size) */
467 if (strcasecmp ("text/collectd", content_type) == 0)
468 {
469 status = handle_putval (stderr, body);
470 if (status != 0)
471 ERROR ("amqp plugin: handle_putval failed with status %i.",
472 status);
473 return (status);
474 }
475 else if (strcasecmp ("application/json", content_type) == 0)
476 {
477 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
478 "been implemented yet. FIXME!");
479 return (0);
480 }
481 else
482 {
483 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
484 content_type);
485 return (EINVAL);
486 }
488 /* not reached */
489 return (0);
490 } /* }}} int camqp_read_body */
492 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
493 {
494 int status;
495 amqp_frame_t frame;
496 amqp_basic_properties_t *properties;
497 char *content_type;
499 status = amqp_simple_wait_frame (conf->connection, &frame);
500 if (status < 0)
501 {
502 char errbuf[1024];
503 status = (-1) * status;
504 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
505 sstrerror (status, errbuf, sizeof (errbuf)));
506 camqp_close_connection (conf);
507 return (status);
508 }
510 if (frame.frame_type != AMQP_FRAME_HEADER)
511 {
512 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
513 frame.frame_type);
514 return (-1);
515 }
517 properties = frame.payload.properties.decoded;
518 content_type = camqp_bytes_cstring (&properties->content_type);
519 if (content_type == NULL)
520 {
521 ERROR ("amqp plugin: Unable to determine content type.");
522 return (-1);
523 }
525 status = camqp_read_body (conf,
526 (size_t) frame.payload.properties.body_size,
527 content_type);
529 sfree (content_type);
530 return (status);
531 } /* }}} int camqp_read_header */
533 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
534 {
535 camqp_config_t *conf = user_data;
536 int status;
538 while (subscriber_threads_running)
539 {
540 amqp_frame_t frame;
542 status = camqp_connect (conf);
543 if (status != 0)
544 {
545 ERROR ("amqp plugin: camqp_connect failed. "
546 "Will sleep for %i seconds.", interval_g);
547 sleep (interval_g);
548 continue;
549 }
551 status = amqp_simple_wait_frame (conf->connection, &frame);
552 if (status < 0)
553 {
554 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
555 "Will sleep for %i seconds.", interval_g);
556 camqp_close_connection (conf);
557 sleep (interval_g);
558 continue;
559 }
561 if (frame.frame_type != AMQP_FRAME_METHOD)
562 {
563 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
564 frame.frame_type);
565 continue;
566 }
568 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
569 {
570 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
571 frame.payload.method.id);
572 continue;
573 }
575 status = camqp_read_header (conf);
577 amqp_maybe_release_buffers (conf->connection);
578 } /* while (subscriber_threads_running) */
580 camqp_config_free (conf);
581 pthread_exit (NULL);
582 } /* }}} void *camqp_subscribe_thread */
584 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
585 {
586 int status;
587 pthread_t *tmp;
589 tmp = realloc (subscriber_threads,
590 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
591 if (tmp == NULL)
592 {
593 ERROR ("amqp plugin: realloc failed.");
594 camqp_config_free (conf);
595 return (ENOMEM);
596 }
597 subscriber_threads = tmp;
598 tmp = subscriber_threads + subscriber_threads_num;
599 memset (tmp, 0, sizeof (*tmp));
601 status = pthread_create (tmp, /* attr = */ NULL,
602 camqp_subscribe_thread, conf);
603 if (status != 0)
604 {
605 char errbuf[1024];
606 ERROR ("amqp plugin: pthread_create failed: %s",
607 sstrerror (status, errbuf, sizeof (errbuf)));
608 camqp_config_free (conf);
609 return (status);
610 }
612 subscriber_threads_num++;
614 return (0);
615 } /* }}} int camqp_subscribe_init */
617 /*
618 * Publishing code
619 */
620 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
621 const char *buffer, const char *routing_key)
622 {
623 amqp_basic_properties_t props;
624 int status;
626 status = camqp_connect (conf);
627 if (status != 0)
628 return (status);
630 memset (&props, 0, sizeof (props));
631 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
632 | AMQP_BASIC_DELIVERY_MODE_FLAG
633 | AMQP_BASIC_APP_ID_FLAG;
634 if (conf->format == CAMQP_FORMAT_COMMAND)
635 props.content_type = amqp_cstring_bytes("text/collectd");
636 else if (conf->format == CAMQP_FORMAT_JSON)
637 props.content_type = amqp_cstring_bytes("application/json");
638 else
639 assert (23 == 42);
640 props.delivery_mode = conf->delivery_mode;
641 props.app_id = amqp_cstring_bytes("collectd");
643 status = amqp_basic_publish(conf->connection,
644 /* channel = */ 1,
645 amqp_cstring_bytes(CONF(conf, exchange)),
646 amqp_cstring_bytes (routing_key),
647 /* mandatory = */ 0,
648 /* immediate = */ 0,
649 &props,
650 amqp_cstring_bytes(buffer));
651 if (status != 0)
652 {
653 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
654 status);
655 camqp_close_connection (conf);
656 }
658 return (status);
659 } /* }}} int camqp_write_locked */
661 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
662 user_data_t *user_data)
663 {
664 camqp_config_t *conf = user_data->data;
665 char routing_key[6 * DATA_MAX_NAME_LEN];
666 char buffer[4096];
667 int status;
669 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
670 return (EINVAL);
672 memset (buffer, 0, sizeof (buffer));
674 if (conf->routing_key != NULL)
675 {
676 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
677 }
678 else
679 {
680 size_t i;
681 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
682 vl->host,
683 vl->plugin, vl->plugin_instance,
684 vl->type, vl->type_instance);
686 /* Switch slashes (the only character forbidden by collectd) and dots
687 * (the separation character used by AMQP). */
688 for (i = 0; routing_key[i] != 0; i++)
689 {
690 if (routing_key[i] == '.')
691 routing_key[i] = '/';
692 else if (routing_key[i] == '/')
693 routing_key[i] = '.';
694 }
695 }
697 if (conf->format == CAMQP_FORMAT_COMMAND)
698 {
699 status = create_putval (buffer, sizeof (buffer), ds, vl);
700 if (status != 0)
701 {
702 ERROR ("amqp plugin: create_putval failed with status %i.",
703 status);
704 return (status);
705 }
706 }
707 else if (conf->format == CAMQP_FORMAT_JSON)
708 {
709 size_t bfree = sizeof (buffer);
710 size_t bfill = 0;
712 format_json_initialize (buffer, &bfill, &bfree);
713 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
714 format_json_finalize (buffer, &bfill, &bfree);
715 }
716 else
717 {
718 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
719 return (-1);
720 }
722 pthread_mutex_lock (&conf->lock);
723 status = camqp_write_locked (conf, buffer, routing_key);
724 pthread_mutex_unlock (&conf->lock);
726 return (status);
727 } /* }}} int camqp_write */
729 /*
730 * Config handling
731 */
732 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
733 camqp_config_t *conf)
734 {
735 char *string;
736 int status;
738 string = NULL;
739 status = cf_util_get_string (ci, &string);
740 if (status != 0)
741 return (status);
743 assert (string != NULL);
744 if (strcasecmp ("Command", string) == 0)
745 conf->format = CAMQP_FORMAT_COMMAND;
746 else if (strcasecmp ("JSON", string) == 0)
747 conf->format = CAMQP_FORMAT_JSON;
748 else
749 {
750 WARNING ("amqp plugin: Invalid format string: %s",
751 string);
752 }
754 free (string);
756 return (0);
757 } /* }}} int config_set_string */
759 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
760 _Bool publish)
761 {
762 camqp_config_t *conf;
763 int status;
764 int i;
766 conf = malloc (sizeof (*conf));
767 if (conf == NULL)
768 {
769 ERROR ("amqp plugin: malloc failed.");
770 return (ENOMEM);
771 }
773 /* Initialize "conf" {{{ */
774 memset (conf, 0, sizeof (*conf));
775 conf->publish = publish;
776 conf->name = NULL;
777 conf->format = CAMQP_FORMAT_COMMAND;
778 conf->host = NULL;
779 conf->port = 5672;
780 conf->vhost = NULL;
781 conf->user = NULL;
782 conf->password = NULL;
783 conf->exchange = NULL;
784 conf->routing_key = NULL;
785 /* publish only */
786 conf->delivery_mode = CAMQP_DM_VOLATILE;
787 conf->store_rates = 0;
788 /* subscribe only */
789 conf->exchange_type = NULL;
790 conf->queue = NULL;
791 /* general */
792 conf->connection = NULL;
793 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
794 /* }}} */
796 status = cf_util_get_string (ci, &conf->name);
797 if (status != 0)
798 {
799 sfree (conf);
800 return (status);
801 }
803 for (i = 0; i < ci->children_num; i++)
804 {
805 oconfig_item_t *child = ci->children + i;
807 if (strcasecmp ("Host", child->key) == 0)
808 status = cf_util_get_string (child, &conf->host);
809 else if (strcasecmp ("Port", child->key) == 0)
810 {
811 status = cf_util_get_port_number (child);
812 if (status > 0)
813 {
814 conf->port = status;
815 status = 0;
816 }
817 }
818 else if (strcasecmp ("VHost", child->key) == 0)
819 status = cf_util_get_string (child, &conf->vhost);
820 else if (strcasecmp ("User", child->key) == 0)
821 status = cf_util_get_string (child, &conf->user);
822 else if (strcasecmp ("Password", child->key) == 0)
823 status = cf_util_get_string (child, &conf->password);
824 else if (strcasecmp ("Exchange", child->key) == 0)
825 status = cf_util_get_string (child, &conf->exchange);
826 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
827 status = cf_util_get_string (child, &conf->exchange_type);
828 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
829 status = cf_util_get_string (child, &conf->queue);
830 else if (strcasecmp ("RoutingKey", child->key) == 0)
831 status = cf_util_get_string (child, &conf->routing_key);
832 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
833 {
834 _Bool tmp = 0;
835 status = cf_util_get_boolean (child, &tmp);
836 if (tmp)
837 conf->delivery_mode = CAMQP_DM_PERSISTENT;
838 else
839 conf->delivery_mode = CAMQP_DM_VOLATILE;
840 }
841 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
842 status = cf_util_get_boolean (child, &conf->store_rates);
843 else if ((strcasecmp ("Format", child->key) == 0) && publish)
844 status = camqp_config_set_format (child, conf);
845 else
846 WARNING ("amqp plugin: Ignoring unknown "
847 "configuration option \"%s\".", child->key);
849 if (status != 0)
850 break;
851 } /* for (i = 0; i < ci->children_num; i++) */
853 if ((status == 0) && !publish && (conf->exchange == NULL))
854 {
855 if (conf->routing_key != NULL)
856 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
857 "without the \"Exchange\" option. It will be ignored.");
859 if (conf->exchange_type != NULL)
860 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
861 "without the \"Exchange\" option. It will be ignored.");
862 }
864 if (status != 0)
865 {
866 camqp_config_free (conf);
867 return (status);
868 }
870 if (conf->exchange != NULL)
871 {
872 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
873 conf->exchange);
874 }
876 if (publish)
877 {
878 char cbname[128];
879 user_data_t ud = { conf, camqp_config_free };
881 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
883 status = plugin_register_write (cbname, camqp_write, &ud);
884 if (status != 0)
885 {
886 camqp_config_free (conf);
887 return (status);
888 }
889 }
890 else
891 {
892 status = camqp_subscribe_init (conf);
893 if (status != 0)
894 {
895 camqp_config_free (conf);
896 return (status);
897 }
898 }
900 return (0);
901 } /* }}} int camqp_config_connection */
903 static int camqp_config (oconfig_item_t *ci) /* {{{ */
904 {
905 int i;
907 for (i = 0; i < ci->children_num; i++)
908 {
909 oconfig_item_t *child = ci->children + i;
911 if (strcasecmp ("Publish", child->key) == 0)
912 camqp_config_connection (child, /* publish = */ 1);
913 else if (strcasecmp ("Subscribe", child->key) == 0)
914 camqp_config_connection (child, /* publish = */ 0);
915 else
916 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
917 child->key);
918 } /* for (ci->children_num) */
920 return (0);
921 } /* }}} int camqp_config */
923 void module_register (void)
924 {
925 plugin_register_complex_config ("amqp", camqp_config);
926 plugin_register_shutdown ("amqp", shutdown);
927 } /* void module_register */
929 /* vim: set sw=4 sts=4 et fdm=marker : */