| 1 |
/** |
| 2 |
* collectd - src/write_http.c |
| 3 |
* Copyright (C) 2009 Paul Sadauskas |
| 4 |
* Copyright (C) 2009 Doug MacEachern |
| 5 |
* Copyright (C) 2007-2014 Florian octo Forster |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of the GNU General Public License as published by the |
| 9 |
* Free Software Foundation; only version 2 of the License is applicable. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License along |
| 17 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
* Authors: |
| 21 |
* Florian octo Forster <octo at collectd.org> |
| 22 |
* Doug MacEachern <[email protected]> |
| 23 |
* Paul Sadauskas <[email protected]> |
| 24 |
**/ |
| 25 |
|
| 26 |
#include "collectd.h" |
| 27 |
|
| 28 |
#include "plugin.h" |
| 29 |
#include "utils/common/common.h" |
| 30 |
#include "utils/curl_stats/curl_stats.h" |
| 31 |
#include "utils/format_json/format_json.h" |
| 32 |
#include "utils/format_kairosdb/format_kairosdb.h" |
| 33 |
|
| 34 |
#include <curl/curl.h> |
| 35 |
|
| 36 |
#ifndef WRITE_HTTP_DEFAULT_BUFFER_SIZE |
| 37 |
#define WRITE_HTTP_DEFAULT_BUFFER_SIZE 4096 |
| 38 |
#endif |
| 39 |
|
| 40 |
#ifndef WRITE_HTTP_DEFAULT_PREFIX |
| 41 |
#define WRITE_HTTP_DEFAULT_PREFIX "collectd" |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifndef WRITE_HTTP_RESPONSE_BUFFER_SIZE |
| 45 |
#define WRITE_HTTP_RESPONSE_BUFFER_SIZE 1024 |
| 46 |
#endif |
| 47 |
|
| 48 |
/* |
| 49 |
* Private variables |
| 50 |
*/ |
| 51 |
struct wh_callback_s { |
| 52 |
char *name; |
| 53 |
|
| 54 |
char *location; |
| 55 |
char *user; |
| 56 |
char *pass; |
| 57 |
char *credentials; |
| 58 |
bool verify_peer; |
| 59 |
bool verify_host; |
| 60 |
char *cacert; |
| 61 |
char *capath; |
| 62 |
char *clientkey; |
| 63 |
char *clientcert; |
| 64 |
char *clientkeypass; |
| 65 |
long sslversion; |
| 66 |
bool store_rates; |
| 67 |
bool log_http_error; |
| 68 |
int low_speed_limit; |
| 69 |
time_t low_speed_time; |
| 70 |
int timeout; |
| 71 |
|
| 72 |
#define WH_FORMAT_COMMAND 0 |
| 73 |
#define WH_FORMAT_JSON 1 |
| 74 |
#define WH_FORMAT_KAIROSDB 2 |
| 75 |
int format; |
| 76 |
bool send_metrics; |
| 77 |
bool send_notifications; |
| 78 |
|
| 79 |
CURL *curl; |
| 80 |
curl_stats_t *curl_stats; |
| 81 |
struct curl_slist *headers; |
| 82 |
char curl_errbuf[CURL_ERROR_SIZE]; |
| 83 |
|
| 84 |
char *send_buffer; |
| 85 |
size_t send_buffer_size; |
| 86 |
size_t send_buffer_free; |
| 87 |
size_t send_buffer_fill; |
| 88 |
cdtime_t send_buffer_init_time; |
| 89 |
|
| 90 |
pthread_mutex_t send_lock; |
| 91 |
|
| 92 |
char response_buffer[WRITE_HTTP_RESPONSE_BUFFER_SIZE]; |
| 93 |
unsigned int response_buffer_pos; |
| 94 |
|
| 95 |
int data_ttl; |
| 96 |
char *metrics_prefix; |
| 97 |
}; |
| 98 |
typedef struct wh_callback_s wh_callback_t; |
| 99 |
|
| 100 |
static char **http_attrs; |
| 101 |
static size_t http_attrs_num; |
| 102 |
|
| 103 |
/* libcurl may call this multiple times depending on how big the server's |
| 104 |
* http response is |
| 105 |
*/ |
| 106 |
static size_t wh_curl_write_callback(char *ptr, size_t size, size_t nmemb, |
| 107 |
void *userdata) { |
| 108 |
|
| 109 |
wh_callback_t *cb = (wh_callback_t *)userdata; |
| 110 |
unsigned int len = 0; |
| 111 |
|
| 112 |
if ((cb->response_buffer_pos + nmemb) > sizeof(cb->response_buffer)) |
| 113 |
len = sizeof(cb->response_buffer) - cb->response_buffer_pos; |
| 114 |
else |
| 115 |
len = nmemb; |
| 116 |
|
| 117 |
DEBUG( |
| 118 |
"write_http plugin: curl callback nmemb=%zu buffer_pos=%u write_len=%u ", |
| 119 |
nmemb, cb->response_buffer_pos, len); |
| 120 |
|
| 121 |
memcpy(cb->response_buffer + cb->response_buffer_pos, ptr, len); |
| 122 |
cb->response_buffer_pos += len; |
| 123 |
cb->response_buffer[sizeof(cb->response_buffer) - 1] = '\0'; |
| 124 |
|
| 125 |
/* Always return nmemb even if we write less so libcurl won't throw an error |
| 126 |
*/ |
| 127 |
return nmemb; |
| 128 |
|
| 129 |
} /* }}} wh_curl_write_callback */ |
| 130 |
|
| 131 |
static void wh_log_http_error(wh_callback_t *cb) { |
| 132 |
if (!cb->log_http_error) |
| 133 |
return; |
| 134 |
|
| 135 |
long http_code = 0; |
| 136 |
|
| 137 |
curl_easy_getinfo(cb->curl, CURLINFO_RESPONSE_CODE, &http_code); |
| 138 |
|
| 139 |
if (http_code != 200) |
| 140 |
INFO("write_http plugin: HTTP Error code: %lu", http_code); |
| 141 |
} |
| 142 |
|
| 143 |
static void wh_reset_buffer(wh_callback_t *cb) /* {{{ */ |
| 144 |
{ |
| 145 |
if ((cb == NULL) || (cb->send_buffer == NULL)) |
| 146 |
return; |
| 147 |
|
| 148 |
memset(cb->send_buffer, 0, cb->send_buffer_size); |
| 149 |
cb->send_buffer_free = cb->send_buffer_size; |
| 150 |
cb->send_buffer_fill = 0; |
| 151 |
cb->send_buffer_init_time = cdtime(); |
| 152 |
|
| 153 |
if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) { |
| 154 |
format_json_initialize(cb->send_buffer, &cb->send_buffer_fill, |
| 155 |
&cb->send_buffer_free); |
| 156 |
} |
| 157 |
|
| 158 |
memset(cb->response_buffer, 0, sizeof(cb->response_buffer)); |
| 159 |
cb->response_buffer_pos = 0; |
| 160 |
|
| 161 |
} /* }}} wh_reset_buffer */ |
| 162 |
|
| 163 |
/* must hold cb->send_lock when calling */ |
| 164 |
static int wh_post_nolock(wh_callback_t *cb, char const *data) /* {{{ */ |
| 165 |
{ |
| 166 |
int status = 0; |
| 167 |
|
| 168 |
curl_easy_setopt(cb->curl, CURLOPT_URL, cb->location); |
| 169 |
curl_easy_setopt(cb->curl, CURLOPT_POSTFIELDS, data); |
| 170 |
curl_easy_setopt(cb->curl, CURLOPT_WRITEFUNCTION, &wh_curl_write_callback); |
| 171 |
curl_easy_setopt(cb->curl, CURLOPT_WRITEDATA, (void *)cb); |
| 172 |
status = curl_easy_perform(cb->curl); |
| 173 |
|
| 174 |
wh_log_http_error(cb); |
| 175 |
|
| 176 |
if (cb->curl_stats != NULL) { |
| 177 |
int rc = curl_stats_dispatch(cb->curl_stats, cb->curl, NULL, "write_http", |
| 178 |
cb->name); |
| 179 |
if (rc != 0) { |
| 180 |
ERROR("write_http plugin: curl_stats_dispatch failed with " |
| 181 |
"status %i", |
| 182 |
rc); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (status != CURLE_OK) { |
| 187 |
ERROR("write_http plugin: curl_easy_perform failed with " |
| 188 |
"status %i: %s", |
| 189 |
status, cb->curl_errbuf); |
| 190 |
if (strlen(cb->response_buffer) > 0) { |
| 191 |
ERROR("write_http plugin: curl_response=%s", cb->response_buffer); |
| 192 |
} |
| 193 |
} else { |
| 194 |
DEBUG("write_http plugin: curl_response=%s", cb->response_buffer); |
| 195 |
} |
| 196 |
return status; |
| 197 |
} /* }}} wh_post_nolock */ |
| 198 |
|
| 199 |
static int wh_callback_init(wh_callback_t *cb) /* {{{ */ |
| 200 |
{ |
| 201 |
if (cb->curl != NULL) |
| 202 |
return 0; |
| 203 |
|
| 204 |
cb->curl = curl_easy_init(); |
| 205 |
if (cb->curl == NULL) { |
| 206 |
ERROR("curl plugin: curl_easy_init failed."); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
|
| 210 |
if (cb->low_speed_limit > 0 && cb->low_speed_time > 0) { |
| 211 |
curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_LIMIT, |
| 212 |
(long)(cb->low_speed_limit * cb->low_speed_time)); |
| 213 |
curl_easy_setopt(cb->curl, CURLOPT_LOW_SPEED_TIME, |
| 214 |
(long)cb->low_speed_time); |
| 215 |
} |
| 216 |
|
| 217 |
#ifdef HAVE_CURLOPT_TIMEOUT_MS |
| 218 |
if (cb->timeout > 0) |
| 219 |
curl_easy_setopt(cb->curl, CURLOPT_TIMEOUT_MS, (long)cb->timeout); |
| 220 |
#endif |
| 221 |
|
| 222 |
curl_easy_setopt(cb->curl, CURLOPT_NOSIGNAL, 1L); |
| 223 |
curl_easy_setopt(cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); |
| 224 |
|
| 225 |
cb->headers = curl_slist_append(cb->headers, "Accept: */*"); |
| 226 |
if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) |
| 227 |
cb->headers = |
| 228 |
curl_slist_append(cb->headers, "Content-Type: application/json"); |
| 229 |
else |
| 230 |
cb->headers = curl_slist_append(cb->headers, "Content-Type: text/plain"); |
| 231 |
cb->headers = curl_slist_append(cb->headers, "Expect:"); |
| 232 |
curl_easy_setopt(cb->curl, CURLOPT_HTTPHEADER, cb->headers); |
| 233 |
|
| 234 |
curl_easy_setopt(cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf); |
| 235 |
curl_easy_setopt(cb->curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 236 |
curl_easy_setopt(cb->curl, CURLOPT_MAXREDIRS, 50L); |
| 237 |
|
| 238 |
if (cb->user != NULL) { |
| 239 |
#ifdef HAVE_CURLOPT_USERNAME |
| 240 |
curl_easy_setopt(cb->curl, CURLOPT_USERNAME, cb->user); |
| 241 |
curl_easy_setopt(cb->curl, CURLOPT_PASSWORD, |
| 242 |
(cb->pass == NULL) ? "" : cb->pass); |
| 243 |
#else |
| 244 |
size_t credentials_size; |
| 245 |
|
| 246 |
credentials_size = strlen(cb->user) + 2; |
| 247 |
if (cb->pass != NULL) |
| 248 |
credentials_size += strlen(cb->pass); |
| 249 |
|
| 250 |
cb->credentials = malloc(credentials_size); |
| 251 |
if (cb->credentials == NULL) { |
| 252 |
ERROR("curl plugin: malloc failed."); |
| 253 |
return -1; |
| 254 |
} |
| 255 |
|
| 256 |
snprintf(cb->credentials, credentials_size, "%s:%s", cb->user, |
| 257 |
(cb->pass == NULL) ? "" : cb->pass); |
| 258 |
curl_easy_setopt(cb->curl, CURLOPT_USERPWD, cb->credentials); |
| 259 |
#endif |
| 260 |
curl_easy_setopt(cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
| 261 |
} |
| 262 |
|
| 263 |
curl_easy_setopt(cb->curl, CURLOPT_SSL_VERIFYPEER, (long)cb->verify_peer); |
| 264 |
curl_easy_setopt(cb->curl, CURLOPT_SSL_VERIFYHOST, cb->verify_host ? 2L : 0L); |
| 265 |
curl_easy_setopt(cb->curl, CURLOPT_SSLVERSION, cb->sslversion); |
| 266 |
if (cb->cacert != NULL) |
| 267 |
curl_easy_setopt(cb->curl, CURLOPT_CAINFO, cb->cacert); |
| 268 |
if (cb->capath != NULL) |
| 269 |
curl_easy_setopt(cb->curl, CURLOPT_CAPATH, cb->capath); |
| 270 |
|
| 271 |
if (cb->clientkey != NULL && cb->clientcert != NULL) { |
| 272 |
curl_easy_setopt(cb->curl, CURLOPT_SSLKEY, cb->clientkey); |
| 273 |
curl_easy_setopt(cb->curl, CURLOPT_SSLCERT, cb->clientcert); |
| 274 |
|
| 275 |
if (cb->clientkeypass != NULL) |
| 276 |
curl_easy_setopt(cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass); |
| 277 |
} |
| 278 |
|
| 279 |
wh_reset_buffer(cb); |
| 280 |
|
| 281 |
return 0; |
| 282 |
} /* }}} int wh_callback_init */ |
| 283 |
|
| 284 |
static int wh_flush_nolock(cdtime_t timeout, wh_callback_t *cb) /* {{{ */ |
| 285 |
{ |
| 286 |
int status; |
| 287 |
|
| 288 |
DEBUG("write_http plugin: wh_flush_nolock: timeout = %.3f; " |
| 289 |
"send_buffer_fill = %" PRIsz ";", |
| 290 |
CDTIME_T_TO_DOUBLE(timeout), cb->send_buffer_fill); |
| 291 |
|
| 292 |
/* timeout == 0 => flush unconditionally */ |
| 293 |
if (timeout > 0) { |
| 294 |
cdtime_t now; |
| 295 |
|
| 296 |
now = cdtime(); |
| 297 |
if ((cb->send_buffer_init_time + timeout) > now) |
| 298 |
return 0; |
| 299 |
} |
| 300 |
|
| 301 |
if (cb->format == WH_FORMAT_COMMAND) { |
| 302 |
if (cb->send_buffer_fill == 0) { |
| 303 |
cb->send_buffer_init_time = cdtime(); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
status = wh_post_nolock(cb, cb->send_buffer); |
| 308 |
wh_reset_buffer(cb); |
| 309 |
} else if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) { |
| 310 |
if (cb->send_buffer_fill <= 2) { |
| 311 |
cb->send_buffer_init_time = cdtime(); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
status = format_json_finalize(cb->send_buffer, &cb->send_buffer_fill, |
| 316 |
&cb->send_buffer_free); |
| 317 |
if (status != 0) { |
| 318 |
ERROR("write_http: wh_flush_nolock: " |
| 319 |
"format_json_finalize failed."); |
| 320 |
wh_reset_buffer(cb); |
| 321 |
return status; |
| 322 |
} |
| 323 |
|
| 324 |
status = wh_post_nolock(cb, cb->send_buffer); |
| 325 |
wh_reset_buffer(cb); |
| 326 |
} else { |
| 327 |
ERROR("write_http: wh_flush_nolock: " |
| 328 |
"Unknown format: %i", |
| 329 |
cb->format); |
| 330 |
return -1; |
| 331 |
} |
| 332 |
|
| 333 |
return status; |
| 334 |
} /* }}} wh_flush_nolock */ |
| 335 |
|
| 336 |
static int wh_flush(cdtime_t timeout, /* {{{ */ |
| 337 |
const char *identifier __attribute__((unused)), |
| 338 |
user_data_t *user_data) { |
| 339 |
wh_callback_t *cb; |
| 340 |
int status; |
| 341 |
|
| 342 |
if (user_data == NULL) |
| 343 |
return -EINVAL; |
| 344 |
|
| 345 |
cb = user_data->data; |
| 346 |
|
| 347 |
pthread_mutex_lock(&cb->send_lock); |
| 348 |
|
| 349 |
if (wh_callback_init(cb) != 0) { |
| 350 |
ERROR("write_http plugin: wh_callback_init failed."); |
| 351 |
pthread_mutex_unlock(&cb->send_lock); |
| 352 |
return -1; |
| 353 |
} |
| 354 |
|
| 355 |
status = wh_flush_nolock(timeout, cb); |
| 356 |
pthread_mutex_unlock(&cb->send_lock); |
| 357 |
|
| 358 |
return status; |
| 359 |
} /* }}} int wh_flush */ |
| 360 |
|
| 361 |
static void wh_callback_free(void *data) /* {{{ */ |
| 362 |
{ |
| 363 |
wh_callback_t *cb; |
| 364 |
|
| 365 |
if (data == NULL) |
| 366 |
return; |
| 367 |
|
| 368 |
cb = data; |
| 369 |
|
| 370 |
if (cb->send_buffer != NULL) |
| 371 |
wh_flush_nolock(/* timeout = */ 0, cb); |
| 372 |
|
| 373 |
if (cb->curl != NULL) { |
| 374 |
curl_easy_cleanup(cb->curl); |
| 375 |
cb->curl = NULL; |
| 376 |
} |
| 377 |
|
| 378 |
curl_stats_destroy(cb->curl_stats); |
| 379 |
cb->curl_stats = NULL; |
| 380 |
|
| 381 |
if (cb->headers != NULL) { |
| 382 |
curl_slist_free_all(cb->headers); |
| 383 |
cb->headers = NULL; |
| 384 |
} |
| 385 |
|
| 386 |
sfree(cb->name); |
| 387 |
sfree(cb->location); |
| 388 |
sfree(cb->user); |
| 389 |
sfree(cb->pass); |
| 390 |
sfree(cb->credentials); |
| 391 |
sfree(cb->cacert); |
| 392 |
sfree(cb->capath); |
| 393 |
sfree(cb->clientkey); |
| 394 |
sfree(cb->clientcert); |
| 395 |
sfree(cb->clientkeypass); |
| 396 |
sfree(cb->send_buffer); |
| 397 |
sfree(cb->metrics_prefix); |
| 398 |
|
| 399 |
sfree(cb); |
| 400 |
} /* }}} void wh_callback_free */ |
| 401 |
|
| 402 |
static int wh_write_command(const data_set_t *ds, |
| 403 |
const value_list_t *vl, /* {{{ */ |
| 404 |
wh_callback_t *cb) { |
| 405 |
char key[10 * DATA_MAX_NAME_LEN]; |
| 406 |
char values[512]; |
| 407 |
char command[1024]; |
| 408 |
size_t command_len; |
| 409 |
|
| 410 |
int status; |
| 411 |
|
| 412 |
/* sanity checks, primarily to make static analyzers happy. */ |
| 413 |
if ((cb == NULL) || (cb->send_buffer == NULL)) |
| 414 |
return -1; |
| 415 |
|
| 416 |
if (strcmp(ds->type, vl->type) != 0) { |
| 417 |
ERROR("write_http plugin: DS type does not match " |
| 418 |
"value list type"); |
| 419 |
return -1; |
| 420 |
} |
| 421 |
|
| 422 |
/* Copy the identifier to `key' and escape it. */ |
| 423 |
status = FORMAT_VL(key, sizeof(key), vl); |
| 424 |
if (status != 0) { |
| 425 |
ERROR("write_http plugin: error with format_name"); |
| 426 |
return status; |
| 427 |
} |
| 428 |
escape_string(key, sizeof(key)); |
| 429 |
|
| 430 |
/* Convert the values to an ASCII representation and put that into |
| 431 |
* `values'. */ |
| 432 |
status = format_values(values, sizeof(values), ds, vl, cb->store_rates); |
| 433 |
if (status != 0) { |
| 434 |
ERROR("write_http plugin: error with " |
| 435 |
"wh_value_list_to_string"); |
| 436 |
return status; |
| 437 |
} |
| 438 |
|
| 439 |
command_len = (size_t)snprintf(command, sizeof(command), |
| 440 |
"PUTVAL %s interval=%.3f %s\r\n", key, |
| 441 |
CDTIME_T_TO_DOUBLE(vl->interval), values); |
| 442 |
if (command_len >= sizeof(command)) { |
| 443 |
ERROR("write_http plugin: Command buffer too small: " |
| 444 |
"Need %" PRIsz " bytes.", |
| 445 |
command_len + 1); |
| 446 |
return -1; |
| 447 |
} |
| 448 |
|
| 449 |
pthread_mutex_lock(&cb->send_lock); |
| 450 |
if (wh_callback_init(cb) != 0) { |
| 451 |
ERROR("write_http plugin: wh_callback_init failed."); |
| 452 |
pthread_mutex_unlock(&cb->send_lock); |
| 453 |
return -1; |
| 454 |
} |
| 455 |
|
| 456 |
if (command_len >= cb->send_buffer_free) { |
| 457 |
status = wh_flush_nolock(/* timeout = */ 0, cb); |
| 458 |
if (status != 0) { |
| 459 |
pthread_mutex_unlock(&cb->send_lock); |
| 460 |
return status; |
| 461 |
} |
| 462 |
} |
| 463 |
assert(command_len < cb->send_buffer_free); |
| 464 |
|
| 465 |
/* Make scan-build happy. */ |
| 466 |
assert(cb->send_buffer != NULL); |
| 467 |
|
| 468 |
/* `command_len + 1' because `command_len' does not include the |
| 469 |
* trailing null byte. Neither does `send_buffer_fill'. */ |
| 470 |
memcpy(cb->send_buffer + cb->send_buffer_fill, command, command_len + 1); |
| 471 |
cb->send_buffer_fill += command_len; |
| 472 |
cb->send_buffer_free -= command_len; |
| 473 |
|
| 474 |
DEBUG("write_http plugin: <%s> buffer %" PRIsz "/%" PRIsz " (%g%%) \"%s\"", |
| 475 |
cb->location, cb->send_buffer_fill, cb->send_buffer_size, |
| 476 |
100.0 * ((double)cb->send_buffer_fill) / ((double)cb->send_buffer_size), |
| 477 |
command); |
| 478 |
|
| 479 |
/* Check if we have enough space for this command. */ |
| 480 |
pthread_mutex_unlock(&cb->send_lock); |
| 481 |
|
| 482 |
return 0; |
| 483 |
} /* }}} int wh_write_command */ |
| 484 |
|
| 485 |
static int wh_write_json(const data_set_t *ds, const value_list_t *vl, /* {{{ */ |
| 486 |
wh_callback_t *cb) { |
| 487 |
int status; |
| 488 |
|
| 489 |
pthread_mutex_lock(&cb->send_lock); |
| 490 |
if (wh_callback_init(cb) != 0) { |
| 491 |
ERROR("write_http plugin: wh_callback_init failed."); |
| 492 |
pthread_mutex_unlock(&cb->send_lock); |
| 493 |
return -1; |
| 494 |
} |
| 495 |
|
| 496 |
status = |
| 497 |
format_json_value_list(cb->send_buffer, &cb->send_buffer_fill, |
| 498 |
&cb->send_buffer_free, ds, vl, cb->store_rates); |
| 499 |
if (status == -ENOMEM) { |
| 500 |
status = wh_flush_nolock(/* timeout = */ 0, cb); |
| 501 |
if (status != 0) { |
| 502 |
wh_reset_buffer(cb); |
| 503 |
pthread_mutex_unlock(&cb->send_lock); |
| 504 |
return status; |
| 505 |
} |
| 506 |
|
| 507 |
status = |
| 508 |
format_json_value_list(cb->send_buffer, &cb->send_buffer_fill, |
| 509 |
&cb->send_buffer_free, ds, vl, cb->store_rates); |
| 510 |
} |
| 511 |
if (status != 0) { |
| 512 |
pthread_mutex_unlock(&cb->send_lock); |
| 513 |
return status; |
| 514 |
} |
| 515 |
|
| 516 |
DEBUG("write_http plugin: <%s> buffer %" PRIsz "/%" PRIsz " (%g%%)", |
| 517 |
cb->location, cb->send_buffer_fill, cb->send_buffer_size, |
| 518 |
100.0 * ((double)cb->send_buffer_fill) / |
| 519 |
((double)cb->send_buffer_size)); |
| 520 |
|
| 521 |
/* Check if we have enough space for this command. */ |
| 522 |
pthread_mutex_unlock(&cb->send_lock); |
| 523 |
|
| 524 |
return 0; |
| 525 |
} /* }}} int wh_write_json */ |
| 526 |
|
| 527 |
static int wh_write_kairosdb(const data_set_t *ds, |
| 528 |
const value_list_t *vl, /* {{{ */ |
| 529 |
wh_callback_t *cb) { |
| 530 |
int status; |
| 531 |
|
| 532 |
pthread_mutex_lock(&cb->send_lock); |
| 533 |
|
| 534 |
if (cb->curl == NULL) { |
| 535 |
status = wh_callback_init(cb); |
| 536 |
if (status != 0) { |
| 537 |
ERROR("write_http plugin: wh_callback_init failed."); |
| 538 |
pthread_mutex_unlock(&cb->send_lock); |
| 539 |
return -1; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
status = format_kairosdb_value_list( |
| 544 |
cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, ds, vl, |
| 545 |
cb->store_rates, (char const *const *)http_attrs, http_attrs_num, |
| 546 |
cb->data_ttl, cb->metrics_prefix); |
| 547 |
if (status == -ENOMEM) { |
| 548 |
status = wh_flush_nolock(/* timeout = */ 0, cb); |
| 549 |
if (status != 0) { |
| 550 |
wh_reset_buffer(cb); |
| 551 |
pthread_mutex_unlock(&cb->send_lock); |
| 552 |
return status; |
| 553 |
} |
| 554 |
|
| 555 |
status = format_kairosdb_value_list( |
| 556 |
cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, ds, vl, |
| 557 |
cb->store_rates, (char const *const *)http_attrs, http_attrs_num, |
| 558 |
cb->data_ttl, cb->metrics_prefix); |
| 559 |
} |
| 560 |
if (status != 0) { |
| 561 |
pthread_mutex_unlock(&cb->send_lock); |
| 562 |
return status; |
| 563 |
} |
| 564 |
|
| 565 |
DEBUG("write_http plugin: <%s> buffer %" PRIsz "/%" PRIsz " (%g%%)", |
| 566 |
cb->location, cb->send_buffer_fill, cb->send_buffer_size, |
| 567 |
100.0 * ((double)cb->send_buffer_fill) / |
| 568 |
((double)cb->send_buffer_size)); |
| 569 |
|
| 570 |
/* Check if we have enough space for this command. */ |
| 571 |
pthread_mutex_unlock(&cb->send_lock); |
| 572 |
|
| 573 |
return 0; |
| 574 |
} /* }}} int wh_write_kairosdb */ |
| 575 |
|
| 576 |
static int wh_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */ |
| 577 |
user_data_t *user_data) { |
| 578 |
wh_callback_t *cb; |
| 579 |
int status; |
| 580 |
|
| 581 |
if (user_data == NULL) |
| 582 |
return -EINVAL; |
| 583 |
|
| 584 |
cb = user_data->data; |
| 585 |
assert(cb->send_metrics); |
| 586 |
|
| 587 |
switch (cb->format) { |
| 588 |
case WH_FORMAT_JSON: |
| 589 |
status = wh_write_json(ds, vl, cb); |
| 590 |
break; |
| 591 |
case WH_FORMAT_KAIROSDB: |
| 592 |
status = wh_write_kairosdb(ds, vl, cb); |
| 593 |
break; |
| 594 |
default: |
| 595 |
status = wh_write_command(ds, vl, cb); |
| 596 |
break; |
| 597 |
} |
| 598 |
return status; |
| 599 |
} /* }}} int wh_write */ |
| 600 |
|
| 601 |
static int wh_notify(notification_t const *n, user_data_t *ud) /* {{{ */ |
| 602 |
{ |
| 603 |
wh_callback_t *cb; |
| 604 |
char alert[4096]; |
| 605 |
int status; |
| 606 |
|
| 607 |
if ((ud == NULL) || (ud->data == NULL)) |
| 608 |
return EINVAL; |
| 609 |
|
| 610 |
cb = ud->data; |
| 611 |
assert(cb->send_notifications); |
| 612 |
|
| 613 |
status = format_json_notification(alert, sizeof(alert), n); |
| 614 |
if (status != 0) { |
| 615 |
ERROR("write_http plugin: formatting notification failed"); |
| 616 |
return status; |
| 617 |
} |
| 618 |
|
| 619 |
pthread_mutex_lock(&cb->send_lock); |
| 620 |
if (wh_callback_init(cb) != 0) { |
| 621 |
ERROR("write_http plugin: wh_callback_init failed."); |
| 622 |
pthread_mutex_unlock(&cb->send_lock); |
| 623 |
return -1; |
| 624 |
} |
| 625 |
|
| 626 |
status = wh_post_nolock(cb, alert); |
| 627 |
pthread_mutex_unlock(&cb->send_lock); |
| 628 |
|
| 629 |
return status; |
| 630 |
} /* }}} int wh_notify */ |
| 631 |
|
| 632 |
static int config_set_format(wh_callback_t *cb, /* {{{ */ |
| 633 |
oconfig_item_t *ci) { |
| 634 |
char *string; |
| 635 |
|
| 636 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 637 |
WARNING("write_http plugin: The `%s' config option " |
| 638 |
"needs exactly one string argument.", |
| 639 |
ci->key); |
| 640 |
return -1; |
| 641 |
} |
| 642 |
|
| 643 |
string = ci->values[0].value.string; |
| 644 |
if (strcasecmp("Command", string) == 0) |
| 645 |
cb->format = WH_FORMAT_COMMAND; |
| 646 |
else if (strcasecmp("JSON", string) == 0) |
| 647 |
cb->format = WH_FORMAT_JSON; |
| 648 |
else if (strcasecmp("KAIROSDB", string) == 0) |
| 649 |
cb->format = WH_FORMAT_KAIROSDB; |
| 650 |
else { |
| 651 |
ERROR("write_http plugin: Invalid format string: %s", string); |
| 652 |
return -1; |
| 653 |
} |
| 654 |
|
| 655 |
return 0; |
| 656 |
} /* }}} int config_set_format */ |
| 657 |
|
| 658 |
static int wh_config_append_string(const char *name, |
| 659 |
struct curl_slist **dest, /* {{{ */ |
| 660 |
oconfig_item_t *ci) { |
| 661 |
struct curl_slist *temp = NULL; |
| 662 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 663 |
WARNING("write_http plugin: `%s' needs exactly one string argument.", name); |
| 664 |
return -1; |
| 665 |
} |
| 666 |
|
| 667 |
temp = curl_slist_append(*dest, ci->values[0].value.string); |
| 668 |
if (temp == NULL) |
| 669 |
return -1; |
| 670 |
|
| 671 |
*dest = temp; |
| 672 |
|
| 673 |
return 0; |
| 674 |
} /* }}} int wh_config_append_string */ |
| 675 |
|
| 676 |
static int wh_config_node(oconfig_item_t *ci) /* {{{ */ |
| 677 |
{ |
| 678 |
wh_callback_t *cb; |
| 679 |
int buffer_size = 0; |
| 680 |
char callback_name[DATA_MAX_NAME_LEN]; |
| 681 |
int status = 0; |
| 682 |
|
| 683 |
cb = calloc(1, sizeof(*cb)); |
| 684 |
if (cb == NULL) { |
| 685 |
ERROR("write_http plugin: calloc failed."); |
| 686 |
return -1; |
| 687 |
} |
| 688 |
cb->verify_peer = true; |
| 689 |
cb->verify_host = true; |
| 690 |
cb->format = WH_FORMAT_COMMAND; |
| 691 |
cb->sslversion = CURL_SSLVERSION_DEFAULT; |
| 692 |
cb->low_speed_limit = 0; |
| 693 |
cb->timeout = 0; |
| 694 |
cb->log_http_error = false; |
| 695 |
cb->headers = NULL; |
| 696 |
cb->send_metrics = true; |
| 697 |
cb->send_notifications = false; |
| 698 |
cb->data_ttl = 0; |
| 699 |
cb->metrics_prefix = strdup(WRITE_HTTP_DEFAULT_PREFIX); |
| 700 |
cb->curl_stats = NULL; |
| 701 |
|
| 702 |
if (cb->metrics_prefix == NULL) { |
| 703 |
ERROR("write_http plugin: strdup failed."); |
| 704 |
sfree(cb); |
| 705 |
return -1; |
| 706 |
} |
| 707 |
|
| 708 |
pthread_mutex_init(&cb->send_lock, /* attr = */ NULL); |
| 709 |
|
| 710 |
cf_util_get_string(ci, &cb->name); |
| 711 |
|
| 712 |
/* FIXME: Remove this legacy mode in version 6. */ |
| 713 |
if (strcasecmp("URL", ci->key) == 0) |
| 714 |
cf_util_get_string(ci, &cb->location); |
| 715 |
|
| 716 |
for (int i = 0; i < ci->children_num; i++) { |
| 717 |
oconfig_item_t *child = ci->children + i; |
| 718 |
|
| 719 |
if (strcasecmp("URL", child->key) == 0) |
| 720 |
status = cf_util_get_string(child, &cb->location); |
| 721 |
else if (strcasecmp("User", child->key) == 0) |
| 722 |
status = cf_util_get_string(child, &cb->user); |
| 723 |
else if (strcasecmp("Password", child->key) == 0) |
| 724 |
status = cf_util_get_string(child, &cb->pass); |
| 725 |
else if (strcasecmp("VerifyPeer", child->key) == 0) |
| 726 |
status = cf_util_get_boolean(child, &cb->verify_peer); |
| 727 |
else if (strcasecmp("VerifyHost", child->key) == 0) |
| 728 |
status = cf_util_get_boolean(child, &cb->verify_host); |
| 729 |
else if (strcasecmp("CACert", child->key) == 0) |
| 730 |
status = cf_util_get_string(child, &cb->cacert); |
| 731 |
else if (strcasecmp("CAPath", child->key) == 0) |
| 732 |
status = cf_util_get_string(child, &cb->capath); |
| 733 |
else if (strcasecmp("ClientKey", child->key) == 0) |
| 734 |
status = cf_util_get_string(child, &cb->clientkey); |
| 735 |
else if (strcasecmp("ClientCert", child->key) == 0) |
| 736 |
status = cf_util_get_string(child, &cb->clientcert); |
| 737 |
else if (strcasecmp("ClientKeyPass", child->key) == 0) |
| 738 |
status = cf_util_get_string(child, &cb->clientkeypass); |
| 739 |
else if (strcasecmp("SSLVersion", child->key) == 0) { |
| 740 |
char *value = NULL; |
| 741 |
|
| 742 |
status = cf_util_get_string(child, &value); |
| 743 |
if (status != 0) |
| 744 |
break; |
| 745 |
|
| 746 |
if (value == NULL || strcasecmp("default", value) == 0) |
| 747 |
cb->sslversion = CURL_SSLVERSION_DEFAULT; |
| 748 |
else if (strcasecmp("SSLv2", value) == 0) |
| 749 |
cb->sslversion = CURL_SSLVERSION_SSLv2; |
| 750 |
else if (strcasecmp("SSLv3", value) == 0) |
| 751 |
cb->sslversion = CURL_SSLVERSION_SSLv3; |
| 752 |
else if (strcasecmp("TLSv1", value) == 0) |
| 753 |
cb->sslversion = CURL_SSLVERSION_TLSv1; |
| 754 |
#if (LIBCURL_VERSION_MAJOR > 7) || \ |
| 755 |
(LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34) |
| 756 |
else if (strcasecmp("TLSv1_0", value) == 0) |
| 757 |
cb->sslversion = CURL_SSLVERSION_TLSv1_0; |
| 758 |
else if (strcasecmp("TLSv1_1", value) == 0) |
| 759 |
cb->sslversion = CURL_SSLVERSION_TLSv1_1; |
| 760 |
else if (strcasecmp("TLSv1_2", value) == 0) |
| 761 |
cb->sslversion = CURL_SSLVERSION_TLSv1_2; |
| 762 |
#endif |
| 763 |
else { |
| 764 |
ERROR("write_http plugin: Invalid SSLVersion " |
| 765 |
"option: %s.", |
| 766 |
value); |
| 767 |
status = EINVAL; |
| 768 |
} |
| 769 |
|
| 770 |
sfree(value); |
| 771 |
} else if (strcasecmp("Format", child->key) == 0) |
| 772 |
status = config_set_format(cb, child); |
| 773 |
else if (strcasecmp("Metrics", child->key) == 0) |
| 774 |
cf_util_get_boolean(child, &cb->send_metrics); |
| 775 |
else if (strcasecmp("Statistics", child->key) == 0) { |
| 776 |
cb->curl_stats = curl_stats_from_config(child); |
| 777 |
if (cb->curl_stats == NULL) |
| 778 |
status = -1; |
| 779 |
} else if (strcasecmp("Notifications", child->key) == 0) |
| 780 |
cf_util_get_boolean(child, &cb->send_notifications); |
| 781 |
else if (strcasecmp("StoreRates", child->key) == 0) |
| 782 |
status = cf_util_get_boolean(child, &cb->store_rates); |
| 783 |
else if (strcasecmp("BufferSize", child->key) == 0) |
| 784 |
status = cf_util_get_int(child, &buffer_size); |
| 785 |
else if (strcasecmp("LowSpeedLimit", child->key) == 0) |
| 786 |
status = cf_util_get_int(child, &cb->low_speed_limit); |
| 787 |
else if (strcasecmp("Timeout", child->key) == 0) |
| 788 |
status = cf_util_get_int(child, &cb->timeout); |
| 789 |
else if (strcasecmp("LogHttpError", child->key) == 0) |
| 790 |
status = cf_util_get_boolean(child, &cb->log_http_error); |
| 791 |
else if (strcasecmp("Header", child->key) == 0) |
| 792 |
status = wh_config_append_string("Header", &cb->headers, child); |
| 793 |
else if (strcasecmp("Attribute", child->key) == 0) { |
| 794 |
char *key = NULL; |
| 795 |
char *val = NULL; |
| 796 |
|
| 797 |
if (child->values_num != 2) { |
| 798 |
WARNING("write_http plugin: Attribute need both a key and a value."); |
| 799 |
break; |
| 800 |
} |
| 801 |
if (child->values[0].type != OCONFIG_TYPE_STRING || |
| 802 |
child->values[1].type != OCONFIG_TYPE_STRING) { |
| 803 |
WARNING("write_http plugin: Attribute needs string arguments."); |
| 804 |
break; |
| 805 |
} |
| 806 |
if ((key = strdup(child->values[0].value.string)) == NULL) { |
| 807 |
WARNING("cannot allocate memory for attribute key."); |
| 808 |
break; |
| 809 |
} |
| 810 |
if ((val = strdup(child->values[1].value.string)) == NULL) { |
| 811 |
WARNING("cannot allocate memory for attribute value."); |
| 812 |
sfree(key); |
| 813 |
break; |
| 814 |
} |
| 815 |
strarray_add(&http_attrs, &http_attrs_num, key); |
| 816 |
strarray_add(&http_attrs, &http_attrs_num, val); |
| 817 |
DEBUG("write_http plugin: got attribute: %s => %s", key, val); |
| 818 |
sfree(key); |
| 819 |
sfree(val); |
| 820 |
} else if (strcasecmp("TTL", child->key) == 0) { |
| 821 |
status = cf_util_get_int(child, &cb->data_ttl); |
| 822 |
} else if (strcasecmp("Prefix", child->key) == 0) { |
| 823 |
status = cf_util_get_string(child, &cb->metrics_prefix); |
| 824 |
} else { |
| 825 |
ERROR("write_http plugin: Invalid configuration " |
| 826 |
"option: %s.", |
| 827 |
child->key); |
| 828 |
status = EINVAL; |
| 829 |
} |
| 830 |
|
| 831 |
if (status != 0) |
| 832 |
break; |
| 833 |
} |
| 834 |
|
| 835 |
if (status != 0) { |
| 836 |
wh_callback_free(cb); |
| 837 |
return status; |
| 838 |
} |
| 839 |
|
| 840 |
if (cb->location == NULL) { |
| 841 |
ERROR("write_http plugin: no URL defined for instance '%s'", cb->name); |
| 842 |
wh_callback_free(cb); |
| 843 |
return -1; |
| 844 |
} |
| 845 |
|
| 846 |
if (!cb->send_metrics && !cb->send_notifications) { |
| 847 |
ERROR("write_http plugin: Neither metrics nor notifications " |
| 848 |
"are enabled for \"%s\".", |
| 849 |
cb->name); |
| 850 |
wh_callback_free(cb); |
| 851 |
return -1; |
| 852 |
} |
| 853 |
|
| 854 |
if (strlen(cb->metrics_prefix) == 0) |
| 855 |
sfree(cb->metrics_prefix); |
| 856 |
|
| 857 |
if (cb->low_speed_limit > 0) |
| 858 |
cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval()); |
| 859 |
|
| 860 |
/* Determine send_buffer_size. */ |
| 861 |
cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE; |
| 862 |
if (buffer_size >= 1024) |
| 863 |
cb->send_buffer_size = (size_t)buffer_size; |
| 864 |
else if (buffer_size != 0) |
| 865 |
ERROR("write_http plugin: Ignoring invalid BufferSize setting (%d).", |
| 866 |
buffer_size); |
| 867 |
|
| 868 |
/* Allocate the buffer. */ |
| 869 |
cb->send_buffer = malloc(cb->send_buffer_size); |
| 870 |
if (cb->send_buffer == NULL) { |
| 871 |
ERROR("write_http plugin: malloc(%" PRIsz ") failed.", |
| 872 |
cb->send_buffer_size); |
| 873 |
wh_callback_free(cb); |
| 874 |
return -1; |
| 875 |
} |
| 876 |
|
| 877 |
/* Nulls the buffer and sets ..._free and ..._fill. */ |
| 878 |
wh_reset_buffer(cb); |
| 879 |
|
| 880 |
snprintf(callback_name, sizeof(callback_name), "write_http/%s", cb->name); |
| 881 |
DEBUG("write_http: Registering write callback '%s' with URL '%s'", |
| 882 |
callback_name, cb->location); |
| 883 |
|
| 884 |
user_data_t user_data = { |
| 885 |
.data = cb, |
| 886 |
.free_func = wh_callback_free, |
| 887 |
}; |
| 888 |
|
| 889 |
if (cb->send_metrics) { |
| 890 |
plugin_register_write(callback_name, wh_write, &user_data); |
| 891 |
user_data.free_func = NULL; |
| 892 |
|
| 893 |
plugin_register_flush(callback_name, wh_flush, &user_data); |
| 894 |
} |
| 895 |
|
| 896 |
if (cb->send_notifications) { |
| 897 |
plugin_register_notification(callback_name, wh_notify, &user_data); |
| 898 |
user_data.free_func = NULL; |
| 899 |
} |
| 900 |
|
| 901 |
return 0; |
| 902 |
} /* }}} int wh_config_node */ |
| 903 |
|
| 904 |
static int wh_config(oconfig_item_t *ci) /* {{{ */ |
| 905 |
{ |
| 906 |
for (int i = 0; i < ci->children_num; i++) { |
| 907 |
oconfig_item_t *child = ci->children + i; |
| 908 |
|
| 909 |
if (strcasecmp("Node", child->key) == 0) |
| 910 |
wh_config_node(child); |
| 911 |
/* FIXME: Remove this legacy mode in version 6. */ |
| 912 |
else if (strcasecmp("URL", child->key) == 0) { |
| 913 |
WARNING("write_http plugin: Legacy <URL> block found. " |
| 914 |
"Please use <Node> instead."); |
| 915 |
wh_config_node(child); |
| 916 |
} else { |
| 917 |
ERROR("write_http plugin: Invalid configuration " |
| 918 |
"option: %s.", |
| 919 |
child->key); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
return 0; |
| 924 |
} /* }}} int wh_config */ |
| 925 |
|
| 926 |
static int wh_init(void) /* {{{ */ |
| 927 |
{ |
| 928 |
/* Call this while collectd is still single-threaded to avoid |
| 929 |
* initialization issues in libgcrypt. */ |
| 930 |
curl_global_init(CURL_GLOBAL_SSL); |
| 931 |
return 0; |
| 932 |
} /* }}} int wh_init */ |
| 933 |
|
| 934 |
void module_register(void) /* {{{ */ |
| 935 |
{ |
| 936 |
plugin_register_complex_config("write_http", wh_config); |
| 937 |
plugin_register_init("write_http", wh_init); |
| 938 |
} /* }}} void module_register */ |