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