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) {
207 case PN_CONNECTION_WAKE: {
209 amqp1_send_out_messages(sender);
214 case PN_TRANSPORT_CLOSED: {
215 check_condition(event, pn_transport_condition(pn_event_transport(event)));
219 case PN_CONNECTION_REMOTE_CLOSE: {
220 check_condition(event,
221 pn_session_remote_condition(pn_event_session(event)));
222 pn_connection_close(pn_event_connection(event));
226 case PN_SESSION_REMOTE_CLOSE: {
227 check_condition(event,
228 pn_session_remote_condition(pn_event_session(event)));
229 pn_connection_close(pn_event_connection(event));
233 case PN_LINK_REMOTE_CLOSE:
234 case PN_LINK_REMOTE_DETACH: {
235 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
236 pn_connection_close(pn_event_connection(event));
240 case PN_PROACTOR_INACTIVE: {
248 } /* }}} bool handle */
250 static void *event_thread(void __attribute__((unused)) * arg) /* {{{ */
254 pn_event_batch_t *events = pn_proactor_wait(proactor);
256 for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) {
261 pn_proactor_done(proactor, events);
264 event_thread_running = 0;
267 } /* }}} void event_thread */
269 static void encqueue(cd_message_t *cdm,
270 amqp1_config_instance_t *instance) /* {{{ */
272 size_t bufsize = BUFSIZE;
274 pn_message_t *message;
277 message = pn_message();
278 pn_message_set_address(message, instance->send_to);
279 body = pn_message_body(message);
281 pn_data_put_binary(body, cdm->mbuf);
284 /* put_binary copies and stores so ok to use mbuf */
285 cdm->mbuf.size = bufsize;
286 pn_message_encode(message, (char *)cdm->mbuf.start, &cdm->mbuf.size);
288 pthread_mutex_lock(&send_lock);
289 DEQ_INSERT_TAIL(out_messages, cdm);
290 pthread_mutex_unlock(&send_lock);
292 pn_message_free(message);
294 /* activate the sender */
296 pn_connection_wake(conn);
299 } /* }}} void encqueue */
301 static int amqp1_notify(notification_t const *n,
302 user_data_t *user_data) /* {{{ */
304 amqp1_config_instance_t *instance;
306 size_t bfree = BUFSIZE;
309 size_t bufsize = BUFSIZE;
311 if ((n == NULL) || (user_data == NULL))
314 instance = user_data->data;
316 if (instance->notify != true) {
317 ERROR("amqp1 plugin: write notification failed");
320 cdm = NEW(cd_message_t);
322 cdm->mbuf = pn_bytes(bufsize, (char *)malloc(bufsize));
323 cdm->instance = instance;
325 switch (instance->format) {
326 case AMQP1_FORMAT_JSON:
327 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
328 status = format_json_notification((char *)cdm->mbuf.start, bufsize, n);
330 ERROR("amqp1 plugin: formatting notification failed");
333 cdm->mbuf.size = strlen(cdm->mbuf.start);
336 ERROR("amqp1 plugin: Invalid notify format (%i).", instance->format);
340 /* encode message and place on outbound queue */
341 encqueue(cdm, instance);
344 } /* }}} int amqp1_notify */
346 static int amqp1_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
347 user_data_t *user_data) {
348 amqp1_config_instance_t *instance;
350 size_t bfree = BUFSIZE;
353 size_t bufsize = BUFSIZE;
355 if ((ds == NULL) || (vl == NULL) || (transport == NULL) ||
359 instance = user_data->data;
361 if (instance->notify != false) {
362 ERROR("amqp1 plugin: write failed");
365 cdm = NEW(cd_message_t);
367 cdm->mbuf = pn_bytes(bufsize, (char *)malloc(bufsize));
368 cdm->instance = instance;
370 switch (instance->format) {
371 case AMQP1_FORMAT_COMMAND:
372 status = cmd_create_putval((char *)cdm->mbuf.start, bufsize, ds, vl);
374 ERROR("amqp1 plugin: cmd_create_putval failed with status %i.", status);
377 cdm->mbuf.size = strlen(cdm->mbuf.start);
379 case AMQP1_FORMAT_JSON:
380 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
381 format_json_value_list((char *)cdm->mbuf.start, &bfill, &bfree, ds, vl,
382 instance->store_rates);
383 format_json_finalize((char *)cdm->mbuf.start, &bfill, &bfree);
384 cdm->mbuf.size = strlen(cdm->mbuf.start);
386 case AMQP1_FORMAT_GRAPHITE:
387 status = format_graphite((char *)cdm->mbuf.start, bufsize, ds, vl,
388 instance->prefix, instance->postfix,
389 instance->escape_char, instance->graphite_flags);
391 ERROR("amqp1 plugin: format_graphite failed with status %i.", status);
394 cdm->mbuf.size = strlen(cdm->mbuf.start);
397 ERROR("amqp1 plugin: Invalid write format (%i).", instance->format);
401 /* encode message and place on outboud queue */
402 encqueue(cdm, instance);
405 } /* }}} int amqp1_write */
407 static void amqp1_config_transport_free(void *ptr) /* {{{ */
409 amqp1_config_transport_t *transport = ptr;
411 if (transport == NULL)
414 sfree(transport->name);
415 sfree(transport->host);
416 sfree(transport->user);
417 sfree(transport->password);
418 sfree(transport->address);
421 } /* }}} void amqp1_config_transport_free */
423 static void amqp1_config_instance_free(void *ptr) /* {{{ */
425 amqp1_config_instance_t *instance = ptr;
427 if (instance == NULL)
430 sfree(instance->name);
431 sfree(instance->prefix);
432 sfree(instance->postfix);
435 } /* }}} void amqp1_config_instance_free */
437 static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */
441 amqp1_config_instance_t *instance;
443 instance = calloc(1, sizeof(*instance));
444 if (instance == NULL) {
445 ERROR("amqp1 plugin: calloc failed.");
449 /* Initialize instance configuration {{{ */
450 instance->name = NULL;
452 status = cf_util_get_string(ci, &instance->name);
458 for (int i = 0; i < ci->children_num; i++) {
459 oconfig_item_t *child = ci->children + i;
461 if (strcasecmp("PreSettle", child->key) == 0)
462 status = cf_util_get_boolean(child, &instance->pre_settle);
463 else if (strcasecmp("Notify", child->key) == 0)
464 status = cf_util_get_boolean(child, &instance->notify);
465 else if (strcasecmp("Format", child->key) == 0) {
466 status = cf_util_get_string(child, &key);
469 /* TODO: goto errout */
472 if (strcasecmp(key, "Command") == 0) {
473 instance->format = AMQP1_FORMAT_COMMAND;
474 } else if (strcasecmp(key, "Graphite") == 0) {
475 instance->format = AMQP1_FORMAT_GRAPHITE;
476 } else if (strcasecmp(key, "JSON") == 0) {
477 instance->format = AMQP1_FORMAT_JSON;
479 WARNING("amqp1 plugin: Invalid format string: %s", key);
482 } else if (strcasecmp("StoreRates", child->key) == 0)
483 status = cf_util_get_boolean(child, &instance->store_rates);
484 else if (strcasecmp("GraphiteSeparateInstances", child->key) == 0)
485 status = cf_util_get_flag(child, &instance->graphite_flags,
486 GRAPHITE_SEPARATE_INSTANCES);
487 else if (strcasecmp("GraphiteAlwaysAppendDS", child->key) == 0)
488 status = cf_util_get_flag(child, &instance->graphite_flags,
489 GRAPHITE_ALWAYS_APPEND_DS);
490 else if (strcasecmp("GraphitePreserveSeparator", child->key) == 0)
491 status = cf_util_get_flag(child, &instance->graphite_flags,
492 GRAPHITE_PRESERVE_SEPARATOR);
493 else if (strcasecmp("GraphitePrefix", child->key) == 0)
494 status = cf_util_get_string(child, &instance->prefix);
495 else if (strcasecmp("GraphitePostfix", child->key) == 0)
496 status = cf_util_get_string(child, &instance->postfix);
497 else if (strcasecmp("GraphiteEscapeChar", child->key) == 0) {
498 char *tmp_buff = NULL;
499 status = cf_util_get_string(child, &tmp_buff);
500 if (strlen(tmp_buff) > 1)
501 WARNING("amqp1 plugin: The option \"GraphiteEscapeChar\" handles "
502 "only one character. Others will be ignored.");
503 instance->escape_char = tmp_buff[0];
506 WARNING("amqp1 plugin: Ignoring unknown "
507 "instance configuration option "
515 amqp1_config_instance_free(instance);
519 snprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name);
520 snprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s",
521 transport->address, instance->name);
522 if (instance->notify == true) {
523 status = plugin_register_notification(
524 tpname, amqp1_notify,
526 .data = instance, .free_func = amqp1_config_instance_free,
529 status = plugin_register_write(
532 .data = instance, .free_func = amqp1_config_instance_free,
537 amqp1_config_instance_free(instance);
542 } /* }}} int amqp1_config_instance */
544 static int amqp1_config_transport(oconfig_item_t *ci) /* {{{ */
548 transport = calloc(1, sizeof(*transport));
549 if (transport == NULL) {
550 ERROR("amqp1 plugin: calloc failed.");
554 /* Initialize transport configuration {{{ */
555 transport->name = NULL;
557 status = cf_util_get_string(ci, &transport->name);
563 for (int i = 0; i < ci->children_num; i++) {
564 oconfig_item_t *child = ci->children + i;
566 if (strcasecmp("Host", child->key) == 0)
567 status = cf_util_get_string(child, &transport->host);
568 else if (strcasecmp("Port", child->key) == 0)
569 status = cf_util_get_string(child, &transport->port);
570 else if (strcasecmp("User", child->key) == 0)
571 status = cf_util_get_string(child, &transport->user);
572 else if (strcasecmp("Password", child->key) == 0)
573 status = cf_util_get_string(child, &transport->password);
574 else if (strcasecmp("Address", child->key) == 0)
575 status = cf_util_get_string(child, &transport->address);
576 else if (strcasecmp("Instance", child->key) == 0)
577 amqp1_config_instance(child);
579 WARNING("amqp1 plugin: Ignoring unknown "
580 "transport configuration option "
589 amqp1_config_transport_free(transport);
592 } /* }}} int amqp1_config_transport */
594 static int amqp1_config(oconfig_item_t *ci) /* {{{ */
597 for (int i = 0; i < ci->children_num; i++) {
598 oconfig_item_t *child = ci->children + i;
600 if (strcasecmp("Transport", child->key) == 0)
601 amqp1_config_transport(child);
603 WARNING("amqp1 plugin: Ignoring unknown config iption \%s\".",
608 } /* }}} int amqp1_config */
610 static int amqp1_init(void) /* {{{ */
612 char addr[PN_MAX_ADDR];
616 if (transport == NULL) {
617 ERROR("amqp1: init failed, no transport configured");
621 if (proactor == NULL) {
622 pthread_mutex_init(&send_lock, /* attr = */ NULL);
623 proactor = pn_proactor();
624 pn_proactor_addr(addr, sizeof(addr), transport->host, transport->port);
625 conn = pn_connection();
626 if (transport->user != NULL) {
627 pn_connection_set_user(conn, transport->user);
628 pn_connection_set_password(conn, transport->password);
630 pn_proactor_connect(proactor, conn, addr);
633 plugin_thread_create(&event_thread_id, NULL /* no attributes */,
634 event_thread, NULL /* no argument */, "handle");
636 ERROR("amqp1: pthread_create failed: %s",
637 sstrerror(errno, errbuf, sizeof(errbuf)));
639 event_thread_running = 1;
643 } /* }}} int amqp1_init */
645 static int amqp1_shutdown(void) /* {{{ */
649 /* Stop the proactor thread */
650 if (event_thread_running != 0) {
652 /* activate the event thread */
653 pn_connection_wake(conn);
654 pthread_join(event_thread_id, NULL /* no return value */);
655 memset(&event_thread_id, 0, sizeof(event_thread_id));
658 /* Free the remaining out_messages */
659 cdm = DEQ_HEAD(out_messages);
661 DEQ_REMOVE_HEAD(out_messages);
662 cd_message_free(cdm);
663 cdm = DEQ_HEAD(out_messages);
666 if (proactor != NULL) {
667 pn_proactor_free(proactor);
670 if (transport != NULL) {
671 amqp1_config_transport_free(transport);
675 } /* }}} int amqp1_shutdown */
677 void module_register(void) {
678 plugin_register_complex_config("amqp1", amqp1_config);
679 plugin_register_init("amqp1", amqp1_init);
680 plugin_register_shutdown("amqp1", amqp1_shutdown);
681 } /* void module_register */