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 "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "utils_cmd_putval.h"
33 #include "utils_format_json.h"
35 #include <pthread.h>
37 #include <amqp.h>
38 #include <amqp_framing.h>
40 /* Defines for the delivery mode. I have no idea why they're not defined by the
41 * library.. */
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
50 /*
51 * Data types
52 */
53 struct camqp_config_s
54 {
55 _Bool publish;
56 char *name;
58 char *host;
59 int port;
60 char *vhost;
61 char *user;
62 char *password;
64 char *exchange;
65 char *routing_key;
67 /* publish only */
68 uint8_t delivery_mode;
69 _Bool store_rates;
70 int format;
72 /* subscribe only */
73 char *exchange_type;
74 char *queue;
76 amqp_connection_state_t connection;
77 pthread_mutex_t lock;
78 };
79 typedef struct camqp_config_s camqp_config_t;
81 /*
82 * Global variables
83 */
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)
96 /*
97 * Functions
98 */
99 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
100 {
101 int sockfd;
103 if ((conf == NULL) || (conf->connection == NULL))
104 return;
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);
110 close (sockfd);
111 conf->connection = NULL;
112 } /* }}} void camqp_close_connection */
114 static void camqp_config_free (void *ptr) /* {{{ */
115 {
116 camqp_config_t *conf = ptr;
118 if (conf == NULL)
119 return;
121 camqp_close_connection (conf);
123 sfree (conf->name);
124 sfree (conf->host);
125 sfree (conf->vhost);
126 sfree (conf->user);
127 sfree (conf->password);
128 sfree (conf->exchange);
129 sfree (conf->exchange_type);
130 sfree (conf->queue);
131 sfree (conf->routing_key);
133 sfree (conf);
134 } /* }}} void camqp_config_free */
136 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
137 {
138 char *ret;
140 if ((in == NULL) || (in->bytes == NULL))
141 return (NULL);
143 ret = malloc (in->len + 1);
144 if (ret == NULL)
145 return (NULL);
147 memcpy (ret, in->bytes, in->len);
148 ret[in->len] = 0;
150 return (ret);
151 } /* }}} char *camqp_bytes_cstring */
153 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
154 {
155 amqp_rpc_reply_t r;
157 r = amqp_get_rpc_reply (conf->connection);
158 if (r.reply_type == AMQP_RESPONSE_NORMAL)
159 return (0);
161 return (1);
162 } /* }}} _Bool camqp_is_error */
164 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
165 char *buffer, size_t buffer_size)
166 {
167 amqp_rpc_reply_t r;
169 r = amqp_get_rpc_reply (conf->connection);
170 switch (r.reply_type)
171 {
172 case AMQP_RESPONSE_NORMAL:
173 sstrncpy (buffer, "Success", sizeof (buffer));
174 break;
176 case AMQP_RESPONSE_NONE:
177 sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
178 break;
180 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
181 if (r.library_errno)
182 return (sstrerror (r.library_errno, buffer, buffer_size));
183 else
184 sstrncpy (buffer, "End of stream", sizeof (buffer));
185 break;
187 case AMQP_RESPONSE_SERVER_EXCEPTION:
188 if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
189 {
190 amqp_connection_close_t *m = r.reply.decoded;
191 char *tmp = camqp_bytes_cstring (&m->reply_text);
192 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
193 m->reply_code, tmp);
194 sfree (tmp);
195 }
196 else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
197 {
198 amqp_channel_close_t *m = r.reply.decoded;
199 char *tmp = camqp_bytes_cstring (&m->reply_text);
200 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
201 m->reply_code, tmp);
202 sfree (tmp);
203 }
204 else
205 {
206 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
207 r.reply.id);
208 }
209 break;
211 default:
212 ssnprintf (buffer, buffer_size, "Unknown reply type %i",
213 (int) r.reply_type);
214 }
216 return (buffer);
217 } /* }}} char *camqp_strerror */
219 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
220 {
221 amqp_exchange_declare_ok_t *ed_ret;
223 if (conf->exchange_type == NULL)
224 return (0);
226 ed_ret = amqp_exchange_declare (conf->connection,
227 /* channel = */ CAMQP_CHANNEL,
228 /* exchange = */ amqp_cstring_bytes (conf->exchange),
229 /* type = */ amqp_cstring_bytes (conf->exchange_type),
230 /* passive = */ 0,
231 /* durable = */ 0,
232 /* auto_delete = */ 1,
233 /* arguments = */ AMQP_EMPTY_TABLE);
234 if ((ed_ret == NULL) && camqp_is_error (conf))
235 {
236 char errbuf[1024];
237 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
238 camqp_strerror (conf, errbuf, sizeof (errbuf)));
239 camqp_close_connection (conf);
240 return (-1);
241 }
243 INFO ("amqp plugin: Successfully created exchange \"%s\" "
244 "with type \"%s\".",
245 conf->exchange, conf->exchange_type);
247 return (0);
248 } /* }}} int camqp_create_exchange */
250 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
251 {
252 amqp_queue_declare_ok_t *qd_ret;
253 amqp_basic_consume_ok_t *cm_ret;
255 qd_ret = amqp_queue_declare (conf->connection,
256 /* channel = */ CAMQP_CHANNEL,
257 /* queue = */ (conf->queue != NULL)
258 ? amqp_cstring_bytes (conf->queue)
259 : AMQP_EMPTY_BYTES,
260 /* passive = */ 0,
261 /* durable = */ 0,
262 /* exclusive = */ 0,
263 /* auto_delete = */ 1,
264 /* arguments = */ AMQP_EMPTY_TABLE);
265 if (qd_ret == NULL)
266 {
267 ERROR ("amqp plugin: amqp_queue_declare failed.");
268 camqp_close_connection (conf);
269 return (-1);
270 }
272 if (conf->queue == NULL)
273 {
274 conf->queue = camqp_bytes_cstring (&qd_ret->queue);
275 if (conf->queue == NULL)
276 {
277 ERROR ("amqp plugin: camqp_bytes_cstring failed.");
278 camqp_close_connection (conf);
279 return (-1);
280 }
282 INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
283 }
284 DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
286 /* bind to an exchange */
287 if (conf->exchange != NULL)
288 {
289 amqp_queue_bind_ok_t *qb_ret;
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 /* routing_key = */ (conf->routing_key != NULL)
297 ? amqp_cstring_bytes (conf->routing_key)
298 : AMQP_EMPTY_BYTES,
299 /* arguments = */ AMQP_EMPTY_TABLE);
300 if ((qb_ret == NULL) && camqp_is_error (conf))
301 {
302 char errbuf[1024];
303 ERROR ("amqp plugin: amqp_queue_bind failed: %s",
304 camqp_strerror (conf, errbuf, sizeof (errbuf)));
305 camqp_close_connection (conf);
306 return (-1);
307 }
309 DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
310 conf->queue, conf->exchange);
311 } /* if (conf->exchange != NULL) */
313 cm_ret = amqp_basic_consume (conf->connection,
314 /* channel = */ CAMQP_CHANNEL,
315 /* queue = */ amqp_cstring_bytes (conf->queue),
316 /* consumer_tag = */ AMQP_EMPTY_BYTES,
317 /* no_local = */ 0,
318 /* no_ack = */ 1,
319 /* exclusive = */ 0);
320 if ((cm_ret == NULL) && camqp_is_error (conf))
321 {
322 char errbuf[1024];
323 ERROR ("amqp plugin: amqp_basic_consume failed: %s",
324 camqp_strerror (conf, errbuf, sizeof (errbuf)));
325 camqp_close_connection (conf);
326 return (-1);
327 }
329 return (0);
330 } /* }}} int camqp_setup_queue */
332 static int camqp_connect (camqp_config_t *conf) /* {{{ */
333 {
334 amqp_rpc_reply_t reply;
335 int sockfd;
336 int status;
338 if (conf->connection != NULL)
339 return (0);
341 conf->connection = amqp_new_connection ();
342 if (conf->connection == NULL)
343 {
344 ERROR ("amqp plugin: amqp_new_connection failed.");
345 return (ENOMEM);
346 }
348 sockfd = amqp_open_socket (CONF(conf, host), conf->port);
349 if (sockfd < 0)
350 {
351 char errbuf[1024];
352 status = (-1) * sockfd;
353 ERROR ("amqp plugin: amqp_open_socket failed: %s",
354 sstrerror (status, errbuf, sizeof (errbuf)));
355 amqp_destroy_connection (conf->connection);
356 conf->connection = NULL;
357 return (status);
358 }
359 amqp_set_sockfd (conf->connection, sockfd);
361 reply = amqp_login (conf->connection, CONF(conf, vhost),
362 /* channel max = */ 0,
363 /* frame max = */ 131072,
364 /* heartbeat = */ 0,
365 /* authentication = */ AMQP_SASL_METHOD_PLAIN,
366 CONF(conf, user), CONF(conf, password));
367 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
368 {
369 ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
370 CONF(conf, vhost), CONF(conf, user));
371 amqp_destroy_connection (conf->connection);
372 close (sockfd);
373 conf->connection = NULL;
374 return (1);
375 }
377 amqp_channel_open (conf->connection, /* channel = */ 1);
378 /* FIXME: Is checking "reply.reply_type" really correct here? How does
379 * it get set? --octo */
380 if (reply.reply_type != AMQP_RESPONSE_NORMAL)
381 {
382 ERROR ("amqp plugin: amqp_channel_open failed.");
383 amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
384 amqp_destroy_connection (conf->connection);
385 close(sockfd);
386 conf->connection = NULL;
387 return (1);
388 }
390 INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
391 "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
393 status = camqp_create_exchange (conf);
394 if (status != 0)
395 return (status);
397 if (!conf->publish)
398 return (camqp_setup_queue (conf));
399 return (0);
400 } /* }}} int camqp_connect */
402 static int camqp_shutdown (void) /* {{{ */
403 {
404 size_t i;
406 DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
407 subscriber_threads_num);
409 subscriber_threads_running = 0;
410 for (i = 0; i < subscriber_threads_num; i++)
411 {
412 /* FIXME: Sending a signal is not very elegant here. Maybe find out how
413 * to use a timeout in the thread and check for the variable in regular
414 * intervals. */
415 pthread_kill (subscriber_threads[i], SIGTERM);
416 pthread_join (subscriber_threads[i], /* retval = */ NULL);
417 }
419 subscriber_threads_num = 0;
420 sfree (subscriber_threads);
422 DEBUG ("amqp plugin: All subscriber threads exited.");
424 return (0);
425 } /* }}} int camqp_shutdown */
427 /*
428 * Subscribing code
429 */
430 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
431 size_t body_size, const char *content_type)
432 {
433 char body[body_size + 1];
434 char *body_ptr;
435 size_t received;
436 amqp_frame_t frame;
437 int status;
439 memset (body, 0, sizeof (body));
440 body_ptr = &body[0];
441 received = 0;
443 while (received < body_size)
444 {
445 status = amqp_simple_wait_frame (conf->connection, &frame);
446 if (status < 0)
447 {
448 char errbuf[1024];
449 status = (-1) * status;
450 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
451 sstrerror (status, errbuf, sizeof (errbuf)));
452 camqp_close_connection (conf);
453 return (status);
454 }
456 if (frame.frame_type != AMQP_FRAME_BODY)
457 {
458 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
459 frame.frame_type);
460 return (-1);
461 }
463 if ((body_size - received) < frame.payload.body_fragment.len)
464 {
465 WARNING ("amqp plugin: Body is larger than indicated by header.");
466 return (-1);
467 }
469 memcpy (body_ptr, frame.payload.body_fragment.bytes,
470 frame.payload.body_fragment.len);
471 body_ptr += frame.payload.body_fragment.len;
472 received += frame.payload.body_fragment.len;
473 } /* while (received < body_size) */
475 if (strcasecmp ("text/collectd", content_type) == 0)
476 {
477 status = handle_putval (stderr, body);
478 if (status != 0)
479 ERROR ("amqp plugin: handle_putval failed with status %i.",
480 status);
481 return (status);
482 }
483 else if (strcasecmp ("application/json", content_type) == 0)
484 {
485 ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
486 "been implemented yet. FIXME!");
487 return (0);
488 }
489 else
490 {
491 ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
492 content_type);
493 return (EINVAL);
494 }
496 /* not reached */
497 return (0);
498 } /* }}} int camqp_read_body */
500 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
501 {
502 int status;
503 amqp_frame_t frame;
504 amqp_basic_properties_t *properties;
505 char *content_type;
507 status = amqp_simple_wait_frame (conf->connection, &frame);
508 if (status < 0)
509 {
510 char errbuf[1024];
511 status = (-1) * status;
512 ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
513 sstrerror (status, errbuf, sizeof (errbuf)));
514 camqp_close_connection (conf);
515 return (status);
516 }
518 if (frame.frame_type != AMQP_FRAME_HEADER)
519 {
520 NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
521 frame.frame_type);
522 return (-1);
523 }
525 properties = frame.payload.properties.decoded;
526 content_type = camqp_bytes_cstring (&properties->content_type);
527 if (content_type == NULL)
528 {
529 ERROR ("amqp plugin: Unable to determine content type.");
530 return (-1);
531 }
533 status = camqp_read_body (conf,
534 (size_t) frame.payload.properties.body_size,
535 content_type);
537 sfree (content_type);
538 return (status);
539 } /* }}} int camqp_read_header */
541 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
542 {
543 camqp_config_t *conf = user_data;
544 int status;
546 while (subscriber_threads_running)
547 {
548 amqp_frame_t frame;
550 status = camqp_connect (conf);
551 if (status != 0)
552 {
553 ERROR ("amqp plugin: camqp_connect failed. "
554 "Will sleep for %.3f seconds.",
555 CDTIME_T_TO_DOUBLE (interval_g));
556 sleep (interval_g);
557 continue;
558 }
560 status = amqp_simple_wait_frame (conf->connection, &frame);
561 if (status < 0)
562 {
563 ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
564 "Will sleep for %.3f seconds.",
565 CDTIME_T_TO_DOUBLE (interval_g));
566 camqp_close_connection (conf);
567 sleep (interval_g);
568 continue;
569 }
571 if (frame.frame_type != AMQP_FRAME_METHOD)
572 {
573 DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
574 frame.frame_type);
575 continue;
576 }
578 if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
579 {
580 DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
581 frame.payload.method.id);
582 continue;
583 }
585 status = camqp_read_header (conf);
587 amqp_maybe_release_buffers (conf->connection);
588 } /* while (subscriber_threads_running) */
590 camqp_config_free (conf);
591 pthread_exit (NULL);
592 } /* }}} void *camqp_subscribe_thread */
594 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
595 {
596 int status;
597 pthread_t *tmp;
599 tmp = realloc (subscriber_threads,
600 sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
601 if (tmp == NULL)
602 {
603 ERROR ("amqp plugin: realloc failed.");
604 camqp_config_free (conf);
605 return (ENOMEM);
606 }
607 subscriber_threads = tmp;
608 tmp = subscriber_threads + subscriber_threads_num;
609 memset (tmp, 0, sizeof (*tmp));
611 status = pthread_create (tmp, /* attr = */ NULL,
612 camqp_subscribe_thread, conf);
613 if (status != 0)
614 {
615 char errbuf[1024];
616 ERROR ("amqp plugin: pthread_create failed: %s",
617 sstrerror (status, errbuf, sizeof (errbuf)));
618 camqp_config_free (conf);
619 return (status);
620 }
622 subscriber_threads_num++;
624 return (0);
625 } /* }}} int camqp_subscribe_init */
627 /*
628 * Publishing code
629 */
630 /* XXX: You must hold "conf->lock" when calling this function! */
631 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
632 const char *buffer, const char *routing_key)
633 {
634 amqp_basic_properties_t props;
635 int status;
637 status = camqp_connect (conf);
638 if (status != 0)
639 return (status);
641 memset (&props, 0, sizeof (props));
642 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
643 | AMQP_BASIC_DELIVERY_MODE_FLAG
644 | AMQP_BASIC_APP_ID_FLAG;
645 if (conf->format == CAMQP_FORMAT_COMMAND)
646 props.content_type = amqp_cstring_bytes("text/collectd");
647 else if (conf->format == CAMQP_FORMAT_JSON)
648 props.content_type = amqp_cstring_bytes("application/json");
649 else
650 assert (23 == 42);
651 props.delivery_mode = conf->delivery_mode;
652 props.app_id = amqp_cstring_bytes("collectd");
654 status = amqp_basic_publish(conf->connection,
655 /* channel = */ 1,
656 amqp_cstring_bytes(CONF(conf, exchange)),
657 amqp_cstring_bytes (routing_key),
658 /* mandatory = */ 0,
659 /* immediate = */ 0,
660 &props,
661 amqp_cstring_bytes(buffer));
662 if (status != 0)
663 {
664 ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
665 status);
666 camqp_close_connection (conf);
667 }
669 return (status);
670 } /* }}} int camqp_write_locked */
672 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
673 user_data_t *user_data)
674 {
675 camqp_config_t *conf = user_data->data;
676 char routing_key[6 * DATA_MAX_NAME_LEN];
677 char buffer[4096];
678 int status;
680 if ((ds == NULL) || (vl == NULL) || (conf == NULL))
681 return (EINVAL);
683 memset (buffer, 0, sizeof (buffer));
685 if (conf->routing_key != NULL)
686 {
687 sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
688 }
689 else
690 {
691 size_t i;
692 ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
693 vl->host,
694 vl->plugin, vl->plugin_instance,
695 vl->type, vl->type_instance);
697 /* Switch slashes (the only character forbidden by collectd) and dots
698 * (the separation character used by AMQP). */
699 for (i = 0; routing_key[i] != 0; i++)
700 {
701 if (routing_key[i] == '.')
702 routing_key[i] = '/';
703 else if (routing_key[i] == '/')
704 routing_key[i] = '.';
705 }
706 }
708 if (conf->format == CAMQP_FORMAT_COMMAND)
709 {
710 status = create_putval (buffer, sizeof (buffer), ds, vl);
711 if (status != 0)
712 {
713 ERROR ("amqp plugin: create_putval failed with status %i.",
714 status);
715 return (status);
716 }
717 }
718 else if (conf->format == CAMQP_FORMAT_JSON)
719 {
720 size_t bfree = sizeof (buffer);
721 size_t bfill = 0;
723 format_json_initialize (buffer, &bfill, &bfree);
724 format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
725 format_json_finalize (buffer, &bfill, &bfree);
726 }
727 else
728 {
729 ERROR ("amqp plugin: Invalid format (%i).", conf->format);
730 return (-1);
731 }
733 pthread_mutex_lock (&conf->lock);
734 status = camqp_write_locked (conf, buffer, routing_key);
735 pthread_mutex_unlock (&conf->lock);
737 return (status);
738 } /* }}} int camqp_write */
740 /*
741 * Config handling
742 */
743 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
744 camqp_config_t *conf)
745 {
746 char *string;
747 int status;
749 string = NULL;
750 status = cf_util_get_string (ci, &string);
751 if (status != 0)
752 return (status);
754 assert (string != NULL);
755 if (strcasecmp ("Command", string) == 0)
756 conf->format = CAMQP_FORMAT_COMMAND;
757 else if (strcasecmp ("JSON", string) == 0)
758 conf->format = CAMQP_FORMAT_JSON;
759 else
760 {
761 WARNING ("amqp plugin: Invalid format string: %s",
762 string);
763 }
765 free (string);
767 return (0);
768 } /* }}} int config_set_string */
770 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
771 _Bool publish)
772 {
773 camqp_config_t *conf;
774 int status;
775 int i;
777 conf = malloc (sizeof (*conf));
778 if (conf == NULL)
779 {
780 ERROR ("amqp plugin: malloc failed.");
781 return (ENOMEM);
782 }
784 /* Initialize "conf" {{{ */
785 memset (conf, 0, sizeof (*conf));
786 conf->publish = publish;
787 conf->name = NULL;
788 conf->format = CAMQP_FORMAT_COMMAND;
789 conf->host = NULL;
790 conf->port = 5672;
791 conf->vhost = NULL;
792 conf->user = NULL;
793 conf->password = NULL;
794 conf->exchange = NULL;
795 conf->routing_key = NULL;
796 /* publish only */
797 conf->delivery_mode = CAMQP_DM_VOLATILE;
798 conf->store_rates = 0;
799 /* subscribe only */
800 conf->exchange_type = NULL;
801 conf->queue = NULL;
802 /* general */
803 conf->connection = NULL;
804 pthread_mutex_init (&conf->lock, /* attr = */ NULL);
805 /* }}} */
807 status = cf_util_get_string (ci, &conf->name);
808 if (status != 0)
809 {
810 sfree (conf);
811 return (status);
812 }
814 for (i = 0; i < ci->children_num; i++)
815 {
816 oconfig_item_t *child = ci->children + i;
818 if (strcasecmp ("Host", child->key) == 0)
819 status = cf_util_get_string (child, &conf->host);
820 else if (strcasecmp ("Port", child->key) == 0)
821 {
822 status = cf_util_get_port_number (child);
823 if (status > 0)
824 {
825 conf->port = status;
826 status = 0;
827 }
828 }
829 else if (strcasecmp ("VHost", child->key) == 0)
830 status = cf_util_get_string (child, &conf->vhost);
831 else if (strcasecmp ("User", child->key) == 0)
832 status = cf_util_get_string (child, &conf->user);
833 else if (strcasecmp ("Password", child->key) == 0)
834 status = cf_util_get_string (child, &conf->password);
835 else if (strcasecmp ("Exchange", child->key) == 0)
836 status = cf_util_get_string (child, &conf->exchange);
837 else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
838 status = cf_util_get_string (child, &conf->exchange_type);
839 else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
840 status = cf_util_get_string (child, &conf->queue);
841 else if (strcasecmp ("RoutingKey", child->key) == 0)
842 status = cf_util_get_string (child, &conf->routing_key);
843 else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
844 {
845 _Bool tmp = 0;
846 status = cf_util_get_boolean (child, &tmp);
847 if (tmp)
848 conf->delivery_mode = CAMQP_DM_PERSISTENT;
849 else
850 conf->delivery_mode = CAMQP_DM_VOLATILE;
851 }
852 else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
853 status = cf_util_get_boolean (child, &conf->store_rates);
854 else if ((strcasecmp ("Format", child->key) == 0) && publish)
855 status = camqp_config_set_format (child, conf);
856 else
857 WARNING ("amqp plugin: Ignoring unknown "
858 "configuration option \"%s\".", child->key);
860 if (status != 0)
861 break;
862 } /* for (i = 0; i < ci->children_num; i++) */
864 if ((status == 0) && (conf->exchange == NULL))
865 {
866 if (conf->exchange_type != NULL)
867 WARNING ("amqp plugin: The option \"ExchangeType\" was given "
868 "without the \"Exchange\" option. It will be ignored.");
870 if (!publish && (conf->routing_key != NULL))
871 WARNING ("amqp plugin: The option \"RoutingKey\" was given "
872 "without the \"Exchange\" option. It will be ignored.");
874 }
876 if (status != 0)
877 {
878 camqp_config_free (conf);
879 return (status);
880 }
882 if (conf->exchange != NULL)
883 {
884 DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
885 conf->exchange);
886 }
888 if (publish)
889 {
890 char cbname[128];
891 user_data_t ud = { conf, camqp_config_free };
893 ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
895 status = plugin_register_write (cbname, camqp_write, &ud);
896 if (status != 0)
897 {
898 camqp_config_free (conf);
899 return (status);
900 }
901 }
902 else
903 {
904 status = camqp_subscribe_init (conf);
905 if (status != 0)
906 {
907 camqp_config_free (conf);
908 return (status);
909 }
910 }
912 return (0);
913 } /* }}} int camqp_config_connection */
915 static int camqp_config (oconfig_item_t *ci) /* {{{ */
916 {
917 int i;
919 for (i = 0; i < ci->children_num; i++)
920 {
921 oconfig_item_t *child = ci->children + i;
923 if (strcasecmp ("Publish", child->key) == 0)
924 camqp_config_connection (child, /* publish = */ 1);
925 else if (strcasecmp ("Subscribe", child->key) == 0)
926 camqp_config_connection (child, /* publish = */ 0);
927 else
928 WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
929 child->key);
930 } /* for (ci->children_num) */
932 return (0);
933 } /* }}} int camqp_config */
935 void module_register (void)
936 {
937 plugin_register_complex_config ("amqp", camqp_config);
938 plugin_register_shutdown ("amqp", camqp_shutdown);
939 } /* void module_register */
941 /* vim: set sw=4 sts=4 et fdm=marker : */