| 1 |
/** |
| 2 |
* collectd - src/write_riemann.c |
| 3 |
* Copyright (C) 2012,2013 Pierre-Yves Ritschard |
| 4 |
* Copyright (C) 2013 Florian octo Forster |
| 5 |
* Copyright (C) 2015,2016 Gergely Nagy |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
* copy of this software and associated documentation files (the "Software"), |
| 9 |
* to deal in the Software without restriction, including without limitation |
| 10 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in |
| 15 |
* all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 |
* DEALINGS IN THE SOFTWARE. |
| 24 |
* |
| 25 |
* Authors: |
| 26 |
* Pierre-Yves Ritschard <pyr at spootnik.org> |
| 27 |
* Florian octo Forster <octo at collectd.org> |
| 28 |
* Gergely Nagy <algernon at madhouse-project.org> |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "collectd.h" |
| 32 |
|
| 33 |
#include "plugin.h" |
| 34 |
#include "utils/common/common.h" |
| 35 |
#include "utils_cache.h" |
| 36 |
#include "utils_complain.h" |
| 37 |
#include "write_riemann_threshold.h" |
| 38 |
|
| 39 |
#include <riemann/riemann-client.h> |
| 40 |
|
| 41 |
#define RIEMANN_HOST "localhost" |
| 42 |
#define RIEMANN_PORT 5555 |
| 43 |
#define RIEMANN_TTL_FACTOR 2.0 |
| 44 |
#define RIEMANN_BATCH_MAX 8192 |
| 45 |
|
| 46 |
struct riemann_host { |
| 47 |
c_complain_t init_complaint; |
| 48 |
char *name; |
| 49 |
char *event_service_prefix; |
| 50 |
pthread_mutex_t lock; |
| 51 |
bool batch_mode; |
| 52 |
bool notifications; |
| 53 |
bool check_thresholds; |
| 54 |
bool store_rates; |
| 55 |
bool always_append_ds; |
| 56 |
char *node; |
| 57 |
int port; |
| 58 |
riemann_client_type_t client_type; |
| 59 |
riemann_client_t *client; |
| 60 |
double ttl_factor; |
| 61 |
cdtime_t batch_init; |
| 62 |
int batch_max; |
| 63 |
int batch_timeout; |
| 64 |
int reference_count; |
| 65 |
riemann_message_t *batch_msg; |
| 66 |
char *tls_ca_file; |
| 67 |
char *tls_cert_file; |
| 68 |
char *tls_key_file; |
| 69 |
struct timeval timeout; |
| 70 |
}; |
| 71 |
|
| 72 |
static char **riemann_tags; |
| 73 |
static size_t riemann_tags_num; |
| 74 |
static char **riemann_attrs; |
| 75 |
static size_t riemann_attrs_num; |
| 76 |
|
| 77 |
/* host->lock must be held when calling this function. */ |
| 78 |
static int wrr_connect(struct riemann_host *host) /* {{{ */ |
| 79 |
{ |
| 80 |
char const *node; |
| 81 |
int port; |
| 82 |
|
| 83 |
if (host->client) |
| 84 |
return 0; |
| 85 |
|
| 86 |
node = (host->node != NULL) ? host->node : RIEMANN_HOST; |
| 87 |
port = (host->port) ? host->port : RIEMANN_PORT; |
| 88 |
|
| 89 |
host->client = NULL; |
| 90 |
|
| 91 |
host->client = riemann_client_create( |
| 92 |
host->client_type, node, port, RIEMANN_CLIENT_OPTION_TLS_CA_FILE, |
| 93 |
host->tls_ca_file, RIEMANN_CLIENT_OPTION_TLS_CERT_FILE, |
| 94 |
host->tls_cert_file, RIEMANN_CLIENT_OPTION_TLS_KEY_FILE, |
| 95 |
host->tls_key_file, RIEMANN_CLIENT_OPTION_NONE); |
| 96 |
if (host->client == NULL) { |
| 97 |
c_complain(LOG_ERR, &host->init_complaint, |
| 98 |
"write_riemann plugin: Unable to connect to Riemann at %s:%d", |
| 99 |
node, port); |
| 100 |
return -1; |
| 101 |
} |
| 102 |
#if RCC_VERSION_NUMBER >= 0x010800 |
| 103 |
if (host->timeout.tv_sec != 0) { |
| 104 |
if (riemann_client_set_timeout(host->client, &host->timeout) != 0) { |
| 105 |
riemann_client_free(host->client); |
| 106 |
host->client = NULL; |
| 107 |
c_complain(LOG_ERR, &host->init_complaint, |
| 108 |
"write_riemann plugin: Unable to connect to Riemann at %s:%d", |
| 109 |
node, port); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
} |
| 113 |
#endif |
| 114 |
|
| 115 |
set_sock_opts(riemann_client_get_fd(host->client)); |
| 116 |
|
| 117 |
c_release(LOG_INFO, &host->init_complaint, |
| 118 |
"write_riemann plugin: Successfully connected to %s:%d", node, |
| 119 |
port); |
| 120 |
|
| 121 |
return 0; |
| 122 |
} /* }}} int wrr_connect */ |
| 123 |
|
| 124 |
/* host->lock must be held when calling this function. */ |
| 125 |
static int wrr_disconnect(struct riemann_host *host) /* {{{ */ |
| 126 |
{ |
| 127 |
if (!host->client) |
| 128 |
return 0; |
| 129 |
|
| 130 |
riemann_client_free(host->client); |
| 131 |
host->client = NULL; |
| 132 |
|
| 133 |
return 0; |
| 134 |
} /* }}} int wrr_disconnect */ |
| 135 |
|
| 136 |
/** |
| 137 |
* Function to send messages to riemann. |
| 138 |
* |
| 139 |
* Acquires the host lock, disconnects on errors. |
| 140 |
*/ |
| 141 |
static int wrr_send_nolock(struct riemann_host *host, |
| 142 |
riemann_message_t *msg) /* {{{ */ |
| 143 |
{ |
| 144 |
int status = 0; |
| 145 |
|
| 146 |
status = wrr_connect(host); |
| 147 |
if (status != 0) { |
| 148 |
return status; |
| 149 |
} |
| 150 |
|
| 151 |
status = riemann_client_send_message(host->client, msg); |
| 152 |
if (status != 0) { |
| 153 |
wrr_disconnect(host); |
| 154 |
return status; |
| 155 |
} |
| 156 |
|
| 157 |
/* |
| 158 |
* For TCP we need to receive message acknowledgemenent. |
| 159 |
*/ |
| 160 |
if (host->client_type != RIEMANN_CLIENT_UDP) { |
| 161 |
riemann_message_t *response; |
| 162 |
|
| 163 |
response = riemann_client_recv_message(host->client); |
| 164 |
|
| 165 |
if (response == NULL) { |
| 166 |
wrr_disconnect(host); |
| 167 |
return errno; |
| 168 |
} |
| 169 |
riemann_message_free(response); |
| 170 |
} |
| 171 |
|
| 172 |
return 0; |
| 173 |
} /* }}} int wrr_send */ |
| 174 |
|
| 175 |
static int wrr_send(struct riemann_host *host, riemann_message_t *msg) { |
| 176 |
int status = 0; |
| 177 |
|
| 178 |
pthread_mutex_lock(&host->lock); |
| 179 |
status = wrr_send_nolock(host, msg); |
| 180 |
pthread_mutex_unlock(&host->lock); |
| 181 |
return status; |
| 182 |
} |
| 183 |
|
| 184 |
static riemann_message_t *wrr_notification_to_message(notification_t const *n) { |
| 185 |
riemann_message_t *msg; |
| 186 |
riemann_event_t *event; |
| 187 |
char service_buffer[6 * DATA_MAX_NAME_LEN]; |
| 188 |
char const *severity; |
| 189 |
|
| 190 |
switch (n->severity) { |
| 191 |
case NOTIF_OKAY: |
| 192 |
severity = "ok"; |
| 193 |
break; |
| 194 |
case NOTIF_WARNING: |
| 195 |
severity = "warning"; |
| 196 |
break; |
| 197 |
case NOTIF_FAILURE: |
| 198 |
severity = "critical"; |
| 199 |
break; |
| 200 |
default: |
| 201 |
severity = "unknown"; |
| 202 |
} |
| 203 |
|
| 204 |
format_name(service_buffer, sizeof(service_buffer), |
| 205 |
/* host = */ "", n->plugin, n->plugin_instance, n->type, |
| 206 |
n->type_instance); |
| 207 |
|
| 208 |
event = riemann_event_create( |
| 209 |
RIEMANN_EVENT_FIELD_HOST, n->host, RIEMANN_EVENT_FIELD_TIME, |
| 210 |
(int64_t)CDTIME_T_TO_TIME_T(n->time), RIEMANN_EVENT_FIELD_TAGS, |
| 211 |
"notification", NULL, RIEMANN_EVENT_FIELD_STATE, severity, |
| 212 |
RIEMANN_EVENT_FIELD_SERVICE, &service_buffer[1], |
| 213 |
RIEMANN_EVENT_FIELD_NONE); |
| 214 |
|
| 215 |
#if RCC_VERSION_NUMBER >= 0x010A00 |
| 216 |
riemann_event_set(event, RIEMANN_EVENT_FIELD_TIME_MICROS, |
| 217 |
(int64_t)CDTIME_T_TO_US(n->time)); |
| 218 |
#endif |
| 219 |
|
| 220 |
if (n->host[0] != 0) |
| 221 |
riemann_event_string_attribute_add(event, "host", n->host); |
| 222 |
if (n->plugin[0] != 0) |
| 223 |
riemann_event_string_attribute_add(event, "plugin", n->plugin); |
| 224 |
if (n->plugin_instance[0] != 0) |
| 225 |
riemann_event_string_attribute_add(event, "plugin_instance", |
| 226 |
n->plugin_instance); |
| 227 |
|
| 228 |
if (n->type[0] != 0) |
| 229 |
riemann_event_string_attribute_add(event, "type", n->type); |
| 230 |
if (n->type_instance[0] != 0) |
| 231 |
riemann_event_string_attribute_add(event, "type_instance", |
| 232 |
n->type_instance); |
| 233 |
|
| 234 |
for (size_t i = 0; i < riemann_attrs_num; i += 2) |
| 235 |
riemann_event_string_attribute_add(event, riemann_attrs[i], |
| 236 |
riemann_attrs[i + 1]); |
| 237 |
|
| 238 |
for (size_t i = 0; i < riemann_tags_num; i++) |
| 239 |
riemann_event_tag_add(event, riemann_tags[i]); |
| 240 |
|
| 241 |
if (n->message[0] != 0) |
| 242 |
riemann_event_string_attribute_add(event, "description", n->message); |
| 243 |
|
| 244 |
/* Pull in values from threshold and add extra attributes */ |
| 245 |
for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) { |
| 246 |
if (strcasecmp("CurrentValue", meta->name) == 0 && |
| 247 |
meta->type == NM_TYPE_DOUBLE) { |
| 248 |
riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D, |
| 249 |
(double)meta->nm_value.nm_double, |
| 250 |
RIEMANN_EVENT_FIELD_NONE); |
| 251 |
continue; |
| 252 |
} |
| 253 |
|
| 254 |
if (meta->type == NM_TYPE_STRING) { |
| 255 |
riemann_event_string_attribute_add(event, meta->name, |
| 256 |
meta->nm_value.nm_string); |
| 257 |
continue; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
msg = riemann_message_create_with_events(event, NULL); |
| 262 |
if (msg == NULL) { |
| 263 |
ERROR("write_riemann plugin: riemann_message_create_with_events() failed."); |
| 264 |
riemann_event_free(event); |
| 265 |
return NULL; |
| 266 |
} |
| 267 |
|
| 268 |
DEBUG("write_riemann plugin: Successfully created message for notification: " |
| 269 |
"host = \"%s\", service = \"%s\", state = \"%s\"", |
| 270 |
event->host, event->service, event->state); |
| 271 |
return msg; |
| 272 |
} |
| 273 |
|
| 274 |
static riemann_event_t * |
| 275 |
wrr_value_to_event(struct riemann_host const *host, /* {{{ */ |
| 276 |
data_set_t const *ds, value_list_t const *vl, size_t index, |
| 277 |
gauge_t const *rates, int status) { |
| 278 |
riemann_event_t *event; |
| 279 |
char name_buffer[5 * DATA_MAX_NAME_LEN]; |
| 280 |
char service_buffer[6 * DATA_MAX_NAME_LEN]; |
| 281 |
size_t i; |
| 282 |
|
| 283 |
event = riemann_event_new(); |
| 284 |
if (event == NULL) { |
| 285 |
ERROR("write_riemann plugin: riemann_event_new() failed."); |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
|
| 289 |
format_name(name_buffer, sizeof(name_buffer), |
| 290 |
/* host = */ "", vl->plugin, vl->plugin_instance, vl->type, |
| 291 |
vl->type_instance); |
| 292 |
if (host->always_append_ds || (ds->ds_num > 1)) { |
| 293 |
if (host->event_service_prefix == NULL) |
| 294 |
ssnprintf(service_buffer, sizeof(service_buffer), "%s/%s", |
| 295 |
&name_buffer[1], ds->ds[index].name); |
| 296 |
else |
| 297 |
ssnprintf(service_buffer, sizeof(service_buffer), "%s%s/%s", |
| 298 |
host->event_service_prefix, &name_buffer[1], |
| 299 |
ds->ds[index].name); |
| 300 |
} else { |
| 301 |
if (host->event_service_prefix == NULL) |
| 302 |
sstrncpy(service_buffer, &name_buffer[1], sizeof(service_buffer)); |
| 303 |
else |
| 304 |
ssnprintf(service_buffer, sizeof(service_buffer), "%s%s", |
| 305 |
host->event_service_prefix, &name_buffer[1]); |
| 306 |
} |
| 307 |
|
| 308 |
riemann_event_set( |
| 309 |
event, RIEMANN_EVENT_FIELD_HOST, vl->host, RIEMANN_EVENT_FIELD_TIME, |
| 310 |
(int64_t)CDTIME_T_TO_TIME_T(vl->time), RIEMANN_EVENT_FIELD_TTL, |
| 311 |
(float)CDTIME_T_TO_DOUBLE(vl->interval) * host->ttl_factor, |
| 312 |
RIEMANN_EVENT_FIELD_STRING_ATTRIBUTES, "plugin", vl->plugin, "type", |
| 313 |
vl->type, "ds_name", ds->ds[index].name, NULL, |
| 314 |
RIEMANN_EVENT_FIELD_SERVICE, service_buffer, RIEMANN_EVENT_FIELD_NONE); |
| 315 |
|
| 316 |
#if RCC_VERSION_NUMBER >= 0x010A00 |
| 317 |
riemann_event_set(event, RIEMANN_EVENT_FIELD_TIME_MICROS, |
| 318 |
(int64_t)CDTIME_T_TO_US(vl->time)); |
| 319 |
#endif |
| 320 |
|
| 321 |
if (host->check_thresholds) { |
| 322 |
const char *state = NULL; |
| 323 |
|
| 324 |
switch (status) { |
| 325 |
case STATE_OKAY: |
| 326 |
state = "ok"; |
| 327 |
break; |
| 328 |
case STATE_ERROR: |
| 329 |
state = "critical"; |
| 330 |
break; |
| 331 |
case STATE_WARNING: |
| 332 |
state = "warning"; |
| 333 |
break; |
| 334 |
case STATE_MISSING: |
| 335 |
state = "unknown"; |
| 336 |
break; |
| 337 |
} |
| 338 |
if (state) |
| 339 |
riemann_event_set(event, RIEMANN_EVENT_FIELD_STATE, state, |
| 340 |
RIEMANN_EVENT_FIELD_NONE); |
| 341 |
} |
| 342 |
|
| 343 |
if (vl->plugin_instance[0] != 0) |
| 344 |
riemann_event_string_attribute_add(event, "plugin_instance", |
| 345 |
vl->plugin_instance); |
| 346 |
if (vl->type_instance[0] != 0) |
| 347 |
riemann_event_string_attribute_add(event, "type_instance", |
| 348 |
vl->type_instance); |
| 349 |
|
| 350 |
if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) { |
| 351 |
char ds_type[DATA_MAX_NAME_LEN]; |
| 352 |
|
| 353 |
ssnprintf(ds_type, sizeof(ds_type), "%s:rate", |
| 354 |
DS_TYPE_TO_STRING(ds->ds[index].type)); |
| 355 |
riemann_event_string_attribute_add(event, "ds_type", ds_type); |
| 356 |
} else { |
| 357 |
riemann_event_string_attribute_add(event, "ds_type", |
| 358 |
DS_TYPE_TO_STRING(ds->ds[index].type)); |
| 359 |
} |
| 360 |
|
| 361 |
{ |
| 362 |
char ds_index[DATA_MAX_NAME_LEN]; |
| 363 |
|
| 364 |
ssnprintf(ds_index, sizeof(ds_index), "%" PRIsz, index); |
| 365 |
riemann_event_string_attribute_add(event, "ds_index", ds_index); |
| 366 |
} |
| 367 |
|
| 368 |
for (i = 0; i < riemann_attrs_num; i += 2) |
| 369 |
riemann_event_string_attribute_add(event, riemann_attrs[i], |
| 370 |
riemann_attrs[i + 1]); |
| 371 |
|
| 372 |
for (i = 0; i < riemann_tags_num; i++) |
| 373 |
riemann_event_tag_add(event, riemann_tags[i]); |
| 374 |
|
| 375 |
if (ds->ds[index].type == DS_TYPE_GAUGE) { |
| 376 |
riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D, |
| 377 |
(double)vl->values[index].gauge, |
| 378 |
RIEMANN_EVENT_FIELD_NONE); |
| 379 |
} else if (rates != NULL) { |
| 380 |
riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D, (double)rates[index], |
| 381 |
RIEMANN_EVENT_FIELD_NONE); |
| 382 |
} else { |
| 383 |
int64_t metric; |
| 384 |
|
| 385 |
if (ds->ds[index].type == DS_TYPE_DERIVE) |
| 386 |
metric = (int64_t)vl->values[index].derive; |
| 387 |
else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) |
| 388 |
metric = (int64_t)vl->values[index].absolute; |
| 389 |
else |
| 390 |
metric = (int64_t)vl->values[index].counter; |
| 391 |
|
| 392 |
riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_S64, (int64_t)metric, |
| 393 |
RIEMANN_EVENT_FIELD_NONE); |
| 394 |
} |
| 395 |
|
| 396 |
if (vl->meta) { |
| 397 |
char **toc; |
| 398 |
int n = meta_data_toc(vl->meta, &toc); |
| 399 |
|
| 400 |
for (int i = 0; i < n; i++) { |
| 401 |
char *key = toc[i]; |
| 402 |
char *value; |
| 403 |
|
| 404 |
if (0 == meta_data_as_string(vl->meta, key, &value)) { |
| 405 |
riemann_event_string_attribute_add(event, key, value); |
| 406 |
free(value); |
| 407 |
} |
| 408 |
free(toc[i]); |
| 409 |
} |
| 410 |
|
| 411 |
free(toc); |
| 412 |
} |
| 413 |
|
| 414 |
DEBUG("write_riemann plugin: Successfully created message for metric: " |
| 415 |
"host = \"%s\", service = \"%s\"", |
| 416 |
event->host, event->service); |
| 417 |
return event; |
| 418 |
} /* }}} riemann_event_t *wrr_value_to_event */ |
| 419 |
|
| 420 |
static riemann_message_t * |
| 421 |
wrr_value_list_to_message(struct riemann_host const *host, /* {{{ */ |
| 422 |
data_set_t const *ds, value_list_t const *vl, |
| 423 |
int *statuses) { |
| 424 |
riemann_message_t *msg; |
| 425 |
size_t i; |
| 426 |
gauge_t *rates = NULL; |
| 427 |
|
| 428 |
/* Initialize the Msg structure. */ |
| 429 |
msg = riemann_message_new(); |
| 430 |
if (msg == NULL) { |
| 431 |
ERROR("write_riemann plugin: riemann_message_new failed."); |
| 432 |
return NULL; |
| 433 |
} |
| 434 |
|
| 435 |
if (host->store_rates) { |
| 436 |
rates = uc_get_rate(ds, vl); |
| 437 |
if (rates == NULL) { |
| 438 |
ERROR("write_riemann plugin: uc_get_rate failed."); |
| 439 |
riemann_message_free(msg); |
| 440 |
return NULL; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
for (i = 0; i < vl->values_len; i++) { |
| 445 |
riemann_event_t *event; |
| 446 |
|
| 447 |
event = wrr_value_to_event(host, ds, vl, (int)i, rates, statuses[i]); |
| 448 |
if (event == NULL) { |
| 449 |
riemann_message_free(msg); |
| 450 |
sfree(rates); |
| 451 |
return NULL; |
| 452 |
} |
| 453 |
riemann_message_append_events(msg, event, NULL); |
| 454 |
} |
| 455 |
|
| 456 |
sfree(rates); |
| 457 |
return msg; |
| 458 |
} /* }}} riemann_message_t *wrr_value_list_to_message */ |
| 459 |
|
| 460 |
/* |
| 461 |
* Always call while holding host->lock ! |
| 462 |
*/ |
| 463 |
static int wrr_batch_flush_nolock(cdtime_t timeout, struct riemann_host *host) { |
| 464 |
cdtime_t now; |
| 465 |
int status = 0; |
| 466 |
|
| 467 |
now = cdtime(); |
| 468 |
if (timeout > 0) { |
| 469 |
if ((host->batch_init + timeout) > now) { |
| 470 |
return status; |
| 471 |
} |
| 472 |
} |
| 473 |
wrr_send_nolock(host, host->batch_msg); |
| 474 |
riemann_message_free(host->batch_msg); |
| 475 |
|
| 476 |
host->batch_init = now; |
| 477 |
host->batch_msg = NULL; |
| 478 |
return status; |
| 479 |
} |
| 480 |
|
| 481 |
static int wrr_batch_flush(cdtime_t timeout, |
| 482 |
const char *identifier __attribute__((unused)), |
| 483 |
user_data_t *user_data) { |
| 484 |
struct riemann_host *host; |
| 485 |
int status; |
| 486 |
|
| 487 |
if (user_data == NULL) |
| 488 |
return -EINVAL; |
| 489 |
|
| 490 |
host = user_data->data; |
| 491 |
pthread_mutex_lock(&host->lock); |
| 492 |
status = wrr_batch_flush_nolock(timeout, host); |
| 493 |
if (status != 0) |
| 494 |
c_complain( |
| 495 |
LOG_ERR, &host->init_complaint, |
| 496 |
"write_riemann plugin: riemann_client_send failed with status %i", |
| 497 |
status); |
| 498 |
else |
| 499 |
c_release(LOG_DEBUG, &host->init_complaint, |
| 500 |
"write_riemann plugin: batch sent."); |
| 501 |
|
| 502 |
pthread_mutex_unlock(&host->lock); |
| 503 |
return status; |
| 504 |
} |
| 505 |
|
| 506 |
static int wrr_batch_add_value_list(struct riemann_host *host, /* {{{ */ |
| 507 |
data_set_t const *ds, |
| 508 |
value_list_t const *vl, int *statuses) { |
| 509 |
riemann_message_t *msg; |
| 510 |
size_t len; |
| 511 |
int ret; |
| 512 |
cdtime_t timeout; |
| 513 |
|
| 514 |
msg = wrr_value_list_to_message(host, ds, vl, statuses); |
| 515 |
if (msg == NULL) |
| 516 |
return -1; |
| 517 |
|
| 518 |
pthread_mutex_lock(&host->lock); |
| 519 |
|
| 520 |
if (host->batch_msg == NULL) { |
| 521 |
host->batch_msg = msg; |
| 522 |
} else { |
| 523 |
int status; |
| 524 |
|
| 525 |
status = riemann_message_append_events_n(host->batch_msg, msg->n_events, |
| 526 |
msg->events); |
| 527 |
msg->n_events = 0; |
| 528 |
msg->events = NULL; |
| 529 |
|
| 530 |
riemann_message_free(msg); |
| 531 |
|
| 532 |
if (status != 0) { |
| 533 |
pthread_mutex_unlock(&host->lock); |
| 534 |
ERROR("write_riemann plugin: out of memory"); |
| 535 |
return -1; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
len = riemann_message_get_packed_size(host->batch_msg); |
| 540 |
ret = 0; |
| 541 |
if ((host->batch_max < 0) || (((size_t)host->batch_max) <= len)) { |
| 542 |
ret = wrr_batch_flush_nolock(0, host); |
| 543 |
} else { |
| 544 |
if (host->batch_timeout > 0) { |
| 545 |
timeout = TIME_T_TO_CDTIME_T((time_t)host->batch_timeout); |
| 546 |
ret = wrr_batch_flush_nolock(timeout, host); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
pthread_mutex_unlock(&host->lock); |
| 551 |
return ret; |
| 552 |
} /* }}} riemann_message_t *wrr_batch_add_value_list */ |
| 553 |
|
| 554 |
static int wrr_notification(const notification_t *n, user_data_t *ud) /* {{{ */ |
| 555 |
{ |
| 556 |
int status; |
| 557 |
struct riemann_host *host = ud->data; |
| 558 |
riemann_message_t *msg; |
| 559 |
|
| 560 |
if (!host->notifications) |
| 561 |
return 0; |
| 562 |
|
| 563 |
/* |
| 564 |
* Never batch for notifications, send them ASAP |
| 565 |
*/ |
| 566 |
msg = wrr_notification_to_message(n); |
| 567 |
if (msg == NULL) |
| 568 |
return -1; |
| 569 |
|
| 570 |
status = wrr_send(host, msg); |
| 571 |
if (status != 0) |
| 572 |
c_complain( |
| 573 |
LOG_ERR, &host->init_complaint, |
| 574 |
"write_riemann plugin: riemann_client_send failed with status %i", |
| 575 |
status); |
| 576 |
else |
| 577 |
c_release(LOG_DEBUG, &host->init_complaint, |
| 578 |
"write_riemann plugin: riemann_client_send succeeded"); |
| 579 |
|
| 580 |
riemann_message_free(msg); |
| 581 |
return status; |
| 582 |
} /* }}} int wrr_notification */ |
| 583 |
|
| 584 |
static int wrr_write(const data_set_t *ds, /* {{{ */ |
| 585 |
const value_list_t *vl, user_data_t *ud) { |
| 586 |
int status = 0; |
| 587 |
int statuses[vl->values_len]; |
| 588 |
struct riemann_host *host = ud->data; |
| 589 |
riemann_message_t *msg; |
| 590 |
|
| 591 |
if (host->check_thresholds) { |
| 592 |
status = write_riemann_threshold_check(ds, vl, statuses); |
| 593 |
if (status != 0) |
| 594 |
return status; |
| 595 |
} else { |
| 596 |
memset(statuses, 0, sizeof(statuses)); |
| 597 |
} |
| 598 |
|
| 599 |
if (host->client_type != RIEMANN_CLIENT_UDP && host->batch_mode) { |
| 600 |
wrr_batch_add_value_list(host, ds, vl, statuses); |
| 601 |
} else { |
| 602 |
msg = wrr_value_list_to_message(host, ds, vl, statuses); |
| 603 |
if (msg == NULL) |
| 604 |
return -1; |
| 605 |
|
| 606 |
status = wrr_send(host, msg); |
| 607 |
|
| 608 |
riemann_message_free(msg); |
| 609 |
} |
| 610 |
return status; |
| 611 |
} /* }}} int wrr_write */ |
| 612 |
|
| 613 |
static void wrr_free(void *p) /* {{{ */ |
| 614 |
{ |
| 615 |
struct riemann_host *host = p; |
| 616 |
|
| 617 |
if (host == NULL) |
| 618 |
return; |
| 619 |
|
| 620 |
pthread_mutex_lock(&host->lock); |
| 621 |
|
| 622 |
host->reference_count--; |
| 623 |
if (host->reference_count > 0) { |
| 624 |
pthread_mutex_unlock(&host->lock); |
| 625 |
return; |
| 626 |
} |
| 627 |
|
| 628 |
wrr_disconnect(host); |
| 629 |
|
| 630 |
pthread_mutex_lock(&host->lock); |
| 631 |
pthread_mutex_destroy(&host->lock); |
| 632 |
sfree(host); |
| 633 |
} /* }}} void wrr_free */ |
| 634 |
|
| 635 |
static int wrr_config_node(oconfig_item_t *ci) /* {{{ */ |
| 636 |
{ |
| 637 |
struct riemann_host *host = NULL; |
| 638 |
int status = 0; |
| 639 |
int i; |
| 640 |
oconfig_item_t *child; |
| 641 |
char callback_name[DATA_MAX_NAME_LEN]; |
| 642 |
|
| 643 |
if ((host = calloc(1, sizeof(*host))) == NULL) { |
| 644 |
ERROR("write_riemann plugin: calloc failed."); |
| 645 |
return ENOMEM; |
| 646 |
} |
| 647 |
pthread_mutex_init(&host->lock, NULL); |
| 648 |
C_COMPLAIN_INIT(&host->init_complaint); |
| 649 |
host->reference_count = 1; |
| 650 |
host->node = NULL; |
| 651 |
host->port = 0; |
| 652 |
host->notifications = true; |
| 653 |
host->check_thresholds = false; |
| 654 |
host->store_rates = true; |
| 655 |
host->always_append_ds = false; |
| 656 |
host->batch_mode = true; |
| 657 |
host->batch_max = RIEMANN_BATCH_MAX; /* typical MSS */ |
| 658 |
host->batch_init = cdtime(); |
| 659 |
host->batch_timeout = 0; |
| 660 |
host->ttl_factor = RIEMANN_TTL_FACTOR; |
| 661 |
host->client = NULL; |
| 662 |
host->client_type = RIEMANN_CLIENT_TCP; |
| 663 |
host->timeout.tv_sec = 0; |
| 664 |
host->timeout.tv_usec = 0; |
| 665 |
|
| 666 |
status = cf_util_get_string(ci, &host->name); |
| 667 |
if (status != 0) { |
| 668 |
WARNING("write_riemann plugin: Required host name is missing."); |
| 669 |
wrr_free(host); |
| 670 |
return -1; |
| 671 |
} |
| 672 |
|
| 673 |
for (i = 0; i < ci->children_num; i++) { |
| 674 |
/* |
| 675 |
* The code here could be simplified but makes room |
| 676 |
* for easy adding of new options later on. |
| 677 |
*/ |
| 678 |
child = &ci->children[i]; |
| 679 |
status = 0; |
| 680 |
|
| 681 |
if (strcasecmp("Host", child->key) == 0) { |
| 682 |
status = cf_util_get_string(child, &host->node); |
| 683 |
if (status != 0) |
| 684 |
break; |
| 685 |
} else if (strcasecmp("Notifications", child->key) == 0) { |
| 686 |
status = cf_util_get_boolean(child, &host->notifications); |
| 687 |
if (status != 0) |
| 688 |
break; |
| 689 |
} else if (strcasecmp("EventServicePrefix", child->key) == 0) { |
| 690 |
status = cf_util_get_string(child, &host->event_service_prefix); |
| 691 |
if (status != 0) |
| 692 |
break; |
| 693 |
} else if (strcasecmp("CheckThresholds", child->key) == 0) { |
| 694 |
status = cf_util_get_boolean(child, &host->check_thresholds); |
| 695 |
if (status != 0) |
| 696 |
break; |
| 697 |
} else if (strcasecmp("Batch", child->key) == 0) { |
| 698 |
status = cf_util_get_boolean(child, &host->batch_mode); |
| 699 |
if (status != 0) |
| 700 |
break; |
| 701 |
} else if (strcasecmp("BatchMaxSize", child->key) == 0) { |
| 702 |
status = cf_util_get_int(child, &host->batch_max); |
| 703 |
if (status != 0) |
| 704 |
break; |
| 705 |
} else if (strcasecmp("BatchFlushTimeout", child->key) == 0) { |
| 706 |
status = cf_util_get_int(child, &host->batch_timeout); |
| 707 |
if (status != 0) |
| 708 |
break; |
| 709 |
} else if (strcasecmp("Timeout", child->key) == 0) { |
| 710 |
#if RCC_VERSION_NUMBER >= 0x010800 |
| 711 |
status = cf_util_get_int(child, (int *)&host->timeout.tv_sec); |
| 712 |
if (status != 0) |
| 713 |
break; |
| 714 |
#else |
| 715 |
WARNING("write_riemann plugin: The Timeout option is not supported. " |
| 716 |
"Please upgrade the Riemann client to at least 1.8.0."); |
| 717 |
#endif |
| 718 |
} else if (strcasecmp("Port", child->key) == 0) { |
| 719 |
host->port = cf_util_get_port_number(child); |
| 720 |
if (host->port == -1) { |
| 721 |
break; |
| 722 |
} |
| 723 |
} else if (strcasecmp("Protocol", child->key) == 0) { |
| 724 |
char tmp[16]; |
| 725 |
status = cf_util_get_string_buffer(child, tmp, sizeof(tmp)); |
| 726 |
if (status != 0) |
| 727 |
break; |
| 728 |
|
| 729 |
if (strcasecmp("UDP", tmp) == 0) |
| 730 |
host->client_type = RIEMANN_CLIENT_UDP; |
| 731 |
else if (strcasecmp("TCP", tmp) == 0) |
| 732 |
host->client_type = RIEMANN_CLIENT_TCP; |
| 733 |
else if (strcasecmp("TLS", tmp) == 0) |
| 734 |
host->client_type = RIEMANN_CLIENT_TLS; |
| 735 |
else |
| 736 |
WARNING("write_riemann plugin: The value " |
| 737 |
"\"%s\" is not valid for the " |
| 738 |
"\"Protocol\" option. Use " |
| 739 |
"either \"UDP\", \"TCP\" or \"TLS\".", |
| 740 |
tmp); |
| 741 |
} else if (strcasecmp("TLSCAFile", child->key) == 0) { |
| 742 |
status = cf_util_get_string(child, &host->tls_ca_file); |
| 743 |
if (status != 0) |
| 744 |
break; |
| 745 |
} else if (strcasecmp("TLSCertFile", child->key) == 0) { |
| 746 |
status = cf_util_get_string(child, &host->tls_cert_file); |
| 747 |
if (status != 0) |
| 748 |
break; |
| 749 |
} else if (strcasecmp("TLSKeyFile", child->key) == 0) { |
| 750 |
status = cf_util_get_string(child, &host->tls_key_file); |
| 751 |
if (status != 0) |
| 752 |
break; |
| 753 |
} else if (strcasecmp("StoreRates", child->key) == 0) { |
| 754 |
status = cf_util_get_boolean(child, &host->store_rates); |
| 755 |
if (status != 0) |
| 756 |
break; |
| 757 |
} else if (strcasecmp("AlwaysAppendDS", child->key) == 0) { |
| 758 |
status = cf_util_get_boolean(child, &host->always_append_ds); |
| 759 |
if (status != 0) |
| 760 |
break; |
| 761 |
} else if (strcasecmp("TTLFactor", child->key) == 0) { |
| 762 |
double tmp = NAN; |
| 763 |
status = cf_util_get_double(child, &tmp); |
| 764 |
if (status != 0) |
| 765 |
break; |
| 766 |
if (tmp >= 2.0) { |
| 767 |
host->ttl_factor = tmp; |
| 768 |
} else if (tmp >= 1.0) { |
| 769 |
NOTICE("write_riemann plugin: The configured " |
| 770 |
"TTLFactor is very small " |
| 771 |
"(%.1f). A value of 2.0 or " |
| 772 |
"greater is recommended.", |
| 773 |
tmp); |
| 774 |
host->ttl_factor = tmp; |
| 775 |
} else if (tmp > 0.0) { |
| 776 |
WARNING("write_riemann plugin: The configured " |
| 777 |
"TTLFactor is too small to be " |
| 778 |
"useful (%.1f). I'll use it " |
| 779 |
"since the user knows best, " |
| 780 |
"but under protest.", |
| 781 |
tmp); |
| 782 |
host->ttl_factor = tmp; |
| 783 |
} else { /* zero, negative and NAN */ |
| 784 |
ERROR("write_riemann plugin: The configured " |
| 785 |
"TTLFactor is invalid (%.1f).", |
| 786 |
tmp); |
| 787 |
} |
| 788 |
} else { |
| 789 |
WARNING("write_riemann plugin: ignoring unknown config " |
| 790 |
"option: \"%s\"", |
| 791 |
child->key); |
| 792 |
} |
| 793 |
} |
| 794 |
if (status != 0) { |
| 795 |
wrr_free(host); |
| 796 |
return status; |
| 797 |
} |
| 798 |
|
| 799 |
ssnprintf(callback_name, sizeof(callback_name), "write_riemann/%s", |
| 800 |
host->name); |
| 801 |
|
| 802 |
user_data_t ud = {.data = host, .free_func = wrr_free}; |
| 803 |
|
| 804 |
pthread_mutex_lock(&host->lock); |
| 805 |
|
| 806 |
status = plugin_register_write(callback_name, wrr_write, &ud); |
| 807 |
|
| 808 |
if (host->client_type != RIEMANN_CLIENT_UDP && host->batch_mode) { |
| 809 |
ud.free_func = NULL; |
| 810 |
plugin_register_flush(callback_name, wrr_batch_flush, &ud); |
| 811 |
} |
| 812 |
if (status != 0) |
| 813 |
WARNING("write_riemann plugin: plugin_register_write (\"%s\") " |
| 814 |
"failed with status %i.", |
| 815 |
callback_name, status); |
| 816 |
else /* success */ |
| 817 |
host->reference_count++; |
| 818 |
|
| 819 |
status = plugin_register_notification(callback_name, wrr_notification, &ud); |
| 820 |
if (status != 0) |
| 821 |
WARNING("write_riemann plugin: plugin_register_notification (\"%s\") " |
| 822 |
"failed with status %i.", |
| 823 |
callback_name, status); |
| 824 |
else /* success */ |
| 825 |
host->reference_count++; |
| 826 |
|
| 827 |
if (host->reference_count <= 1) { |
| 828 |
/* Both callbacks failed => free memory. |
| 829 |
* We need to unlock here, because riemann_free() will lock. |
| 830 |
* This is not a race condition, because we're the only one |
| 831 |
* holding a reference. */ |
| 832 |
pthread_mutex_unlock(&host->lock); |
| 833 |
wrr_free(host); |
| 834 |
return -1; |
| 835 |
} |
| 836 |
|
| 837 |
host->reference_count--; |
| 838 |
pthread_mutex_unlock(&host->lock); |
| 839 |
|
| 840 |
return status; |
| 841 |
} /* }}} int wrr_config_node */ |
| 842 |
|
| 843 |
static int wrr_config(oconfig_item_t *ci) /* {{{ */ |
| 844 |
{ |
| 845 |
int i; |
| 846 |
oconfig_item_t *child; |
| 847 |
int status; |
| 848 |
|
| 849 |
for (i = 0; i < ci->children_num; i++) { |
| 850 |
child = &ci->children[i]; |
| 851 |
|
| 852 |
if (strcasecmp("Node", child->key) == 0) { |
| 853 |
wrr_config_node(child); |
| 854 |
} else if (strcasecmp(child->key, "attribute") == 0) { |
| 855 |
char *key = NULL; |
| 856 |
char *val = NULL; |
| 857 |
|
| 858 |
if (child->values_num != 2) { |
| 859 |
WARNING("riemann attributes need both a key and a value."); |
| 860 |
return -1; |
| 861 |
} |
| 862 |
if (child->values[0].type != OCONFIG_TYPE_STRING || |
| 863 |
child->values[1].type != OCONFIG_TYPE_STRING) { |
| 864 |
WARNING("riemann attribute needs string arguments."); |
| 865 |
return -1; |
| 866 |
} |
| 867 |
if ((key = strdup(child->values[0].value.string)) == NULL) { |
| 868 |
WARNING("cannot allocate memory for attribute key."); |
| 869 |
return -1; |
| 870 |
} |
| 871 |
if ((val = strdup(child->values[1].value.string)) == NULL) { |
| 872 |
WARNING("cannot allocate memory for attribute value."); |
| 873 |
sfree(key); |
| 874 |
return -1; |
| 875 |
} |
| 876 |
strarray_add(&riemann_attrs, &riemann_attrs_num, key); |
| 877 |
strarray_add(&riemann_attrs, &riemann_attrs_num, val); |
| 878 |
DEBUG("write_riemann: got attr: %s => %s", key, val); |
| 879 |
sfree(key); |
| 880 |
sfree(val); |
| 881 |
} else if (strcasecmp(child->key, "tag") == 0) { |
| 882 |
char *tmp = NULL; |
| 883 |
status = cf_util_get_string(child, &tmp); |
| 884 |
if (status != 0) |
| 885 |
continue; |
| 886 |
|
| 887 |
strarray_add(&riemann_tags, &riemann_tags_num, tmp); |
| 888 |
DEBUG("write_riemann plugin: Got tag: %s", tmp); |
| 889 |
sfree(tmp); |
| 890 |
} else { |
| 891 |
WARNING("write_riemann plugin: Ignoring unknown " |
| 892 |
"configuration option \"%s\" at top level.", |
| 893 |
child->key); |
| 894 |
} |
| 895 |
} |
| 896 |
return 0; |
| 897 |
} /* }}} int wrr_config */ |
| 898 |
|
| 899 |
void module_register(void) { |
| 900 |
plugin_register_complex_config("write_riemann", wrr_config); |
| 901 |
} |