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 */
170 static void check_condition(pn_event_t *e, pn_condition_t *cond) /* {{{ */
172 if (pn_condition_is_set(cond)) {
173 ERROR("amqp1 plugin: %s: %s: %s", pn_event_type_name(pn_event_type(e)),
174 pn_condition_get_name(cond), pn_condition_get_description(cond));
175 pn_connection_close(pn_event_connection(e));
178 } /* }}} void check_condition */
180 static bool handle(pn_event_t *event) /* {{{ */
183 switch (pn_event_type(event)) {
185 case PN_CONNECTION_INIT: {
186 conn = pn_event_connection(event);
187 pn_connection_set_container(conn, transport->address);
188 pn_connection_open(conn);
189 pn_session_t *ssn = pn_session(conn);
190 pn_session_open(ssn);
191 sender = pn_sender(ssn, "cd-sender");
192 pn_link_set_snd_settle_mode(sender, PN_SND_MIXED);
193 pn_link_open(sender);
198 /* peer has given us credit, send outbound messages */
199 amqp1_send_out_messages(sender);
204 /* acknowledgement from peer that a message was delivered */
205 pn_delivery_t *dlv = pn_event_delivery(event);
206 if (pn_delivery_remote_state(dlv) == PN_ACCEPTED) {
207 pn_delivery_settle(dlv);
213 case PN_CONNECTION_WAKE: {
215 amqp1_send_out_messages(sender);
220 case PN_TRANSPORT_CLOSED: {
221 check_condition(event, pn_transport_condition(pn_event_transport(event)));
225 case PN_CONNECTION_REMOTE_CLOSE: {
226 check_condition(event,
227 pn_session_remote_condition(pn_event_session(event)));
228 pn_connection_close(pn_event_connection(event));
232 case PN_SESSION_REMOTE_CLOSE: {
233 check_condition(event,
234 pn_session_remote_condition(pn_event_session(event)));
235 pn_connection_close(pn_event_connection(event));
239 case PN_LINK_REMOTE_CLOSE:
240 case PN_LINK_REMOTE_DETACH: {
241 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
242 pn_connection_close(pn_event_connection(event));
246 case PN_PROACTOR_INACTIVE: {
254 } /* }}} bool handle */
256 static void *event_thread(void __attribute__((unused)) * arg) /* {{{ */
258 char addr[PN_MAX_ADDR];
262 proactor = pn_proactor();
263 pn_proactor_addr(addr, sizeof(addr), transport->host, transport->port);
266 /* make connection */
267 conn = pn_connection();
268 if (transport->user != NULL) {
269 pn_connection_set_user(conn, transport->user);
270 pn_connection_set_password(conn, transport->password);
272 pn_proactor_connect(proactor, conn, addr);
274 bool engine_running = true;
275 while (engine_running && !stopping) {
276 pn_event_batch_t *events = pn_proactor_wait(proactor);
278 while (( e = pn_event_batch_next(events))){
279 engine_running = handle(e);
280 if (!engine_running) {
284 pn_proactor_done(proactor, events);
287 pn_proactor_release_connection(conn);
289 DEBUG("amqp1 plugin: retrying connection");
290 int delay = transport->retry_delay;
291 while (delay-- > 0 && !stopping) {
296 pn_proactor_disconnect(proactor, NULL);
298 /* Free the remaining out_messages */
299 cdm = DEQ_HEAD(out_messages);
301 DEQ_REMOVE_HEAD(out_messages);
302 cd_message_free(cdm);
303 cdm = DEQ_HEAD(out_messages);
306 event_thread_running = 0;
309 } /* }}} void event_thread */
311 static void encqueue(cd_message_t *cdm,
312 amqp1_config_instance_t *instance) /* {{{ */
314 size_t bufsize = BUFSIZE;
316 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, cdm->mbuf);
326 /* put_binary copies and stores so ok to use mbuf */
327 cdm->mbuf.size = bufsize;
328 pn_message_encode(message, (char *)cdm->mbuf.start, &cdm->mbuf.size);
330 pthread_mutex_lock(&send_lock);
331 DEQ_INSERT_TAIL(out_messages, cdm);
332 pthread_mutex_unlock(&send_lock);
334 pn_message_free(message);
336 /* activate the sender */
338 pn_connection_wake(conn);
341 } /* }}} void encqueue */
343 static int amqp1_notify(notification_t const *n,
344 user_data_t *user_data) /* {{{ */
346 amqp1_config_instance_t *instance;
348 size_t bfree = BUFSIZE;
351 size_t bufsize = BUFSIZE;
353 if ((n == NULL) || (user_data == NULL))
356 instance = user_data->data;
358 if (instance->notify != true) {
359 ERROR("amqp1 plugin: write notification failed");
362 cdm = NEW(cd_message_t);
364 cdm->mbuf = pn_bytes(bufsize, (char *)malloc(bufsize));
365 cdm->instance = instance;
367 switch (instance->format) {
368 case AMQP1_FORMAT_JSON:
369 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
370 status = format_json_notification((char *)cdm->mbuf.start, bufsize, n);
372 ERROR("amqp1 plugin: formatting notification failed");
375 cdm->mbuf.size = strlen(cdm->mbuf.start);
378 ERROR("amqp1 plugin: Invalid notify format (%i).", instance->format);
382 /* encode message and place on outbound queue */
383 encqueue(cdm, instance);
386 } /* }}} int amqp1_notify */
388 static int amqp1_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
389 user_data_t *user_data) {
390 amqp1_config_instance_t *instance;
392 size_t bfree = BUFSIZE;
395 size_t bufsize = BUFSIZE;
397 if ((ds == NULL) || (vl == NULL) || (transport == NULL) ||
401 instance = user_data->data;
403 if (instance->notify != false) {
404 ERROR("amqp1 plugin: write failed");
407 cdm = NEW(cd_message_t);
409 cdm->mbuf = pn_bytes(bufsize, (char *)malloc(bufsize));
410 cdm->instance = instance;
412 switch (instance->format) {
413 case AMQP1_FORMAT_COMMAND:
414 status = cmd_create_putval((char *)cdm->mbuf.start, bufsize, ds, vl);
416 ERROR("amqp1 plugin: cmd_create_putval failed with status %i.", status);
419 cdm->mbuf.size = strlen(cdm->mbuf.start);
421 case AMQP1_FORMAT_JSON:
422 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
423 format_json_value_list((char *)cdm->mbuf.start, &bfill, &bfree, ds, vl,
424 instance->store_rates);
425 format_json_finalize((char *)cdm->mbuf.start, &bfill, &bfree);
426 cdm->mbuf.size = strlen(cdm->mbuf.start);
428 case AMQP1_FORMAT_GRAPHITE:
429 status = format_graphite((char *)cdm->mbuf.start, bufsize, ds, vl,
430 instance->prefix, instance->postfix,
431 instance->escape_char, instance->graphite_flags);
433 ERROR("amqp1 plugin: format_graphite failed with status %i.", status);
436 cdm->mbuf.size = strlen(cdm->mbuf.start);
439 ERROR("amqp1 plugin: Invalid write format (%i).", instance->format);
443 /* encode message and place on outboud queue */
444 encqueue(cdm, instance);
447 } /* }}} int amqp1_write */
449 static void amqp1_config_transport_free(void *ptr) /* {{{ */
451 amqp1_config_transport_t *transport = ptr;
453 if (transport == NULL)
456 sfree(transport->name);
457 sfree(transport->host);
458 sfree(transport->user);
459 sfree(transport->password);
460 sfree(transport->address);
463 } /* }}} void amqp1_config_transport_free */
465 static void amqp1_config_instance_free(void *ptr) /* {{{ */
467 amqp1_config_instance_t *instance = ptr;
469 if (instance == NULL)
472 sfree(instance->name);
473 sfree(instance->prefix);
474 sfree(instance->postfix);
477 } /* }}} void amqp1_config_instance_free */
479 static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */
483 amqp1_config_instance_t *instance;
485 instance = calloc(1, sizeof(*instance));
486 if (instance == NULL) {
487 ERROR("amqp1 plugin: calloc failed.");
491 /* Initialize instance configuration {{{ */
492 instance->name = NULL;
494 status = cf_util_get_string(ci, &instance->name);
500 for (int i = 0; i < ci->children_num; i++) {
501 oconfig_item_t *child = ci->children + i;
503 if (strcasecmp("PreSettle", child->key) == 0)
504 status = cf_util_get_boolean(child, &instance->pre_settle);
505 else if (strcasecmp("Notify", child->key) == 0)
506 status = cf_util_get_boolean(child, &instance->notify);
507 else if (strcasecmp("Format", child->key) == 0) {
508 status = cf_util_get_string(child, &key);
511 /* TODO: goto errout */
514 if (strcasecmp(key, "Command") == 0) {
515 instance->format = AMQP1_FORMAT_COMMAND;
516 } else if (strcasecmp(key, "Graphite") == 0) {
517 instance->format = AMQP1_FORMAT_GRAPHITE;
518 } else if (strcasecmp(key, "JSON") == 0) {
519 instance->format = AMQP1_FORMAT_JSON;
521 WARNING("amqp1 plugin: Invalid format string: %s", key);
524 } else if (strcasecmp("StoreRates", child->key) == 0)
525 status = cf_util_get_boolean(child, &instance->store_rates);
526 else if (strcasecmp("GraphiteSeparateInstances", child->key) == 0)
527 status = cf_util_get_flag(child, &instance->graphite_flags,
528 GRAPHITE_SEPARATE_INSTANCES);
529 else if (strcasecmp("GraphiteAlwaysAppendDS", child->key) == 0)
530 status = cf_util_get_flag(child, &instance->graphite_flags,
531 GRAPHITE_ALWAYS_APPEND_DS);
532 else if (strcasecmp("GraphitePreserveSeparator", child->key) == 0)
533 status = cf_util_get_flag(child, &instance->graphite_flags,
534 GRAPHITE_PRESERVE_SEPARATOR);
535 else if (strcasecmp("GraphitePrefix", child->key) == 0)
536 status = cf_util_get_string(child, &instance->prefix);
537 else if (strcasecmp("GraphitePostfix", child->key) == 0)
538 status = cf_util_get_string(child, &instance->postfix);
539 else if (strcasecmp("GraphiteEscapeChar", child->key) == 0) {
540 char *tmp_buff = NULL;
541 status = cf_util_get_string(child, &tmp_buff);
542 if (strlen(tmp_buff) > 1)
543 WARNING("amqp1 plugin: The option \"GraphiteEscapeChar\" handles "
544 "only one character. Others will be ignored.");
545 instance->escape_char = tmp_buff[0];
548 WARNING("amqp1 plugin: Ignoring unknown "
549 "instance configuration option "
557 amqp1_config_instance_free(instance);
561 snprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name);
562 snprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s",
563 transport->address, instance->name);
564 if (instance->notify == true) {
565 status = plugin_register_notification(
566 tpname, amqp1_notify,
568 .data = instance, .free_func = amqp1_config_instance_free,
571 status = plugin_register_write(
574 .data = instance, .free_func = amqp1_config_instance_free,
579 amqp1_config_instance_free(instance);
584 } /* }}} int amqp1_config_instance */
586 static int amqp1_config_transport(oconfig_item_t *ci) /* {{{ */
590 transport = calloc(1, sizeof(*transport));
591 if (transport == NULL) {
592 ERROR("amqp1 plugin: calloc failed.");
596 /* Initialize transport configuration {{{ */
597 transport->name = NULL;
598 transport->retry_delay = 1;
600 status = cf_util_get_string(ci, &transport->name);
606 for (int i = 0; i < ci->children_num; i++) {
607 oconfig_item_t *child = ci->children + i;
609 if (strcasecmp("Host", child->key) == 0)
610 status = cf_util_get_string(child, &transport->host);
611 else if (strcasecmp("Port", child->key) == 0)
612 status = cf_util_get_string(child, &transport->port);
613 else if (strcasecmp("User", child->key) == 0)
614 status = cf_util_get_string(child, &transport->user);
615 else if (strcasecmp("Password", child->key) == 0)
616 status = cf_util_get_string(child, &transport->password);
617 else if (strcasecmp("Address", child->key) == 0)
618 status = cf_util_get_string(child, &transport->address);
619 else if (strcasecmp("RetryDelay", child->key) == 0)
620 status = cf_util_get_int(child, &transport->retry_delay);
621 else if (strcasecmp("Instance", child->key) == 0)
622 amqp1_config_instance(child);
624 WARNING("amqp1 plugin: Ignoring unknown "
625 "transport configuration option "
634 amqp1_config_transport_free(transport);
637 } /* }}} int amqp1_config_transport */
639 static int amqp1_config(oconfig_item_t *ci) /* {{{ */
642 for (int i = 0; i < ci->children_num; i++) {
643 oconfig_item_t *child = ci->children + i;
645 if (strcasecmp("Transport", child->key) == 0)
646 amqp1_config_transport(child);
648 WARNING("amqp1 plugin: Ignoring unknown config iption \%s\".",
653 } /* }}} int amqp1_config */
655 static int amqp1_init(void) /* {{{ */
660 if (transport == NULL) {
661 ERROR("amqp1: init failed, no transport configured");
665 if (proactor == NULL) {
666 pthread_mutex_init(&send_lock, /* attr = */ NULL);
669 plugin_thread_create(&event_thread_id, NULL /* no attributes */,
670 event_thread, NULL /* no argument */, "handle");
672 ERROR("amqp1 plugin: pthread_create failed: %s",
673 sstrerror(errno, errbuf, sizeof(errbuf)));
675 event_thread_running = 1;
679 } /* }}} int amqp1_init */
681 static int amqp1_shutdown(void) /* {{{ */
685 /* Stop the proactor thread */
686 if (event_thread_running == 1) {
687 DEBUG("amqp1 plugin: Shutting down proactor thread.");
688 pn_connection_wake(conn);
690 pthread_join(event_thread_id, NULL /* no return value */);
691 memset(&event_thread_id, 0, sizeof(event_thread_id));
693 DEBUG("amqp1 plugin: proactor thread exited.");
695 if (transport != NULL) {
696 amqp1_config_transport_free(transport);
700 } /* }}} int amqp1_shutdown */
702 void module_register(void) {
703 plugin_register_complex_config("amqp1", amqp1_config);
704 plugin_register_init("amqp1", amqp1_init);
705 plugin_register_shutdown("amqp1", amqp1_shutdown);
706 } /* void module_register */