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);
65 } amqp1_config_transport_t;
67 typedef struct amqp1_config_instance_t {
68 DEQ_LINKS(struct amqp1_config_instance_t);
72 unsigned int graphite_flags;
79 } amqp1_config_instance_t;
81 DEQ_DECLARE(amqp1_config_instance_t, amqp1_config_instance_list_t);
83 typedef struct cd_message_t {
84 DEQ_LINKS(struct cd_message_t);
86 amqp1_config_instance_t *instance;
89 DEQ_DECLARE(cd_message_t, cd_message_list_t);
94 pn_connection_t *conn = NULL;
95 pn_session_t *ssn = 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;
103 bool finished = 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);
129 pthread_mutex_lock(&send_lock);
131 if (link_credit > 0) {
133 cdm = DEQ_HEAD(out_messages);
135 DEQ_REMOVE_HEAD(out_messages);
136 DEQ_INSERT_TAIL(to_send, cdm);
137 if (DEQ_SIZE(to_send) == link_credit)
139 cdm = DEQ_HEAD(out_messages);
141 cd_tag += DEQ_SIZE(to_send);
144 pthread_mutex_unlock(&send_lock);
146 /* message is already formatted and encoded */
147 cdm = DEQ_HEAD(to_send);
149 DEQ_REMOVE_HEAD(to_send);
151 dlv = pn_delivery(link, pn_dtag((const char *)&dtag, sizeof(dtag)));
152 pn_link_send(link, cdm->mbuf.start, cdm->mbuf.size);
153 pn_link_advance(link);
154 if (cdm->instance->pre_settle == true) {
155 pn_delivery_settle(dlv);
158 cd_message_free(cdm);
159 cdm = DEQ_HEAD(to_send);
163 } /* }}} int amqp1_send_out_messages */
165 static void check_condition(pn_event_t *e, pn_condition_t *cond) /* {{{ */
167 if (pn_condition_is_set(cond)) {
168 ERROR("amqp1 plugin: %s: %s: %s", pn_event_type_name(pn_event_type(e)),
169 pn_condition_get_name(cond), pn_condition_get_description(cond));
170 pn_connection_close(pn_event_connection(e));
173 } /* }}} void check_condition */
175 static bool handle(pn_event_t *event) /* {{{ */
178 switch (pn_event_type(event)) {
180 case PN_CONNECTION_INIT: {
181 conn = pn_event_connection(event);
182 pn_connection_set_container(conn, transport->address);
183 pn_connection_open(conn);
184 ssn = pn_session(conn);
185 pn_session_open(ssn);
186 sender = pn_sender(ssn, "cd-sender");
187 pn_link_set_snd_settle_mode(sender, PN_SND_MIXED);
188 pn_link_open(sender);
193 /* peer has given us credit, send outbound messages */
194 amqp1_send_out_messages(sender);
199 /* acknowledgement from peer that a message was delivered */
200 pn_delivery_t *dlv = pn_event_delivery(event);
201 if (pn_delivery_remote_state(dlv) == PN_ACCEPTED) {
202 pn_delivery_settle(dlv);
208 case PN_CONNECTION_WAKE: {
210 amqp1_send_out_messages(sender);
215 case PN_TRANSPORT_CLOSED: {
216 check_condition(event, pn_transport_condition(pn_event_transport(event)));
220 case PN_CONNECTION_REMOTE_CLOSE: {
221 check_condition(event,
222 pn_session_remote_condition(pn_event_session(event)));
223 pn_connection_close(pn_event_connection(event));
227 case PN_SESSION_REMOTE_CLOSE: {
228 check_condition(event,
229 pn_session_remote_condition(pn_event_session(event)));
230 pn_connection_close(pn_event_connection(event));
234 case PN_LINK_REMOTE_CLOSE:
235 case PN_LINK_REMOTE_DETACH: {
236 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
237 pn_connection_close(pn_event_connection(event));
241 case PN_PROACTOR_INACTIVE: {
249 } /* }}} bool handle */
251 static void *event_thread(void __attribute__((unused)) * arg) /* {{{ */
255 pn_event_batch_t *events = pn_proactor_wait(proactor);
257 for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) {
262 pn_proactor_done(proactor, events);
265 event_thread_running = 0;
268 } /* }}} void event_thread */
270 static void encqueue(cd_message_t *cdm,
271 amqp1_config_instance_t *instance) /* {{{ */
273 size_t bufsize = BUFSIZE;
275 pn_message_t *message;
278 message = pn_message();
279 pn_message_set_address(message, instance->send_to);
280 body = pn_message_body(message);
282 pn_data_put_binary(body, cdm->mbuf);
285 /* put_binary copies and stores so ok to use mbuf */
286 cdm->mbuf.size = bufsize;
287 pn_message_encode(message, (char *)cdm->mbuf.start, &cdm->mbuf.size);
289 pthread_mutex_lock(&send_lock);
290 DEQ_INSERT_TAIL(out_messages, cdm);
291 pthread_mutex_unlock(&send_lock);
293 pn_message_free(message);
295 /* activate the sender */
297 pn_connection_wake(conn);
300 } /* }}} void encqueue */
302 static int amqp1_notify(notification_t const *n,
303 user_data_t *user_data) /* {{{ */
305 amqp1_config_instance_t *instance;
307 size_t bfree = BUFSIZE;
310 size_t bufsize = BUFSIZE;
312 if ((n == NULL) || (user_data == NULL))
315 instance = user_data->data;
317 if (instance->notify != true) {
318 ERROR("amqp1 plugin: write notification failed");
321 cdm = NEW(cd_message_t);
323 cdm->mbuf = pn_bytes(bufsize, (char *)malloc(bufsize));
324 cdm->instance = instance;
326 switch (instance->format) {
327 case AMQP1_FORMAT_JSON:
328 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
329 status = format_json_notification((char *)cdm->mbuf.start, bufsize, n);
331 ERROR("amqp1 plugin: formatting notification failed");
334 cdm->mbuf.size = strlen(cdm->mbuf.start);
337 ERROR("amqp1 plugin: Invalid notify format (%i).", instance->format);
341 /* encode message and place on outbound queue */
342 encqueue(cdm, instance);
345 } /* }}} int amqp1_notify */
347 static int amqp1_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
348 user_data_t *user_data) {
349 amqp1_config_instance_t *instance;
351 size_t bfree = BUFSIZE;
354 size_t bufsize = BUFSIZE;
356 if ((ds == NULL) || (vl == NULL) || (transport == NULL) ||
360 instance = user_data->data;
362 if (instance->notify != false) {
363 ERROR("amqp1 plugin: write failed");
366 cdm = NEW(cd_message_t);
368 cdm->mbuf = pn_bytes(bufsize, (char *)malloc(bufsize));
369 cdm->instance = instance;
371 switch (instance->format) {
372 case AMQP1_FORMAT_COMMAND:
373 status = cmd_create_putval((char *)cdm->mbuf.start, bufsize, ds, vl);
375 ERROR("amqp1 plugin: cmd_create_putval failed with status %i.", status);
378 cdm->mbuf.size = strlen(cdm->mbuf.start);
380 case AMQP1_FORMAT_JSON:
381 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
382 format_json_value_list((char *)cdm->mbuf.start, &bfill, &bfree, ds, vl,
383 instance->store_rates);
384 format_json_finalize((char *)cdm->mbuf.start, &bfill, &bfree);
385 cdm->mbuf.size = strlen(cdm->mbuf.start);
387 case AMQP1_FORMAT_GRAPHITE:
388 status = format_graphite((char *)cdm->mbuf.start, bufsize, ds, vl,
389 instance->prefix, instance->postfix,
390 instance->escape_char, instance->graphite_flags);
392 ERROR("amqp1 plugin: format_graphite failed with status %i.", status);
395 cdm->mbuf.size = strlen(cdm->mbuf.start);
398 ERROR("amqp1 plugin: Invalid write format (%i).", instance->format);
402 /* encode message and place on outboud queue */
403 encqueue(cdm, instance);
406 } /* }}} int amqp1_write */
408 static void amqp1_config_transport_free(void *ptr) /* {{{ */
410 amqp1_config_transport_t *transport = ptr;
412 if (transport == NULL)
415 sfree(transport->name);
416 sfree(transport->host);
417 sfree(transport->user);
418 sfree(transport->password);
419 sfree(transport->address);
422 } /* }}} void amqp1_config_transport_free */
424 static void amqp1_config_instance_free(void *ptr) /* {{{ */
426 amqp1_config_instance_t *instance = ptr;
428 if (instance == NULL)
431 sfree(instance->name);
432 sfree(instance->prefix);
433 sfree(instance->postfix);
436 } /* }}} void amqp1_config_instance_free */
438 static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */
442 amqp1_config_instance_t *instance;
444 instance = calloc(1, sizeof(*instance));
445 if (instance == NULL) {
446 ERROR("amqp1 plugin: calloc failed.");
450 /* Initialize instance configuration {{{ */
451 instance->name = NULL;
453 status = cf_util_get_string(ci, &instance->name);
459 for (int i = 0; i < ci->children_num; i++) {
460 oconfig_item_t *child = ci->children + i;
462 if (strcasecmp("PreSettle", child->key) == 0)
463 status = cf_util_get_boolean(child, &instance->pre_settle);
464 else if (strcasecmp("Notify", child->key) == 0)
465 status = cf_util_get_boolean(child, &instance->notify);
466 else if (strcasecmp("Format", child->key) == 0) {
467 status = cf_util_get_string(child, &key);
470 /* TODO: goto errout */
473 if (strcasecmp(key, "Command") == 0) {
474 instance->format = AMQP1_FORMAT_COMMAND;
475 } else if (strcasecmp(key, "Graphite") == 0) {
476 instance->format = AMQP1_FORMAT_GRAPHITE;
477 } else if (strcasecmp(key, "JSON") == 0) {
478 instance->format = AMQP1_FORMAT_JSON;
480 WARNING("amqp1 plugin: Invalid format string: %s", key);
483 } else if (strcasecmp("StoreRates", child->key) == 0)
484 status = cf_util_get_boolean(child, &instance->store_rates);
485 else if (strcasecmp("GraphiteSeparateInstances", child->key) == 0)
486 status = cf_util_get_flag(child, &instance->graphite_flags,
487 GRAPHITE_SEPARATE_INSTANCES);
488 else if (strcasecmp("GraphiteAlwaysAppendDS", child->key) == 0)
489 status = cf_util_get_flag(child, &instance->graphite_flags,
490 GRAPHITE_ALWAYS_APPEND_DS);
491 else if (strcasecmp("GraphitePreserveSeparator", child->key) == 0)
492 status = cf_util_get_flag(child, &instance->graphite_flags,
493 GRAPHITE_PRESERVE_SEPARATOR);
494 else if (strcasecmp("GraphitePrefix", child->key) == 0)
495 status = cf_util_get_string(child, &instance->prefix);
496 else if (strcasecmp("GraphitePostfix", child->key) == 0)
497 status = cf_util_get_string(child, &instance->postfix);
498 else if (strcasecmp("GraphiteEscapeChar", child->key) == 0) {
499 char *tmp_buff = NULL;
500 status = cf_util_get_string(child, &tmp_buff);
501 if (strlen(tmp_buff) > 1)
502 WARNING("amqp1 plugin: The option \"GraphiteEscapeChar\" handles "
503 "only one character. Others will be ignored.");
504 instance->escape_char = tmp_buff[0];
507 WARNING("amqp1 plugin: Ignoring unknown "
508 "instance configuration option "
516 amqp1_config_instance_free(instance);
520 snprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name);
521 snprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s",
522 transport->address, instance->name);
523 if (instance->notify == true) {
524 status = plugin_register_notification(
525 tpname, amqp1_notify,
527 .data = instance, .free_func = amqp1_config_instance_free,
530 status = plugin_register_write(
533 .data = instance, .free_func = amqp1_config_instance_free,
538 amqp1_config_instance_free(instance);
543 } /* }}} int amqp1_config_instance */
545 static int amqp1_config_transport(oconfig_item_t *ci) /* {{{ */
549 transport = calloc(1, sizeof(*transport));
550 if (transport == NULL) {
551 ERROR("amqp1 plugin: calloc failed.");
555 /* Initialize transport configuration {{{ */
556 transport->name = NULL;
558 status = cf_util_get_string(ci, &transport->name);
564 for (int i = 0; i < ci->children_num; i++) {
565 oconfig_item_t *child = ci->children + i;
567 if (strcasecmp("Host", child->key) == 0)
568 status = cf_util_get_string(child, &transport->host);
569 else if (strcasecmp("Port", child->key) == 0)
570 status = cf_util_get_string(child, &transport->port);
571 else if (strcasecmp("User", child->key) == 0)
572 status = cf_util_get_string(child, &transport->user);
573 else if (strcasecmp("Password", child->key) == 0)
574 status = cf_util_get_string(child, &transport->password);
575 else if (strcasecmp("Address", child->key) == 0)
576 status = cf_util_get_string(child, &transport->address);
577 else if (strcasecmp("Instance", child->key) == 0)
578 amqp1_config_instance(child);
580 WARNING("amqp1 plugin: Ignoring unknown "
581 "transport configuration option "
590 amqp1_config_transport_free(transport);
593 } /* }}} int amqp1_config_transport */
595 static int amqp1_config(oconfig_item_t *ci) /* {{{ */
598 for (int i = 0; i < ci->children_num; i++) {
599 oconfig_item_t *child = ci->children + i;
601 if (strcasecmp("Transport", child->key) == 0)
602 amqp1_config_transport(child);
604 WARNING("amqp1 plugin: Ignoring unknown config iption \%s\".",
609 } /* }}} int amqp1_config */
611 static int amqp1_init(void) /* {{{ */
613 char addr[PN_MAX_ADDR];
617 if (transport == NULL) {
618 ERROR("amqp1: init failed, no transport configured");
622 if (proactor == NULL) {
623 pthread_mutex_init(&send_lock, /* attr = */ NULL);
624 proactor = pn_proactor();
625 pn_proactor_addr(addr, sizeof(addr), transport->host, transport->port);
626 conn = pn_connection();
627 if (transport->user != NULL) {
628 pn_connection_set_user(conn, transport->user);
629 pn_connection_set_password(conn, transport->password);
631 pn_proactor_connect(proactor, conn, addr);
634 plugin_thread_create(&event_thread_id, NULL /* no attributes */,
635 event_thread, NULL /* no argument */, "handle");
637 ERROR("amqp1: pthread_create failed: %s",
638 sstrerror(errno, errbuf, sizeof(errbuf)));
640 event_thread_running = 1;
644 } /* }}} int amqp1_init */
646 static int amqp1_shutdown(void) /* {{{ */
650 /* Stop the proactor thread */
651 if (event_thread_running != 0) {
653 /* activate the event thread */
654 pn_connection_wake(conn);
655 pthread_join(event_thread_id, NULL /* no return value */);
656 memset(&event_thread_id, 0, sizeof(event_thread_id));
659 /* Free the remaining out_messages */
660 cdm = DEQ_HEAD(out_messages);
662 DEQ_REMOVE_HEAD(out_messages);
663 cd_message_free(cdm);
664 cdm = DEQ_HEAD(out_messages);
667 if (proactor != NULL) {
668 pn_proactor_free(proactor);
671 if (transport != NULL) {
672 amqp1_config_transport_free(transport);
676 } /* }}} int amqp1_shutdown */
678 void module_register(void) {
679 plugin_register_complex_config("amqp1", amqp1_config);
680 plugin_register_init("amqp1", amqp1_init);
681 plugin_register_shutdown("amqp1", amqp1_shutdown);
682 } /* void module_register */