| 1 |
/** |
| 2 |
* collectd - src/amqp1.c |
| 3 |
* Copyright(c) 2017 Red Hat Inc. |
| 4 |
* |
| 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: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 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. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Andy Smith <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "collectd.h" |
| 28 |
|
| 29 |
#include "plugin.h" |
| 30 |
#include "utils/cmds/putval.h" |
| 31 |
#include "utils/common/common.h" |
| 32 |
#include "utils/deq/deq.h" |
| 33 |
#include "utils/format_graphite/format_graphite.h" |
| 34 |
#include "utils/format_json/format_json.h" |
| 35 |
#include "utils_random.h" |
| 36 |
|
| 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> |
| 46 |
|
| 47 |
#include <errno.h> |
| 48 |
#include <stdint.h> |
| 49 |
#include <stdio.h> |
| 50 |
#include <stdlib.h> |
| 51 |
|
| 52 |
#define BUFSIZE 8192 |
| 53 |
#define AMQP1_FORMAT_JSON 0 |
| 54 |
#define AMQP1_FORMAT_COMMAND 1 |
| 55 |
#define AMQP1_FORMAT_GRAPHITE 2 |
| 56 |
|
| 57 |
typedef struct amqp1_config_transport_s { |
| 58 |
DEQ_LINKS(struct amqp1_config_transport_s); |
| 59 |
char *name; |
| 60 |
char *host; |
| 61 |
char *port; |
| 62 |
char *user; |
| 63 |
char *password; |
| 64 |
char *address; |
| 65 |
int retry_delay; |
| 66 |
int sendq_limit; |
| 67 |
} amqp1_config_transport_t; |
| 68 |
|
| 69 |
typedef struct amqp1_config_instance_s { |
| 70 |
DEQ_LINKS(struct amqp1_config_instance_s); |
| 71 |
char *name; |
| 72 |
bool notify; |
| 73 |
uint8_t format; |
| 74 |
unsigned int graphite_flags; |
| 75 |
bool store_rates; |
| 76 |
char *prefix; |
| 77 |
char *postfix; |
| 78 |
char escape_char; |
| 79 |
bool pre_settle; |
| 80 |
char send_to[1024]; |
| 81 |
} amqp1_config_instance_t; |
| 82 |
|
| 83 |
DEQ_DECLARE(amqp1_config_instance_t, amqp1_config_instance_list_t); |
| 84 |
|
| 85 |
typedef struct cd_message_s { |
| 86 |
DEQ_LINKS(struct cd_message_s); |
| 87 |
pn_rwbytes_t mbuf; |
| 88 |
amqp1_config_instance_t *instance; |
| 89 |
} cd_message_t; |
| 90 |
|
| 91 |
DEQ_DECLARE(cd_message_t, cd_message_list_t); |
| 92 |
|
| 93 |
/* |
| 94 |
* Globals |
| 95 |
*/ |
| 96 |
static pn_connection_t *conn; |
| 97 |
static pn_link_t *sender; |
| 98 |
static pn_proactor_t *proactor; |
| 99 |
static pthread_mutex_t send_lock; |
| 100 |
static cd_message_list_t out_messages; |
| 101 |
static uint64_t cd_tag = 1; |
| 102 |
static uint64_t acknowledged; |
| 103 |
static amqp1_config_transport_t *transport; |
| 104 |
static bool stopping; |
| 105 |
static bool event_thread_running; |
| 106 |
static pthread_t event_thread_id; |
| 107 |
|
| 108 |
/* |
| 109 |
* Functions |
| 110 |
*/ |
| 111 |
static void cd_message_free(cd_message_t *cdm) { |
| 112 |
free(cdm->mbuf.start); |
| 113 |
free(cdm); |
| 114 |
} /* }}} void cd_message_free */ |
| 115 |
|
| 116 |
static int amqp1_send_out_messages(pn_link_t *link) /* {{{ */ |
| 117 |
{ |
| 118 |
uint64_t dtag; |
| 119 |
cd_message_list_t to_send; |
| 120 |
cd_message_t *cdm; |
| 121 |
int link_credit = pn_link_credit(link); |
| 122 |
int event_count = 0; |
| 123 |
pn_delivery_t *dlv; |
| 124 |
|
| 125 |
if (stopping) { |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
DEQ_INIT(to_send); |
| 130 |
|
| 131 |
pthread_mutex_lock(&send_lock); |
| 132 |
|
| 133 |
if (link_credit > 0) { |
| 134 |
dtag = cd_tag; |
| 135 |
cdm = DEQ_HEAD(out_messages); |
| 136 |
while (cdm) { |
| 137 |
DEQ_REMOVE_HEAD(out_messages); |
| 138 |
DEQ_INSERT_TAIL(to_send, cdm); |
| 139 |
if (DEQ_SIZE(to_send) == (size_t)link_credit) |
| 140 |
break; |
| 141 |
cdm = DEQ_HEAD(out_messages); |
| 142 |
} |
| 143 |
cd_tag += DEQ_SIZE(to_send); |
| 144 |
} |
| 145 |
|
| 146 |
pthread_mutex_unlock(&send_lock); |
| 147 |
|
| 148 |
/* message is already formatted and encoded */ |
| 149 |
cdm = DEQ_HEAD(to_send); |
| 150 |
while (cdm) { |
| 151 |
DEQ_REMOVE_HEAD(to_send); |
| 152 |
dtag++; |
| 153 |
dlv = pn_delivery(link, pn_dtag((const char *)&dtag, sizeof(dtag))); |
| 154 |
pn_link_send(link, cdm->mbuf.start, cdm->mbuf.size); |
| 155 |
pn_link_advance(link); |
| 156 |
if (cdm->instance->pre_settle == true) { |
| 157 |
pn_delivery_settle(dlv); |
| 158 |
} |
| 159 |
event_count++; |
| 160 |
cd_message_free(cdm); |
| 161 |
cdm = DEQ_HEAD(to_send); |
| 162 |
} |
| 163 |
|
| 164 |
return event_count; |
| 165 |
} /* }}} int amqp1_send_out_messages */ |
| 166 |
|
| 167 |
static void check_condition(pn_event_t *e, pn_condition_t *cond) /* {{{ */ |
| 168 |
{ |
| 169 |
if (pn_condition_is_set(cond)) { |
| 170 |
ERROR("amqp1 plugin: %s: %s: %s", pn_event_type_name(pn_event_type(e)), |
| 171 |
pn_condition_get_name(cond), pn_condition_get_description(cond)); |
| 172 |
pn_connection_close(pn_event_connection(e)); |
| 173 |
conn = NULL; |
| 174 |
} |
| 175 |
} /* }}} void check_condition */ |
| 176 |
|
| 177 |
static bool handle(pn_event_t *event) /* {{{ */ |
| 178 |
{ |
| 179 |
|
| 180 |
switch (pn_event_type(event)) { |
| 181 |
|
| 182 |
case PN_CONNECTION_INIT: { |
| 183 |
conn = pn_event_connection(event); |
| 184 |
pn_connection_set_container(conn, transport->name); |
| 185 |
pn_connection_open(conn); |
| 186 |
pn_session_t *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); |
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
case PN_LINK_FLOW: { |
| 195 |
/* peer has given us credit, send outbound messages */ |
| 196 |
amqp1_send_out_messages(sender); |
| 197 |
break; |
| 198 |
} |
| 199 |
|
| 200 |
case PN_DELIVERY: { |
| 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) { |
| 204 |
pn_delivery_settle(dlv); |
| 205 |
acknowledged++; |
| 206 |
} |
| 207 |
break; |
| 208 |
} |
| 209 |
|
| 210 |
case PN_CONNECTION_WAKE: { |
| 211 |
if (!stopping) { |
| 212 |
amqp1_send_out_messages(sender); |
| 213 |
} |
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
case PN_TRANSPORT_CLOSED: { |
| 218 |
check_condition(event, pn_transport_condition(pn_event_transport(event))); |
| 219 |
break; |
| 220 |
} |
| 221 |
|
| 222 |
case PN_CONNECTION_REMOTE_CLOSE: { |
| 223 |
check_condition(event, |
| 224 |
pn_session_remote_condition(pn_event_session(event))); |
| 225 |
pn_connection_close(pn_event_connection(event)); |
| 226 |
break; |
| 227 |
} |
| 228 |
|
| 229 |
case PN_SESSION_REMOTE_CLOSE: { |
| 230 |
check_condition(event, |
| 231 |
pn_session_remote_condition(pn_event_session(event))); |
| 232 |
pn_connection_close(pn_event_connection(event)); |
| 233 |
break; |
| 234 |
} |
| 235 |
|
| 236 |
case PN_LINK_REMOTE_CLOSE: |
| 237 |
case PN_LINK_REMOTE_DETACH: { |
| 238 |
check_condition(event, pn_link_remote_condition(pn_event_link(event))); |
| 239 |
pn_connection_close(pn_event_connection(event)); |
| 240 |
break; |
| 241 |
} |
| 242 |
|
| 243 |
case PN_PROACTOR_INACTIVE: { |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
default: |
| 248 |
break; |
| 249 |
} |
| 250 |
return true; |
| 251 |
} /* }}} bool handle */ |
| 252 |
|
| 253 |
static void *event_thread(void __attribute__((unused)) * arg) /* {{{ */ |
| 254 |
{ |
| 255 |
char addr[PN_MAX_ADDR]; |
| 256 |
cd_message_t *cdm; |
| 257 |
|
| 258 |
/* setup proactor */ |
| 259 |
proactor = pn_proactor(); |
| 260 |
pn_proactor_addr(addr, sizeof(addr), transport->host, transport->port); |
| 261 |
|
| 262 |
while (!stopping) { |
| 263 |
/* make connection */ |
| 264 |
conn = pn_connection(); |
| 265 |
if (transport->user != NULL) { |
| 266 |
pn_connection_set_user(conn, transport->user); |
| 267 |
pn_connection_set_password(conn, transport->password); |
| 268 |
} |
| 269 |
pn_proactor_connect(proactor, conn, addr); |
| 270 |
|
| 271 |
bool engine_running = true; |
| 272 |
while (engine_running && !stopping) { |
| 273 |
pn_event_batch_t *events = pn_proactor_wait(proactor); |
| 274 |
pn_event_t *e; |
| 275 |
while ((e = pn_event_batch_next(events))) { |
| 276 |
engine_running = handle(e); |
| 277 |
if (!engine_running) { |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
pn_proactor_done(proactor, events); |
| 282 |
} |
| 283 |
|
| 284 |
pn_proactor_release_connection(conn); |
| 285 |
|
| 286 |
DEBUG("amqp1 plugin: retrying connection"); |
| 287 |
int delay = transport->retry_delay; |
| 288 |
while (delay-- > 0 && !stopping) { |
| 289 |
sleep(1.0); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
pn_proactor_disconnect(proactor, NULL); |
| 294 |
|
| 295 |
/* Free the remaining out_messages */ |
| 296 |
cdm = DEQ_HEAD(out_messages); |
| 297 |
while (cdm) { |
| 298 |
DEQ_REMOVE_HEAD(out_messages); |
| 299 |
cd_message_free(cdm); |
| 300 |
cdm = DEQ_HEAD(out_messages); |
| 301 |
} |
| 302 |
|
| 303 |
event_thread_running = false; |
| 304 |
|
| 305 |
return NULL; |
| 306 |
} /* }}} void event_thread */ |
| 307 |
|
| 308 |
static int encqueue(cd_message_t *cdm, |
| 309 |
amqp1_config_instance_t *instance) /* {{{ */ |
| 310 |
{ |
| 311 |
/* encode message */ |
| 312 |
pn_message_t *message = pn_message(); |
| 313 |
pn_message_set_address(message, instance->send_to); |
| 314 |
pn_data_t *body = pn_message_body(message); |
| 315 |
pn_data_clear(body); |
| 316 |
pn_data_put_binary(body, pn_bytes(cdm->mbuf.size, cdm->mbuf.start)); |
| 317 |
pn_data_exit(body); |
| 318 |
|
| 319 |
/* put_binary copies and stores so ok to use mbuf */ |
| 320 |
cdm->mbuf.size = BUFSIZE; |
| 321 |
|
| 322 |
int status; |
| 323 |
char *start; |
| 324 |
while ((status = pn_message_encode(message, cdm->mbuf.start, |
| 325 |
&cdm->mbuf.size)) == PN_OVERFLOW) { |
| 326 |
DEBUG("amqp1 plugin: increasing message buffer size %zu", cdm->mbuf.size); |
| 327 |
cdm->mbuf.size *= 2; |
| 328 |
start = realloc(cdm->mbuf.start, cdm->mbuf.size); |
| 329 |
if (start == NULL) { |
| 330 |
status = -1; |
| 331 |
break; |
| 332 |
} else { |
| 333 |
cdm->mbuf.start = start; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
if (status != 0) { |
| 338 |
ERROR("amqp1 plugin: error encoding message: %s", |
| 339 |
pn_error_text(pn_message_error(message))); |
| 340 |
pn_message_free(message); |
| 341 |
return -1; |
| 342 |
} |
| 343 |
|
| 344 |
pthread_mutex_lock(&send_lock); |
| 345 |
if (transport->sendq_limit > 0 && |
| 346 |
DEQ_SIZE(out_messages) >= transport->sendq_limit) { |
| 347 |
cd_message_t *evict; |
| 348 |
|
| 349 |
DEBUG("amqp1 plugin: dropping oldest message because sendq is full"); |
| 350 |
evict = DEQ_HEAD(out_messages); |
| 351 |
DEQ_REMOVE_HEAD(out_messages); |
| 352 |
cd_message_free(evict); |
| 353 |
} |
| 354 |
DEQ_INSERT_TAIL(out_messages, cdm); |
| 355 |
pthread_mutex_unlock(&send_lock); |
| 356 |
|
| 357 |
pn_message_free(message); |
| 358 |
|
| 359 |
/* activate the sender */ |
| 360 |
if (conn) { |
| 361 |
pn_connection_wake(conn); |
| 362 |
} |
| 363 |
|
| 364 |
return 0; |
| 365 |
} /* }}} int encqueue */ |
| 366 |
|
| 367 |
static int amqp1_notify(notification_t const *n, |
| 368 |
user_data_t *user_data) /* {{{ */ |
| 369 |
{ |
| 370 |
int status = 0; |
| 371 |
size_t bfree = BUFSIZE; |
| 372 |
size_t bfill = 0; |
| 373 |
size_t bufsize = BUFSIZE; |
| 374 |
|
| 375 |
if (n == NULL || user_data == NULL) |
| 376 |
return EINVAL; |
| 377 |
|
| 378 |
amqp1_config_instance_t *instance = user_data->data; |
| 379 |
|
| 380 |
if (instance->notify != true) { |
| 381 |
ERROR("amqp1 plugin: write notification failed"); |
| 382 |
} |
| 383 |
|
| 384 |
cd_message_t *cdm = malloc(sizeof(*cdm)); |
| 385 |
if (cdm == NULL) { |
| 386 |
ERROR("amqp1 plugin: notify failed"); |
| 387 |
return -1; |
| 388 |
} |
| 389 |
|
| 390 |
DEQ_ITEM_INIT(cdm); |
| 391 |
char *start = malloc(bufsize); |
| 392 |
if (start == NULL) { |
| 393 |
ERROR("amqp1 plugin: malloc failed"); |
| 394 |
free(cdm); |
| 395 |
return -1; |
| 396 |
} |
| 397 |
cdm->mbuf.size = bufsize; |
| 398 |
cdm->mbuf.start = start; |
| 399 |
cdm->instance = instance; |
| 400 |
|
| 401 |
switch (instance->format) { |
| 402 |
case AMQP1_FORMAT_JSON: |
| 403 |
format_json_initialize(cdm->mbuf.start, &bfill, &bfree); |
| 404 |
status = format_json_notification(cdm->mbuf.start, bufsize, n); |
| 405 |
if (status != 0) { |
| 406 |
ERROR("amqp1 plugin: formatting notification failed"); |
| 407 |
cd_message_free(cdm); |
| 408 |
return status; |
| 409 |
} |
| 410 |
cdm->mbuf.size = strlen(cdm->mbuf.start); |
| 411 |
if (cdm->mbuf.size >= BUFSIZE) { |
| 412 |
ERROR("amqp1 plugin: notify format json failed"); |
| 413 |
cd_message_free(cdm); |
| 414 |
return -1; |
| 415 |
} |
| 416 |
break; |
| 417 |
default: |
| 418 |
ERROR("amqp1 plugin: Invalid notify format (%i).", instance->format); |
| 419 |
cd_message_free(cdm); |
| 420 |
return -1; |
| 421 |
} |
| 422 |
|
| 423 |
/* encode message and place on outbound queue */ |
| 424 |
status = encqueue(cdm, instance); |
| 425 |
if (status != 0) { |
| 426 |
ERROR("amqp1 plugin: notify enqueue failed"); |
| 427 |
cd_message_free(cdm); |
| 428 |
} |
| 429 |
return status; |
| 430 |
|
| 431 |
} /* }}} int amqp1_notify */ |
| 432 |
|
| 433 |
static int amqp1_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */ |
| 434 |
user_data_t *user_data) { |
| 435 |
int status = 0; |
| 436 |
size_t bfree = BUFSIZE; |
| 437 |
size_t bfill = 0; |
| 438 |
size_t bufsize = BUFSIZE; |
| 439 |
|
| 440 |
if (ds == NULL || vl == NULL || transport == NULL || user_data == NULL) |
| 441 |
return EINVAL; |
| 442 |
|
| 443 |
amqp1_config_instance_t *instance = user_data->data; |
| 444 |
|
| 445 |
if (instance->notify != false) { |
| 446 |
ERROR("amqp1 plugin: write failed"); |
| 447 |
} |
| 448 |
|
| 449 |
cd_message_t *cdm = malloc(sizeof(*cdm)); |
| 450 |
if (cdm == NULL) { |
| 451 |
ERROR("amqp1 plugin: malloc failed."); |
| 452 |
return -1; |
| 453 |
} |
| 454 |
DEQ_ITEM_INIT(cdm); |
| 455 |
char *start = malloc(bufsize); |
| 456 |
if (start == NULL) { |
| 457 |
ERROR("amqp1 plugin: malloc failed."); |
| 458 |
free(cdm); |
| 459 |
return -1; |
| 460 |
} |
| 461 |
cdm->mbuf.size = bufsize; |
| 462 |
cdm->mbuf.start = start; |
| 463 |
cdm->instance = instance; |
| 464 |
|
| 465 |
switch (instance->format) { |
| 466 |
case AMQP1_FORMAT_COMMAND: |
| 467 |
status = cmd_create_putval((char *)cdm->mbuf.start, bufsize, ds, vl); |
| 468 |
if (status != 0) { |
| 469 |
ERROR("amqp1 plugin: cmd_create_putval failed with status %i.", status); |
| 470 |
cd_message_free(cdm); |
| 471 |
return status; |
| 472 |
} |
| 473 |
cdm->mbuf.size = strlen(cdm->mbuf.start); |
| 474 |
if (cdm->mbuf.size >= BUFSIZE) { |
| 475 |
ERROR("amqp1 plugin: format cmd failed"); |
| 476 |
cd_message_free(cdm); |
| 477 |
return -1; |
| 478 |
} |
| 479 |
break; |
| 480 |
case AMQP1_FORMAT_JSON: |
| 481 |
format_json_initialize((char *)cdm->mbuf.start, &bfill, &bfree); |
| 482 |
format_json_value_list((char *)cdm->mbuf.start, &bfill, &bfree, ds, vl, |
| 483 |
instance->store_rates); |
| 484 |
status = format_json_finalize((char *)cdm->mbuf.start, &bfill, &bfree); |
| 485 |
if (status != 0) { |
| 486 |
ERROR("amqp1 plugin: format_json_finalize failed with status %i.", |
| 487 |
status); |
| 488 |
cd_message_free(cdm); |
| 489 |
return status; |
| 490 |
} |
| 491 |
cdm->mbuf.size = strlen(cdm->mbuf.start); |
| 492 |
if (cdm->mbuf.size >= BUFSIZE) { |
| 493 |
ERROR("amqp1 plugin: format json failed"); |
| 494 |
cd_message_free(cdm); |
| 495 |
return -1; |
| 496 |
} |
| 497 |
break; |
| 498 |
case AMQP1_FORMAT_GRAPHITE: |
| 499 |
status = format_graphite((char *)cdm->mbuf.start, bufsize, ds, vl, |
| 500 |
instance->prefix, instance->postfix, |
| 501 |
instance->escape_char, instance->graphite_flags); |
| 502 |
if (status != 0) { |
| 503 |
ERROR("amqp1 plugin: format_graphite failed with status %i.", status); |
| 504 |
cd_message_free(cdm); |
| 505 |
return status; |
| 506 |
} |
| 507 |
cdm->mbuf.size = strlen(cdm->mbuf.start); |
| 508 |
if (cdm->mbuf.size >= BUFSIZE) { |
| 509 |
ERROR("amqp1 plugin: format graphite failed"); |
| 510 |
cd_message_free(cdm); |
| 511 |
return -1; |
| 512 |
} |
| 513 |
break; |
| 514 |
default: |
| 515 |
ERROR("amqp1 plugin: Invalid write format (%i).", instance->format); |
| 516 |
cd_message_free(cdm); |
| 517 |
return -1; |
| 518 |
} |
| 519 |
|
| 520 |
/* encode message and place on outbound queue */ |
| 521 |
status = encqueue(cdm, instance); |
| 522 |
if (status != 0) { |
| 523 |
ERROR("amqp1 plugin: write enqueue failed"); |
| 524 |
cd_message_free(cdm); |
| 525 |
} |
| 526 |
return status; |
| 527 |
|
| 528 |
} /* }}} int amqp1_write */ |
| 529 |
|
| 530 |
static void amqp1_config_transport_free(void *ptr) /* {{{ */ |
| 531 |
{ |
| 532 |
amqp1_config_transport_t *transport = ptr; |
| 533 |
|
| 534 |
if (transport == NULL) |
| 535 |
return; |
| 536 |
|
| 537 |
sfree(transport->name); |
| 538 |
sfree(transport->host); |
| 539 |
sfree(transport->port); |
| 540 |
sfree(transport->user); |
| 541 |
sfree(transport->password); |
| 542 |
sfree(transport->address); |
| 543 |
|
| 544 |
sfree(transport); |
| 545 |
} /* }}} void amqp1_config_transport_free */ |
| 546 |
|
| 547 |
static void amqp1_config_instance_free(void *ptr) /* {{{ */ |
| 548 |
{ |
| 549 |
amqp1_config_instance_t *instance = ptr; |
| 550 |
|
| 551 |
if (instance == NULL) |
| 552 |
return; |
| 553 |
|
| 554 |
sfree(instance->name); |
| 555 |
sfree(instance->prefix); |
| 556 |
sfree(instance->postfix); |
| 557 |
|
| 558 |
sfree(instance); |
| 559 |
} /* }}} void amqp1_config_instance_free */ |
| 560 |
|
| 561 |
static int amqp1_config_instance(oconfig_item_t *ci) /* {{{ */ |
| 562 |
{ |
| 563 |
amqp1_config_instance_t *instance = calloc(1, sizeof(*instance)); |
| 564 |
if (instance == NULL) { |
| 565 |
ERROR("amqp1 plugin: calloc failed."); |
| 566 |
return ENOMEM; |
| 567 |
} |
| 568 |
|
| 569 |
int status = cf_util_get_string(ci, &instance->name); |
| 570 |
if (status != 0) { |
| 571 |
sfree(instance); |
| 572 |
return status; |
| 573 |
} |
| 574 |
|
| 575 |
for (int i = 0; i < ci->children_num; i++) { |
| 576 |
oconfig_item_t *child = ci->children + i; |
| 577 |
|
| 578 |
if (strcasecmp("PreSettle", child->key) == 0) |
| 579 |
status = cf_util_get_boolean(child, &instance->pre_settle); |
| 580 |
else if (strcasecmp("Notify", child->key) == 0) |
| 581 |
status = cf_util_get_boolean(child, &instance->notify); |
| 582 |
else if (strcasecmp("Format", child->key) == 0) { |
| 583 |
char *key = NULL; |
| 584 |
status = cf_util_get_string(child, &key); |
| 585 |
if (status != 0) { |
| 586 |
amqp1_config_instance_free(instance); |
| 587 |
return status; |
| 588 |
} |
| 589 |
assert(key != NULL); |
| 590 |
if (strcasecmp(key, "Command") == 0) { |
| 591 |
instance->format = AMQP1_FORMAT_COMMAND; |
| 592 |
} else if (strcasecmp(key, "Graphite") == 0) { |
| 593 |
instance->format = AMQP1_FORMAT_GRAPHITE; |
| 594 |
} else if (strcasecmp(key, "JSON") == 0) { |
| 595 |
instance->format = AMQP1_FORMAT_JSON; |
| 596 |
} else { |
| 597 |
WARNING("amqp1 plugin: Invalid format string: %s", key); |
| 598 |
} |
| 599 |
sfree(key); |
| 600 |
} else if (strcasecmp("StoreRates", child->key) == 0) |
| 601 |
status = cf_util_get_boolean(child, &instance->store_rates); |
| 602 |
else if (strcasecmp("GraphiteSeparateInstances", child->key) == 0) |
| 603 |
status = cf_util_get_flag(child, &instance->graphite_flags, |
| 604 |
GRAPHITE_SEPARATE_INSTANCES); |
| 605 |
else if (strcasecmp("GraphiteAlwaysAppendDS", child->key) == 0) |
| 606 |
status = cf_util_get_flag(child, &instance->graphite_flags, |
| 607 |
GRAPHITE_ALWAYS_APPEND_DS); |
| 608 |
else if (strcasecmp("GraphitePreserveSeparator", child->key) == 0) |
| 609 |
status = cf_util_get_flag(child, &instance->graphite_flags, |
| 610 |
GRAPHITE_PRESERVE_SEPARATOR); |
| 611 |
else if (strcasecmp("GraphitePrefix", child->key) == 0) |
| 612 |
status = cf_util_get_string(child, &instance->prefix); |
| 613 |
else if (strcasecmp("GraphitePostfix", child->key) == 0) |
| 614 |
status = cf_util_get_string(child, &instance->postfix); |
| 615 |
else if (strcasecmp("GraphiteEscapeChar", child->key) == 0) { |
| 616 |
char *tmp_buff = NULL; |
| 617 |
status = cf_util_get_string(child, &tmp_buff); |
| 618 |
if (status == 0) { |
| 619 |
if (strlen(tmp_buff) > 1) |
| 620 |
WARNING("amqp1 plugin: The option \"GraphiteEscapeChar\" handles " |
| 621 |
"only one character. Others will be ignored."); |
| 622 |
instance->escape_char = tmp_buff[0]; |
| 623 |
} |
| 624 |
sfree(tmp_buff); |
| 625 |
} else |
| 626 |
WARNING("amqp1 plugin: Ignoring unknown " |
| 627 |
"instance configuration option " |
| 628 |
"\"%s\".", |
| 629 |
child->key); |
| 630 |
if (status != 0) |
| 631 |
break; |
| 632 |
} |
| 633 |
|
| 634 |
if (status != 0) { |
| 635 |
amqp1_config_instance_free(instance); |
| 636 |
return status; |
| 637 |
} else { |
| 638 |
char tpname[DATA_MAX_NAME_LEN]; |
| 639 |
status = ssnprintf(tpname, sizeof(tpname), "amqp1/%s", instance->name); |
| 640 |
if ((status < 0) || (size_t)status >= sizeof(tpname)) { |
| 641 |
ERROR("amqp1 plugin: Instance name would have been truncated."); |
| 642 |
amqp1_config_instance_free(instance); |
| 643 |
return -1; |
| 644 |
} |
| 645 |
status = ssnprintf(instance->send_to, sizeof(instance->send_to), "/%s/%s", |
| 646 |
transport->address, instance->name); |
| 647 |
if ((status < 0) || (size_t)status >= sizeof(instance->send_to)) { |
| 648 |
ERROR("amqp1 plugin: send_to address would have been truncated."); |
| 649 |
amqp1_config_instance_free(instance); |
| 650 |
return -1; |
| 651 |
} |
| 652 |
if (instance->notify) { |
| 653 |
status = plugin_register_notification( |
| 654 |
tpname, amqp1_notify, |
| 655 |
&(user_data_t){ |
| 656 |
.data = instance, |
| 657 |
.free_func = amqp1_config_instance_free, |
| 658 |
}); |
| 659 |
} else { |
| 660 |
status = |
| 661 |
plugin_register_write(tpname, amqp1_write, |
| 662 |
&(user_data_t){ |
| 663 |
.data = instance, |
| 664 |
.free_func = amqp1_config_instance_free, |
| 665 |
}); |
| 666 |
} |
| 667 |
|
| 668 |
if (status != 0) { |
| 669 |
amqp1_config_instance_free(instance); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
return status; |
| 674 |
} /* }}} int amqp1_config_instance */ |
| 675 |
|
| 676 |
static int amqp1_config_transport(oconfig_item_t *ci) /* {{{ */ |
| 677 |
{ |
| 678 |
transport = calloc(1, sizeof(*transport)); |
| 679 |
if (transport == NULL) { |
| 680 |
ERROR("amqp1 plugin: calloc failed."); |
| 681 |
return ENOMEM; |
| 682 |
} |
| 683 |
|
| 684 |
/* Initialize transport configuration {{{ */ |
| 685 |
transport->retry_delay = 1; |
| 686 |
|
| 687 |
int status = cf_util_get_string(ci, &transport->name); |
| 688 |
if (status != 0) { |
| 689 |
sfree(transport); |
| 690 |
return status; |
| 691 |
} |
| 692 |
|
| 693 |
for (int i = 0; i < ci->children_num; i++) { |
| 694 |
oconfig_item_t *child = ci->children + i; |
| 695 |
|
| 696 |
if (strcasecmp("Host", child->key) == 0) |
| 697 |
status = cf_util_get_string(child, &transport->host); |
| 698 |
else if (strcasecmp("Port", child->key) == 0) |
| 699 |
status = cf_util_get_string(child, &transport->port); |
| 700 |
else if (strcasecmp("User", child->key) == 0) |
| 701 |
status = cf_util_get_string(child, &transport->user); |
| 702 |
else if (strcasecmp("Password", child->key) == 0) |
| 703 |
status = cf_util_get_string(child, &transport->password); |
| 704 |
else if (strcasecmp("Address", child->key) == 0) |
| 705 |
status = cf_util_get_string(child, &transport->address); |
| 706 |
else if (strcasecmp("RetryDelay", child->key) == 0) |
| 707 |
status = cf_util_get_int(child, &transport->retry_delay); |
| 708 |
else if (strcasecmp("Instance", child->key) == 0) |
| 709 |
amqp1_config_instance(child); |
| 710 |
else if (strcasecmp("SendQueueLimit", child->key) == 0) |
| 711 |
status = cf_util_get_int(child, &transport->sendq_limit); |
| 712 |
else |
| 713 |
WARNING("amqp1 plugin: Ignoring unknown " |
| 714 |
"transport configuration option " |
| 715 |
"\"%s\".", |
| 716 |
child->key); |
| 717 |
|
| 718 |
if (status != 0) |
| 719 |
break; |
| 720 |
} |
| 721 |
|
| 722 |
if (status != 0) { |
| 723 |
amqp1_config_transport_free(transport); |
| 724 |
} |
| 725 |
return status; |
| 726 |
} /* }}} int amqp1_config_transport */ |
| 727 |
|
| 728 |
static int amqp1_config(oconfig_item_t *ci) /* {{{ */ |
| 729 |
{ |
| 730 |
|
| 731 |
for (int i = 0; i < ci->children_num; i++) { |
| 732 |
oconfig_item_t *child = ci->children + i; |
| 733 |
|
| 734 |
if (strcasecmp("Transport", child->key) == 0) |
| 735 |
amqp1_config_transport(child); |
| 736 |
else |
| 737 |
WARNING("amqp1 plugin: Ignoring unknown config option \"%s\".", |
| 738 |
child->key); |
| 739 |
} |
| 740 |
|
| 741 |
return 0; |
| 742 |
} /* }}} int amqp1_config */ |
| 743 |
|
| 744 |
static int amqp1_init(void) /* {{{ */ |
| 745 |
{ |
| 746 |
if (transport == NULL) { |
| 747 |
ERROR("amqp1: init failed, no transport configured"); |
| 748 |
return -1; |
| 749 |
} |
| 750 |
|
| 751 |
if (proactor == NULL) { |
| 752 |
pthread_mutex_init(&send_lock, /* attr = */ NULL); |
| 753 |
/* start_thread */ |
| 754 |
int status = plugin_thread_create(&event_thread_id, event_thread, |
| 755 |
NULL /* no argument */, "handle"); |
| 756 |
if (status != 0) { |
| 757 |
ERROR("amqp1 plugin: pthread_create failed: %s", STRERRNO); |
| 758 |
} else { |
| 759 |
event_thread_running = true; |
| 760 |
} |
| 761 |
} |
| 762 |
return 0; |
| 763 |
} /* }}} int amqp1_init */ |
| 764 |
|
| 765 |
static int amqp1_shutdown(void) /* {{{ */ |
| 766 |
{ |
| 767 |
stopping = true; |
| 768 |
|
| 769 |
/* Stop the proactor thread */ |
| 770 |
if (event_thread_running) { |
| 771 |
DEBUG("amqp1 plugin: Shutting down proactor thread."); |
| 772 |
pn_connection_wake(conn); |
| 773 |
} |
| 774 |
pthread_join(event_thread_id, NULL /* no return value */); |
| 775 |
memset(&event_thread_id, 0, sizeof(event_thread_id)); |
| 776 |
|
| 777 |
DEBUG("amqp1 plugin: proactor thread exited."); |
| 778 |
|
| 779 |
if (transport) { |
| 780 |
amqp1_config_transport_free(transport); |
| 781 |
} |
| 782 |
|
| 783 |
return 0; |
| 784 |
} /* }}} int amqp1_shutdown */ |
| 785 |
|
| 786 |
void module_register(void) { |
| 787 |
plugin_register_complex_config("amqp1", amqp1_config); |
| 788 |
plugin_register_init("amqp1", amqp1_init); |
| 789 |
plugin_register_shutdown("amqp1", amqp1_shutdown); |
| 790 |
} /* void module_register */ |