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