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