1 /**
2 * collectd - src/curl_json.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2013 Florian octo Forster
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Doug MacEachern <dougm at hyperic.com>
21 * Florian octo Forster <octo at collectd.org>
22 **/
24 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "utils_avltree.h"
29 #include "utils_complain.h"
30 #include "utils_curl_stats.h"
32 #include <sys/types.h>
33 #include <sys/un.h>
35 #include <curl/curl.h>
37 #include <yajl/yajl_parse.h>
38 #if HAVE_YAJL_YAJL_VERSION_H
39 #include <yajl/yajl_version.h>
40 #endif
42 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
43 #define HAVE_YAJL_V2 1
44 #endif
46 #define CJ_DEFAULT_HOST "localhost"
47 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
48 #define CJ_IS_KEY(key) ((key)->magic == CJ_KEY_MAGIC)
49 #define CJ_ANY "*"
50 #define COUCH_MIN(x, y) ((x) < (y) ? (x) : (y))
52 struct cj_key_s;
53 typedef struct cj_key_s cj_key_t;
54 struct cj_key_s /* {{{ */
55 {
56 unsigned long magic;
57 char *path;
58 char *type;
59 char *instance;
60 };
61 /* }}} */
63 struct cj_s /* {{{ */
64 {
65 char *instance;
66 char *host;
68 char *sock;
70 char *url;
71 char *user;
72 char *pass;
73 char *credentials;
74 _Bool digest;
75 _Bool verify_peer;
76 _Bool verify_host;
77 char *cacert;
78 struct curl_slist *headers;
79 char *post_body;
80 cdtime_t interval;
81 int timeout;
82 curl_stats_t *stats;
84 CURL *curl;
85 char curl_errbuf[CURL_ERROR_SIZE];
87 yajl_handle yajl;
88 c_avl_tree_t *tree;
89 cj_key_t *key;
90 int depth;
91 struct {
92 union {
93 c_avl_tree_t *tree;
94 cj_key_t *key;
95 };
96 _Bool in_array;
97 int index;
98 char name[DATA_MAX_NAME_LEN];
99 } state[YAJL_MAX_DEPTH];
100 };
101 typedef struct cj_s cj_t; /* }}} */
103 #if HAVE_YAJL_V2
104 typedef size_t yajl_len_t;
105 #else
106 typedef unsigned int yajl_len_t;
107 #endif
109 static int cj_read(user_data_t *ud);
110 static void cj_submit(cj_t *db, cj_key_t *key, value_t *value);
112 static size_t cj_curl_callback(void *buf, /* {{{ */
113 size_t size, size_t nmemb, void *user_data) {
114 cj_t *db;
115 size_t len;
116 yajl_status status;
118 len = size * nmemb;
120 if (len == 0)
121 return (len);
123 db = user_data;
124 if (db == NULL)
125 return (0);
127 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
128 if (status == yajl_status_ok)
129 return (len);
130 #if !HAVE_YAJL_V2
131 else if (status == yajl_status_insufficient_data)
132 return (len);
133 #endif
135 unsigned char *msg =
136 yajl_get_error(db->yajl, /* verbose = */ 1,
137 /* jsonText = */ (unsigned char *)buf, (unsigned int)len);
138 ERROR("curl_json plugin: yajl_parse failed: %s", msg);
139 yajl_free_error(db->yajl, msg);
140 return (0); /* abort write callback */
141 } /* }}} size_t cj_curl_callback */
143 static int cj_get_type(cj_key_t *key) {
144 const data_set_t *ds;
146 if ((key == NULL) || !CJ_IS_KEY(key))
147 return -EINVAL;
149 ds = plugin_get_ds(key->type);
150 if (ds == NULL) {
151 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
153 assert(key->type != NULL);
154 if (strcmp(type, key->type) != 0) {
155 ERROR("curl_json plugin: Unable to look up DS type \"%s\".", key->type);
156 sstrncpy(type, key->type, sizeof(type));
157 }
159 return -1;
160 } else if (ds->ds_num > 1) {
161 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
163 c_complain_once(
164 LOG_WARNING, &complaint,
165 "curl_json plugin: The type \"%s\" has more than one data source. "
166 "This is currently not supported. I will return the type of the "
167 "first data source, but this will likely lead to problems later on.",
168 key->type);
169 }
171 return ds->ds[0].type;
172 }
174 /* cj_load_key loads the configuration for "key" from the parent context and
175 * sets either .key or .tree in the current context. */
176 static int cj_load_key(cj_t *db, char const *key) {
177 if (db == NULL || key == NULL || db->depth <= 0)
178 return EINVAL;
180 sstrncpy(db->state[db->depth].name, key, sizeof(db->state[db->depth].name));
182 c_avl_tree_t *tree = db->state[db->depth - 1].tree;
183 if (tree == NULL) {
184 return 0;
185 }
187 /* the parent has a key, so the tree pointer is invalid. */
188 if (CJ_IS_KEY(db->state[db->depth - 1].key)) {
189 return 0;
190 }
192 void *value = NULL;
193 if (c_avl_get(tree, key, (void *)&value) == 0) {
194 if (CJ_IS_KEY((cj_key_t *)value)) {
195 db->state[db->depth].key = value;
196 } else {
197 db->state[db->depth].tree = value;
198 }
199 } else if (c_avl_get(tree, CJ_ANY, (void *)&value) == 0) {
200 if (CJ_IS_KEY((cj_key_t *)value)) {
201 db->state[db->depth].key = value;
202 } else {
203 db->state[db->depth].tree = value;
204 }
205 } else {
206 db->state[db->depth].key = NULL;
207 }
209 return 0;
210 }
212 static void cj_advance_array(cj_t *db) {
213 if (!db->state[db->depth].in_array)
214 return;
216 db->state[db->depth].index++;
218 char name[DATA_MAX_NAME_LEN];
219 ssnprintf(name, sizeof(name), "%d", db->state[db->depth].index);
220 cj_load_key(db, name);
221 }
223 /* yajl callbacks */
224 #define CJ_CB_ABORT 0
225 #define CJ_CB_CONTINUE 1
227 static int cj_cb_boolean(void *ctx, int boolVal) {
228 cj_advance_array(ctx);
229 return (CJ_CB_CONTINUE);
230 }
232 static int cj_cb_null(void *ctx) {
233 cj_advance_array(ctx);
234 return (CJ_CB_CONTINUE);
235 }
237 static int cj_cb_number(void *ctx, const char *number, yajl_len_t number_len) {
238 char buffer[number_len + 1];
240 cj_t *db = (cj_t *)ctx;
241 cj_key_t *key = db->state[db->depth].key;
243 /* Create a null-terminated version of the string. */
244 memcpy(buffer, number, number_len);
245 buffer[sizeof(buffer) - 1] = 0;
249 if (key == NULL) {
250 /* no config for this element. */
251 cj_advance_array(ctx);
252 return CJ_CB_CONTINUE;
253 } else if (!CJ_IS_KEY(key)) {
254 /* the config expects a map or an array. */
255 NOTICE(
256 "curl_json plugin: Found \"%s\", but the configuration expects a map.",
257 buffer);
258 cj_advance_array(ctx);
259 return CJ_CB_CONTINUE;
260 }
262 int type = cj_get_type(key);
263 value_t vt;
264 int status = parse_value(buffer, &vt, type);
265 if (status != 0) {
266 NOTICE("curl_json plugin: Unable to parse number: \"%s\"", buffer);
267 cj_advance_array(ctx);
268 return (CJ_CB_CONTINUE);
269 }
271 cj_submit(db, key, &vt);
272 cj_advance_array(ctx);
273 return (CJ_CB_CONTINUE);
274 } /* int cj_cb_number */
276 /* Queries the key-tree of the parent context for "in_name" and, if found,
277 * updates the "key" field of the current context. Otherwise, "key" is set to
278 * NULL. */
279 static int cj_cb_map_key(void *ctx, unsigned char const *in_name,
280 yajl_len_t in_name_len) {
281 char name[in_name_len + 1];
283 memmove(name, in_name, in_name_len);
284 name[sizeof(name) - 1] = 0;
286 if (cj_load_key(ctx, name) != 0)
287 return CJ_CB_ABORT;
289 return CJ_CB_CONTINUE;
290 }
292 static int cj_cb_string(void *ctx, const unsigned char *val, yajl_len_t len) {
293 /* Handle the string as if it was a number. */
294 return (cj_cb_number(ctx, (const char *)val, len));
295 } /* int cj_cb_string */
297 static int cj_cb_end(void *ctx) {
298 cj_t *db = (cj_t *)ctx;
299 db->state[db->depth].tree = NULL;
300 db->depth--;
301 cj_advance_array(ctx);
302 return (CJ_CB_CONTINUE);
303 }
305 static int cj_cb_start_map(void *ctx) {
306 cj_t *db = (cj_t *)ctx;
308 if ((db->depth + 1) >= YAJL_MAX_DEPTH) {
309 ERROR("curl_json plugin: %s depth exceeds max, aborting.",
310 db->url ? db->url : db->sock);
311 return (CJ_CB_ABORT);
312 }
313 db->depth++;
314 return (CJ_CB_CONTINUE);
315 }
317 static int cj_cb_end_map(void *ctx) { return cj_cb_end(ctx); }
319 static int cj_cb_start_array(void *ctx) {
320 cj_t *db = (cj_t *)ctx;
322 if ((db->depth + 1) >= YAJL_MAX_DEPTH) {
323 ERROR("curl_json plugin: %s depth exceeds max, aborting.",
324 db->url ? db->url : db->sock);
325 return CJ_CB_ABORT;
326 }
327 db->depth++;
328 db->state[db->depth].in_array = 1;
329 db->state[db->depth].index = 0;
331 cj_load_key(db, "0");
333 return CJ_CB_CONTINUE;
334 }
336 static int cj_cb_end_array(void *ctx) {
337 cj_t *db = (cj_t *)ctx;
338 db->state[db->depth].in_array = 0;
339 return cj_cb_end(ctx);
340 }
342 static yajl_callbacks ycallbacks = {
343 cj_cb_null, /* null */
344 cj_cb_boolean, /* boolean */
345 NULL, /* integer */
346 NULL, /* double */
347 cj_cb_number, cj_cb_string, cj_cb_start_map, cj_cb_map_key,
348 cj_cb_end_map, cj_cb_start_array, cj_cb_end_array};
350 /* end yajl callbacks */
352 static void cj_key_free(cj_key_t *key) /* {{{ */
353 {
354 if (key == NULL)
355 return;
357 sfree(key->path);
358 sfree(key->type);
359 sfree(key->instance);
361 sfree(key);
362 } /* }}} void cj_key_free */
364 static void cj_tree_free(c_avl_tree_t *tree) /* {{{ */
365 {
366 char *name;
367 void *value;
369 while (c_avl_pick(tree, (void *)&name, (void *)&value) == 0) {
370 cj_key_t *key = (cj_key_t *)value;
372 if (CJ_IS_KEY(key))
373 cj_key_free(key);
374 else
375 cj_tree_free((c_avl_tree_t *)value);
377 sfree(name);
378 }
380 c_avl_destroy(tree);
381 } /* }}} void cj_tree_free */
383 static void cj_free(void *arg) /* {{{ */
384 {
385 cj_t *db;
387 DEBUG("curl_json plugin: cj_free (arg = %p);", arg);
389 db = (cj_t *)arg;
391 if (db == NULL)
392 return;
394 if (db->curl != NULL)
395 curl_easy_cleanup(db->curl);
396 db->curl = NULL;
398 if (db->tree != NULL)
399 cj_tree_free(db->tree);
400 db->tree = NULL;
402 sfree(db->instance);
403 sfree(db->host);
405 sfree(db->sock);
407 sfree(db->url);
408 sfree(db->user);
409 sfree(db->pass);
410 sfree(db->credentials);
411 sfree(db->cacert);
412 sfree(db->post_body);
413 curl_slist_free_all(db->headers);
414 curl_stats_destroy(db->stats);
416 sfree(db);
417 } /* }}} void cj_free */
419 /* Configuration handling functions {{{ */
421 static c_avl_tree_t *cj_avl_create(void) {
422 return c_avl_create((int (*)(const void *, const void *))strcmp);
423 }
425 static int cj_config_append_string(const char *name,
426 struct curl_slist **dest, /* {{{ */
427 oconfig_item_t *ci) {
428 struct curl_slist *temp = NULL;
429 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
430 WARNING("curl_json plugin: `%s' needs exactly one string argument.", name);
431 return (-1);
432 }
434 temp = curl_slist_append(*dest, ci->values[0].value.string);
435 if (temp == NULL)
436 return (-1);
438 *dest = temp;
440 return (0);
441 } /* }}} int cj_config_append_string */
443 static int cj_config_add_key(cj_t *db, /* {{{ */
444 oconfig_item_t *ci) {
445 cj_key_t *key;
446 int status;
448 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
449 WARNING("curl_json plugin: The `Key' block "
450 "needs exactly one string argument.");
451 return (-1);
452 }
454 key = calloc(1, sizeof(*key));
455 if (key == NULL) {
456 ERROR("curl_json plugin: calloc failed.");
457 return (-1);
458 }
459 key->magic = CJ_KEY_MAGIC;
461 if (strcasecmp("Key", ci->key) == 0) {
462 status = cf_util_get_string(ci, &key->path);
463 if (status != 0) {
464 sfree(key);
465 return (status);
466 }
467 } else {
468 ERROR("curl_json plugin: cj_config: "
469 "Invalid key: %s",
470 ci->key);
471 cj_key_free(key);
472 return (-1);
473 }
475 status = 0;
476 for (int i = 0; i < ci->children_num; i++) {
477 oconfig_item_t *child = ci->children + i;
479 if (strcasecmp("Type", child->key) == 0)
480 status = cf_util_get_string(child, &key->type);
481 else if (strcasecmp("Instance", child->key) == 0)
482 status = cf_util_get_string(child, &key->instance);
483 else {
484 WARNING("curl_json plugin: Option `%s' not allowed here.", child->key);
485 status = -1;
486 }
488 if (status != 0)
489 break;
490 } /* for (i = 0; i < ci->children_num; i++) */
492 if (status != 0) {
493 cj_key_free(key);
494 return (-1);
495 }
497 if (key->type == NULL) {
498 WARNING("curl_json plugin: `Type' missing in `Key' block.");
499 cj_key_free(key);
500 return (-1);
501 }
503 /* store path in a tree that will match the json map structure, example:
504 * "httpd/requests/count",
505 * "httpd/requests/current" ->
506 * { "httpd": { "requests": { "count": $key, "current": $key } } }
507 */
508 char *ptr;
509 char *name;
510 c_avl_tree_t *tree;
512 if (db->tree == NULL)
513 db->tree = cj_avl_create();
515 tree = db->tree;
516 ptr = key->path;
517 if (*ptr == '/')
518 ++ptr;
520 name = ptr;
521 while ((ptr = strchr(name, '/')) != NULL) {
522 char ent[PATH_MAX];
523 c_avl_tree_t *value;
524 size_t len;
526 len = ptr - name;
527 if (len == 0)
528 break;
530 len = COUCH_MIN(len, sizeof(ent) - 1);
531 sstrncpy(ent, name, len + 1);
533 if (c_avl_get(tree, ent, (void *)&value) != 0) {
534 value = cj_avl_create();
535 c_avl_insert(tree, strdup(ent), value);
536 }
538 tree = value;
539 name = ptr + 1;
540 }
542 if (strlen(name) == 0) {
543 ERROR("curl_json plugin: invalid key: %s", key->path);
544 cj_key_free(key);
545 return (-1);
546 }
548 c_avl_insert(tree, strdup(name), key);
549 return (status);
550 } /* }}} int cj_config_add_key */
552 static int cj_init_curl(cj_t *db) /* {{{ */
553 {
554 db->curl = curl_easy_init();
555 if (db->curl == NULL) {
556 ERROR("curl_json plugin: curl_easy_init failed.");
557 return (-1);
558 }
560 curl_easy_setopt(db->curl, CURLOPT_NOSIGNAL, 1L);
561 curl_easy_setopt(db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
562 curl_easy_setopt(db->curl, CURLOPT_WRITEDATA, db);
563 curl_easy_setopt(db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
564 curl_easy_setopt(db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
565 curl_easy_setopt(db->curl, CURLOPT_URL, db->url);
566 curl_easy_setopt(db->curl, CURLOPT_FOLLOWLOCATION, 1L);
567 curl_easy_setopt(db->curl, CURLOPT_MAXREDIRS, 50L);
569 if (db->user != NULL) {
570 #ifdef HAVE_CURLOPT_USERNAME
571 curl_easy_setopt(db->curl, CURLOPT_USERNAME, db->user);
572 curl_easy_setopt(db->curl, CURLOPT_PASSWORD,
573 (db->pass == NULL) ? "" : db->pass);
574 #else
575 size_t credentials_size;
577 credentials_size = strlen(db->user) + 2;
578 if (db->pass != NULL)
579 credentials_size += strlen(db->pass);
581 db->credentials = malloc(credentials_size);
582 if (db->credentials == NULL) {
583 ERROR("curl_json plugin: malloc failed.");
584 return (-1);
585 }
587 ssnprintf(db->credentials, credentials_size, "%s:%s", db->user,
588 (db->pass == NULL) ? "" : db->pass);
589 curl_easy_setopt(db->curl, CURLOPT_USERPWD, db->credentials);
590 #endif
592 if (db->digest)
593 curl_easy_setopt(db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
594 }
596 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYPEER, (long)db->verify_peer);
597 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYHOST, db->verify_host ? 2L : 0L);
598 if (db->cacert != NULL)
599 curl_easy_setopt(db->curl, CURLOPT_CAINFO, db->cacert);
600 if (db->headers != NULL)
601 curl_easy_setopt(db->curl, CURLOPT_HTTPHEADER, db->headers);
602 if (db->post_body != NULL)
603 curl_easy_setopt(db->curl, CURLOPT_POSTFIELDS, db->post_body);
605 #ifdef HAVE_CURLOPT_TIMEOUT_MS
606 if (db->timeout >= 0)
607 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS, (long)db->timeout);
608 else if (db->interval > 0)
609 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
610 (long)CDTIME_T_TO_MS(db->interval));
611 else
612 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
613 (long)CDTIME_T_TO_MS(plugin_get_interval()));
614 #endif
616 return (0);
617 } /* }}} int cj_init_curl */
619 static int cj_config_add_url(oconfig_item_t *ci) /* {{{ */
620 {
621 cj_t *db;
622 int status = 0;
624 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
625 WARNING("curl_json plugin: The `URL' block "
626 "needs exactly one string argument.");
627 return (-1);
628 }
630 db = calloc(1, sizeof(*db));
631 if (db == NULL) {
632 ERROR("curl_json plugin: calloc failed.");
633 return (-1);
634 }
636 db->timeout = -1;
638 if (strcasecmp("URL", ci->key) == 0)
639 status = cf_util_get_string(ci, &db->url);
640 else if (strcasecmp("Sock", ci->key) == 0)
641 status = cf_util_get_string(ci, &db->sock);
642 else {
643 ERROR("curl_json plugin: cj_config: "
644 "Invalid key: %s",
645 ci->key);
646 cj_free(db);
647 return (-1);
648 }
649 if (status != 0) {
650 sfree(db);
651 return (status);
652 }
654 /* Fill the `cj_t' structure.. */
655 for (int i = 0; i < ci->children_num; i++) {
656 oconfig_item_t *child = ci->children + i;
658 if (strcasecmp("Instance", child->key) == 0)
659 status = cf_util_get_string(child, &db->instance);
660 else if (strcasecmp("Host", child->key) == 0)
661 status = cf_util_get_string(child, &db->host);
662 else if (db->url && strcasecmp("User", child->key) == 0)
663 status = cf_util_get_string(child, &db->user);
664 else if (db->url && strcasecmp("Password", child->key) == 0)
665 status = cf_util_get_string(child, &db->pass);
666 else if (strcasecmp("Digest", child->key) == 0)
667 status = cf_util_get_boolean(child, &db->digest);
668 else if (db->url && strcasecmp("VerifyPeer", child->key) == 0)
669 status = cf_util_get_boolean(child, &db->verify_peer);
670 else if (db->url && strcasecmp("VerifyHost", child->key) == 0)
671 status = cf_util_get_boolean(child, &db->verify_host);
672 else if (db->url && strcasecmp("CACert", child->key) == 0)
673 status = cf_util_get_string(child, &db->cacert);
674 else if (db->url && strcasecmp("Header", child->key) == 0)
675 status = cj_config_append_string("Header", &db->headers, child);
676 else if (db->url && strcasecmp("Post", child->key) == 0)
677 status = cf_util_get_string(child, &db->post_body);
678 else if (strcasecmp("Key", child->key) == 0)
679 status = cj_config_add_key(db, child);
680 else if (strcasecmp("Interval", child->key) == 0)
681 status = cf_util_get_cdtime(child, &db->interval);
682 else if (strcasecmp("Timeout", child->key) == 0)
683 status = cf_util_get_int(child, &db->timeout);
684 else if (strcasecmp("Statistics", child->key) == 0) {
685 db->stats = curl_stats_from_config(child);
686 if (db->stats == NULL)
687 status = -1;
688 } else {
689 WARNING("curl_json plugin: Option `%s' not allowed here.", child->key);
690 status = -1;
691 }
693 if (status != 0)
694 break;
695 }
697 if (status == 0) {
698 if (db->tree == NULL) {
699 WARNING("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".",
700 db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
701 status = -1;
702 }
703 if (status == 0 && db->url)
704 status = cj_init_curl(db);
705 }
707 /* If all went well, register this database for reading */
708 if (status == 0) {
709 char *cb_name;
711 if (db->instance == NULL)
712 db->instance = strdup("default");
714 DEBUG("curl_json plugin: Registering new read callback: %s", db->instance);
716 cb_name = ssnprintf_alloc("curl_json-%s-%s", db->instance,
717 db->url ? db->url : db->sock);
719 user_data_t ud = {.data = db, .free_func = cj_free};
721 plugin_register_complex_read(/* group = */ NULL, cb_name, cj_read,
722 /* interval = */ db->interval, &ud);
723 sfree(cb_name);
724 } else {
725 cj_free(db);
726 return (-1);
727 }
729 return (0);
730 }
731 /* }}} int cj_config_add_database */
733 static int cj_config(oconfig_item_t *ci) /* {{{ */
734 {
735 int success;
736 int errors;
737 int status;
739 success = 0;
740 errors = 0;
742 for (int i = 0; i < ci->children_num; i++) {
743 oconfig_item_t *child = ci->children + i;
745 if (strcasecmp("Sock", child->key) == 0 ||
746 strcasecmp("URL", child->key) == 0) {
747 status = cj_config_add_url(child);
748 if (status == 0)
749 success++;
750 else
751 errors++;
752 } else {
753 WARNING("curl_json plugin: Option `%s' not allowed here.", child->key);
754 errors++;
755 }
756 }
758 if ((success == 0) && (errors > 0)) {
759 ERROR("curl_json plugin: All statements failed.");
760 return (-1);
761 }
763 return (0);
764 } /* }}} int cj_config */
766 /* }}} End of configuration handling functions */
768 static const char *cj_host(cj_t *db) /* {{{ */
769 {
770 if ((db->host == NULL) || (strcmp("", db->host) == 0) ||
771 (strcmp(CJ_DEFAULT_HOST, db->host) == 0))
772 return hostname_g;
773 return db->host;
774 } /* }}} cj_host */
776 static void cj_submit(cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
777 {
778 value_list_t vl = VALUE_LIST_INIT;
780 vl.values = value;
781 vl.values_len = 1;
783 if (key->instance == NULL) {
784 int len = 0;
785 for (int i = 0; i < db->depth; i++)
786 len += ssnprintf(vl.type_instance + len, sizeof(vl.type_instance) - len,
787 i ? "-%s" : "%s", db->state[i + 1].name);
788 } else
789 sstrncpy(vl.type_instance, key->instance, sizeof(vl.type_instance));
791 sstrncpy(vl.host, cj_host(db), sizeof(vl.host));
792 sstrncpy(vl.plugin, "curl_json", sizeof(vl.plugin));
793 sstrncpy(vl.plugin_instance, db->instance, sizeof(vl.plugin_instance));
794 sstrncpy(vl.type, key->type, sizeof(vl.type));
796 if (db->interval > 0)
797 vl.interval = db->interval;
799 plugin_dispatch_values(&vl);
800 } /* }}} int cj_submit */
802 static int cj_sock_perform(cj_t *db) /* {{{ */
803 {
804 char errbuf[1024];
805 struct sockaddr_un sa_unix = {0};
806 sa_unix.sun_family = AF_UNIX;
807 sstrncpy(sa_unix.sun_path, db->sock, sizeof(sa_unix.sun_path));
809 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
810 if (fd < 0)
811 return (-1);
812 if (connect(fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0) {
813 ERROR("curl_json plugin: connect(%s) failed: %s",
814 (db->sock != NULL) ? db->sock : "<null>",
815 sstrerror(errno, errbuf, sizeof(errbuf)));
816 close(fd);
817 return (-1);
818 }
820 ssize_t red;
821 do {
822 unsigned char buffer[4096];
823 red = read(fd, buffer, sizeof(buffer));
824 if (red < 0) {
825 ERROR("curl_json plugin: read(%s) failed: %s",
826 (db->sock != NULL) ? db->sock : "<null>",
827 sstrerror(errno, errbuf, sizeof(errbuf)));
828 close(fd);
829 return (-1);
830 }
831 if (!cj_curl_callback(buffer, red, 1, db))
832 break;
833 } while (red > 0);
834 close(fd);
835 return (0);
836 } /* }}} int cj_sock_perform */
838 static int cj_curl_perform(cj_t *db) /* {{{ */
839 {
840 int status;
841 long rc;
842 char *url;
843 url = db->url;
845 status = curl_easy_perform(db->curl);
846 if (status != CURLE_OK) {
847 ERROR("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
848 status, db->curl_errbuf, url);
849 return (-1);
850 }
851 if (db->stats != NULL)
852 curl_stats_dispatch(db->stats, db->curl, cj_host(db), "curl_json",
853 db->instance);
855 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
856 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
858 /* The response code is zero if a non-HTTP transport was used. */
859 if ((rc != 0) && (rc != 200)) {
860 ERROR("curl_json plugin: curl_easy_perform failed with "
861 "response code %ld (%s)",
862 rc, url);
863 return (-1);
864 }
865 return (0);
866 } /* }}} int cj_curl_perform */
868 static int cj_perform(cj_t *db) /* {{{ */
869 {
870 int status;
871 yajl_handle yprev = db->yajl;
873 db->yajl = yajl_alloc(&ycallbacks,
874 #if HAVE_YAJL_V2
875 /* alloc funcs = */ NULL,
876 #else
877 /* alloc funcs = */ NULL, NULL,
878 #endif
879 /* context = */ (void *)db);
880 if (db->yajl == NULL) {
881 ERROR("curl_json plugin: yajl_alloc failed.");
882 db->yajl = yprev;
883 return (-1);
884 }
886 if (db->url)
887 status = cj_curl_perform(db);
888 else
889 status = cj_sock_perform(db);
890 if (status < 0) {
891 yajl_free(db->yajl);
892 db->yajl = yprev;
893 return (-1);
894 }
896 #if HAVE_YAJL_V2
897 status = yajl_complete_parse(db->yajl);
898 #else
899 status = yajl_parse_complete(db->yajl);
900 #endif
901 if (status != yajl_status_ok) {
902 unsigned char *errmsg;
904 errmsg = yajl_get_error(db->yajl, /* verbose = */ 0,
905 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
906 ERROR("curl_json plugin: yajl_parse_complete failed: %s", (char *)errmsg);
907 yajl_free_error(db->yajl, errmsg);
908 yajl_free(db->yajl);
909 db->yajl = yprev;
910 return (-1);
911 }
913 yajl_free(db->yajl);
914 db->yajl = yprev;
915 return (0);
916 } /* }}} int cj_perform */
918 static int cj_read(user_data_t *ud) /* {{{ */
919 {
920 cj_t *db;
922 if ((ud == NULL) || (ud->data == NULL)) {
923 ERROR("curl_json plugin: cj_read: Invalid user data.");
924 return (-1);
925 }
927 db = (cj_t *)ud->data;
929 db->depth = 0;
930 memset(&db->state, 0, sizeof(db->state));
931 db->state[db->depth].tree = db->tree;
932 db->key = NULL;
934 return cj_perform(db);
935 } /* }}} int cj_read */
937 static int cj_init(void) /* {{{ */
938 {
939 /* Call this while collectd is still single-threaded to avoid
940 * initialization issues in libgcrypt. */
941 curl_global_init(CURL_GLOBAL_SSL);
942 return (0);
943 } /* }}} int cj_init */
945 void module_register(void) {
946 plugin_register_complex_config("curl_json", cj_config);
947 plugin_register_init("curl_json", cj_init);
948 } /* void module_register */
950 /* vim: set sw=2 sts=2 et fdm=marker : */