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_format_graphite.h"
33 #include "utils_format_json.h"
34 #include "utils_random.h"
35 #include "utils_deq.h"
37 #include <proton/connection.h>
38 #include <proton/condition.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)
113 if (cdm->mbuf.start) {
114 free((void *)cdm->mbuf.start);
117 } /* }}} void cd_message_free */
119 static int amqp1_send_out_messages(pn_link_t *link) /* {{{ */
122 cd_message_list_t to_send;
124 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) == 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",
170 pn_event_type_name(pn_event_type(e)),
171 pn_condition_get_name(cond),
172 pn_condition_get_description(cond));
173 pn_connection_close(pn_event_connection(e));
176 } /* }}} void check_condition */
178 static bool handle(pn_event_t *event) /* {{{ */
181 switch (pn_event_type(event)) {
183 case PN_CONNECTION_INIT:{
184 conn = pn_event_connection(event);
185 pn_connection_set_container(conn, transport->address);
186 pn_connection_open(conn);
187 ssn = pn_session(conn);
188 pn_session_open(ssn);
189 sender = pn_sender(ssn, "cd-sender");
190 pn_link_set_snd_settle_mode(sender, PN_SND_MIXED);
191 pn_link_open(sender);
196 /* peer has given us credit, send outbound messages */
197 amqp1_send_out_messages(sender);
202 /* acknowledgement from peer that a message was delivered */
203 pn_delivery_t * dlv = pn_event_delivery(event);
204 if (pn_delivery_remote_state(dlv) == PN_ACCEPTED) {
210 case PN_CONNECTION_WAKE: {
212 amqp1_send_out_messages(sender);
217 case PN_TRANSPORT_CLOSED: {
218 check_condition(event, pn_transport_condition(pn_event_transport(event)));
222 case PN_CONNECTION_REMOTE_CLOSE: {
223 check_condition(event, 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, 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: {
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, amqp1_config_instance_t *instance ) /* {{{ */
271 size_t bufsize = BUFSIZE;
273 pn_message_t *message;
276 message = pn_message();
277 pn_message_set_address(message, instance->send_to);
278 body = pn_message_body(message);
280 pn_data_put_binary(body, cdm->mbuf);
283 /* put_binary copies and stores so ok to use mbuf */
284 cdm->mbuf.size = bufsize;
285 pn_message_encode(message, (char *)cdm->mbuf.start, &cdm->mbuf.size);
287 pthread_mutex_lock(&send_lock);
288 DEQ_INSERT_TAIL(out_messages, cdm);
289 pthread_mutex_unlock(&send_lock);
291 pn_message_free(message);
293 /* activate the sender */
295 pn_connection_wake(conn);
298 } /* }}} void encqueue */
300 static int amqp1_notify(notification_t const *n, user_data_t *user_data) /* {{{ */
302 amqp1_config_instance_t *instance;
304 size_t bfree = BUFSIZE;
307 size_t bufsize = BUFSIZE;
309 if ((n == NULL) || (user_data == NULL))
312 instance = user_data->data;
314 if (instance->notify != true) {
315 ERROR("amqp1 plugin: write notification failed");
318 cdm = NEW(cd_message_t);
320 cdm->mbuf = pn_bytes(bufsize, (char *) malloc(bufsize));
321 cdm->instance = instance;
323 switch (instance->format) {
324 case AMQP1_FORMAT_JSON:
325 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
326 status = format_json_notification((char *)cdm->mbuf.start, bufsize, n);
328 ERROR("amqp1 plugin: formatting notification failed");
331 cdm->mbuf.size = strlen(cdm->mbuf.start);
334 ERROR("amqp1 plugin: Invalid notify format (%i).", instance->format);
338 /* encode message and place on outbound queue */
339 encqueue(cdm, instance);
342 } /* }}} int amqp1_notify */
344 static int amqp1_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
345 user_data_t *user_data)
347 amqp1_config_instance_t *instance;
349 size_t bfree = BUFSIZE;
352 size_t bufsize = BUFSIZE;
354 if ((ds == NULL) || (vl == NULL) || (transport == NULL) || (user_data == NULL))
357 instance = user_data->data;
359 if (instance->notify != false) {
360 ERROR("amqp1 plugin: write failed");
363 cdm = NEW(cd_message_t);
365 cdm->mbuf = pn_bytes(bufsize, (char *) malloc(bufsize));
366 cdm->instance = instance;
368 switch (instance->format) {
369 case AMQP1_FORMAT_COMMAND:
370 status = cmd_create_putval((char *)cdm->mbuf.start, bufsize, ds, vl);
372 ERROR("amqp1 plugin: cmd_create_putval failed with status %i.", status);
375 cdm->mbuf.size = strlen(cdm->mbuf.start);
377 case AMQP1_FORMAT_JSON:
378 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
379 format_json_value_list((char *)cdm->mbuf.start, &bfill, &bfree, ds, vl,
380 instance->store_rates);
381 format_json_finalize((char *)cdm->mbuf.start, &bfill, &bfree);
382 cdm->mbuf.size = strlen(cdm->mbuf.start);
384 case AMQP1_FORMAT_GRAPHITE:
386 format_graphite((char *)cdm->mbuf.start, bufsize, ds, vl, instance->prefix,
387 instance->postfix, instance->escape_char, instance->graphite_flags);
389 ERROR("amqp1 plugin: format_graphite failed with status %i.", status);
392 cdm->mbuf.size = strlen(cdm->mbuf.start);
395 ERROR("amqp1 plugin: Invalid write format (%i).", instance->format);
399 /* encode message and place on outboud queue */
400 encqueue(cdm, instance);
403 } /* }}} int amqp1_write */
405 static void amqp1_config_transport_free(void *ptr) /* {{{ */
407 amqp1_config_transport_t *transport = ptr;
409 if (transport == NULL)
412 sfree(transport->name);
413 sfree(transport->host);
414 sfree(transport->user);
415 sfree(transport->password);
416 sfree(transport->address);
419 } /* }}} void amqp1_config_transport_free */
421 static void amqp1_config_instance_free(void *ptr) /* {{{ */
423 amqp1_config_instance_t *instance = ptr;
425 if (instance == NULL)
428 sfree(instance->name);
429 sfree(instance->prefix);
430 sfree(instance->postfix);
433 } /* }}} void amqp1_config_instance_free */
435 static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */
439 amqp1_config_instance_t *instance;
441 instance = calloc(1, sizeof(*instance));
442 if (instance == NULL) {
443 ERROR("amqp1 plugin: calloc failed.");
447 /* Initialize instance configuration {{{ */
448 instance->name = NULL;
450 status = cf_util_get_string(ci, &instance->name);
456 for (int i = 0; i < ci->children_num; i++) {
457 oconfig_item_t *child = ci->children + i;
459 if (strcasecmp("PreSettle", child->key) == 0)
460 status = cf_util_get_boolean(child, &instance->pre_settle);
461 else if (strcasecmp("Notify", child->key) == 0)
462 status = cf_util_get_boolean(child, &instance->notify);
463 else if (strcasecmp("Format", child->key) == 0) {
464 status = cf_util_get_string(child, &key);
467 /* TODO: goto errout */
470 if (strcasecmp(key, "Command") == 0) {
471 instance->format = AMQP1_FORMAT_COMMAND;
472 } else if (strcasecmp(key, "Graphite") == 0) {
473 instance->format = AMQP1_FORMAT_GRAPHITE;
474 } else if (strcasecmp(key, "JSON") == 0) {
475 instance->format = AMQP1_FORMAT_JSON;
477 WARNING("amqp1 plugin: Invalid format string: %s", key);
481 else if (strcasecmp("StoreRates", child->key) == 0)
482 status = cf_util_get_boolean(child, &instance->store_rates);
483 else if (strcasecmp("GraphiteSeparateInstances", child->key) == 0)
484 status = cf_util_get_flag(child, &instance->graphite_flags,
485 GRAPHITE_SEPARATE_INSTANCES);
486 else if (strcasecmp("GraphiteAlwaysAppendDS", child->key) == 0)
487 status = cf_util_get_flag(child, &instance->graphite_flags,
488 GRAPHITE_ALWAYS_APPEND_DS);
489 else if (strcasecmp("GraphitePreserveSeparator", child->key) == 0)
490 status = cf_util_get_flag(child, &instance->graphite_flags,
491 GRAPHITE_PRESERVE_SEPARATOR);
492 else if (strcasecmp("GraphitePrefix", child->key) == 0)
493 status = cf_util_get_string(child, &instance->prefix);
494 else if (strcasecmp("GraphitePostfix", child->key) == 0)
495 status = cf_util_get_string(child, &instance->postfix);
496 else if (strcasecmp("GraphiteEscapeChar", child->key) == 0) {
497 char *tmp_buff = NULL;
498 status = cf_util_get_string(child, &tmp_buff);
499 if (strlen(tmp_buff) > 1)
500 WARNING("amqp1 plugin: The option \"GraphiteEscapeChar\" handles "
501 "only one character. Others will be ignored.");
502 instance->escape_char = tmp_buff[0];
506 WARNING("amqp1 plugin: Ignoring unknown "
507 "instance configuration option "
508 "\%s\".", child->key);
514 amqp1_config_instance_free(instance);
518 snprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name);
519 snprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s",
520 transport->address,instance->name);
521 if (instance->notify == true) {
522 status = plugin_register_notification(tpname, amqp1_notify, &(user_data_t) {
523 .data = instance, .free_func = amqp1_config_instance_free, });
525 status = plugin_register_write(tpname, amqp1_write, &(user_data_t) {
526 .data = instance, .free_func = amqp1_config_instance_free, });
530 amqp1_config_instance_free(instance);
535 } /* }}} int amqp1_config_instance */
537 static int amqp1_config_transport(oconfig_item_t *ci) /* {{{ */
541 transport = calloc(1, sizeof(*transport));
542 if (transport == NULL) {
543 ERROR("amqp1 plugin: calloc failed.");
547 /* Initialize transport configuration {{{ */
548 transport->name = NULL;
550 status = cf_util_get_string(ci, &transport->name);
556 for (int i = 0; i < ci->children_num; i++) {
557 oconfig_item_t *child = ci->children + i;
559 if (strcasecmp("Host", child->key) == 0)
560 status = cf_util_get_string(child, &transport->host);
561 else if (strcasecmp("Port", child->key) == 0)
562 status = cf_util_get_string(child, &transport->port);
563 else if (strcasecmp("User", child->key) == 0)
564 status = cf_util_get_string(child, &transport->user);
565 else if (strcasecmp("Password", child->key) == 0)
566 status = cf_util_get_string(child, &transport->password);
567 else if (strcasecmp("Address", child->key) == 0)
568 status = cf_util_get_string(child, &transport->address);
569 else if (strcasecmp("Instance",child->key) == 0)
570 amqp1_config_instance(child);
572 WARNING("amqp1 plugin: Ignoring unknown "
573 "transport configuration option "
574 "\%s\".", child->key);
581 amqp1_config_transport_free(transport);
584 } /* }}} int amqp1_config_transport */
586 static int amqp1_config(oconfig_item_t *ci) /* {{{ */
589 for (int i = 0; i < ci->children_num; i++) {
590 oconfig_item_t *child = ci->children + i;
592 if (strcasecmp("Transport", child->key) == 0)
593 amqp1_config_transport(child);
595 WARNING("amqp1 plugin: Ignoring unknown config iption \%s\".",
600 } /* }}} int amqp1_config */
602 static int amqp1_init(void) /* {{{ */
604 char addr[PN_MAX_ADDR];
608 if (transport == NULL) {
609 ERROR("amqp1: init failed, no transport configured");
613 if (proactor == NULL) {
614 pthread_mutex_init(&send_lock, /* attr = */ NULL);
615 proactor = pn_proactor();
616 pn_proactor_addr(addr, sizeof(addr),transport->host,transport->port);
617 conn = pn_connection();
618 if (transport->user != NULL) {
619 pn_connection_set_user(conn, transport->user);
620 pn_connection_set_password(conn, transport->password);
622 pn_proactor_connect(proactor, conn, addr);
624 status = plugin_thread_create(&event_thread_id, NULL /* no attributes */,
625 event_thread, NULL /* no argument */,
628 ERROR("amqp1: pthread_create failed: %s",
629 sstrerror(errno, errbuf, sizeof(errbuf)));
631 event_thread_running = 1;
635 } /* }}} int amqp1_init */
637 static int amqp1_shutdown(void) /* {{{ */
641 /* Stop the proactor thread */
642 if (event_thread_running != 0) {
644 /* activate the event thread */
645 pn_connection_wake(conn);
646 pthread_join(event_thread_id, NULL /* no return value */);
647 memset(&event_thread_id, 0, sizeof(event_thread_id));
650 /* Free the remaining out_messages */
651 cdm = DEQ_HEAD(out_messages);
653 DEQ_REMOVE_HEAD(out_messages);
654 cd_message_free(cdm);
655 cdm = DEQ_HEAD(out_messages);
658 if (proactor != NULL) {
659 pn_proactor_free(proactor);
662 if (transport != NULL) {
663 amqp1_config_transport_free(transport);
667 } /* }}} int amqp1_shutdown */
669 void module_register(void)
671 plugin_register_complex_config("amqp1", amqp1_config);
672 plugin_register_init("amqp1", amqp1_init);
673 plugin_register_shutdown("amqp1",amqp1_shutdown);
674 } /* void module_register */