Merge branch 'master' into sp/amqp
[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_format_json.h"
39
40 #include <amqp.h>
41 #include <amqp_framing.h>
42
43 /* Defines for the delivery mode. I have no idea why they're not defined by the
44  * library.. */
45 #define CAMQP_DM_VOLATILE   1
46 #define CAMQP_DM_PERSISTENT 2
47
48 #define CAMQP_CHANNEL 1
49
50 /*
51  * Data types
52  */
53 struct camqp_config_s
54 {
55     _Bool   publish;
56     char   *name;
57
58     char   *host;
59     int     port;
60     char   *vhost;
61     char   *user;
62     char   *password;
63
64     char   *exchange;
65     char   *routingkey;
66
67     /* publish only */
68     uint8_t delivery_mode;
69     _Bool   store_rates;
70
71     /* subscribe only */
72     char   *exchange_type;
73     char   *queue;
74
75     amqp_connection_state_t connection;
76     pthread_mutex_t lock;
77 };
78 typedef struct camqp_config_s camqp_config_t;
79
80 /*
81  * Global variables
82  */
83 static const char *def_host       = "localhost";
84 static const char *def_vhost      = "/";
85 static const char *def_user       = "guest";
86 static const char *def_password   = "guest";
87 static const char *def_exchange   = "amq.fanout";
88 static const char *def_routingkey = "collectd";
89
90 static pthread_t *subscriber_threads     = NULL;
91 static size_t     subscriber_threads_num = 0;
92 static _Bool      subscriber_threads_running = 1;
93
94 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
95
96 /*
97  * Functions
98  */
99 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
100 {
101     int sockfd;
102
103     if ((conf == NULL) || (conf->connection == NULL))
104         return;
105
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 */
113
114 static void camqp_config_free (void *ptr) /* {{{ */
115 {
116     camqp_config_t *conf = ptr;
117
118     if (conf == NULL)
119         return;
120
121     camqp_close_connection (conf);
122
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->routingkey);
132
133     sfree (conf);
134 } /* }}} void camqp_config_free */
135
136 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
137 {
138     char *ret;
139
140     if ((in == NULL) || (in->bytes == NULL))
141         return (NULL);
142
143     ret = malloc (in->len + 1);
144     if (ret == NULL)
145         return (NULL);
146
147     memcpy (ret, in->bytes, in->len);
148     ret[in->len] = 0;
149
150     return (ret);
151 } /* }}} char *camqp_bytes_cstring */
152
153 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
154 {
155     amqp_rpc_reply_t r;
156
157     r = amqp_get_rpc_reply (conf->connection);
158     if (r.reply_type == AMQP_RESPONSE_NORMAL)
159         return (0);
160
161     return (1);
162 } /* }}} _Bool camqp_is_error */
163
164 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
165         char *buffer, size_t buffer_size)
166 {
167     amqp_rpc_reply_t r;
168
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;
175
176         case AMQP_RESPONSE_NONE:
177             sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
178             break;
179
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;
186
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;
210
211         default:
212             ssnprintf (buffer, buffer_size, "Unknown reply type %i",
213                     (int) r.reply_type);
214     }
215
216     return (buffer);
217 } /* }}} char *camqp_strerror */
218
219 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
220 {
221     amqp_queue_declare_ok_t *qd_ret;
222     amqp_basic_consume_ok_t *cm_ret;
223
224     qd_ret = amqp_queue_declare (conf->connection,
225             /* channel     = */ CAMQP_CHANNEL,
226             /* queue       = */ (conf->queue != NULL)
227             ? amqp_cstring_bytes (conf->queue)
228             : AMQP_EMPTY_BYTES,
229             /* passive     = */ 0,
230             /* durable     = */ 0,
231             /* exclusive   = */ 0,
232             /* auto_delete = */ 1,
233             /* arguments   = */ AMQP_EMPTY_TABLE);
234     if (qd_ret == NULL)
235     {
236         ERROR ("amqp plugin: amqp_queue_declare failed.");
237         camqp_close_connection (conf);
238         return (-1);
239     }
240
241     if (conf->queue == NULL)
242     {
243         conf->queue = camqp_bytes_cstring (&qd_ret->queue);
244         if (conf->queue == NULL)
245         {
246             ERROR ("amqp plugin: camqp_bytes_cstring failed.");
247             camqp_close_connection (conf);
248             return (-1);
249         }
250
251         INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
252     }
253     DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
254
255     /* bind to an exchange */
256     if (conf->exchange != NULL)
257     {
258         amqp_queue_bind_ok_t *qb_ret;
259
260         /* create the exchange */
261         if (conf->exchange_type != NULL)
262         {
263             amqp_exchange_declare_ok_t *ed_ret;
264
265             ed_ret = amqp_exchange_declare (conf->connection,
266                     /* channel     = */ CAMQP_CHANNEL,
267                     /* exchange    = */ amqp_cstring_bytes (conf->exchange),
268                     /* type        = */ amqp_cstring_bytes (conf->exchange_type),
269                     /* passive     = */ 0,
270                     /* durable     = */ 0,
271                     /* auto_delete = */ 1,
272                     /* arguments   = */ AMQP_EMPTY_TABLE);
273             if ((ed_ret == NULL) && camqp_is_error (conf))
274             {
275                 char errbuf[1024];
276                 ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
277                         camqp_strerror (conf, errbuf, sizeof (errbuf)));
278                 camqp_close_connection (conf);
279                 return (-1);
280             }
281         }
282
283         DEBUG ("amqp plugin: queue = %s; exchange = %s; routing_key = %s;",
284                 conf->queue, conf->exchange, CONF (conf, routingkey));
285
286         assert (conf->queue != NULL);
287         qb_ret = amqp_queue_bind (conf->connection,
288                 /* channel     = */ CAMQP_CHANNEL,
289                 /* queue       = */ amqp_cstring_bytes (conf->queue),
290                 /* exchange    = */ amqp_cstring_bytes (conf->exchange),
291 #if 1
292                 /* routing_key = */ amqp_cstring_bytes (CONF (conf, routingkey)),
293 #else
294                 /* routing_key = */ AMQP_EMPTY_BYTES,
295 #endif
296                 /* arguments   = */ AMQP_EMPTY_TABLE);
297         if ((qb_ret == NULL) && camqp_is_error (conf))
298         {
299             char errbuf[1024];
300             ERROR ("amqp plugin: amqp_queue_bind failed: %s",
301                     camqp_strerror (conf, errbuf, sizeof (errbuf)));
302             camqp_close_connection (conf);
303             return (-1);
304         }
305
306         DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
307                 conf->queue, conf->exchange);
308     } /* if (conf->exchange != NULL) */
309
310     cm_ret = amqp_basic_consume (conf->connection,
311             /* channel      = */ CAMQP_CHANNEL,
312             /* queue        = */ amqp_cstring_bytes (conf->queue),
313             /* consumer_tag = */ AMQP_EMPTY_BYTES,
314             /* no_local     = */ 0,
315             /* no_ack       = */ 1,
316             /* exclusive    = */ 0);
317     if ((cm_ret == NULL) && camqp_is_error (conf))
318     {
319         char errbuf[1024];
320         ERROR ("amqp plugin: amqp_basic_consume failed: %s",
321                     camqp_strerror (conf, errbuf, sizeof (errbuf)));
322         camqp_close_connection (conf);
323         return (-1);
324     }
325
326     return (0);
327 } /* }}} int camqp_setup_queue */
328
329 static int camqp_connect (camqp_config_t *conf) /* {{{ */
330 {
331     amqp_rpc_reply_t reply;
332     int sockfd;
333     int status;
334
335     if (conf->connection != NULL)
336         return (0);
337
338     conf->connection = amqp_new_connection ();
339     if (conf->connection == NULL)
340     {
341         ERROR ("amqp plugin: amqp_new_connection failed.");
342         return (ENOMEM);
343     }
344
345     sockfd = amqp_open_socket (CONF(conf, host), conf->port);
346     if (sockfd < 0)
347     {
348         char errbuf[1024];
349         status = (-1) * sockfd;
350         ERROR ("amqp plugin: amqp_open_socket failed: %s",
351                 sstrerror (status, errbuf, sizeof (errbuf)));
352         amqp_destroy_connection (conf->connection);
353         conf->connection = NULL;
354         return (status);
355     }
356     amqp_set_sockfd (conf->connection, sockfd);
357
358     reply = amqp_login (conf->connection, CONF(conf, vhost),
359             /* channel max = */      0,
360             /* frame max   = */ 131072,
361             /* heartbeat   = */      0,
362             /* authentication = */ AMQP_SASL_METHOD_PLAIN,
363             CONF(conf, user), CONF(conf, password));
364     if (reply.reply_type != AMQP_RESPONSE_NORMAL)
365     {
366         ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
367                 CONF(conf, vhost), CONF(conf, user));
368         amqp_destroy_connection (conf->connection);
369         close (sockfd);
370         conf->connection = NULL;
371         return (1);
372     }
373
374     amqp_channel_open (conf->connection, /* channel = */ 1);
375     /* FIXME: Is checking "reply.reply_type" really correct here? How does
376      * it get set? --octo */
377     if (reply.reply_type != AMQP_RESPONSE_NORMAL)
378     {
379         ERROR ("amqp plugin: amqp_channel_open failed.");
380         amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
381         amqp_destroy_connection (conf->connection);
382         close(sockfd);
383         conf->connection = NULL;
384         return (1);
385     }
386
387     INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
388             "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
389
390     if (!conf->publish)
391         return (camqp_setup_queue (conf));
392     return (0);
393 } /* }}} int camqp_connect */
394
395 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
396         size_t body_size)
397 {
398     char body[body_size + 1];
399     char *body_ptr;
400     size_t received;
401     amqp_frame_t frame;
402     int status;
403
404     memset (body, 0, sizeof (body));
405     body_ptr = &body[0];
406     received = 0;
407
408     while (received < body_size)
409     {
410         status = amqp_simple_wait_frame (conf->connection, &frame);
411         if (status < 0)
412         {
413             char errbuf[1024];
414             status = (-1) * status;
415             ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
416                     sstrerror (status, errbuf, sizeof (errbuf)));
417             camqp_close_connection (conf);
418             return (status);
419         }
420
421         if (frame.frame_type != AMQP_FRAME_BODY)
422         {
423             NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
424                     frame.frame_type);
425             return (-1);
426         }
427
428         if ((body_size - received) < frame.payload.body_fragment.len)
429         {
430             WARNING ("amqp plugin: Body is larger than indicated by header.");
431             return (-1);
432         }
433
434         memcpy (body_ptr, frame.payload.body_fragment.bytes,
435                 frame.payload.body_fragment.len);
436         body_ptr += frame.payload.body_fragment.len;
437         received += frame.payload.body_fragment.len;
438     } /* while (received < body_size) */
439
440     DEBUG ("amqp plugin: camqp_read_body: body = %s", body);
441
442     return (0);
443 } /* }}} int camqp_read_body */
444
445 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
446 {
447     int status;
448     amqp_frame_t frame;
449
450     status = amqp_simple_wait_frame (conf->connection, &frame);
451     if (status < 0)
452     {
453         char errbuf[1024];
454         status = (-1) * status;
455         ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
456                     sstrerror (status, errbuf, sizeof (errbuf)));
457         camqp_close_connection (conf);
458         return (status);
459     }
460
461     if (frame.frame_type != AMQP_FRAME_HEADER)
462     {
463         NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
464                 frame.frame_type);
465         return (-1);
466     }
467
468     return (camqp_read_body (conf, frame.payload.properties.body_size));
469 } /* }}} int camqp_read_header */
470
471 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
472 {
473     camqp_config_t *conf = user_data;
474     int status;
475
476     while (subscriber_threads_running)
477     {
478         amqp_frame_t frame;
479
480         status = camqp_connect (conf);
481         if (status != 0)
482         {
483             ERROR ("amqp plugin: camqp_connect failed. "
484                     "Will sleep for %i seconds.", interval_g);
485             sleep (interval_g);
486             continue;
487         }
488
489         status = amqp_simple_wait_frame (conf->connection, &frame);
490         if (status < 0)
491         {
492             ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
493                     "Will sleep for %i seconds.", interval_g);
494             camqp_close_connection (conf);
495             sleep (interval_g);
496             continue;
497         }
498
499         if (frame.frame_type != AMQP_FRAME_METHOD)
500         {
501             DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
502                     frame.frame_type);
503             continue;
504         }
505
506         if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
507         {
508             DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
509                     frame.payload.method.id);
510             continue;
511         }
512
513         status = camqp_read_header (conf);
514
515         amqp_maybe_release_buffers (conf->connection);
516     } /* while (subscriber_threads_running) */
517
518     camqp_config_free (conf);
519     pthread_exit (NULL);
520 } /* }}} void *camqp_subscribe_thread */
521
522 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
523 {
524     int status;
525     pthread_t *tmp;
526
527     tmp = realloc (subscriber_threads,
528             sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
529     if (tmp == NULL)
530     {
531         ERROR ("amqp plugin: realloc failed.");
532         camqp_config_free (conf);
533         return (ENOMEM);
534     }
535     subscriber_threads = tmp;
536     tmp = subscriber_threads + subscriber_threads_num;
537     memset (tmp, 0, sizeof (*tmp));
538
539     status = pthread_create (tmp, /* attr = */ NULL,
540             camqp_subscribe_thread, conf);
541     if (status != 0)
542     {
543         char errbuf[1024];
544         ERROR ("amqp plugin: pthread_create failed: %s",
545                 sstrerror (status, errbuf, sizeof (errbuf)));
546         camqp_config_free (conf);
547         return (status);
548     }
549
550     subscriber_threads_num++;
551
552     return (0);
553 } /* }}} int camqp_subscribe_init */
554
555 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
556         const char *buffer)
557 {
558     amqp_basic_properties_t props;
559     int status;
560
561     status = camqp_connect (conf);
562     if (status != 0)
563         return (status);
564
565     memset (&props, 0, sizeof (props));
566     props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
567         | AMQP_BASIC_DELIVERY_MODE_FLAG
568         | AMQP_BASIC_APP_ID_FLAG;
569     props.content_type = amqp_cstring_bytes("application/json");
570     props.delivery_mode = conf->delivery_mode;
571     props.app_id = amqp_cstring_bytes("collectd");
572
573     status = amqp_basic_publish(conf->connection,
574                 /* channel = */ 1,
575                 amqp_cstring_bytes(CONF(conf, exchange)),
576                 amqp_cstring_bytes(CONF(conf, routingkey)),
577                 /* mandatory = */ 0,
578                 /* immediate = */ 0,
579                 &props,
580                 amqp_cstring_bytes(buffer));
581     if (status != 0)
582     {
583         ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
584                 status);
585         camqp_close_connection (conf);
586     }
587
588     return (status);
589 } /* }}} int camqp_write_locked */
590
591 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
592         user_data_t *user_data)
593 {
594     camqp_config_t *conf = user_data->data;
595     char buffer[4096];
596     size_t bfree;
597     size_t bfill;
598     int status;
599
600     if ((ds == NULL) || (vl == NULL) || (conf == NULL))
601         return (EINVAL);
602
603     memset (buffer, 0, sizeof (buffer));
604     bfree = sizeof (buffer);
605     bfill = 0;
606
607     format_json_initialize (buffer, &bfill, &bfree);
608     format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
609     format_json_finalize (buffer, &bfill, &bfree);
610
611     pthread_mutex_lock (&conf->lock);
612     status = camqp_write_locked (conf, buffer);
613     pthread_mutex_unlock (&conf->lock);
614
615     return (status);
616 } /* }}} int camqp_write */
617
618 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
619         _Bool publish)
620 {
621     camqp_config_t *conf;
622     int status;
623     int i;
624
625     conf = malloc (sizeof (*conf));
626     if (conf == NULL)
627     {
628         ERROR ("amqp plugin: malloc failed.");
629         return (ENOMEM);
630     }
631
632     /* Initialize "conf" {{{ */
633     memset (conf, 0, sizeof (*conf));
634     conf->publish = publish;
635     conf->name = NULL;
636     conf->host = NULL;
637     conf->port = 5672;
638     conf->vhost = NULL;
639     conf->user = NULL;
640     conf->password = NULL;
641     conf->exchange = NULL;
642     conf->routingkey = NULL;
643     /* publish only */
644     conf->delivery_mode = CAMQP_DM_VOLATILE;
645     conf->store_rates = 0;
646     /* subscribe only */
647     conf->exchange_type = NULL;
648     conf->queue = NULL;
649     /* general */
650     conf->connection = NULL;
651     pthread_mutex_init (&conf->lock, /* attr = */ NULL);
652     /* }}} */
653
654     status = cf_util_get_string (ci, &conf->name);
655     if (status != 0)
656     {
657         sfree (conf);
658         return (status);
659     }
660
661     for (i = 0; i < ci->children_num; i++)
662     {
663         oconfig_item_t *child = ci->children + i;
664
665         if (strcasecmp ("Host", child->key) == 0)
666             status = cf_util_get_string (child, &conf->host);
667         else if (strcasecmp ("Port", child->key) == 0)
668         {
669             status = cf_util_get_port_number (child);
670             if (status > 0)
671             {
672                 conf->port = status;
673                 status = 0;
674             }
675         }
676         else if (strcasecmp ("VHost", child->key) == 0)
677             status = cf_util_get_string (child, &conf->vhost);
678         else if (strcasecmp ("User", child->key) == 0)
679             status = cf_util_get_string (child, &conf->user);
680         else if (strcasecmp ("Password", child->key) == 0)
681             status = cf_util_get_string (child, &conf->password);
682         else if (strcasecmp ("Exchange", child->key) == 0)
683             status = cf_util_get_string (child, &conf->exchange);
684         else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
685             status = cf_util_get_string (child, &conf->exchange_type);
686         else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
687             status = cf_util_get_string (child, &conf->queue);
688         else if (strcasecmp ("RoutingKey", child->key) == 0)
689             status = cf_util_get_string (child, &conf->routingkey);
690         else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
691         {
692             _Bool tmp = 0;
693             status = cf_util_get_boolean (child, &tmp);
694             if (tmp)
695                 conf->delivery_mode = CAMQP_DM_PERSISTENT;
696             else
697                 conf->delivery_mode = CAMQP_DM_VOLATILE;
698         }
699         else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
700             status = cf_util_get_boolean (child, &conf->store_rates);
701         else
702             WARNING ("amqp plugin: Ignoring unknown "
703                     "configuration option \"%s\".", child->key);
704
705         if (status != 0)
706             break;
707     } /* for (i = 0; i < ci->children_num; i++) */
708
709     if ((status == 0) && !publish && (conf->exchange == NULL))
710     {
711         if (conf->routingkey != NULL)
712             WARNING ("amqp plugin: The option \"RoutingKey\" was given "
713                     "without the \"Exchange\" option. It will be ignored.");
714
715         if (conf->exchange_type != NULL)
716             WARNING ("amqp plugin: The option \"ExchangeType\" was given "
717                     "without the \"Exchange\" option. It will be ignored.");
718     }
719
720     if (status != 0)
721     {
722         camqp_config_free (conf);
723         return (status);
724     }
725
726     if (conf->exchange != NULL)
727     {
728         DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
729                 conf->exchange);
730     }
731
732     if (publish)
733     {
734         char cbname[128];
735         user_data_t ud = { conf, camqp_config_free };
736
737         ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
738
739         status = plugin_register_write (cbname, camqp_write, &ud);
740         if (status != 0)
741         {
742             camqp_config_free (conf);
743             return (status);
744         }
745     }
746     else
747     {
748         status = camqp_subscribe_init (conf);
749         if (status != 0)
750         {
751             camqp_config_free (conf);
752             return (status);
753         }
754     }
755
756     return (0);
757 } /* }}} int camqp_config_connection */
758
759 static int camqp_config (oconfig_item_t *ci) /* {{{ */
760 {
761     int i;
762
763     for (i = 0; i < ci->children_num; i++)
764     {
765         oconfig_item_t *child = ci->children + i;
766
767         if (strcasecmp ("Publish", child->key) == 0)
768             camqp_config_connection (child, /* publish = */ 1);
769         else if (strcasecmp ("Subscribe", child->key) == 0)
770             camqp_config_connection (child, /* publish = */ 0);
771         else
772             WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
773                     child->key);
774     } /* for (ci->children_num) */
775
776     return (0);
777 } /* }}} int camqp_config */
778
779 static int shutdown (void) /* {{{ */
780 {
781     size_t i;
782
783     DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
784             subscriber_threads_num);
785
786     subscriber_threads_running = 0;
787     for (i = 0; i < subscriber_threads_num; i++)
788     {
789         /* FIXME: Sending a signal is not very elegant here. Maybe find out how
790          * to use a timeout in the thread and check for the variable in regular
791          * intervals. */
792         pthread_kill (subscriber_threads[i], SIGTERM);
793         pthread_join (subscriber_threads[i], /* retval = */ NULL);
794     }
795
796     subscriber_threads_num = 0;
797     sfree (subscriber_threads);
798
799     DEBUG ("amqp plugin: All subscriber threads exited.");
800
801     return (0);
802 } /* }}} int shutdown */
803
804 void module_register (void)
805 {
806     plugin_register_complex_config ("amqp", camqp_config);
807     plugin_register_shutdown ("amqp", shutdown);
808 } /* void module_register */
809
810 /* vim: set sw=4 sts=4 et fdm=marker : */