bebaea7c8e7eb4bdbbcff93684321a851fc94cbd
[collectd.git] / src / amqp.c
1 /**
2  * collectd - src/amqp.c
3  * Copyright (C) 2009       Sebastien Pahl
4  * Copyright (C) 2010-2012  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 "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "utils_cmd_putval.h"
33 #include "utils_format_json.h"
34 #include "utils_format_graphite.h"
35
36 #include <pthread.h>
37
38 #include <amqp.h>
39 #include <amqp_framing.h>
40
41 #ifdef HAVE_AMQP_TCP_SOCKET_H
42 # include <amqp_tcp_socket.h>
43 #endif
44 #ifdef HAVE_AMQP_SOCKET_H
45 # include <amqp_socket.h>
46 #endif
47 #if defined HAVE_DECL_AMQP_SOCKET_CLOSE && !HAVE_DECL_AMQP_SOCKET_CLOSE
48 /* rabbitmq-c does not currently ship amqp_socket.h
49  * and, thus, does not define this function. */
50 int amqp_socket_close(amqp_socket_t *);
51 #endif
52
53 /* Defines for the delivery mode. I have no idea why they're not defined by the
54  * library.. */
55 #define CAMQP_DM_VOLATILE   1
56 #define CAMQP_DM_PERSISTENT 2
57
58 #define CAMQP_FORMAT_COMMAND    1
59 #define CAMQP_FORMAT_JSON       2
60 #define CAMQP_FORMAT_GRAPHITE   3
61
62 #define CAMQP_CHANNEL 1
63
64 /*
65  * Data types
66  */
67 struct camqp_config_s
68 {
69     _Bool   publish;
70     char   *name;
71
72     char   *host;
73     int     port;
74     char   *vhost;
75     char   *user;
76     char   *password;
77
78     char   *exchange;
79     char   *routing_key;
80
81     /* publish only */
82     uint8_t delivery_mode;
83     _Bool   store_rates;
84     int     format;
85     /* publish & graphite format only */
86     char    *prefix;
87     char    *postfix;
88     char    escape_char;
89     unsigned int graphite_flags;
90
91     /* subscribe only */
92     char   *exchange_type;
93     char   *queue;
94
95     amqp_connection_state_t connection;
96     pthread_mutex_t lock;
97 };
98 typedef struct camqp_config_s camqp_config_t;
99
100 /*
101  * Global variables
102  */
103 static const char *def_host       = "localhost";
104 static const char *def_vhost      = "/";
105 static const char *def_user       = "guest";
106 static const char *def_password   = "guest";
107 static const char *def_exchange   = "amq.fanout";
108
109 static pthread_t *subscriber_threads     = NULL;
110 static size_t     subscriber_threads_num = 0;
111 static _Bool      subscriber_threads_running = 1;
112
113 #define CONF(c,f) (((c)->f != NULL) ? (c)->f : def_##f)
114
115 /*
116  * Functions
117  */
118 static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
119 {
120     int sockfd;
121
122     if ((conf == NULL) || (conf->connection == NULL))
123         return;
124
125     sockfd = amqp_get_sockfd (conf->connection);
126     amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
127     amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
128     amqp_destroy_connection (conf->connection);
129     close (sockfd);
130     conf->connection = NULL;
131 } /* }}} void camqp_close_connection */
132
133 static void camqp_config_free (void *ptr) /* {{{ */
134 {
135     camqp_config_t *conf = ptr;
136
137     if (conf == NULL)
138         return;
139
140     camqp_close_connection (conf);
141
142     sfree (conf->name);
143     sfree (conf->host);
144     sfree (conf->vhost);
145     sfree (conf->user);
146     sfree (conf->password);
147     sfree (conf->exchange);
148     sfree (conf->exchange_type);
149     sfree (conf->queue);
150     sfree (conf->routing_key);
151     sfree (conf->prefix);
152     sfree (conf->postfix);
153
154
155     sfree (conf);
156 } /* }}} void camqp_config_free */
157
158 static char *camqp_bytes_cstring (amqp_bytes_t *in) /* {{{ */
159 {
160     char *ret;
161
162     if ((in == NULL) || (in->bytes == NULL))
163         return (NULL);
164
165     ret = malloc (in->len + 1);
166     if (ret == NULL)
167         return (NULL);
168
169     memcpy (ret, in->bytes, in->len);
170     ret[in->len] = 0;
171
172     return (ret);
173 } /* }}} char *camqp_bytes_cstring */
174
175 static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */
176 {
177     amqp_rpc_reply_t r;
178
179     r = amqp_get_rpc_reply (conf->connection);
180     if (r.reply_type == AMQP_RESPONSE_NORMAL)
181         return (0);
182
183     return (1);
184 } /* }}} _Bool camqp_is_error */
185
186 static char *camqp_strerror (camqp_config_t *conf, /* {{{ */
187         char *buffer, size_t buffer_size)
188 {
189     amqp_rpc_reply_t r;
190
191     r = amqp_get_rpc_reply (conf->connection);
192     switch (r.reply_type)
193     {
194         case AMQP_RESPONSE_NORMAL:
195             sstrncpy (buffer, "Success", sizeof (buffer));
196             break;
197
198         case AMQP_RESPONSE_NONE:
199             sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer));
200             break;
201
202         case AMQP_RESPONSE_LIBRARY_EXCEPTION:
203 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
204             if (r.library_errno)
205                 return (sstrerror (r.library_errno, buffer, buffer_size));
206 #else
207             if (r.library_error)
208                 return (sstrerror (r.library_error, buffer, buffer_size));
209 #endif
210             else
211                 sstrncpy (buffer, "End of stream", sizeof (buffer));
212             break;
213
214         case AMQP_RESPONSE_SERVER_EXCEPTION:
215             if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD)
216             {
217                 amqp_connection_close_t *m = r.reply.decoded;
218                 char *tmp = camqp_bytes_cstring (&m->reply_text);
219                 ssnprintf (buffer, buffer_size, "Server connection error %d: %s",
220                         m->reply_code, tmp);
221                 sfree (tmp);
222             }
223             else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD)
224             {
225                 amqp_channel_close_t *m = r.reply.decoded;
226                 char *tmp = camqp_bytes_cstring (&m->reply_text);
227                 ssnprintf (buffer, buffer_size, "Server channel error %d: %s",
228                         m->reply_code, tmp);
229                 sfree (tmp);
230             }
231             else
232             {
233                 ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32,
234                         r.reply.id);
235             }
236             break;
237
238         default:
239             ssnprintf (buffer, buffer_size, "Unknown reply type %i",
240                     (int) r.reply_type);
241     }
242
243     return (buffer);
244 } /* }}} char *camqp_strerror */
245
246 #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO
247 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
248 {
249     amqp_exchange_declare_ok_t *ed_ret;
250
251     if (conf->exchange_type == NULL)
252         return (0);
253
254     ed_ret = amqp_exchange_declare (conf->connection,
255             /* channel     = */ CAMQP_CHANNEL,
256             /* exchange    = */ amqp_cstring_bytes (conf->exchange),
257             /* type        = */ amqp_cstring_bytes (conf->exchange_type),
258             /* passive     = */ 0,
259             /* durable     = */ 0,
260             /* auto_delete = */ 1,
261             /* arguments   = */ AMQP_EMPTY_TABLE);
262     if ((ed_ret == NULL) && camqp_is_error (conf))
263     {
264         char errbuf[1024];
265         ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
266                 camqp_strerror (conf, errbuf, sizeof (errbuf)));
267         camqp_close_connection (conf);
268         return (-1);
269     }
270
271     INFO ("amqp plugin: Successfully created exchange \"%s\" "
272             "with type \"%s\".",
273             conf->exchange, conf->exchange_type);
274
275     return (0);
276 } /* }}} int camqp_create_exchange */
277 #else
278 static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */
279 {
280     amqp_exchange_declare_ok_t *ed_ret;
281     amqp_table_t argument_table;
282     struct amqp_table_entry_t_ argument_table_entries[1];
283
284     if (conf->exchange_type == NULL)
285         return (0);
286
287     /* Valid arguments: "auto_delete", "internal" */
288     argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries);
289     argument_table.entries = argument_table_entries;
290     argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete");
291     argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN;
292     argument_table_entries[0].value.value.boolean = 1;
293
294     ed_ret = amqp_exchange_declare (conf->connection,
295             /* channel     = */ CAMQP_CHANNEL,
296             /* exchange    = */ amqp_cstring_bytes (conf->exchange),
297             /* type        = */ amqp_cstring_bytes (conf->exchange_type),
298             /* passive     = */ 0,
299             /* durable     = */ 0,
300             /* arguments   = */ argument_table);
301     if ((ed_ret == NULL) && camqp_is_error (conf))
302     {
303         char errbuf[1024];
304         ERROR ("amqp plugin: amqp_exchange_declare failed: %s",
305                 camqp_strerror (conf, errbuf, sizeof (errbuf)));
306         camqp_close_connection (conf);
307         return (-1);
308     }
309
310     INFO ("amqp plugin: Successfully created exchange \"%s\" "
311             "with type \"%s\".",
312             conf->exchange, conf->exchange_type);
313
314     return (0);
315 } /* }}} int camqp_create_exchange */
316 #endif
317
318 static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
319 {
320     amqp_queue_declare_ok_t *qd_ret;
321     amqp_basic_consume_ok_t *cm_ret;
322
323     qd_ret = amqp_queue_declare (conf->connection,
324             /* channel     = */ CAMQP_CHANNEL,
325             /* queue       = */ (conf->queue != NULL)
326             ? amqp_cstring_bytes (conf->queue)
327             : AMQP_EMPTY_BYTES,
328             /* passive     = */ 0,
329             /* durable     = */ 0,
330             /* exclusive   = */ 0,
331             /* auto_delete = */ 1,
332             /* arguments   = */ AMQP_EMPTY_TABLE);
333     if (qd_ret == NULL)
334     {
335         ERROR ("amqp plugin: amqp_queue_declare failed.");
336         camqp_close_connection (conf);
337         return (-1);
338     }
339
340     if (conf->queue == NULL)
341     {
342         conf->queue = camqp_bytes_cstring (&qd_ret->queue);
343         if (conf->queue == NULL)
344         {
345             ERROR ("amqp plugin: camqp_bytes_cstring failed.");
346             camqp_close_connection (conf);
347             return (-1);
348         }
349
350         INFO ("amqp plugin: Created queue \"%s\".", conf->queue);
351     }
352     DEBUG ("amqp plugin: Successfully created queue \"%s\".", conf->queue);
353
354     /* bind to an exchange */
355     if (conf->exchange != NULL)
356     {
357         amqp_queue_bind_ok_t *qb_ret;
358
359         assert (conf->queue != NULL);
360         qb_ret = amqp_queue_bind (conf->connection,
361                 /* channel     = */ CAMQP_CHANNEL,
362                 /* queue       = */ amqp_cstring_bytes (conf->queue),
363                 /* exchange    = */ amqp_cstring_bytes (conf->exchange),
364                 /* routing_key = */ (conf->routing_key != NULL)
365                 ? amqp_cstring_bytes (conf->routing_key)
366                 : AMQP_EMPTY_BYTES,
367                 /* arguments   = */ AMQP_EMPTY_TABLE);
368         if ((qb_ret == NULL) && camqp_is_error (conf))
369         {
370             char errbuf[1024];
371             ERROR ("amqp plugin: amqp_queue_bind failed: %s",
372                     camqp_strerror (conf, errbuf, sizeof (errbuf)));
373             camqp_close_connection (conf);
374             return (-1);
375         }
376
377         DEBUG ("amqp plugin: Successfully bound queue \"%s\" to exchange \"%s\".",
378                 conf->queue, conf->exchange);
379     } /* if (conf->exchange != NULL) */
380
381     cm_ret = amqp_basic_consume (conf->connection,
382             /* channel      = */ CAMQP_CHANNEL,
383             /* queue        = */ amqp_cstring_bytes (conf->queue),
384             /* consumer_tag = */ AMQP_EMPTY_BYTES,
385             /* no_local     = */ 0,
386             /* no_ack       = */ 1,
387             /* exclusive    = */ 0,
388             /* arguments    = */ AMQP_EMPTY_TABLE
389         );
390     if ((cm_ret == NULL) && camqp_is_error (conf))
391     {
392         char errbuf[1024];
393         ERROR ("amqp plugin: amqp_basic_consume failed: %s",
394                     camqp_strerror (conf, errbuf, sizeof (errbuf)));
395         camqp_close_connection (conf);
396         return (-1);
397     }
398
399     return (0);
400 } /* }}} int camqp_setup_queue */
401
402 static int camqp_connect (camqp_config_t *conf) /* {{{ */
403 {
404     amqp_rpc_reply_t reply;
405     int status;
406 #ifdef HAVE_AMQP_TCP_SOCKET
407     amqp_socket_t *socket;
408 #else
409     int sockfd;
410 #endif
411
412     if (conf->connection != NULL)
413         return (0);
414
415     conf->connection = amqp_new_connection ();
416     if (conf->connection == NULL)
417     {
418         ERROR ("amqp plugin: amqp_new_connection failed.");
419         return (ENOMEM);
420     }
421
422 #ifdef HAVE_AMQP_TCP_SOCKET
423 # define CLOSE_SOCKET() amqp_socket_close (socket)
424     /* TODO: add support for SSL using amqp_ssl_socket_new
425      *       and related functions */
426     socket = amqp_tcp_socket_new (conf->connection);
427     if (! socket)
428     {
429         ERROR ("amqp plugin: amqp_tcp_socket_new failed.");
430         amqp_destroy_connection (conf->connection);
431         conf->connection = NULL;
432         return (ENOMEM);
433     }
434
435     status = amqp_socket_open (socket, CONF(conf, host), conf->port);
436     if (status < 0)
437     {
438         char errbuf[1024];
439         status *= -1;
440         ERROR ("amqp plugin: amqp_socket_open failed: %s",
441                 sstrerror (status, errbuf, sizeof (errbuf)));
442         CLOSE_SOCKET ();
443         amqp_destroy_connection (conf->connection);
444         conf->connection = NULL;
445         return (status);
446     }
447 #else /* HAVE_AMQP_TCP_SOCKET */
448 # define CLOSE_SOCKET close(sockfd)
449     /* this interface is deprecated as of rabbitmq-c 0.4 */
450     sockfd = amqp_open_socket (CONF(conf, host), conf->port);
451     if (sockfd < 0)
452     {
453         char errbuf[1024];
454         status = (-1) * sockfd;
455         ERROR ("amqp plugin: amqp_open_socket failed: %s",
456                 sstrerror (status, errbuf, sizeof (errbuf)));
457         amqp_destroy_connection (conf->connection);
458         conf->connection = NULL;
459         return (status);
460     }
461     amqp_set_sockfd (conf->connection, sockfd);
462 #endif
463
464     reply = amqp_login (conf->connection, CONF(conf, vhost),
465             /* channel max = */      0,
466             /* frame max   = */ 131072,
467             /* heartbeat   = */      0,
468             /* authentication = */ AMQP_SASL_METHOD_PLAIN,
469             CONF(conf, user), CONF(conf, password));
470     if (reply.reply_type != AMQP_RESPONSE_NORMAL)
471     {
472         ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.",
473                 CONF(conf, vhost), CONF(conf, user));
474         amqp_destroy_connection (conf->connection);
475         CLOSE_SOCKET ();
476         conf->connection = NULL;
477         return (1);
478     }
479
480     amqp_channel_open (conf->connection, /* channel = */ 1);
481     /* FIXME: Is checking "reply.reply_type" really correct here? How does
482      * it get set? --octo */
483     if (reply.reply_type != AMQP_RESPONSE_NORMAL)
484     {
485         ERROR ("amqp plugin: amqp_channel_open failed.");
486         amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
487         amqp_destroy_connection (conf->connection);
488         CLOSE_SOCKET ();
489         conf->connection = NULL;
490         return (1);
491     }
492
493     INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" "
494             "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port);
495
496     status = camqp_create_exchange (conf);
497     if (status != 0)
498         return (status);
499
500     if (!conf->publish)
501         return (camqp_setup_queue (conf));
502     return (0);
503 } /* }}} int camqp_connect */
504
505 static int camqp_shutdown (void) /* {{{ */
506 {
507     size_t i;
508
509     DEBUG ("amqp plugin: Shutting down %zu subscriber threads.",
510             subscriber_threads_num);
511
512     subscriber_threads_running = 0;
513     for (i = 0; i < subscriber_threads_num; i++)
514     {
515         /* FIXME: Sending a signal is not very elegant here. Maybe find out how
516          * to use a timeout in the thread and check for the variable in regular
517          * intervals. */
518         pthread_kill (subscriber_threads[i], SIGTERM);
519         pthread_join (subscriber_threads[i], /* retval = */ NULL);
520     }
521
522     subscriber_threads_num = 0;
523     sfree (subscriber_threads);
524
525     DEBUG ("amqp plugin: All subscriber threads exited.");
526
527     return (0);
528 } /* }}} int camqp_shutdown */
529
530 /*
531  * Subscribing code
532  */
533 static int camqp_read_body (camqp_config_t *conf, /* {{{ */
534         size_t body_size, const char *content_type)
535 {
536     char body[body_size + 1];
537     char *body_ptr;
538     size_t received;
539     amqp_frame_t frame;
540     int status;
541
542     memset (body, 0, sizeof (body));
543     body_ptr = &body[0];
544     received = 0;
545
546     while (received < body_size)
547     {
548         status = amqp_simple_wait_frame (conf->connection, &frame);
549         if (status < 0)
550         {
551             char errbuf[1024];
552             status = (-1) * status;
553             ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
554                     sstrerror (status, errbuf, sizeof (errbuf)));
555             camqp_close_connection (conf);
556             return (status);
557         }
558
559         if (frame.frame_type != AMQP_FRAME_BODY)
560         {
561             NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
562                     frame.frame_type);
563             return (-1);
564         }
565
566         if ((body_size - received) < frame.payload.body_fragment.len)
567         {
568             WARNING ("amqp plugin: Body is larger than indicated by header.");
569             return (-1);
570         }
571
572         memcpy (body_ptr, frame.payload.body_fragment.bytes,
573                 frame.payload.body_fragment.len);
574         body_ptr += frame.payload.body_fragment.len;
575         received += frame.payload.body_fragment.len;
576     } /* while (received < body_size) */
577
578     if (strcasecmp ("text/collectd", content_type) == 0)
579     {
580         status = handle_putval (stderr, body);
581         if (status != 0)
582             ERROR ("amqp plugin: handle_putval failed with status %i.",
583                     status);
584         return (status);
585     }
586     else if (strcasecmp ("application/json", content_type) == 0)
587     {
588         ERROR ("amqp plugin: camqp_read_body: Parsing JSON data has not "
589                 "been implemented yet. FIXME!");
590         return (0);
591     }
592     else
593     {
594         ERROR ("amqp plugin: camqp_read_body: Unknown content type \"%s\".",
595                 content_type);
596         return (EINVAL);
597     }
598
599     /* not reached */
600     return (0);
601 } /* }}} int camqp_read_body */
602
603 static int camqp_read_header (camqp_config_t *conf) /* {{{ */
604 {
605     int status;
606     amqp_frame_t frame;
607     amqp_basic_properties_t *properties;
608     char *content_type;
609
610     status = amqp_simple_wait_frame (conf->connection, &frame);
611     if (status < 0)
612     {
613         char errbuf[1024];
614         status = (-1) * status;
615         ERROR ("amqp plugin: amqp_simple_wait_frame failed: %s",
616                     sstrerror (status, errbuf, sizeof (errbuf)));
617         camqp_close_connection (conf);
618         return (status);
619     }
620
621     if (frame.frame_type != AMQP_FRAME_HEADER)
622     {
623         NOTICE ("amqp plugin: Unexpected frame type: %#"PRIx8,
624                 frame.frame_type);
625         return (-1);
626     }
627
628     properties = frame.payload.properties.decoded;
629     content_type = camqp_bytes_cstring (&properties->content_type);
630     if (content_type == NULL)
631     {
632         ERROR ("amqp plugin: Unable to determine content type.");
633         return (-1);
634     }
635
636     status = camqp_read_body (conf,
637             (size_t) frame.payload.properties.body_size,
638             content_type);
639
640     sfree (content_type);
641     return (status);
642 } /* }}} int camqp_read_header */
643
644 static void *camqp_subscribe_thread (void *user_data) /* {{{ */
645 {
646     camqp_config_t *conf = user_data;
647     int status;
648
649     cdtime_t interval = plugin_get_interval ();
650
651     while (subscriber_threads_running)
652     {
653         amqp_frame_t frame;
654
655         status = camqp_connect (conf);
656         if (status != 0)
657         {
658             struct timespec ts_interval;
659             ERROR ("amqp plugin: camqp_connect failed. "
660                     "Will sleep for %.3f seconds.",
661                     CDTIME_T_TO_DOUBLE (interval));
662             CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
663             nanosleep (&ts_interval, /* remaining = */ NULL);
664             continue;
665         }
666
667         status = amqp_simple_wait_frame (conf->connection, &frame);
668         if (status < 0)
669         {
670             struct timespec ts_interval;
671             ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
672                     "Will sleep for %.3f seconds.",
673                     CDTIME_T_TO_DOUBLE (interval));
674             camqp_close_connection (conf);
675             CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
676             nanosleep (&ts_interval, /* remaining = */ NULL);
677             continue;
678         }
679
680         if (frame.frame_type != AMQP_FRAME_METHOD)
681         {
682             DEBUG ("amqp plugin: Unexpected frame type: %#"PRIx8,
683                     frame.frame_type);
684             continue;
685         }
686
687         if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
688         {
689             DEBUG ("amqp plugin: Unexpected method id: %#"PRIx32,
690                     frame.payload.method.id);
691             continue;
692         }
693
694         status = camqp_read_header (conf);
695
696         amqp_maybe_release_buffers (conf->connection);
697     } /* while (subscriber_threads_running) */
698
699     camqp_config_free (conf);
700     pthread_exit (NULL);
701     return (NULL);
702 } /* }}} void *camqp_subscribe_thread */
703
704 static int camqp_subscribe_init (camqp_config_t *conf) /* {{{ */
705 {
706     int status;
707     pthread_t *tmp;
708
709     tmp = realloc (subscriber_threads,
710             sizeof (*subscriber_threads) * (subscriber_threads_num + 1));
711     if (tmp == NULL)
712     {
713         ERROR ("amqp plugin: realloc failed.");
714         camqp_config_free (conf);
715         return (ENOMEM);
716     }
717     subscriber_threads = tmp;
718     tmp = subscriber_threads + subscriber_threads_num;
719     memset (tmp, 0, sizeof (*tmp));
720
721     status = plugin_thread_create (tmp, /* attr = */ NULL,
722             camqp_subscribe_thread, conf);
723     if (status != 0)
724     {
725         char errbuf[1024];
726         ERROR ("amqp plugin: pthread_create failed: %s",
727                 sstrerror (status, errbuf, sizeof (errbuf)));
728         camqp_config_free (conf);
729         return (status);
730     }
731
732     subscriber_threads_num++;
733
734     return (0);
735 } /* }}} int camqp_subscribe_init */
736
737 /*
738  * Publishing code
739  */
740 /* XXX: You must hold "conf->lock" when calling this function! */
741 static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
742         const char *buffer, const char *routing_key)
743 {
744     amqp_basic_properties_t props;
745     int status;
746
747     status = camqp_connect (conf);
748     if (status != 0)
749         return (status);
750
751     memset (&props, 0, sizeof (props));
752     props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
753         | AMQP_BASIC_DELIVERY_MODE_FLAG
754         | AMQP_BASIC_APP_ID_FLAG;
755     if (conf->format == CAMQP_FORMAT_COMMAND)
756         props.content_type = amqp_cstring_bytes("text/collectd");
757     else if (conf->format == CAMQP_FORMAT_JSON)
758         props.content_type = amqp_cstring_bytes("application/json");
759     else if (conf->format == CAMQP_FORMAT_GRAPHITE)
760         props.content_type = amqp_cstring_bytes("text/graphite");
761     else
762         assert (23 == 42);
763     props.delivery_mode = conf->delivery_mode;
764     props.app_id = amqp_cstring_bytes("collectd");
765
766     status = amqp_basic_publish(conf->connection,
767                 /* channel = */ 1,
768                 amqp_cstring_bytes(CONF(conf, exchange)),
769                 amqp_cstring_bytes (routing_key),
770                 /* mandatory = */ 0,
771                 /* immediate = */ 0,
772                 &props,
773                 amqp_cstring_bytes(buffer));
774     if (status != 0)
775     {
776         ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
777                 status);
778         camqp_close_connection (conf);
779     }
780
781     return (status);
782 } /* }}} int camqp_write_locked */
783
784 static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
785         user_data_t *user_data)
786 {
787     camqp_config_t *conf = user_data->data;
788     char routing_key[6 * DATA_MAX_NAME_LEN];
789     char buffer[4096];
790     int status;
791
792     if ((ds == NULL) || (vl == NULL) || (conf == NULL))
793         return (EINVAL);
794
795     memset (buffer, 0, sizeof (buffer));
796
797     if (conf->routing_key != NULL)
798     {
799         sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
800     }
801     else
802     {
803         size_t i;
804         ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
805                 vl->host,
806                 vl->plugin, vl->plugin_instance,
807                 vl->type, vl->type_instance);
808
809         /* Switch slashes (the only character forbidden by collectd) and dots
810          * (the separation character used by AMQP). */
811         for (i = 0; routing_key[i] != 0; i++)
812         {
813             if (routing_key[i] == '.')
814                 routing_key[i] = '/';
815             else if (routing_key[i] == '/')
816                 routing_key[i] = '.';
817         }
818     }
819
820     if (conf->format == CAMQP_FORMAT_COMMAND)
821     {
822         status = create_putval (buffer, sizeof (buffer), ds, vl);
823         if (status != 0)
824         {
825             ERROR ("amqp plugin: create_putval failed with status %i.",
826                     status);
827             return (status);
828         }
829     }
830     else if (conf->format == CAMQP_FORMAT_JSON)
831     {
832         size_t bfree = sizeof (buffer);
833         size_t bfill = 0;
834
835         format_json_initialize (buffer, &bfill, &bfree);
836         format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
837         format_json_finalize (buffer, &bfill, &bfree);
838     }
839     else if (conf->format == CAMQP_FORMAT_GRAPHITE)
840     {
841         status = format_graphite (buffer, sizeof (buffer), ds, vl,
842                     conf->prefix, conf->postfix, conf->escape_char,
843                     conf->graphite_flags);
844         if (status != 0)
845         {
846             ERROR ("amqp plugin: format_graphite failed with status %i.",
847                     status);
848             return (status);
849         }
850     }
851     else
852     {
853         ERROR ("amqp plugin: Invalid format (%i).", conf->format);
854         return (-1);
855     }
856
857     pthread_mutex_lock (&conf->lock);
858     status = camqp_write_locked (conf, buffer, routing_key);
859     pthread_mutex_unlock (&conf->lock);
860
861     return (status);
862 } /* }}} int camqp_write */
863
864 /*
865  * Config handling
866  */
867 static int camqp_config_set_format (oconfig_item_t *ci, /* {{{ */
868         camqp_config_t *conf)
869 {
870     char *string;
871     int status;
872
873     string = NULL;
874     status = cf_util_get_string (ci, &string);
875     if (status != 0)
876         return (status);
877
878     assert (string != NULL);
879     if (strcasecmp ("Command", string) == 0)
880         conf->format = CAMQP_FORMAT_COMMAND;
881     else if (strcasecmp ("JSON", string) == 0)
882         conf->format = CAMQP_FORMAT_JSON;
883     else if (strcasecmp ("Graphite", string) == 0)
884         conf->format = CAMQP_FORMAT_GRAPHITE;
885     else
886     {
887         WARNING ("amqp plugin: Invalid format string: %s",
888                 string);
889     }
890
891     free (string);
892
893     return (0);
894 } /* }}} int config_set_string */
895
896 static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
897         _Bool publish)
898 {
899     camqp_config_t *conf;
900     int status;
901     int i;
902
903     conf = malloc (sizeof (*conf));
904     if (conf == NULL)
905     {
906         ERROR ("amqp plugin: malloc failed.");
907         return (ENOMEM);
908     }
909
910     /* Initialize "conf" {{{ */
911     memset (conf, 0, sizeof (*conf));
912     conf->publish = publish;
913     conf->name = NULL;
914     conf->format = CAMQP_FORMAT_COMMAND;
915     conf->host = NULL;
916     conf->port = 5672;
917     conf->vhost = NULL;
918     conf->user = NULL;
919     conf->password = NULL;
920     conf->exchange = NULL;
921     conf->routing_key = NULL;
922     /* publish only */
923     conf->delivery_mode = CAMQP_DM_VOLATILE;
924     conf->store_rates = 0;
925     /* publish & graphite only */
926     conf->prefix = NULL;
927     conf->postfix = NULL;
928     conf->escape_char = '_';
929     /* subscribe only */
930     conf->exchange_type = NULL;
931     conf->queue = NULL;
932     /* general */
933     conf->connection = NULL;
934     pthread_mutex_init (&conf->lock, /* attr = */ NULL);
935     /* }}} */
936
937     status = cf_util_get_string (ci, &conf->name);
938     if (status != 0)
939     {
940         sfree (conf);
941         return (status);
942     }
943
944     for (i = 0; i < ci->children_num; i++)
945     {
946         oconfig_item_t *child = ci->children + i;
947
948         if (strcasecmp ("Host", child->key) == 0)
949             status = cf_util_get_string (child, &conf->host);
950         else if (strcasecmp ("Port", child->key) == 0)
951         {
952             status = cf_util_get_port_number (child);
953             if (status > 0)
954             {
955                 conf->port = status;
956                 status = 0;
957             }
958         }
959         else if (strcasecmp ("VHost", child->key) == 0)
960             status = cf_util_get_string (child, &conf->vhost);
961         else if (strcasecmp ("User", child->key) == 0)
962             status = cf_util_get_string (child, &conf->user);
963         else if (strcasecmp ("Password", child->key) == 0)
964             status = cf_util_get_string (child, &conf->password);
965         else if (strcasecmp ("Exchange", child->key) == 0)
966             status = cf_util_get_string (child, &conf->exchange);
967         else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
968             status = cf_util_get_string (child, &conf->exchange_type);
969         else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
970             status = cf_util_get_string (child, &conf->queue);
971         else if (strcasecmp ("RoutingKey", child->key) == 0)
972             status = cf_util_get_string (child, &conf->routing_key);
973         else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
974         {
975             _Bool tmp = 0;
976             status = cf_util_get_boolean (child, &tmp);
977             if (tmp)
978                 conf->delivery_mode = CAMQP_DM_PERSISTENT;
979             else
980                 conf->delivery_mode = CAMQP_DM_VOLATILE;
981         }
982         else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
983         {
984             status = cf_util_get_boolean (child, &conf->store_rates);
985             (void) cf_util_get_flag (child, &conf->graphite_flags,
986                     GRAPHITE_STORE_RATES);
987         }
988         else if ((strcasecmp ("Format", child->key) == 0) && publish)
989             status = camqp_config_set_format (child, conf);
990         else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
991             status = cf_util_get_string (child, &conf->prefix);
992         else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
993             status = cf_util_get_string (child, &conf->postfix);
994         else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
995         {
996             char *tmp_buff = NULL;
997             status = cf_util_get_string (child, &tmp_buff);
998             if (strlen (tmp_buff) > 1)
999                 WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
1000                         "only one character. Others will be ignored.");
1001             conf->escape_char = tmp_buff[0];
1002             sfree (tmp_buff);
1003         }
1004         else
1005             WARNING ("amqp plugin: Ignoring unknown "
1006                     "configuration option \"%s\".", child->key);
1007
1008         if (status != 0)
1009             break;
1010     } /* for (i = 0; i < ci->children_num; i++) */
1011
1012     if ((status == 0) && (conf->exchange == NULL))
1013     {
1014         if (conf->exchange_type != NULL)
1015             WARNING ("amqp plugin: The option \"ExchangeType\" was given "
1016                     "without the \"Exchange\" option. It will be ignored.");
1017
1018         if (!publish && (conf->routing_key != NULL))
1019             WARNING ("amqp plugin: The option \"RoutingKey\" was given "
1020                     "without the \"Exchange\" option. It will be ignored.");
1021
1022     }
1023
1024     if (status != 0)
1025     {
1026         camqp_config_free (conf);
1027         return (status);
1028     }
1029
1030     if (conf->exchange != NULL)
1031     {
1032         DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
1033                 conf->exchange);
1034     }
1035
1036     if (publish)
1037     {
1038         char cbname[128];
1039         user_data_t ud = { conf, camqp_config_free };
1040
1041         ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);
1042
1043         status = plugin_register_write (cbname, camqp_write, &ud);
1044         if (status != 0)
1045         {
1046             camqp_config_free (conf);
1047             return (status);
1048         }
1049     }
1050     else
1051     {
1052         status = camqp_subscribe_init (conf);
1053         if (status != 0)
1054         {
1055             camqp_config_free (conf);
1056             return (status);
1057         }
1058     }
1059
1060     return (0);
1061 } /* }}} int camqp_config_connection */
1062
1063 static int camqp_config (oconfig_item_t *ci) /* {{{ */
1064 {
1065     int i;
1066
1067     for (i = 0; i < ci->children_num; i++)
1068     {
1069         oconfig_item_t *child = ci->children + i;
1070
1071         if (strcasecmp ("Publish", child->key) == 0)
1072             camqp_config_connection (child, /* publish = */ 1);
1073         else if (strcasecmp ("Subscribe", child->key) == 0)
1074             camqp_config_connection (child, /* publish = */ 0);
1075         else
1076             WARNING ("amqp plugin: Ignoring unknown config option \"%s\".",
1077                     child->key);
1078     } /* for (ci->children_num) */
1079
1080     return (0);
1081 } /* }}} int camqp_config */
1082
1083 void module_register (void)
1084 {
1085     plugin_register_complex_config ("amqp", camqp_config);
1086     plugin_register_shutdown ("amqp", camqp_shutdown);
1087 } /* void module_register */
1088
1089 /* vim: set sw=4 sts=4 et fdm=marker : */