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