2 * collectd - src/curl_json.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2013 Florian octo Forster
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.
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.
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
20 * Doug MacEachern <dougm at hyperic.com>
21 * Florian octo Forster <octo at collectd.org>
28 #include "utils_avltree.h"
29 #include "utils_complain.h"
30 #include "utils_curl_stats.h"
32 #include <sys/types.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>
42 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
43 #define HAVE_YAJL_V2 1
46 #define CJ_DEFAULT_HOST "localhost"
48 #define COUCH_MIN(x, y) ((x) < (y) ? (x) : (y))
51 typedef struct cj_key_s cj_key_t;
52 struct cj_key_s /* {{{ */
60 /* cj_tree_entry_t is a union of either a metric configuration ("key") or a tree
61 * mapping array indexes / map keys to a descendant cj_tree_entry_t*. */
63 enum { KEY, TREE } type;
70 /* cj_state_t is a stack providing the configuration relevant for the context
71 * that is currently being parsed. If entry->type == KEY, the parser should
72 * expect a metric (a numeric value). If entry->type == TREE, the parser should
73 * expect an array of map to descent into. If entry == NULL, no configuration
74 * exists for this part of the JSON structure. */
76 cj_tree_entry_t *entry;
79 char name[DATA_MAX_NAME_LEN];
98 struct curl_slist *headers;
105 char curl_errbuf[CURL_ERROR_SIZE];
110 cj_state_t state[YAJL_MAX_DEPTH];
112 typedef struct cj_s cj_t; /* }}} */
115 typedef size_t yajl_len_t;
117 typedef unsigned int yajl_len_t;
120 static int cj_read(user_data_t *ud);
121 static void cj_submit_impl(cj_t *db, cj_key_t *key, value_t *value);
123 /* cj_submit is a function pointer to cj_submit_impl, allowing the unit-test to
124 * overwrite which function is called. */
125 static void (*cj_submit)(cj_t *, cj_key_t *, value_t *) = cj_submit_impl;
127 static size_t cj_curl_callback(void *buf, /* {{{ */
128 size_t size, size_t nmemb, void *user_data) {
142 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
143 if (status == yajl_status_ok)
146 else if (status == yajl_status_insufficient_data)
151 yajl_get_error(db->yajl, /* verbose = */ 1,
152 /* jsonText = */ (unsigned char *)buf, (unsigned int)len);
153 ERROR("curl_json plugin: yajl_parse failed: %s", msg);
154 yajl_free_error(db->yajl, msg);
155 return 0; /* abort write callback */
156 } /* }}} size_t cj_curl_callback */
158 static int cj_get_type(cj_key_t *key) {
162 const data_set_t *ds = plugin_get_ds(key->type);
164 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
166 assert(key->type != NULL);
167 if (strcmp(type, key->type) != 0) {
168 ERROR("curl_json plugin: Unable to look up DS type \"%s\".", key->type);
169 sstrncpy(type, key->type, sizeof(type));
173 } else if (ds->ds_num > 1) {
174 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
177 LOG_WARNING, &complaint,
178 "curl_json plugin: The type \"%s\" has more than one data source. "
179 "This is currently not supported. I will return the type of the "
180 "first data source, but this will likely lead to problems later on.",
184 return ds->ds[0].type;
187 /* cj_load_key loads the configuration for "key" from the parent context and
188 * sets either .key or .tree in the current context. */
189 static int cj_load_key(cj_t *db, char const *key) {
190 if (db == NULL || key == NULL || db->depth <= 0)
193 sstrncpy(db->state[db->depth].name, key, sizeof(db->state[db->depth].name));
195 if (db->state[db->depth - 1].entry == NULL ||
196 db->state[db->depth - 1].entry->type != TREE) {
200 c_avl_tree_t *tree = db->state[db->depth - 1].entry->tree;
201 cj_tree_entry_t *e = NULL;
203 if (c_avl_get(tree, key, (void *)&e) == 0) {
204 db->state[db->depth].entry = e;
205 } else if (c_avl_get(tree, CJ_ANY, (void *)&e) == 0) {
206 db->state[db->depth].entry = e;
208 db->state[db->depth].entry = NULL;
214 static void cj_advance_array(cj_t *db) {
215 if (!db->state[db->depth].in_array)
218 db->state[db->depth].index++;
220 char name[DATA_MAX_NAME_LEN];
221 snprintf(name, sizeof(name), "%d", db->state[db->depth].index);
222 cj_load_key(db, name);
226 #define CJ_CB_ABORT 0
227 #define CJ_CB_CONTINUE 1
229 static int cj_cb_boolean(void *ctx, int boolVal) {
230 cj_advance_array(ctx);
231 return CJ_CB_CONTINUE;
234 static int cj_cb_null(void *ctx) {
235 cj_advance_array(ctx);
236 return CJ_CB_CONTINUE;
239 static int cj_cb_number(void *ctx, const char *number, yajl_len_t number_len) {
240 cj_t *db = (cj_t *)ctx;
242 /* Create a null-terminated version of the string. */
243 char buffer[number_len + 1];
244 memcpy(buffer, number, number_len);
245 buffer[sizeof(buffer) - 1] = 0;
247 if (db->state[db->depth].entry == NULL ||
248 db->state[db->depth].entry->type != KEY) {
249 if (db->state[db->depth].entry != NULL) {
250 NOTICE("curl_json plugin: Found \"%s\", but the configuration expects a "
254 cj_advance_array(ctx);
255 return CJ_CB_CONTINUE;
258 cj_key_t *key = db->state[db->depth].entry->key;
260 int type = cj_get_type(key);
262 int status = parse_value(buffer, &vt, type);
264 NOTICE("curl_json plugin: Unable to parse number: \"%s\"", buffer);
265 cj_advance_array(ctx);
266 return CJ_CB_CONTINUE;
269 cj_submit(db, key, &vt);
270 cj_advance_array(ctx);
271 return CJ_CB_CONTINUE;
272 } /* int cj_cb_number */
274 /* Queries the key-tree of the parent context for "in_name" and, if found,
275 * updates the "key" field of the current context. Otherwise, "key" is set to
277 static int cj_cb_map_key(void *ctx, unsigned char const *in_name,
278 yajl_len_t in_name_len) {
279 char name[in_name_len + 1];
281 memmove(name, in_name, in_name_len);
282 name[sizeof(name) - 1] = 0;
284 if (cj_load_key(ctx, name) != 0)
287 return CJ_CB_CONTINUE;
290 static int cj_cb_string(void *ctx, const unsigned char *val, yajl_len_t len) {
291 /* Handle the string as if it was a number. */
292 return cj_cb_number(ctx, (const char *)val, len);
293 } /* int cj_cb_string */
295 static int cj_cb_end(void *ctx) {
296 cj_t *db = (cj_t *)ctx;
297 memset(&db->state[db->depth], 0, sizeof(db->state[db->depth]));
299 cj_advance_array(ctx);
300 return CJ_CB_CONTINUE;
303 static int cj_cb_start_map(void *ctx) {
304 cj_t *db = (cj_t *)ctx;
306 if ((db->depth + 1) >= YAJL_MAX_DEPTH) {
307 ERROR("curl_json plugin: %s depth exceeds max, aborting.",
308 db->url ? db->url : db->sock);
312 return CJ_CB_CONTINUE;
315 static int cj_cb_end_map(void *ctx) { return cj_cb_end(ctx); }
317 static int cj_cb_start_array(void *ctx) {
318 cj_t *db = (cj_t *)ctx;
320 if ((db->depth + 1) >= YAJL_MAX_DEPTH) {
321 ERROR("curl_json plugin: %s depth exceeds max, aborting.",
322 db->url ? db->url : db->sock);
326 db->state[db->depth].in_array = 1;
327 db->state[db->depth].index = 0;
329 cj_load_key(db, "0");
331 return CJ_CB_CONTINUE;
334 static int cj_cb_end_array(void *ctx) {
335 cj_t *db = (cj_t *)ctx;
336 db->state[db->depth].in_array = 0;
337 return cj_cb_end(ctx);
340 static yajl_callbacks ycallbacks = {
341 cj_cb_null, /* null */
342 cj_cb_boolean, /* boolean */
345 cj_cb_number, cj_cb_string, cj_cb_start_map, cj_cb_map_key,
346 cj_cb_end_map, cj_cb_start_array, cj_cb_end_array};
348 /* end yajl callbacks */
350 static void cj_key_free(cj_key_t *key) /* {{{ */
357 sfree(key->instance);
360 } /* }}} void cj_key_free */
362 static void cj_tree_free(c_avl_tree_t *tree) /* {{{ */
367 while (c_avl_pick(tree, (void *)&name, (void *)&e) == 0) {
373 cj_tree_free(e->tree);
378 } /* }}} void cj_tree_free */
380 static void cj_free(void *arg) /* {{{ */
384 DEBUG("curl_json plugin: cj_free (arg = %p);", arg);
391 if (db->curl != NULL)
392 curl_easy_cleanup(db->curl);
395 if (db->tree != NULL)
396 cj_tree_free(db->tree);
400 sfree(db->plugin_name);
408 sfree(db->credentials);
410 sfree(db->post_body);
411 curl_slist_free_all(db->headers);
412 curl_stats_destroy(db->stats);
415 } /* }}} void cj_free */
417 /* Configuration handling functions {{{ */
419 static c_avl_tree_t *cj_avl_create(void) {
420 return c_avl_create((int (*)(const void *, const void *))strcmp);
423 static int cj_config_append_string(const char *name,
424 struct curl_slist **dest, /* {{{ */
425 oconfig_item_t *ci) {
426 struct curl_slist *temp = NULL;
427 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
428 WARNING("curl_json plugin: `%s' needs exactly one string argument.", name);
432 temp = curl_slist_append(*dest, ci->values[0].value.string);
439 } /* }}} int cj_config_append_string */
441 /* cj_append_key adds key to the configuration stored in db.
444 * "httpd/requests/count",
445 * "httpd/requests/current" ->
446 * { "httpd": { "requests": { "count": $key, "current": $key } } }
448 static int cj_append_key(cj_t *db, cj_key_t *key) { /* {{{ */
449 if (db->tree == NULL)
450 db->tree = cj_avl_create();
452 c_avl_tree_t *tree = db->tree;
454 char const *start = key->path;
459 while ((end = strchr(start, '/')) != NULL) {
462 size_t len = end - start;
466 len = COUCH_MIN(len, sizeof(name) - 1);
467 sstrncpy(name, start, len + 1);
470 if (c_avl_get(tree, name, (void *)&e) != 0) {
471 e = calloc(1, sizeof(*e));
475 e->tree = cj_avl_create();
477 c_avl_insert(tree, strdup(name), e);
487 if (strlen(start) == 0) {
488 ERROR("curl_json plugin: invalid key: %s", key->path);
492 cj_tree_entry_t *e = calloc(1, sizeof(*e));
498 c_avl_insert(tree, strdup(start), e);
500 } /* }}} int cj_append_key */
502 static int cj_config_add_key(cj_t *db, /* {{{ */
503 oconfig_item_t *ci) {
507 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
508 WARNING("curl_json plugin: The `Key' block "
509 "needs exactly one string argument.");
513 key = calloc(1, sizeof(*key));
515 ERROR("curl_json plugin: calloc failed.");
519 if (strcasecmp("Key", ci->key) == 0) {
520 status = cf_util_get_string(ci, &key->path);
526 ERROR("curl_json plugin: cj_config: "
534 for (int i = 0; i < ci->children_num; i++) {
535 oconfig_item_t *child = ci->children + i;
537 if (strcasecmp("Type", child->key) == 0)
538 status = cf_util_get_string(child, &key->type);
539 else if (strcasecmp("Instance", child->key) == 0)
540 status = cf_util_get_string(child, &key->instance);
542 WARNING("curl_json plugin: Option `%s' not allowed here.", child->key);
548 } /* for (i = 0; i < ci->children_num; i++) */
555 if (key->type == NULL) {
556 WARNING("curl_json plugin: `Type' missing in `Key' block.");
561 status = cj_append_key(db, key);
568 } /* }}} int cj_config_add_key */
570 static int cj_init_curl(cj_t *db) /* {{{ */
572 db->curl = curl_easy_init();
573 if (db->curl == NULL) {
574 ERROR("curl_json plugin: curl_easy_init failed.");
578 curl_easy_setopt(db->curl, CURLOPT_NOSIGNAL, 1L);
579 curl_easy_setopt(db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
580 curl_easy_setopt(db->curl, CURLOPT_WRITEDATA, db);
581 curl_easy_setopt(db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
582 curl_easy_setopt(db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
583 curl_easy_setopt(db->curl, CURLOPT_FOLLOWLOCATION, 1L);
584 curl_easy_setopt(db->curl, CURLOPT_MAXREDIRS, 50L);
586 if (db->user != NULL) {
587 #ifdef HAVE_CURLOPT_USERNAME
588 curl_easy_setopt(db->curl, CURLOPT_USERNAME, db->user);
589 curl_easy_setopt(db->curl, CURLOPT_PASSWORD,
590 (db->pass == NULL) ? "" : db->pass);
592 size_t credentials_size;
594 credentials_size = strlen(db->user) + 2;
595 if (db->pass != NULL)
596 credentials_size += strlen(db->pass);
598 db->credentials = malloc(credentials_size);
599 if (db->credentials == NULL) {
600 ERROR("curl_json plugin: malloc failed.");
604 snprintf(db->credentials, credentials_size, "%s:%s", db->user,
605 (db->pass == NULL) ? "" : db->pass);
606 curl_easy_setopt(db->curl, CURLOPT_USERPWD, db->credentials);
610 curl_easy_setopt(db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
613 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYPEER, (long)db->verify_peer);
614 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYHOST, db->verify_host ? 2L : 0L);
615 if (db->cacert != NULL)
616 curl_easy_setopt(db->curl, CURLOPT_CAINFO, db->cacert);
617 if (db->headers != NULL)
618 curl_easy_setopt(db->curl, CURLOPT_HTTPHEADER, db->headers);
619 if (db->post_body != NULL)
620 curl_easy_setopt(db->curl, CURLOPT_POSTFIELDS, db->post_body);
622 #ifdef HAVE_CURLOPT_TIMEOUT_MS
623 if (db->timeout >= 0)
624 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS, (long)db->timeout);
625 else if (db->interval > 0)
626 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
627 (long)CDTIME_T_TO_MS(db->interval));
629 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
630 (long)CDTIME_T_TO_MS(plugin_get_interval()));
634 } /* }}} int cj_init_curl */
636 static int cj_config_add_url(oconfig_item_t *ci) /* {{{ */
641 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
642 WARNING("curl_json plugin: The `URL' block "
643 "needs exactly one string argument.");
647 db = calloc(1, sizeof(*db));
649 ERROR("curl_json plugin: calloc failed.");
655 if (strcasecmp("URL", ci->key) == 0)
656 status = cf_util_get_string(ci, &db->url);
657 else if (strcasecmp("Sock", ci->key) == 0)
658 status = cf_util_get_string(ci, &db->sock);
660 ERROR("curl_json plugin: cj_config: "
671 /* Fill the `cj_t' structure.. */
672 for (int i = 0; i < ci->children_num; i++) {
673 oconfig_item_t *child = ci->children + i;
675 if (strcasecmp("Instance", child->key) == 0)
676 status = cf_util_get_string(child, &db->instance);
677 else if (strcasecmp("Plugin", child->key) == 0)
678 status = cf_util_get_string(child, &db->plugin_name);
679 else if (strcasecmp("Host", child->key) == 0)
680 status = cf_util_get_string(child, &db->host);
681 else if (db->url && strcasecmp("User", child->key) == 0)
682 status = cf_util_get_string(child, &db->user);
683 else if (db->url && strcasecmp("Password", child->key) == 0)
684 status = cf_util_get_string(child, &db->pass);
685 else if (strcasecmp("Digest", child->key) == 0)
686 status = cf_util_get_boolean(child, &db->digest);
687 else if (db->url && strcasecmp("VerifyPeer", child->key) == 0)
688 status = cf_util_get_boolean(child, &db->verify_peer);
689 else if (db->url && strcasecmp("VerifyHost", child->key) == 0)
690 status = cf_util_get_boolean(child, &db->verify_host);
691 else if (db->url && strcasecmp("CACert", child->key) == 0)
692 status = cf_util_get_string(child, &db->cacert);
693 else if (db->url && strcasecmp("Header", child->key) == 0)
694 status = cj_config_append_string("Header", &db->headers, child);
695 else if (db->url && strcasecmp("Post", child->key) == 0)
696 status = cf_util_get_string(child, &db->post_body);
697 else if (strcasecmp("Key", child->key) == 0)
698 status = cj_config_add_key(db, child);
699 else if (strcasecmp("Interval", child->key) == 0)
700 status = cf_util_get_cdtime(child, &db->interval);
701 else if (strcasecmp("Timeout", child->key) == 0)
702 status = cf_util_get_int(child, &db->timeout);
703 else if (strcasecmp("Statistics", child->key) == 0) {
704 db->stats = curl_stats_from_config(child);
705 if (db->stats == NULL)
708 WARNING("curl_json plugin: Option `%s' not allowed here.", child->key);
717 if (db->tree == NULL) {
718 WARNING("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".",
719 db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
722 if (status == 0 && db->url)
723 status = cj_init_curl(db);
726 /* If all went well, register this database for reading */
730 if (db->instance == NULL)
731 db->instance = strdup("default");
733 DEBUG("curl_json plugin: Registering new read callback: %s", db->instance);
735 cb_name = ssnprintf_alloc("curl_json-%s-%s", db->instance,
736 db->url ? db->url : db->sock);
738 plugin_register_complex_read(/* group = */ NULL, cb_name, cj_read,
739 /* interval = */ db->interval,
741 .data = db, .free_func = cj_free,
751 /* }}} int cj_config_add_database */
753 static int cj_config(oconfig_item_t *ci) /* {{{ */
762 for (int i = 0; i < ci->children_num; i++) {
763 oconfig_item_t *child = ci->children + i;
765 if (strcasecmp("Sock", child->key) == 0 ||
766 strcasecmp("URL", child->key) == 0) {
767 status = cj_config_add_url(child);
773 WARNING("curl_json plugin: Option `%s' not allowed here.", child->key);
778 if ((success == 0) && (errors > 0)) {
779 ERROR("curl_json plugin: All statements failed.");
784 } /* }}} int cj_config */
786 /* }}} End of configuration handling functions */
788 static const char *cj_host(cj_t *db) /* {{{ */
790 if ((db->host == NULL) || (strcmp("", db->host) == 0) ||
791 (strcmp(CJ_DEFAULT_HOST, db->host) == 0))
796 static void cj_submit_impl(cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
798 value_list_t vl = VALUE_LIST_INIT;
803 if (key->instance == NULL) {
805 for (int i = 0; i < db->depth; i++)
806 len += snprintf(vl.type_instance + len, sizeof(vl.type_instance) - len,
807 i ? "-%s" : "%s", db->state[i + 1].name);
809 sstrncpy(vl.type_instance, key->instance, sizeof(vl.type_instance));
811 sstrncpy(vl.host, cj_host(db), sizeof(vl.host));
812 sstrncpy(vl.plugin, (db->plugin_name != NULL) ? db->plugin_name : "curl_json",
814 sstrncpy(vl.plugin_instance, db->instance, sizeof(vl.plugin_instance));
815 sstrncpy(vl.type, key->type, sizeof(vl.type));
817 if (db->interval > 0)
818 vl.interval = db->interval;
820 plugin_dispatch_values(&vl);
821 } /* }}} int cj_submit_impl */
823 static int cj_sock_perform(cj_t *db) /* {{{ */
826 struct sockaddr_un sa_unix = {
827 .sun_family = AF_UNIX,
829 sstrncpy(sa_unix.sun_path, db->sock, sizeof(sa_unix.sun_path));
831 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
834 if (connect(fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0) {
835 ERROR("curl_json plugin: connect(%s) failed: %s",
836 (db->sock != NULL) ? db->sock : "<null>",
837 sstrerror(errno, errbuf, sizeof(errbuf)));
844 unsigned char buffer[4096];
845 red = read(fd, buffer, sizeof(buffer));
847 ERROR("curl_json plugin: read(%s) failed: %s",
848 (db->sock != NULL) ? db->sock : "<null>",
849 sstrerror(errno, errbuf, sizeof(errbuf)));
853 if (!cj_curl_callback(buffer, red, 1, db))
858 } /* }}} int cj_sock_perform */
860 static int cj_curl_perform(cj_t *db) /* {{{ */
866 curl_easy_setopt(db->curl, CURLOPT_URL, db->url);
868 status = curl_easy_perform(db->curl);
869 if (status != CURLE_OK) {
870 ERROR("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
871 status, db->curl_errbuf, db->url);
874 if (db->stats != NULL)
875 curl_stats_dispatch(db->stats, db->curl, cj_host(db), "curl_json",
878 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
879 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
881 /* The response code is zero if a non-HTTP transport was used. */
882 if ((rc != 0) && (rc != 200)) {
883 ERROR("curl_json plugin: curl_easy_perform failed with "
884 "response code %ld (%s)",
889 } /* }}} int cj_curl_perform */
891 static int cj_perform(cj_t *db) /* {{{ */
894 yajl_handle yprev = db->yajl;
896 db->yajl = yajl_alloc(&ycallbacks,
898 /* alloc funcs = */ NULL,
900 /* alloc funcs = */ NULL, NULL,
902 /* context = */ (void *)db);
903 if (db->yajl == NULL) {
904 ERROR("curl_json plugin: yajl_alloc failed.");
910 status = cj_curl_perform(db);
912 status = cj_sock_perform(db);
920 status = yajl_complete_parse(db->yajl);
922 status = yajl_parse_complete(db->yajl);
924 if (status != yajl_status_ok) {
925 unsigned char *errmsg;
927 errmsg = yajl_get_error(db->yajl, /* verbose = */ 0,
928 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
929 ERROR("curl_json plugin: yajl_parse_complete failed: %s", (char *)errmsg);
930 yajl_free_error(db->yajl, errmsg);
939 } /* }}} int cj_perform */
941 static int cj_read(user_data_t *ud) /* {{{ */
945 if ((ud == NULL) || (ud->data == NULL)) {
946 ERROR("curl_json plugin: cj_read: Invalid user data.");
950 db = (cj_t *)ud->data;
953 memset(&db->state, 0, sizeof(db->state));
955 /* This is not a compound literal because EPEL6's GCC is not cool enough to
956 * handle anonymous unions within compound literals. */
957 cj_tree_entry_t root = {0};
959 root.tree = db->tree;
960 db->state[0].entry = &root;
962 int status = cj_perform(db);
964 db->state[0].entry = NULL;
967 } /* }}} int cj_read */
969 static int cj_init(void) /* {{{ */
971 /* Call this while collectd is still single-threaded to avoid
972 * initialization issues in libgcrypt. */
973 curl_global_init(CURL_GLOBAL_SSL);
975 } /* }}} int cj_init */
977 void module_register(void) {
978 plugin_register_complex_config("curl_json", cj_config);
979 plugin_register_init("curl_json", cj_init);
980 } /* void module_register */