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