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_t {
58 DEQ_LINKS(struct amqp1_config_transport_t);
66 } amqp1_config_transport_t;
68 typedef struct amqp1_config_instance_t {
69 DEQ_LINKS(struct amqp1_config_instance_t);
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_t {
85 DEQ_LINKS(struct cd_message_t);
87 amqp1_config_instance_t *instance;
90 DEQ_DECLARE(cd_message_t, cd_message_list_t);
95 pn_connection_t *conn = NULL;
96 pn_link_t *sender = NULL;
97 pn_proactor_t *proactor = NULL;
98 pthread_mutex_t send_lock;
99 cd_message_list_t out_messages;
101 uint64_t acknowledged = 0;
102 amqp1_config_transport_t *transport = NULL;
104 static bool stopping = false;
105 static int event_thread_running = 0;
106 static pthread_t event_thread_id;
111 static void cd_message_free(cd_message_t *cdm) {
112 if (cdm->mbuf.start) {
113 free((void *)cdm->mbuf.start);
116 } /* }}} void cd_message_free */
118 static int amqp1_send_out_messages(pn_link_t *link) /* {{{ */
121 cd_message_list_t to_send;
123 int link_credit = pn_link_credit(link);
133 pthread_mutex_lock(&send_lock);
135 if (link_credit > 0) {
137 cdm = DEQ_HEAD(out_messages);
139 DEQ_REMOVE_HEAD(out_messages);
140 DEQ_INSERT_TAIL(to_send, cdm);
141 if (DEQ_SIZE(to_send) == link_credit)
143 cdm = DEQ_HEAD(out_messages);
145 cd_tag += DEQ_SIZE(to_send);
148 pthread_mutex_unlock(&send_lock);
150 /* message is already formatted and encoded */
151 cdm = DEQ_HEAD(to_send);
153 DEQ_REMOVE_HEAD(to_send);
155 dlv = pn_delivery(link, pn_dtag((const char *)&dtag, sizeof(dtag)));
156 pn_link_send(link, cdm->mbuf.start, cdm->mbuf.size);
157 pn_link_advance(link);
158 if (cdm->instance->pre_settle == true) {
159 pn_delivery_settle(dlv);
162 cd_message_free(cdm);
163 cdm = DEQ_HEAD(to_send);
167 } /* }}} int amqp1_send_out_messages */
169 static void check_condition(pn_event_t *e, pn_condition_t *cond) /* {{{ */
171 if (pn_condition_is_set(cond)) {
172 ERROR("amqp1 plugin: %s: %s: %s", pn_event_type_name(pn_event_type(e)),
173 pn_condition_get_name(cond), pn_condition_get_description(cond));
174 pn_connection_close(pn_event_connection(e));
177 } /* }}} void check_condition */
179 static bool handle(pn_event_t *event) /* {{{ */
182 switch (pn_event_type(event)) {
184 case PN_CONNECTION_INIT: {
185 conn = pn_event_connection(event);
186 pn_connection_set_container(conn, transport->name);
187 pn_connection_open(conn);
188 pn_session_t *ssn = pn_session(conn);
189 pn_session_open(ssn);
190 sender = pn_sender(ssn, "cd-sender");
191 pn_link_set_snd_settle_mode(sender, PN_SND_MIXED);
192 pn_link_open(sender);
197 /* peer has given us credit, send outbound messages */
198 amqp1_send_out_messages(sender);
203 /* acknowledgement from peer that a message was delivered */
204 pn_delivery_t *dlv = pn_event_delivery(event);
205 if (pn_delivery_remote_state(dlv) == PN_ACCEPTED) {
206 pn_delivery_settle(dlv);
212 case PN_CONNECTION_WAKE: {
214 amqp1_send_out_messages(sender);
219 case PN_TRANSPORT_CLOSED: {
220 check_condition(event, pn_transport_condition(pn_event_transport(event)));
224 case PN_CONNECTION_REMOTE_CLOSE: {
225 check_condition(event,
226 pn_session_remote_condition(pn_event_session(event)));
227 pn_connection_close(pn_event_connection(event));
231 case PN_SESSION_REMOTE_CLOSE: {
232 check_condition(event,
233 pn_session_remote_condition(pn_event_session(event)));
234 pn_connection_close(pn_event_connection(event));
238 case PN_LINK_REMOTE_CLOSE:
239 case PN_LINK_REMOTE_DETACH: {
240 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
241 pn_connection_close(pn_event_connection(event));
245 case PN_PROACTOR_INACTIVE: {
253 } /* }}} bool handle */
255 static void *event_thread(void __attribute__((unused)) * arg) /* {{{ */
257 char addr[PN_MAX_ADDR];
261 proactor = pn_proactor();
262 pn_proactor_addr(addr, sizeof(addr), transport->host, transport->port);
265 /* make connection */
266 conn = pn_connection();
267 if (transport->user != NULL) {
268 pn_connection_set_user(conn, transport->user);
269 pn_connection_set_password(conn, transport->password);
271 pn_proactor_connect(proactor, conn, addr);
273 bool engine_running = true;
274 while (engine_running && !stopping) {
275 pn_event_batch_t *events = pn_proactor_wait(proactor);
277 while ((e = pn_event_batch_next(events))) {
278 engine_running = handle(e);
279 if (!engine_running) {
283 pn_proactor_done(proactor, events);
286 pn_proactor_release_connection(conn);
288 DEBUG("amqp1 plugin: retrying connection");
289 int delay = transport->retry_delay;
290 while (delay-- > 0 && !stopping) {
295 pn_proactor_disconnect(proactor, NULL);
297 /* Free the remaining out_messages */
298 cdm = DEQ_HEAD(out_messages);
300 DEQ_REMOVE_HEAD(out_messages);
301 cd_message_free(cdm);
302 cdm = DEQ_HEAD(out_messages);
305 event_thread_running = 0;
308 } /* }}} void event_thread */
310 static int encqueue(cd_message_t *cdm,
311 amqp1_config_instance_t *instance) /* {{{ */
313 size_t bufsize = BUFSIZE;
315 pn_message_t *message;
319 message = pn_message();
320 pn_message_set_address(message, instance->send_to);
321 body = pn_message_body(message);
323 pn_data_put_binary(body, pn_bytes(cdm->mbuf.size, cdm->mbuf.start));
326 /* put_binary copies and stores so ok to use mbuf */
327 cdm->mbuf.size = bufsize;
328 while ((status = pn_message_encode(message, (char *)cdm->mbuf.start,
329 &cdm->mbuf.size)) == PN_OVERFLOW) {
330 DEBUG("amqp1 plugin: increasing message buffer size %i",
331 (int)cdm->mbuf.size);
333 cdm->mbuf.start = (char *)realloc(cdm->mbuf.start, cdm->mbuf.size);
337 ERROR("amqp1 plugin: error encoding message: %s",
338 pn_error_text(pn_message_error(message)));
339 pn_message_free(message);
340 cd_message_free(cdm);
344 pthread_mutex_lock(&send_lock);
345 DEQ_INSERT_TAIL(out_messages, cdm);
346 pthread_mutex_unlock(&send_lock);
348 pn_message_free(message);
350 /* activate the sender */
352 pn_connection_wake(conn);
356 } /* }}} int encqueue */
358 static int amqp1_notify(notification_t const *n,
359 user_data_t *user_data) /* {{{ */
361 amqp1_config_instance_t *instance;
363 size_t bfree = BUFSIZE;
366 size_t bufsize = BUFSIZE;
368 if ((n == NULL) || (user_data == NULL))
371 instance = user_data->data;
373 if (instance->notify != true) {
374 ERROR("amqp1 plugin: write notification failed");
377 cdm = NEW(cd_message_t);
379 cdm->mbuf = pn_rwbytes(bufsize, (char *)malloc(bufsize));
380 cdm->instance = instance;
382 switch (instance->format) {
383 case AMQP1_FORMAT_JSON:
384 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
385 status = format_json_notification((char *)cdm->mbuf.start, bufsize, n);
387 ERROR("amqp1 plugin: formatting notification failed");
390 cdm->mbuf.size = strlen(cdm->mbuf.start);
393 ERROR("amqp1 plugin: Invalid notify format (%i).", instance->format);
397 /* encode message and place on outbound queue */
398 status = encqueue(cdm, instance);
401 } /* }}} int amqp1_notify */
403 static int amqp1_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
404 user_data_t *user_data) {
405 amqp1_config_instance_t *instance;
407 size_t bfree = BUFSIZE;
410 size_t bufsize = BUFSIZE;
412 if ((ds == NULL) || (vl == NULL) || (transport == NULL) ||
416 instance = user_data->data;
418 if (instance->notify != false) {
419 ERROR("amqp1 plugin: write failed");
422 cdm = NEW(cd_message_t);
424 cdm->mbuf = pn_rwbytes(bufsize, (char *)malloc(bufsize));
425 cdm->instance = instance;
427 switch (instance->format) {
428 case AMQP1_FORMAT_COMMAND:
429 status = cmd_create_putval((char *)cdm->mbuf.start, bufsize, ds, vl);
431 ERROR("amqp1 plugin: cmd_create_putval failed with status %i.", status);
434 cdm->mbuf.size = strlen(cdm->mbuf.start);
436 case AMQP1_FORMAT_JSON:
437 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
438 format_json_value_list((char *)cdm->mbuf.start, &bfill, &bfree, ds, vl,
439 instance->store_rates);
440 format_json_finalize((char *)cdm->mbuf.start, &bfill, &bfree);
441 cdm->mbuf.size = strlen(cdm->mbuf.start);
443 case AMQP1_FORMAT_GRAPHITE:
444 status = format_graphite((char *)cdm->mbuf.start, bufsize, ds, vl,
445 instance->prefix, instance->postfix,
446 instance->escape_char, instance->graphite_flags);
448 ERROR("amqp1 plugin: format_graphite failed with status %i.", status);
451 cdm->mbuf.size = strlen(cdm->mbuf.start);
454 ERROR("amqp1 plugin: Invalid write format (%i).", instance->format);
458 /* encode message and place on outboud queue */
459 encqueue(cdm, instance);
462 } /* }}} int amqp1_write */
464 static void amqp1_config_transport_free(void *ptr) /* {{{ */
466 amqp1_config_transport_t *transport = ptr;
468 if (transport == NULL)
471 sfree(transport->name);
472 sfree(transport->host);
473 sfree(transport->user);
474 sfree(transport->password);
475 sfree(transport->address);
478 } /* }}} void amqp1_config_transport_free */
480 static void amqp1_config_instance_free(void *ptr) /* {{{ */
482 amqp1_config_instance_t *instance = ptr;
484 if (instance == NULL)
487 sfree(instance->name);
488 sfree(instance->prefix);
489 sfree(instance->postfix);
492 } /* }}} void amqp1_config_instance_free */
494 static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */
498 amqp1_config_instance_t *instance;
500 instance = calloc(1, sizeof(*instance));
501 if (instance == NULL) {
502 ERROR("amqp1 plugin: calloc failed.");
506 /* Initialize instance configuration {{{ */
507 instance->name = NULL;
509 status = cf_util_get_string(ci, &instance->name);
515 for (int i = 0; i < ci->children_num; i++) {
516 oconfig_item_t *child = ci->children + i;
518 if (strcasecmp("PreSettle", child->key) == 0)
519 status = cf_util_get_boolean(child, &instance->pre_settle);
520 else if (strcasecmp("Notify", child->key) == 0)
521 status = cf_util_get_boolean(child, &instance->notify);
522 else if (strcasecmp("Format", child->key) == 0) {
523 status = cf_util_get_string(child, &key);
526 /* TODO: goto errout */
529 if (strcasecmp(key, "Command") == 0) {
530 instance->format = AMQP1_FORMAT_COMMAND;
531 } else if (strcasecmp(key, "Graphite") == 0) {
532 instance->format = AMQP1_FORMAT_GRAPHITE;
533 } else if (strcasecmp(key, "JSON") == 0) {
534 instance->format = AMQP1_FORMAT_JSON;
536 WARNING("amqp1 plugin: Invalid format string: %s", key);
539 } else if (strcasecmp("StoreRates", child->key) == 0)
540 status = cf_util_get_boolean(child, &instance->store_rates);
541 else if (strcasecmp("GraphiteSeparateInstances", child->key) == 0)
542 status = cf_util_get_flag(child, &instance->graphite_flags,
543 GRAPHITE_SEPARATE_INSTANCES);
544 else if (strcasecmp("GraphiteAlwaysAppendDS", child->key) == 0)
545 status = cf_util_get_flag(child, &instance->graphite_flags,
546 GRAPHITE_ALWAYS_APPEND_DS);
547 else if (strcasecmp("GraphitePreserveSeparator", child->key) == 0)
548 status = cf_util_get_flag(child, &instance->graphite_flags,
549 GRAPHITE_PRESERVE_SEPARATOR);
550 else if (strcasecmp("GraphitePrefix", child->key) == 0)
551 status = cf_util_get_string(child, &instance->prefix);
552 else if (strcasecmp("GraphitePostfix", child->key) == 0)
553 status = cf_util_get_string(child, &instance->postfix);
554 else if (strcasecmp("GraphiteEscapeChar", child->key) == 0) {
555 char *tmp_buff = NULL;
556 status = cf_util_get_string(child, &tmp_buff);
557 if (strlen(tmp_buff) > 1)
558 WARNING("amqp1 plugin: The option \"GraphiteEscapeChar\" handles "
559 "only one character. Others will be ignored.");
560 instance->escape_char = tmp_buff[0];
563 WARNING("amqp1 plugin: Ignoring unknown "
564 "instance configuration option "
572 amqp1_config_instance_free(instance);
577 status = snprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name);
578 if ((status < 0) || (size_t)status >= sizeof(tpname)) {
579 ERROR("amqp1 plugin: Instance name would have been truncated.");
582 status = snprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s",
583 transport->address, instance->name);
584 if ((status < 0) || (size_t)status >= sizeof(instance->send_to)) {
585 ERROR("amqp1 plugin: send_to address would have been truncated.");
588 if (instance->notify == true) {
589 status = plugin_register_notification(
590 tpname, amqp1_notify,
592 .data = instance, .free_func = amqp1_config_instance_free,
595 status = plugin_register_write(
598 .data = instance, .free_func = amqp1_config_instance_free,
603 amqp1_config_instance_free(instance);
608 } /* }}} int amqp1_config_instance */
610 static int amqp1_config_transport(oconfig_item_t *ci) /* {{{ */
614 transport = calloc(1, sizeof(*transport));
615 if (transport == NULL) {
616 ERROR("amqp1 plugin: calloc failed.");
620 /* Initialize transport configuration {{{ */
621 transport->name = NULL;
622 transport->retry_delay = 1;
624 status = cf_util_get_string(ci, &transport->name);
630 for (int i = 0; i < ci->children_num; i++) {
631 oconfig_item_t *child = ci->children + i;
633 if (strcasecmp("Host", child->key) == 0)
634 status = cf_util_get_string(child, &transport->host);
635 else if (strcasecmp("Port", child->key) == 0)
636 status = cf_util_get_string(child, &transport->port);
637 else if (strcasecmp("User", child->key) == 0)
638 status = cf_util_get_string(child, &transport->user);
639 else if (strcasecmp("Password", child->key) == 0)
640 status = cf_util_get_string(child, &transport->password);
641 else if (strcasecmp("Address", child->key) == 0)
642 status = cf_util_get_string(child, &transport->address);
643 else if (strcasecmp("RetryDelay", child->key) == 0)
644 status = cf_util_get_int(child, &transport->retry_delay);
645 else if (strcasecmp("Instance", child->key) == 0)
646 amqp1_config_instance(child);
648 WARNING("amqp1 plugin: Ignoring unknown "
649 "transport configuration option "
658 amqp1_config_transport_free(transport);
661 } /* }}} int amqp1_config_transport */
663 static int amqp1_config(oconfig_item_t *ci) /* {{{ */
666 for (int i = 0; i < ci->children_num; i++) {
667 oconfig_item_t *child = ci->children + i;
669 if (strcasecmp("Transport", child->key) == 0)
670 amqp1_config_transport(child);
672 WARNING("amqp1 plugin: Ignoring unknown config iption \%s\".",
677 } /* }}} int amqp1_config */
679 static int amqp1_init(void) /* {{{ */
684 if (transport == NULL) {
685 ERROR("amqp1: init failed, no transport configured");
689 if (proactor == NULL) {
690 pthread_mutex_init(&send_lock, /* attr = */ NULL);
693 plugin_thread_create(&event_thread_id, NULL /* no attributes */,
694 event_thread, NULL /* no argument */, "handle");
696 ERROR("amqp1 plugin: pthread_create failed: %s",
697 sstrerror(errno, errbuf, sizeof(errbuf)));
699 event_thread_running = 1;
703 } /* }}} int amqp1_init */
705 static int amqp1_shutdown(void) /* {{{ */
709 /* Stop the proactor thread */
710 if (event_thread_running == 1) {
711 DEBUG("amqp1 plugin: Shutting down proactor thread.");
712 pn_connection_wake(conn);
714 pthread_join(event_thread_id, NULL /* no return value */);
715 memset(&event_thread_id, 0, sizeof(event_thread_id));
717 DEBUG("amqp1 plugin: proactor thread exited.");
719 if (transport != NULL) {
720 amqp1_config_transport_free(transport);
724 } /* }}} int amqp1_shutdown */
726 void module_register(void) {
727 plugin_register_complex_config("amqp1", amqp1_config);
728 plugin_register_init("amqp1", amqp1_init);
729 plugin_register_shutdown("amqp1", amqp1_shutdown);
730 } /* void module_register */