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);
71 unsigned int graphite_flags;
78 } amqp1_config_instance_t;
80 DEQ_DECLARE(amqp1_config_instance_t, amqp1_config_instance_list_t);
82 typedef struct cd_message_t {
83 DEQ_LINKS(struct cd_message_t);
85 amqp1_config_instance_t *instance;
88 DEQ_DECLARE(cd_message_t, cd_message_list_t);
93 pn_connection_t *conn = NULL;
94 pn_session_t *ssn = NULL;
95 pn_link_t *sender = NULL;
96 pn_proactor_t *proactor = NULL;
97 pthread_mutex_t send_lock;
98 cd_message_list_t out_messages;
100 uint64_t acknowledged = 0;
101 amqp1_config_transport_t *transport = NULL;
102 bool finished = false;
104 static int event_thread_running = 0;
105 static pthread_t event_thread_id;
110 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",
169 pn_event_type_name(pn_event_type(e)),
170 pn_condition_get_name(cond),
171 pn_condition_get_description(cond));
172 pn_connection_close(pn_event_connection(e));
175 } /* }}} void check_condition */
177 static bool handle(pn_event_t *event) /* {{{ */
180 switch (pn_event_type(event)) {
182 case PN_CONNECTION_INIT:{
183 conn = pn_event_connection(event);
184 pn_connection_set_container(conn, transport->address);
185 pn_connection_open(conn);
186 ssn = pn_session(conn);
187 pn_session_open(ssn);
188 sender = pn_sender(ssn, "cd-sender");
189 pn_link_set_snd_settle_mode(sender, PN_SND_MIXED);
190 pn_link_open(sender);
195 /* peer has given us credit, send outbound messages */
196 amqp1_send_out_messages(sender);
201 /* acknowledgement from peer that a message was delivered */
202 pn_delivery_t * dlv = pn_event_delivery(event);
203 if (pn_delivery_remote_state(dlv) == PN_ACCEPTED) {
209 case PN_CONNECTION_WAKE: {
211 amqp1_send_out_messages(sender);
216 case PN_TRANSPORT_CLOSED: {
217 check_condition(event, pn_transport_condition(pn_event_transport(event)));
221 case PN_CONNECTION_REMOTE_CLOSE: {
222 check_condition(event, 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, 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: {
247 } /* }}} bool handle */
249 static void *event_thread(void __attribute__((unused)) * arg) /* {{{ */
253 pn_event_batch_t *events = pn_proactor_wait(proactor);
255 for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) {
260 pn_proactor_done(proactor, events);
263 event_thread_running = 0;
266 } /* }}} void event_thread */
268 static int amqp1_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
269 user_data_t *user_data)
271 amqp1_config_instance_t *instance = user_data->data;
273 size_t bfree = BUFSIZE;
276 size_t bufsize = BUFSIZE;
278 pn_message_t *message;
280 if ((ds == NULL) || (vl == NULL) || (transport == NULL))
283 cdm = NEW(cd_message_t);
285 cdm->mbuf = pn_bytes(bufsize, (char *) malloc(bufsize));
286 cdm->instance = instance;
288 switch (instance->format) {
289 case AMQP1_FORMAT_COMMAND:
290 status = cmd_create_putval((char *)cdm->mbuf.start, bufsize, ds, vl);
292 ERROR("amqp1 plugin: cmd_create_putval failed with status %i.", status);
295 cdm->mbuf.size = strlen(cdm->mbuf.start);
297 case AMQP1_FORMAT_JSON:
298 format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree);
299 format_json_value_list((char *)cdm->mbuf.start, &bfill, &bfree, ds, vl,
300 instance->store_rates);
301 format_json_finalize((char *)cdm->mbuf.start, &bfill, &bfree);
302 cdm->mbuf.size = strlen(cdm->mbuf.start);
304 case AMQP1_FORMAT_GRAPHITE:
306 format_graphite((char *)cdm->mbuf.start, bufsize, ds, vl, instance->prefix,
307 instance->postfix, instance->escape_char, instance->graphite_flags);
309 ERROR("amqp1 plugin: format_graphite failed with status %i.", status);
312 cdm->mbuf.size = strlen(cdm->mbuf.start);
315 ERROR("amqp1 plugin: Invalid format (%i).", instance->format);
320 message = pn_message();
321 pn_message_set_address(message, instance->send_to);
322 body = pn_message_body(message);
324 pn_data_put_binary(body, cdm->mbuf);
327 /* put_binary copies and stores so ok to use mbuf */
328 cdm->mbuf.size = bufsize;
329 pn_message_encode(message, (char *)cdm->mbuf.start, &cdm->mbuf.size);
331 pthread_mutex_lock(&send_lock);
332 DEQ_INSERT_TAIL(out_messages, cdm);
333 pthread_mutex_unlock(&send_lock);
335 pn_message_free(message);
337 /* activate the sender */
339 pn_connection_wake(conn);
343 } /* }}} int amqp_write1 */
345 static void amqp1_config_transport_free(void *ptr) /* {{{ */
347 amqp1_config_transport_t *transport = ptr;
349 if (transport == NULL)
352 sfree(transport->name);
353 sfree(transport->host);
354 sfree(transport->user);
355 sfree(transport->password);
356 sfree(transport->address);
359 } /* }}} void amqp1_config_transport_free */
361 static void amqp1_config_instance_free(void *ptr) /* {{{ */
363 amqp1_config_instance_t *instance = ptr;
365 if (instance == NULL)
368 sfree(instance->name);
369 sfree(instance->prefix);
370 sfree(instance->postfix);
373 } /* }}} void amqp1_config_instance_free */
375 static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */
379 amqp1_config_instance_t *instance;
381 instance = calloc(1, sizeof(*instance));
382 if (instance == NULL) {
383 ERROR("amqp1 plugin: calloc failed.");
387 /* Initialize instance configuration {{{ */
388 instance->name = NULL;
390 status = cf_util_get_string(ci, &instance->name);
396 for (int i = 0; i < ci->children_num; i++) {
397 oconfig_item_t *child = ci->children + i;
399 if (strcasecmp("PreSettle", child->key) == 0)
400 status = cf_util_get_boolean(child, &instance->pre_settle);
401 else if (strcasecmp("Format", child->key) == 0) {
402 status = cf_util_get_string(child, &key);
405 /* TODO: goto errout */
408 if (strcasecmp(key, "Command") == 0) {
409 instance->format = AMQP1_FORMAT_COMMAND;
410 } else if (strcasecmp(key, "Graphite") == 0) {
411 instance->format = AMQP1_FORMAT_GRAPHITE;
412 } else if (strcasecmp(key, "JSON") == 0) {
413 instance->format = AMQP1_FORMAT_JSON;
415 WARNING("amqp1 plugin: Invalid format string: %s", key);
419 else if (strcasecmp("StoreRates", child->key) == 0)
420 status = cf_util_get_boolean(child, &instance->store_rates);
421 else if (strcasecmp("GraphiteSeparateInstances", child->key) == 0)
422 status = cf_util_get_flag(child, &instance->graphite_flags,
423 GRAPHITE_SEPARATE_INSTANCES);
424 else if (strcasecmp("GraphiteAlwaysAppendDS", child->key) == 0)
425 status = cf_util_get_flag(child, &instance->graphite_flags,
426 GRAPHITE_ALWAYS_APPEND_DS);
427 else if (strcasecmp("GraphitePreserveSeparator", child->key) == 0)
428 status = cf_util_get_flag(child, &instance->graphite_flags,
429 GRAPHITE_PRESERVE_SEPARATOR);
430 else if (strcasecmp("GraphitePrefix", child->key) == 0)
431 status = cf_util_get_string(child, &instance->prefix);
432 else if (strcasecmp("GraphitePostfix", child->key) == 0)
433 status = cf_util_get_string(child, &instance->postfix);
434 else if (strcasecmp("GraphiteEscapeChar", child->key) == 0) {
435 char *tmp_buff = NULL;
436 status = cf_util_get_string(child, &tmp_buff);
437 if (strlen(tmp_buff) > 1)
438 WARNING("amqp1 plugin: The option \"GraphiteEscapeChar\" handles "
439 "only one character. Others will be ignored.");
440 instance->escape_char = tmp_buff[0];
444 WARNING("amqp1 plugin: Ignoring unknown "
445 "instance configuration option "
446 "\%s\".", child->key);
452 amqp1_config_instance_free(instance);
456 snprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name);
457 snprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s",
458 transport->address,instance->name);
459 status = plugin_register_write(tpname, amqp1_write, &(user_data_t) {
460 .data = instance, .free_func = amqp1_config_instance_free, });
462 amqp1_config_instance_free(instance);
467 } /* }}} int amqp1_config_instance */
469 static int amqp1_config_transport(oconfig_item_t *ci) /* {{{ */
473 transport = calloc(1, sizeof(*transport));
474 if (transport == NULL) {
475 ERROR("amqp1 plugin: calloc failed.");
479 /* Initialize transport configuration {{{ */
480 transport->name = NULL;
482 status = cf_util_get_string(ci, &transport->name);
488 for (int i = 0; i < ci->children_num; i++) {
489 oconfig_item_t *child = ci->children + i;
491 if (strcasecmp("Host", child->key) == 0)
492 status = cf_util_get_string(child, &transport->host);
493 else if (strcasecmp("Port", child->key) == 0)
494 status = cf_util_get_string(child, &transport->port);
495 else if (strcasecmp("User", child->key) == 0)
496 status = cf_util_get_string(child, &transport->user);
497 else if (strcasecmp("Password", child->key) == 0)
498 status = cf_util_get_string(child, &transport->password);
499 else if (strcasecmp("Address", child->key) == 0)
500 status = cf_util_get_string(child, &transport->address);
501 else if (strcasecmp("Instance",child->key) == 0)
502 amqp1_config_instance(child);
504 WARNING("amqp1 plugin: Ignoring unknown "
505 "transport configuration option "
506 "\%s\".", child->key);
513 amqp1_config_transport_free(transport);
516 } /* }}} int amqp1_config_transport */
518 static int amqp1_config(oconfig_item_t *ci) /* {{{ */
521 for (int i = 0; i < ci->children_num; i++) {
522 oconfig_item_t *child = ci->children + i;
524 if (strcasecmp("Transport", child->key) == 0)
525 amqp1_config_transport(child);
527 WARNING("amqp1 plugin: Ignoring unknown config iption \%s\".",
532 } /* }}} int amqp1_config */
534 static int amqp1_init(void) /* {{{ */
536 char addr[PN_MAX_ADDR];
540 if (transport == NULL) {
541 ERROR("amqp1: init failed, no transport configured");
545 if (proactor == NULL) {
546 pthread_mutex_init(&send_lock, /* attr = */ NULL);
547 proactor = pn_proactor();
548 pn_proactor_addr(addr, sizeof(addr),transport->host,transport->port);
549 conn = pn_connection();
550 if (transport->user != NULL) {
551 pn_connection_set_user(conn, transport->user);
552 pn_connection_set_password(conn, transport->password);
554 pn_proactor_connect(proactor, conn, addr);
556 status = plugin_thread_create(&event_thread_id, NULL /* no attributes */,
557 event_thread, NULL /* no argument */,
560 ERROR("amqp1: pthread_create failed: %s",
561 sstrerror(errno, errbuf, sizeof(errbuf)));
563 event_thread_running = 1;
567 } /* }}} int amqp1_init */
569 static int amqp1_shutdown
574 /* Stop the proactor thread */
575 if (event_thread_running != 0) {
577 /* activate the event thread */
578 pn_connection_wake(conn);
579 pthread_join(event_thread_id, NULL /* no return value */);
580 memset(&event_thread_id, 0, sizeof(event_thread_id));
583 /* Free the remaining out_messages */
584 cdm = DEQ_HEAD(out_messages);
586 DEQ_REMOVE_HEAD(out_messages);
587 cd_message_free(cdm);
588 cdm = DEQ_HEAD(out_messages);
591 if (proactor != NULL) {
592 pn_proactor_free(proactor);
595 if (transport != NULL) {
596 amqp1_config_transport_free(transport);
600 } /* }}} int amqp1_shutdown */
602 void module_register(void)
604 plugin_register_complex_config("amqp1", amqp1_config);
605 plugin_register_init("amqp1", amqp1_init);
606 plugin_register_shutdown("amqp1",amqp1_shutdown);
607 } /* void module_register */