84bcc066efcf41a93a14e11d82640e62c0dc6e14
[collectd.git] / src / amqp.c
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  **/
28
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <strings.h>
33 #include <pthread.h>
34
35 #include "collectd.h"
36 #include "common.h"
37 #include "plugin.h"
38 #include "utils_cmd_putval.h"
39 #include "utils_format_json.h"
40
41 #include <amqp.h>
42 #include <amqp_framing.h>
43
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
48
49 #define CAMQP_FORMAT_COMMAND 1
50 #define CAMQP_FORMAT_JSON    2
51
52 #define CAMQP_CHANNEL 1
53
54 /*
55  * Data types
56  */
57 struct camqp_config_s
58 {
59     _Bool   publish;
60     char   *name;
61     int     format;
62
63     char   *host;
64     int     port;
65     char   *vhost;
66     char   *user;
67     char   *password;
68
69     char   *exchange;
70     char   *routingkey;
71
72     /* publish only */
73     uint8_t delivery_mode;
74     _Bool   store_rates;
75
76     /* subscribe only */
77     char   *exchange_type;
78     char   *queue;
79
80     amqp_connection_state_t connection;
81     pthread_mutex_t lock;
82 };
83 typedef struct camqp_config_s camqp_config_t;
84
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";
94
95 static pthread_t *subscriber_threads     = NULL;
96 static size_t     subscriber_threads_num = 0;
97 static _Bool      subscriber_threads_running = 1;
98
99 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
100
101 /*
102  * Functions
103  */
104 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
105 {
106     int sockfd;
107
108     if ((conf == NULL) || (conf->connection == NULL))
109         return;
110
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 */
118
119 static void camqp_config_free (void *ptr) /* {{{ */
120 {
121     camqp_config_t *conf = ptr;
122
123     if (conf == NULL)
124         return;
125
126     camqp_close_connection (conf);
127
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);
137
138     sfree (conf);
139 } /* }}} void camqp_config_free */
140
141 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
142 {
143     char *ret;
144
145     if ((in == NULL) || (in->bytes == NULL))
146         return (NULL);
147
148     ret = malloc (in->len + 1);
149     if (ret == NULL)
150         return (NULL);
151
152     memcpy (ret, in->bytes, in->len);
153     ret[in->len] = 0;
154
155     return (ret);
156 } /* }}} char *camqp_bytes_cstring */
157
158 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
159 {
160     amqp_rpc_reply_t r;
161
162     r = amqp_get_rpc_reply (conf->connection);
163     if (r.reply_type == AMQP_RESPONSE_NORMAL)
164         return (0);
165
166     return (1);
167 } /* }}} _Bool camqp_is_error */
168
169 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
170         char *buffer, size_t buffer_size)
171 {
172     amqp_rpc_reply_t r;
173
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;
180
181         case AMQP_RESPONSE_NONE:
182             sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
183             break;
184
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;
191
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;
215
216         default:
217             ssnprintf (buffer, buffer_size, "Unknown reply type %i",
218                     (int) r.reply_type);
219     }
220
221     return (buffer);
222 } /* }}} char *camqp_strerror */
223
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;
228
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     }
245
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         }
255
256         INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
257     }
258     DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
259
260     /* bind to an exchange */
261     if (conf->exchange != NULL)
262     {
263         amqp_queue_bind_ok_t *qb_ret;
264
265         /* create the exchange */
266         if (conf->exchange_type != NULL)
267         {
268             amqp_exchange_declare_ok_t *ed_ret;
269
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         }
287
288         DEBUG ("amqp plugin: queue = %s; exchange = %s; routing_key = %s;",
289                 conf->queue, conf->exchange, CONF (conf, routingkey));
290
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         }
310
311         DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
312                 conf->queue, conf->exchange);
313     } /* if (conf->exchange != NULL) */
314
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     }
330
331     return (0);
332 } /* }}} int camqp_setup_queue */
333
334 static int camqp_connect (camqp_config_t *conf) /* {{{ */
335 {
336     amqp_rpc_reply_t reply;
337     int sockfd;
338     int status;
339
340     if (conf->connection != NULL)
341         return (0);
342
343     conf->connection = amqp_new_connection ();
344     if (conf->connection == NULL)
345     {
346         ERROR ("amqp plugin: amqp_new_connection failed.");
347         return (ENOMEM);
348     }
349
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);
362
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     }
378
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     }
391
392     INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
393             "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
394
395     if (!conf->publish)
396         return (camqp_setup_queue (conf));
397     return (0);
398 } /* }}} int camqp_connect */
399
400 static int shutdown (void) /* {{{ */
401 {
402     size_t i;
403
404     DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
405             subscriber_threads_num);
406
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     }
416
417     subscriber_threads_num = 0;
418     sfree (subscriber_threads);
419
420     DEBUG ("amqp plugin: All subscriber threads exited.");
421
422     return (0);
423 } /* }}} int shutdown */
424
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;
436
437     memset (body, 0, sizeof (body));
438     body_ptr = &body[0];
439     received = 0;
440
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         }
453
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         }
460
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         }
466
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) */
472
473     DEBUG ("amqp plugin: camqp_read_body: body = %s", body);
474
475     return (0);
476 } /* }}} int camqp_read_body */
477
478 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
479 {
480     int status;
481     amqp_frame_t frame;
482
483     status = amqp_simple_wait_frame (conf->connection, &frame);
484     if (status < 0)
485     {
486         char errbuf[1024];
487         status = (-1) * status;
488         ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
489                     sstrerror (status, errbuf, sizeof (errbuf)));
490         camqp_close_connection (conf);
491         return (status);
492     }
493
494     if (frame.frame_type != AMQP_FRAME_HEADER)
495     {
496         NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
497                 frame.frame_type);
498         return (-1);
499     }
500
501     return (camqp_read_body (conf, frame.payload.properties.body_size));
502 } /* }}} int camqp_read_header */
503
504 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
505 {
506     camqp_config_t *conf = user_data;
507     int status;
508
509     while (subscriber_threads_running)
510     {
511         amqp_frame_t frame;
512
513         status = camqp_connect (conf);
514         if (status != 0)
515         {
516             ERROR ("amqp plugin: camqp_connect failed. "
517                     "Will sleep for %i seconds.", interval_g);
518             sleep (interval_g);
519             continue;
520         }
521
522         status = amqp_simple_wait_frame (conf->connection, &frame);
523         if (status < 0)
524         {
525             ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
526                     "Will sleep for %i seconds.", interval_g);
527             camqp_close_connection (conf);
528             sleep (interval_g);
529             continue;
530         }
531
532         if (frame.frame_type != AMQP_FRAME_METHOD)
533         {
534             DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
535                     frame.frame_type);
536             continue;
537         }
538
539         if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
540         {
541             DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
542                     frame.payload.method.id);
543             continue;
544         }
545
546         status = camqp_read_header (conf);
547
548         amqp_maybe_release_buffers (conf->connection);
549     } /* while (subscriber_threads_running) */
550
551     camqp_config_free (conf);
552     pthread_exit (NULL);
553 } /* }}} void *camqp_subscribe_thread */
554
555 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
556 {
557     int status;
558     pthread_t *tmp;
559
560     tmp = realloc (subscriber_threads,
561             sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
562     if (tmp == NULL)
563     {
564         ERROR ("amqp plugin: realloc failed.");
565         camqp_config_free (conf);
566         return (ENOMEM);
567     }
568     subscriber_threads = tmp;
569     tmp = subscriber_threads + subscriber_threads_num;
570     memset (tmp, 0, sizeof (*tmp));
571
572     status = pthread_create (tmp, /* attr = */ NULL,
573             camqp_subscribe_thread, conf);
574     if (status != 0)
575     {
576         char errbuf[1024];
577         ERROR ("amqp plugin: pthread_create failed: %s",
578                 sstrerror (status, errbuf, sizeof (errbuf)));
579         camqp_config_free (conf);
580         return (status);
581     }
582
583     subscriber_threads_num++;
584
585     return (0);
586 } /* }}} int camqp_subscribe_init */
587
588 /*
589  * Publishing code
590  */
591 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
592         const char *buffer)
593 {
594     amqp_basic_properties_t props;
595     int status;
596
597     status = camqp_connect (conf);
598     if (status != 0)
599         return (status);
600
601     memset (&props, 0, sizeof (props));
602     props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
603         | AMQP_BASIC_DELIVERY_MODE_FLAG
604         | AMQP_BASIC_APP_ID_FLAG;
605     props.content_type = amqp_cstring_bytes("application/json");
606     props.delivery_mode = conf->delivery_mode;
607     props.app_id = amqp_cstring_bytes("collectd");
608
609     status = amqp_basic_publish(conf->connection,
610                 /* channel = */ 1,
611                 amqp_cstring_bytes(CONF(conf, exchange)),
612                 amqp_cstring_bytes(CONF(conf, routingkey)),
613                 /* mandatory = */ 0,
614                 /* immediate = */ 0,
615                 &props,
616                 amqp_cstring_bytes(buffer));
617     if (status != 0)
618     {
619         ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
620                 status);
621         camqp_close_connection (conf);
622     }
623
624     return (status);
625 } /* }}} int camqp_write_locked */
626
627 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
628         user_data_t *user_data)
629 {
630     camqp_config_t *conf = user_data->data;
631     char buffer[4096];
632     size_t bfree;
633     size_t bfill;
634     int status;
635
636     if ((ds == NULL) || (vl == NULL) || (conf == NULL))
637         return (EINVAL);
638
639     memset (buffer, 0, sizeof (buffer));
640     bfree = sizeof (buffer);
641     bfill = 0;
642
643     if (conf->format == CAMQP_FORMAT_COMMAND)
644     {
645         status = create_putval (buffer, sizeof (buffer), ds, vl);
646         if (status != 0)
647         {
648             ERROR ("amqp plugin: create_putval failed with status %i.",
649                     status);
650             return (status);
651         }
652     }
653     else if (conf->format == CAMQP_FORMAT_JSON)
654     {
655         format_json_initialize (buffer, &bfill, &bfree);
656         format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
657         format_json_finalize (buffer, &bfill, &bfree);
658     }
659     else
660     {
661         ERROR ("amqp plugin: Invalid format (%i).", conf->format);
662         return (-1);
663     }
664
665     pthread_mutex_lock (&conf->lock);
666     status = camqp_write_locked (conf, buffer);
667     pthread_mutex_unlock (&conf->lock);
668
669     return (status);
670 } /* }}} int camqp_write */
671
672 /*
673  * Config handling
674  */
675 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
676         camqp_config_t *conf)
677 {
678     char *string;
679     int status;
680
681     string = NULL;
682     status = cf_util_get_string (ci, &string);
683     if (status != 0)
684         return (status);
685
686     assert (string != NULL);
687     if (strcasecmp ("Command", string) == 0)
688         conf->format = CAMQP_FORMAT_COMMAND;
689     else if (strcasecmp ("JSON", string) == 0)
690         conf->format = CAMQP_FORMAT_JSON;
691     else
692     {
693         WARNING ("amqp plugin: Invalid format string: %s",
694                 string);
695     }
696
697     free (string);
698
699     return (0);
700 } /* }}} int config_set_string */
701
702 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
703         _Bool publish)
704 {
705     camqp_config_t *conf;
706     int status;
707     int i;
708
709     conf = malloc (sizeof (*conf));
710     if (conf == NULL)
711     {
712         ERROR ("amqp plugin: malloc failed.");
713         return (ENOMEM);
714     }
715
716     /* Initialize "conf" {{{ */
717     memset (conf, 0, sizeof (*conf));
718     conf->publish = publish;
719     conf->name = NULL;
720     conf->format = CAMQP_FORMAT_COMMAND;
721     conf->host = NULL;
722     conf->port = 5672;
723     conf->vhost = NULL;
724     conf->user = NULL;
725     conf->password = NULL;
726     conf->exchange = NULL;
727     conf->routingkey = NULL;
728     /* publish only */
729     conf->delivery_mode = CAMQP_DM_VOLATILE;
730     conf->store_rates = 0;
731     /* subscribe only */
732     conf->exchange_type = NULL;
733     conf->queue = NULL;
734     /* general */
735     conf->connection = NULL;
736     pthread_mutex_init (&conf->lock, /* attr = */ NULL);
737     /* }}} */
738
739     status = cf_util_get_string (ci, &conf->name);
740     if (status != 0)
741     {
742         sfree (conf);
743         return (status);
744     }
745
746     for (i = 0; i < ci->children_num; i++)
747     {
748         oconfig_item_t *child = ci->children + i;
749
750         if (strcasecmp ("Format", child->key) == 0)
751             status = camqp_config_set_format (child, conf);
752         else if (strcasecmp ("Host", child->key) == 0)
753             status = cf_util_get_string (child, &conf->host);
754         else if (strcasecmp ("Port", child->key) == 0)
755         {
756             status = cf_util_get_port_number (child);
757             if (status > 0)
758             {
759                 conf->port = status;
760                 status = 0;
761             }
762         }
763         else if (strcasecmp ("VHost", child->key) == 0)
764             status = cf_util_get_string (child, &conf->vhost);
765         else if (strcasecmp ("User", child->key) == 0)
766             status = cf_util_get_string (child, &conf->user);
767         else if (strcasecmp ("Password", child->key) == 0)
768             status = cf_util_get_string (child, &conf->password);
769         else if (strcasecmp ("Exchange", child->key) == 0)
770             status = cf_util_get_string (child, &conf->exchange);
771         else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
772             status = cf_util_get_string (child, &conf->exchange_type);
773         else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
774             status = cf_util_get_string (child, &conf->queue);
775         else if (strcasecmp ("RoutingKey", child->key) == 0)
776             status = cf_util_get_string (child, &conf->routingkey);
777         else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
778         {
779             _Bool tmp = 0;
780             status = cf_util_get_boolean (child, &tmp);
781             if (tmp)
782                 conf->delivery_mode = CAMQP_DM_PERSISTENT;
783             else
784                 conf->delivery_mode = CAMQP_DM_VOLATILE;
785         }
786         else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
787             status = cf_util_get_boolean (child, &conf->store_rates);
788         else
789             WARNING ("amqp plugin: Ignoring unknown "
790                     "configuration option \"%s\".", child->key);
791
792         if (status != 0)
793             break;
794     } /* for (i = 0; i < ci->children_num; i++) */
795
796     if ((status == 0) && !publish && (conf->exchange == NULL))
797     {
798         if (conf->routingkey != NULL)
799             WARNING ("amqp plugin: The option \"RoutingKey\" was given "
800                     "without the \"Exchange\" option. It will be ignored.");
801
802         if (conf->exchange_type != NULL)
803             WARNING ("amqp plugin: The option \"ExchangeType\" was given "
804                     "without the \"Exchange\" option. It will be ignored.");
805     }
806
807     if (status != 0)
808     {
809         camqp_config_free (conf);
810         return (status);
811     }
812
813     if (conf->exchange != NULL)
814     {
815         DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
816                 conf->exchange);
817     }
818
819     if (publish)
820     {
821         char cbname[128];
822         user_data_t ud = { conf, camqp_config_free };
823
824         ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
825
826         status = plugin_register_write (cbname, camqp_write, &ud);
827         if (status != 0)
828         {
829             camqp_config_free (conf);
830             return (status);
831         }
832     }
833     else
834     {
835         status = camqp_subscribe_init (conf);
836         if (status != 0)
837         {
838             camqp_config_free (conf);
839             return (status);
840         }
841     }
842
843     return (0);
844 } /* }}} int camqp_config_connection */
845
846 static int camqp_config (oconfig_item_t *ci) /* {{{ */
847 {
848     int i;
849
850     for (i = 0; i < ci->children_num; i++)
851     {
852         oconfig_item_t *child = ci->children + i;
853
854         if (strcasecmp ("Publish", child->key) == 0)
855             camqp_config_connection (child, /* publish = */ 1);
856         else if (strcasecmp ("Subscribe", child->key) == 0)
857             camqp_config_connection (child, /* publish = */ 0);
858         else
859             WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
860                     child->key);
861     } /* for (ci->children_num) */
862
863     return (0);
864 } /* }}} int camqp_config */
865
866 void module_register (void)
867 {
868     plugin_register_complex_config ("amqp", camqp_config);
869     plugin_register_shutdown ("amqp", shutdown);
870 } /* void module_register */
871
872 /* vim: set sw=4 sts=4 et fdm=marker : */