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