2 * collectd - src/amqp1.c
3 * Copyright(c) 2017 Red Hat Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Andy Smith <ansmith@redhat.com>
31 #include "utils_cmd_putval.h"
32 #include "utils_deq.h"
33 #include "utils_format_graphite.h"
34 #include "utils_format_json.h"
35 #include "utils_random.h"
37 #include <proton/condition.h>
38 #include <proton/connection.h>
39 #include <proton/delivery.h>
40 #include <proton/link.h>
41 #include <proton/message.h>
42 #include <proton/proactor.h>
43 #include <proton/sasl.h>
44 #include <proton/session.h>
45 #include <proton/transport.h>
53 #define AMQP1_FORMAT_JSON 0
54 #define AMQP1_FORMAT_COMMAND 1
55 #define AMQP1_FORMAT_GRAPHITE 2
57 typedef struct amqp1_config_transport_s {
58 DEQ_LINKS(struct amqp1_config_transport_s);
66 } amqp1_config_transport_t;
68 typedef struct amqp1_config_instance_s {
69 DEQ_LINKS(struct amqp1_config_instance_s);
73 unsigned int graphite_flags;
80 } amqp1_config_instance_t;
82 DEQ_DECLARE(amqp1_config_instance_t, amqp1_config_instance_list_t);
84 typedef struct cd_message_s {
85 DEQ_LINKS(struct cd_message_s);
87 amqp1_config_instance_t *instance;
90 DEQ_DECLARE(cd_message_t, cd_message_list_t);
95 static pn_connection_t *conn;
96 static pn_link_t *sender;
97 static pn_proactor_t *proactor;
98 static pthread_mutex_t send_lock;
99 static cd_message_list_t out_messages;
100 static uint64_t cd_tag = 1;
101 static uint64_t acknowledged;
102 static amqp1_config_transport_t *transport;
103 static bool stopping;
104 static bool event_thread_running;
105 static pthread_t event_thread_id;
110 static void cd_message_free(cd_message_t *cdm) {
111 free(cdm->mbuf.start);
113 } /* }}} void cd_message_free */
115 static int amqp1_send_out_messages(pn_link_t *link) /* {{{ */
118 cd_message_list_t to_send;
120 int link_credit = pn_link_credit(link);
130 pthread_mutex_lock(&send_lock);
132 if (link_credit > 0) {
134 cdm = DEQ_HEAD(out_messages);
136 DEQ_REMOVE_HEAD(out_messages);
137 DEQ_INSERT_TAIL(to_send, cdm);
138 if (DEQ_SIZE(to_send) == (size_t)link_credit)
140 cdm = DEQ_HEAD(out_messages);
142 cd_tag += DEQ_SIZE(to_send);
145 pthread_mutex_unlock(&send_lock);
147 /* message is already formatted and encoded */
148 cdm = DEQ_HEAD(to_send);
150 DEQ_REMOVE_HEAD(to_send);
152 dlv = pn_delivery(link, pn_dtag((const char *)&dtag, sizeof(dtag)));
153 pn_link_send(link, cdm->mbuf.start, cdm->mbuf.size);
154 pn_link_advance(link);
155 if (cdm->instance->pre_settle == true) {
156 pn_delivery_settle(dlv);
159 cd_message_free(cdm);
160 cdm = DEQ_HEAD(to_send);
164 } /* }}} int amqp1_send_out_messages */
166 static void check_condition(pn_event_t *e, pn_condition_t *cond) /* {{{ */
168 if (pn_condition_is_set(cond)) {
169 ERROR("amqp1 plugin: %s: %s: %s", pn_event_type_name(pn_event_type(e)),
170 pn_condition_get_name(cond), pn_condition_get_description(cond));
171 pn_connection_close(pn_event_connection(e));
174 } /* }}} void check_condition */
176 static bool handle(pn_event_t *event) /* {{{ */
179 switch (pn_event_type(event)) {
181 case PN_CONNECTION_INIT: {
182 conn = pn_event_connection(event);
183 pn_connection_set_container(conn, transport->name);
184 pn_connection_open(conn);
185 pn_session_t *ssn = pn_session(conn);
186 pn_session_open(ssn);
187 sender = pn_sender(ssn, "cd-sender");
188 pn_link_set_snd_settle_mode(sender, PN_SND_MIXED);
189 pn_link_open(sender);
194 /* peer has given us credit, send outbound messages */
195 amqp1_send_out_messages(sender);
200 /* acknowledgement from peer that a message was delivered */
201 pn_delivery_t *dlv = pn_event_delivery(event);
202 if (pn_delivery_remote_state(dlv) == PN_ACCEPTED) {
203 pn_delivery_settle(dlv);
209 case PN_CONNECTION_WAKE: {
211 amqp1_send_out_messages(sender);
216 case PN_TRANSPORT_CLOSED: {
217 check_condition(event, pn_transport_condition(pn_event_transport(event)));
221 case PN_CONNECTION_REMOTE_CLOSE: {
222 check_condition(event,
223 pn_session_remote_condition(pn_event_session(event)));
224 pn_connection_close(pn_event_connection(event));
228 case PN_SESSION_REMOTE_CLOSE: {
229 check_condition(event,
230 pn_session_remote_condition(pn_event_session(event)));
231 pn_connection_close(pn_event_connection(event));
235 case PN_LINK_REMOTE_CLOSE:
236 case PN_LINK_REMOTE_DETACH: {
237 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
238 pn_connection_close(pn_event_connection(event));
242 case PN_PROACTOR_INACTIVE: {
250 } /* }}} bool handle */
252 static void *event_thread(void __attribute__((unused)) * arg) /* {{{ */
254 char addr[PN_MAX_ADDR];
258 proactor = pn_proactor();
259 pn_proactor_addr(addr, sizeof(addr), transport->host, transport->port);
262 /* make connection */
263 conn = pn_connection();
264 if (transport->user != NULL) {
265 pn_connection_set_user(conn, transport->user);
266 pn_connection_set_password(conn, transport->password);
268 pn_proactor_connect(proactor, conn, addr);
270 bool engine_running = true;
271 while (engine_running && !stopping) {
272 pn_event_batch_t *events = pn_proactor_wait(proactor);
274 while ((e = pn_event_batch_next(events))) {
275 engine_running = handle(e);
276 if (!engine_running) {
280 pn_proactor_done(proactor, events);
283 pn_proactor_release_connection(conn);
285 DEBUG("amqp1 plugin: retrying connection");
286 int delay = transport->retry_delay;
287 while (delay-- > 0 && !stopping) {
292 pn_proactor_disconnect(proactor, NULL);
294 /* Free the remaining out_messages */
295 cdm = DEQ_HEAD(out_messages);
297 DEQ_REMOVE_HEAD(out_messages);
298 cd_message_free(cdm);
299 cdm = DEQ_HEAD(out_messages);
302 event_thread_running = false;
305 } /* }}} void event_thread */
307 static int encqueue(cd_message_t *cdm,
308 amqp1_config_instance_t *instance) /* {{{ */
311 pn_message_t *message = pn_message();
312 pn_message_set_address(message, instance->send_to);
313 pn_data_t *body = pn_message_body(message);
315 pn_data_put_binary(body, pn_bytes(cdm->mbuf.size, cdm->mbuf.start));
318 /* put_binary copies and stores so ok to use mbuf */
319 cdm->mbuf.size = BUFSIZE;
323 while ((status = pn_message_encode(message, cdm->mbuf.start,
324 &cdm->mbuf.size)) == PN_OVERFLOW) {
325 DEBUG("amqp1 plugin: increasing message buffer size %zu", cdm->mbuf.size);
327 start = realloc(cdm->mbuf.start, cdm->mbuf.size);
332 cdm->mbuf.start = start;
337 ERROR("amqp1 plugin: error encoding message: %s",
338 pn_error_text(pn_message_error(message)));
339 pn_message_free(message);
343 pthread_mutex_lock(&send_lock);
344 DEQ_INSERT_TAIL(out_messages, cdm);
345 pthread_mutex_unlock(&send_lock);
347 pn_message_free(message);
349 /* activate the sender */
351 pn_connection_wake(conn);
355 } /* }}} int encqueue */
357 static int amqp1_notify(notification_t const *n,
358 user_data_t *user_data) /* {{{ */
361 size_t bfree = BUFSIZE;
363 size_t bufsize = BUFSIZE;
365 if (n == NULL || user_data == NULL)
368 amqp1_config_instance_t *instance = user_data->data;
370 if (instance->notify != true) {
371 ERROR("amqp1 plugin: write notification failed");
374 cd_message_t *cdm = malloc(sizeof(*cdm));
376 ERROR("amqp1 plugin: notify failed");
381 char *start = malloc(bufsize);
383 ERROR("amqp1 plugin: malloc failed");
387 cdm->mbuf.size = bufsize;
388 cdm->mbuf.start = start;
389 cdm->instance = instance;
391 switch (instance->format) {
392 case AMQP1_FORMAT_JSON:
393 format_json_initialize(cdm->mbuf.start, &bfill, &bfree);
394 status = format_json_notification(cdm->mbuf.start, bufsize, n);
396 ERROR("amqp1 plugin: formatting notification failed");
397 cd_message_free(cdm);
400 cdm->mbuf.size = strlen(cdm->mbuf.start);
401 if (cdm->mbuf.size >= BUFSIZE) {
402 ERROR("amqp1 plugin: notify format json failed");
403 cd_message_free(cdm);
408 ERROR("amqp1 plugin: Invalid notify format (%i).", instance->format);
409 cd_message_free(cdm);
413 /* encode message and place on outbound queue */
414 status = encqueue(cdm, instance);
416 ERROR("amqp1 plugin: notify enqueue failed");
417 cd_message_free(cdm);
421 } /* }}} int amqp1_notify */
423 static int amqp1_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
424 user_data_t *user_data) {
426 size_t bfree = BUFSIZE;
428 size_t bufsize = BUFSIZE;
430 if (ds == NULL || vl == NULL || transport == NULL || user_data == NULL)
433 amqp1_config_instance_t *instance = user_data->data;
435 if (instance->notify != false) {
436 ERROR("amqp1 plugin: write failed");
439 cd_message_t *cdm = malloc(sizeof(*cdm));
441 ERROR("amqp1 plugin: malloc failed.");
445 char *start = malloc(bufsize);
447 ERROR("amqp1 plugin: malloc failed.");
451 cdm->mbuf.size = bufsize;
452 cdm->mbuf.start = start;
453 cdm->instance = instance;
455 switch (instance->format) {
456 case AMQP1_FORMAT_COMMAND:
457 status = cmd_create_putval((char *)cdm->mbuf.start, bufsize, ds, vl);
459 ERROR("amqp1 plugin: cmd_create_putval failed with status %i.", status);
460 cd_message_free(cdm);
463 cdm->mbuf.size = strlen(cdm->mbuf.start);
464 if (cdm->mbuf.size >= BUFSIZE) {
465 ERROR("amqp1 plugin: format cmd failed");
466 cd_message_free(cdm);
470 case AMQP1_FORMAT_JSON:
471 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
472 format_json_value_list((char *)cdm->mbuf.start, &bfill, &bfree, ds, vl,
473 instance->store_rates);
474 status= format_json_finalize((char *)cdm->mbuf.start, &bfill, &bfree);
476 ERROR("amqp1 plugin: format_json_finalize failed with status %i.", status);
477 cd_message_free(cdm);
480 cdm->mbuf.size = strlen(cdm->mbuf.start);
481 if (cdm->mbuf.size >= BUFSIZE) {
482 ERROR("amqp1 plugin: format graphite failed");
483 cd_message_free(cdm);
487 case AMQP1_FORMAT_GRAPHITE:
488 status = format_graphite((char *)cdm->mbuf.start, bufsize, ds, vl,
489 instance->prefix, instance->postfix,
490 instance->escape_char, instance->graphite_flags);
492 ERROR("amqp1 plugin: format_graphite failed with status %i.", status);
493 cd_message_free(cdm);
496 cdm->mbuf.size = strlen(cdm->mbuf.start);
497 if (cdm->mbuf.size >= BUFSIZE) {
498 ERROR("amqp1 plugin: format graphite failed");
499 cd_message_free(cdm);
504 ERROR("amqp1 plugin: Invalid write format (%i).", instance->format);
505 cd_message_free(cdm);
509 /* encode message and place on outbound queue */
510 status = encqueue(cdm, instance);
512 ERROR("amqp1 plugin: write enqueue failed");
513 cd_message_free(cdm);
517 } /* }}} int amqp1_write */
519 static void amqp1_config_transport_free(void *ptr) /* {{{ */
521 amqp1_config_transport_t *transport = ptr;
523 if (transport == NULL)
526 sfree(transport->name);
527 sfree(transport->host);
528 sfree(transport->port);
529 sfree(transport->user);
530 sfree(transport->password);
531 sfree(transport->address);
534 } /* }}} void amqp1_config_transport_free */
536 static void amqp1_config_instance_free(void *ptr) /* {{{ */
538 amqp1_config_instance_t *instance = ptr;
540 if (instance == NULL)
543 sfree(instance->name);
544 sfree(instance->prefix);
545 sfree(instance->postfix);
548 } /* }}} void amqp1_config_instance_free */
550 static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */
552 amqp1_config_instance_t *instance = calloc(1, sizeof(*instance));
553 if (instance == NULL) {
554 ERROR("amqp1 plugin: calloc failed.");
558 int status = cf_util_get_string(ci, &instance->name);
564 for (int i = 0; i < ci->children_num; i++) {
565 oconfig_item_t *child = ci->children + i;
567 if (strcasecmp("PreSettle", child->key) == 0)
568 status = cf_util_get_boolean(child, &instance->pre_settle);
569 else if (strcasecmp("Notify", child->key) == 0)
570 status = cf_util_get_boolean(child, &instance->notify);
571 else if (strcasecmp("Format", child->key) == 0) {
573 status = cf_util_get_string(child, &key);
577 if (strcasecmp(key, "Command") == 0) {
578 instance->format = AMQP1_FORMAT_COMMAND;
579 } else if (strcasecmp(key, "Graphite") == 0) {
580 instance->format = AMQP1_FORMAT_GRAPHITE;
581 } else if (strcasecmp(key, "JSON") == 0) {
582 instance->format = AMQP1_FORMAT_JSON;
584 WARNING("amqp1 plugin: Invalid format string: %s", key);
587 } else if (strcasecmp("StoreRates", child->key) == 0)
588 status = cf_util_get_boolean(child, &instance->store_rates);
589 else if (strcasecmp("GraphiteSeparateInstances", child->key) == 0)
590 status = cf_util_get_flag(child, &instance->graphite_flags,
591 GRAPHITE_SEPARATE_INSTANCES);
592 else if (strcasecmp("GraphiteAlwaysAppendDS", child->key) == 0)
593 status = cf_util_get_flag(child, &instance->graphite_flags,
594 GRAPHITE_ALWAYS_APPEND_DS);
595 else if (strcasecmp("GraphitePreserveSeparator", child->key) == 0)
596 status = cf_util_get_flag(child, &instance->graphite_flags,
597 GRAPHITE_PRESERVE_SEPARATOR);
598 else if (strcasecmp("GraphitePrefix", child->key) == 0)
599 status = cf_util_get_string(child, &instance->prefix);
600 else if (strcasecmp("GraphitePostfix", child->key) == 0)
601 status = cf_util_get_string(child, &instance->postfix);
602 else if (strcasecmp("GraphiteEscapeChar", child->key) == 0) {
603 char *tmp_buff = NULL;
604 status = cf_util_get_string(child, &tmp_buff);
606 if (strlen(tmp_buff) > 1)
607 WARNING("amqp1 plugin: The option \"GraphiteEscapeChar\" handles "
608 "only one character. Others will be ignored.");
609 instance->escape_char = tmp_buff[0];
613 WARNING("amqp1 plugin: Ignoring unknown "
614 "instance configuration option "
622 amqp1_config_instance_free(instance);
625 char tpname[DATA_MAX_NAME_LEN];
626 status = snprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name);
627 if ((status < 0) || (size_t)status >= sizeof(tpname)) {
628 ERROR("amqp1 plugin: Instance name would have been truncated.");
631 status = snprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s",
632 transport->address, instance->name);
633 if ((status < 0) || (size_t)status >= sizeof(instance->send_to)) {
634 ERROR("amqp1 plugin: send_to address would have been truncated.");
637 if (instance->notify) {
638 status = plugin_register_notification(
639 tpname, amqp1_notify,
641 .data = instance, .free_func = amqp1_config_instance_free,
644 status = plugin_register_write(
647 .data = instance, .free_func = amqp1_config_instance_free,
652 amqp1_config_instance_free(instance);
657 } /* }}} int amqp1_config_instance */
659 static int amqp1_config_transport(oconfig_item_t *ci) /* {{{ */
661 transport = calloc(1, sizeof(*transport));
662 if (transport == NULL) {
663 ERROR("amqp1 plugin: calloc failed.");
667 /* Initialize transport configuration {{{ */
668 transport->retry_delay = 1;
670 int status = cf_util_get_string(ci, &transport->name);
676 for (int i = 0; i < ci->children_num; i++) {
677 oconfig_item_t *child = ci->children + i;
679 if (strcasecmp("Host", child->key) == 0)
680 status = cf_util_get_string(child, &transport->host);
681 else if (strcasecmp("Port", child->key) == 0)
682 status = cf_util_get_string(child, &transport->port);
683 else if (strcasecmp("User", child->key) == 0)
684 status = cf_util_get_string(child, &transport->user);
685 else if (strcasecmp("Password", child->key) == 0)
686 status = cf_util_get_string(child, &transport->password);
687 else if (strcasecmp("Address", child->key) == 0)
688 status = cf_util_get_string(child, &transport->address);
689 else if (strcasecmp("RetryDelay", child->key) == 0)
690 status = cf_util_get_int(child, &transport->retry_delay);
691 else if (strcasecmp("Instance", child->key) == 0)
692 amqp1_config_instance(child);
694 WARNING("amqp1 plugin: Ignoring unknown "
695 "transport configuration option "
704 amqp1_config_transport_free(transport);
707 } /* }}} int amqp1_config_transport */
709 static int amqp1_config(oconfig_item_t *ci) /* {{{ */
712 for (int i = 0; i < ci->children_num; i++) {
713 oconfig_item_t *child = ci->children + i;
715 if (strcasecmp("Transport", child->key) == 0)
716 amqp1_config_transport(child);
718 WARNING("amqp1 plugin: Ignoring unknown config option \"%s\".",
723 } /* }}} int amqp1_config */
725 static int amqp1_init(void) /* {{{ */
727 if (transport == NULL) {
728 ERROR("amqp1: init failed, no transport configured");
732 if (proactor == NULL) {
733 pthread_mutex_init(&send_lock, /* attr = */ NULL);
736 plugin_thread_create(&event_thread_id, NULL /* no attributes */,
737 event_thread, NULL /* no argument */, "handle");
739 ERROR("amqp1 plugin: pthread_create failed: %s", STRERRNO);
741 event_thread_running = true;
745 } /* }}} int amqp1_init */
747 static int amqp1_shutdown(void) /* {{{ */
751 /* Stop the proactor thread */
752 if (event_thread_running) {
753 DEBUG("amqp1 plugin: Shutting down proactor thread.");
754 pn_connection_wake(conn);
756 pthread_join(event_thread_id, NULL /* no return value */);
757 memset(&event_thread_id, 0, sizeof(event_thread_id));
759 DEBUG("amqp1 plugin: proactor thread exited.");
762 amqp1_config_transport_free(transport);
766 } /* }}} int amqp1_shutdown */
768 void module_register(void) {
769 plugin_register_complex_config("amqp1", amqp1_config);
770 plugin_register_init("amqp1", amqp1_init);
771 plugin_register_shutdown("amqp1", amqp1_shutdown);
772 } /* void module_register */