1 /**
2 * collectd - src/curl_json.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2011 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"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_avltree.h"
29 #include "utils_complain.h"
31 #include <curl/curl.h>
32 #include <yajl/yajl_parse.h>
33 #if HAVE_YAJL_YAJL_VERSION_H
34 # include <yajl/yajl_version.h>
35 #endif
37 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
38 # define HAVE_YAJL_V2 1
39 #endif
41 #define CJ_DEFAULT_HOST "localhost"
42 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
43 #define CJ_IS_KEY(key) ((key)->magic == CJ_KEY_MAGIC)
44 #define CJ_ANY "*"
45 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
47 struct cj_key_s;
48 typedef struct cj_key_s cj_key_t;
49 struct cj_key_s /* {{{ */
50 {
51 char *path;
52 char *type;
53 char *instance;
54 unsigned long magic;
55 };
56 /* }}} */
58 struct cj_s /* {{{ */
59 {
60 char *instance;
61 char *host;
63 char *url;
64 char *user;
65 char *pass;
66 char *credentials;
67 _Bool verify_peer;
68 _Bool verify_host;
69 char *cacert;
70 struct curl_slist *headers;
71 char *post_body;
73 CURL *curl;
74 char curl_errbuf[CURL_ERROR_SIZE];
76 yajl_handle yajl;
77 c_avl_tree_t *tree;
78 cj_key_t *key;
79 int depth;
80 struct {
81 union {
82 c_avl_tree_t *tree;
83 cj_key_t *key;
84 };
85 char name[DATA_MAX_NAME_LEN];
86 } state[YAJL_MAX_DEPTH];
87 };
88 typedef struct cj_s cj_t; /* }}} */
90 #if HAVE_YAJL_V2
91 typedef size_t yajl_len_t;
92 #else
93 typedef unsigned int yajl_len_t;
94 #endif
96 static int cj_read (user_data_t *ud);
97 static int cj_curl_perform (cj_t *db, CURL *curl);
98 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
100 static size_t cj_curl_callback (void *buf, /* {{{ */
101 size_t size, size_t nmemb, void *user_data)
102 {
103 cj_t *db;
104 size_t len;
105 yajl_status status;
107 len = size * nmemb;
109 if (len <= 0)
110 return (len);
112 db = user_data;
113 if (db == NULL)
114 return (0);
116 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
117 if (status == yajl_status_ok)
118 return (len);
119 #if !HAVE_YAJL_V2
120 else if (status == yajl_status_insufficient_data)
121 return (len);
122 #endif
124 if (status != yajl_status_ok)
125 {
126 unsigned char *msg =
127 yajl_get_error(db->yajl, /* verbose = */ 1,
128 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
129 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
130 yajl_free_error(db->yajl, msg);
131 return (0); /* abort write callback */
132 }
134 return (len);
135 } /* }}} size_t cj_curl_callback */
137 static int cj_get_type (cj_key_t *key)
138 {
139 const data_set_t *ds;
141 ds = plugin_get_ds (key->type);
142 if (ds == NULL)
143 {
144 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
146 assert (key->type != NULL);
147 if (strcmp (type, key->type) != 0)
148 {
149 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
150 key->type);
151 sstrncpy (type, key->type, sizeof (type));
152 }
154 return -1;
155 }
156 else if (ds->ds_num > 1)
157 {
158 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
160 c_complain_once (LOG_WARNING, &complaint,
161 "curl_json plugin: The type \"%s\" has more than one data source. "
162 "This is currently not supported. I will return the type of the "
163 "first data source, but this will likely lead to problems later on.",
164 key->type);
165 }
167 return ds->ds[0].type;
168 }
170 /* yajl callbacks */
171 #define CJ_CB_ABORT 0
172 #define CJ_CB_CONTINUE 1
174 /* "number" may not be null terminated, so copy it into a buffer before
175 * parsing. */
176 static int cj_cb_number (void *ctx,
177 const char *number, yajl_len_t number_len)
178 {
179 char buffer[number_len + 1];
181 cj_t *db = (cj_t *)ctx;
182 cj_key_t *key = db->state[db->depth].key;
183 value_t vt;
184 int type;
185 int status;
187 if ((key == NULL) || !CJ_IS_KEY (key))
188 return (CJ_CB_CONTINUE);
190 memcpy (buffer, number, number_len);
191 buffer[sizeof (buffer) - 1] = 0;
193 type = cj_get_type (key);
194 status = parse_value (buffer, &vt, type);
195 if (status != 0)
196 {
197 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
198 return (CJ_CB_CONTINUE);
199 }
201 cj_submit (db, key, &vt);
202 return (CJ_CB_CONTINUE);
203 } /* int cj_cb_number */
205 static int cj_cb_map_key (void *ctx, const unsigned char *val,
206 yajl_len_t len)
207 {
208 cj_t *db = (cj_t *)ctx;
209 c_avl_tree_t *tree;
211 tree = db->state[db->depth-1].tree;
213 if (tree != NULL)
214 {
215 cj_key_t *value;
216 char *name;
218 name = db->state[db->depth].name;
219 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
220 sstrncpy (name, (char *)val, len+1);
222 if (c_avl_get (tree, name, (void *) &value) == 0)
223 db->state[db->depth].key = value;
224 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
225 db->state[db->depth].key = value;
226 else
227 db->state[db->depth].key = NULL;
228 }
230 return (CJ_CB_CONTINUE);
231 }
233 static int cj_cb_string (void *ctx, const unsigned char *val,
234 yajl_len_t len)
235 {
236 cj_t *db = (cj_t *)ctx;
237 char str[len + 1];
239 /* Create a null-terminated version of the string. */
240 memcpy (str, val, len);
241 str[len] = 0;
243 /* No configuration for this string -> simply return. */
244 if (db->state[db->depth].key == NULL)
245 return (CJ_CB_CONTINUE);
247 if (!CJ_IS_KEY (db->state[db->depth].key))
248 {
249 NOTICE ("curl_json plugin: Found string \"%s\", but the configuration "
250 "expects a map here.", str);
251 return (CJ_CB_CONTINUE);
252 }
254 /* Handle the string as if it was a number. */
255 return (cj_cb_number (ctx, (const char *) val, len));
256 } /* int cj_cb_string */
258 static int cj_cb_start (void *ctx)
259 {
260 cj_t *db = (cj_t *)ctx;
261 if (++db->depth >= YAJL_MAX_DEPTH)
262 {
263 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
264 return (CJ_CB_ABORT);
265 }
266 return (CJ_CB_CONTINUE);
267 }
269 static int cj_cb_end (void *ctx)
270 {
271 cj_t *db = (cj_t *)ctx;
272 db->state[db->depth].tree = NULL;
273 --db->depth;
274 return (CJ_CB_CONTINUE);
275 }
277 static int cj_cb_start_map (void *ctx)
278 {
279 return cj_cb_start (ctx);
280 }
282 static int cj_cb_end_map (void *ctx)
283 {
284 return cj_cb_end (ctx);
285 }
287 static int cj_cb_start_array (void * ctx)
288 {
289 return cj_cb_start (ctx);
290 }
292 static int cj_cb_end_array (void * ctx)
293 {
294 return cj_cb_end (ctx);
295 }
297 static yajl_callbacks ycallbacks = {
298 NULL, /* null */
299 NULL, /* boolean */
300 NULL, /* integer */
301 NULL, /* double */
302 cj_cb_number,
303 cj_cb_string,
304 cj_cb_start_map,
305 cj_cb_map_key,
306 cj_cb_end_map,
307 cj_cb_start_array,
308 cj_cb_end_array
309 };
311 /* end yajl callbacks */
313 static void cj_key_free (cj_key_t *key) /* {{{ */
314 {
315 if (key == NULL)
316 return;
318 sfree (key->path);
319 sfree (key->type);
320 sfree (key->instance);
322 sfree (key);
323 } /* }}} void cj_key_free */
325 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
326 {
327 char *name;
328 void *value;
330 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
331 {
332 cj_key_t *key = (cj_key_t *)value;
334 if (CJ_IS_KEY(key))
335 cj_key_free (key);
336 else
337 cj_tree_free ((c_avl_tree_t *)value);
339 sfree (name);
340 }
342 c_avl_destroy (tree);
343 } /* }}} void cj_tree_free */
345 static void cj_free (void *arg) /* {{{ */
346 {
347 cj_t *db;
349 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
351 db = (cj_t *) arg;
353 if (db == NULL)
354 return;
356 if (db->curl != NULL)
357 curl_easy_cleanup (db->curl);
358 db->curl = NULL;
360 if (db->tree != NULL)
361 cj_tree_free (db->tree);
362 db->tree = NULL;
364 sfree (db->instance);
365 sfree (db->host);
367 sfree (db->url);
368 sfree (db->user);
369 sfree (db->pass);
370 sfree (db->credentials);
371 sfree (db->cacert);
372 sfree (db->post_body);
373 curl_slist_free_all (db->headers);
375 sfree (db);
376 } /* }}} void cj_free */
378 /* Configuration handling functions {{{ */
380 static c_avl_tree_t *cj_avl_create(void)
381 {
382 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
383 }
385 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
386 oconfig_item_t *ci)
387 {
388 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
389 {
390 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
391 return (-1);
392 }
394 *dest = curl_slist_append(*dest, ci->values[0].value.string);
395 if (*dest == NULL)
396 return (-1);
398 return (0);
399 } /* }}} int cj_config_append_string */
401 static int cj_config_add_key (cj_t *db, /* {{{ */
402 oconfig_item_t *ci)
403 {
404 cj_key_t *key;
405 int status;
406 int i;
408 if ((ci->values_num != 1)
409 || (ci->values[0].type != OCONFIG_TYPE_STRING))
410 {
411 WARNING ("curl_json plugin: The `Key' block "
412 "needs exactly one string argument.");
413 return (-1);
414 }
416 key = (cj_key_t *) malloc (sizeof (*key));
417 if (key == NULL)
418 {
419 ERROR ("curl_json plugin: malloc failed.");
420 return (-1);
421 }
422 memset (key, 0, sizeof (*key));
423 key->magic = CJ_KEY_MAGIC;
425 if (strcasecmp ("Key", ci->key) == 0)
426 {
427 status = cf_util_get_string (ci, &key->path);
428 if (status != 0)
429 {
430 sfree (key);
431 return (status);
432 }
433 }
434 else
435 {
436 ERROR ("curl_json plugin: cj_config: "
437 "Invalid key: %s", ci->key);
438 return (-1);
439 }
441 status = 0;
442 for (i = 0; i < ci->children_num; i++)
443 {
444 oconfig_item_t *child = ci->children + i;
446 if (strcasecmp ("Type", child->key) == 0)
447 status = cf_util_get_string (child, &key->type);
448 else if (strcasecmp ("Instance", child->key) == 0)
449 status = cf_util_get_string (child, &key->instance);
450 else
451 {
452 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
453 status = -1;
454 }
456 if (status != 0)
457 break;
458 } /* for (i = 0; i < ci->children_num; i++) */
460 while (status == 0)
461 {
462 if (key->type == NULL)
463 {
464 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
465 status = -1;
466 }
468 break;
469 } /* while (status == 0) */
471 /* store path in a tree that will match the json map structure, example:
472 * "httpd/requests/count",
473 * "httpd/requests/current" ->
474 * { "httpd": { "requests": { "count": $key, "current": $key } } }
475 */
476 if (status == 0)
477 {
478 char *ptr;
479 char *name;
480 char ent[PATH_MAX];
481 c_avl_tree_t *tree;
483 if (db->tree == NULL)
484 db->tree = cj_avl_create();
486 tree = db->tree;
487 name = key->path;
488 ptr = key->path;
489 if (*ptr == '/')
490 ++ptr;
492 name = ptr;
493 while (*ptr)
494 {
495 if (*ptr == '/')
496 {
497 c_avl_tree_t *value;
498 int len;
500 len = ptr-name;
501 if (len == 0)
502 break;
503 sstrncpy (ent, name, len+1);
505 if (c_avl_get (tree, ent, (void *) &value) != 0)
506 {
507 value = cj_avl_create ();
508 c_avl_insert (tree, strdup (ent), value);
509 }
511 tree = value;
512 name = ptr+1;
513 }
514 ++ptr;
515 }
516 if (*name)
517 c_avl_insert (tree, strdup(name), key);
518 else
519 {
520 ERROR ("curl_json plugin: invalid key: %s", key->path);
521 status = -1;
522 }
523 }
525 return (status);
526 } /* }}} int cj_config_add_key */
528 static int cj_init_curl (cj_t *db) /* {{{ */
529 {
530 db->curl = curl_easy_init ();
531 if (db->curl == NULL)
532 {
533 ERROR ("curl_json plugin: curl_easy_init failed.");
534 return (-1);
535 }
537 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
538 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
539 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
540 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
541 PACKAGE_NAME"/"PACKAGE_VERSION);
542 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
543 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
545 if (db->user != NULL)
546 {
547 size_t credentials_size;
549 credentials_size = strlen (db->user) + 2;
550 if (db->pass != NULL)
551 credentials_size += strlen (db->pass);
553 db->credentials = (char *) malloc (credentials_size);
554 if (db->credentials == NULL)
555 {
556 ERROR ("curl_json plugin: malloc failed.");
557 return (-1);
558 }
560 ssnprintf (db->credentials, credentials_size, "%s:%s",
561 db->user, (db->pass == NULL) ? "" : db->pass);
562 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
563 }
565 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
566 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
567 db->verify_host ? 2L : 0L);
568 if (db->cacert != NULL)
569 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
570 if (db->headers != NULL)
571 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
572 if (db->post_body != NULL)
573 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
575 return (0);
576 } /* }}} int cj_init_curl */
578 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
579 {
580 cj_t *db;
581 int status = 0;
582 int i;
584 if ((ci->values_num != 1)
585 || (ci->values[0].type != OCONFIG_TYPE_STRING))
586 {
587 WARNING ("curl_json plugin: The `URL' block "
588 "needs exactly one string argument.");
589 return (-1);
590 }
592 db = (cj_t *) malloc (sizeof (*db));
593 if (db == NULL)
594 {
595 ERROR ("curl_json plugin: malloc failed.");
596 return (-1);
597 }
598 memset (db, 0, sizeof (*db));
600 if (strcasecmp ("URL", ci->key) == 0)
601 {
602 status = cf_util_get_string (ci, &db->url);
603 if (status != 0)
604 {
605 sfree (db);
606 return (status);
607 }
608 }
609 else
610 {
611 ERROR ("curl_json plugin: cj_config: "
612 "Invalid key: %s", ci->key);
613 return (-1);
614 }
616 /* Fill the `cj_t' structure.. */
617 for (i = 0; i < ci->children_num; i++)
618 {
619 oconfig_item_t *child = ci->children + i;
621 if (strcasecmp ("Instance", child->key) == 0)
622 status = cf_util_get_string (child, &db->instance);
623 else if (strcasecmp ("Host", child->key) == 0)
624 status = cf_util_get_string (child, &db->host);
625 else if (strcasecmp ("User", child->key) == 0)
626 status = cf_util_get_string (child, &db->user);
627 else if (strcasecmp ("Password", child->key) == 0)
628 status = cf_util_get_string (child, &db->pass);
629 else if (strcasecmp ("VerifyPeer", child->key) == 0)
630 status = cf_util_get_boolean (child, &db->verify_peer);
631 else if (strcasecmp ("VerifyHost", child->key) == 0)
632 status = cf_util_get_boolean (child, &db->verify_host);
633 else if (strcasecmp ("CACert", child->key) == 0)
634 status = cf_util_get_string (child, &db->cacert);
635 else if (strcasecmp ("Header", child->key) == 0)
636 status = cj_config_append_string ("Header", &db->headers, child);
637 else if (strcasecmp ("Post", child->key) == 0)
638 status = cf_util_get_string (child, &db->post_body);
639 else if (strcasecmp ("Key", child->key) == 0)
640 status = cj_config_add_key (db, child);
641 else
642 {
643 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
644 status = -1;
645 }
647 if (status != 0)
648 break;
649 }
651 if (status == 0)
652 {
653 if (db->tree == NULL)
654 {
655 WARNING ("curl_json plugin: No (valid) `Key' block "
656 "within `URL' block `%s'.", db->url);
657 status = -1;
658 }
659 if (status == 0)
660 status = cj_init_curl (db);
661 }
663 /* If all went well, register this database for reading */
664 if (status == 0)
665 {
666 user_data_t ud;
667 char cb_name[DATA_MAX_NAME_LEN];
669 if (db->instance == NULL)
670 db->instance = strdup("default");
672 DEBUG ("curl_json plugin: Registering new read callback: %s",
673 db->instance);
675 memset (&ud, 0, sizeof (ud));
676 ud.data = (void *) db;
677 ud.free_func = cj_free;
679 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
680 db->instance, db->url);
682 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
683 /* interval = */ NULL, &ud);
684 }
685 else
686 {
687 cj_free (db);
688 return (-1);
689 }
691 return (0);
692 }
693 /* }}} int cj_config_add_database */
695 static int cj_config (oconfig_item_t *ci) /* {{{ */
696 {
697 int success;
698 int errors;
699 int status;
700 int i;
702 success = 0;
703 errors = 0;
705 for (i = 0; i < ci->children_num; i++)
706 {
707 oconfig_item_t *child = ci->children + i;
709 if (strcasecmp ("URL", child->key) == 0)
710 {
711 status = cj_config_add_url (child);
712 if (status == 0)
713 success++;
714 else
715 errors++;
716 }
717 else
718 {
719 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
720 errors++;
721 }
722 }
724 if ((success == 0) && (errors > 0))
725 {
726 ERROR ("curl_json plugin: All statements failed.");
727 return (-1);
728 }
730 return (0);
731 } /* }}} int cj_config */
733 /* }}} End of configuration handling functions */
735 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
736 {
737 value_list_t vl = VALUE_LIST_INIT;
738 char *host;
740 vl.values = value;
741 vl.values_len = 1;
743 if ((db->host == NULL)
744 || (strcmp ("", db->host) == 0)
745 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
746 host = hostname_g;
747 else
748 host = db->host;
750 if (key->instance == NULL)
751 {
752 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
753 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
754 else
755 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
756 db->state[db->depth-1].name, db->state[db->depth].name);
757 }
758 else
759 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
761 sstrncpy (vl.host, host, sizeof (vl.host));
762 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
763 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
764 sstrncpy (vl.type, key->type, sizeof (vl.type));
766 plugin_dispatch_values (&vl);
767 } /* }}} int cj_submit */
769 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
770 {
771 int status;
772 long rc;
773 char *url;
774 yajl_handle yprev = db->yajl;
776 db->yajl = yajl_alloc (&ycallbacks,
777 #if HAVE_YAJL_V2
778 /* alloc funcs = */ NULL,
779 #else
780 /* alloc funcs = */ NULL, NULL,
781 #endif
782 /* context = */ (void *)db);
783 if (db->yajl == NULL)
784 {
785 ERROR ("curl_json plugin: yajl_alloc failed.");
786 db->yajl = yprev;
787 return (-1);
788 }
790 url = NULL;
791 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
793 status = curl_easy_perform (curl);
794 if (status != CURLE_OK)
795 {
796 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
797 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
798 yajl_free (db->yajl);
799 db->yajl = yprev;
800 return (-1);
801 }
803 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
805 /* The response code is zero if a non-HTTP transport was used. */
806 if ((rc != 0) && (rc != 200))
807 {
808 ERROR ("curl_json plugin: curl_easy_perform failed with "
809 "response code %ld (%s)", rc, url);
810 yajl_free (db->yajl);
811 db->yajl = yprev;
812 return (-1);
813 }
815 #if HAVE_YAJL_V2
816 status = yajl_complete_parse(db->yajl);
817 #else
818 status = yajl_parse_complete(db->yajl);
819 #endif
820 if (status != yajl_status_ok)
821 {
822 unsigned char *errmsg;
824 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
825 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
826 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
827 (char *) errmsg);
828 yajl_free_error (db->yajl, errmsg);
829 yajl_free (db->yajl);
830 db->yajl = yprev;
831 return (-1);
832 }
834 yajl_free (db->yajl);
835 db->yajl = yprev;
836 return (0);
837 } /* }}} int cj_curl_perform */
839 static int cj_read (user_data_t *ud) /* {{{ */
840 {
841 cj_t *db;
843 if ((ud == NULL) || (ud->data == NULL))
844 {
845 ERROR ("curl_json plugin: cj_read: Invalid user data.");
846 return (-1);
847 }
849 db = (cj_t *) ud->data;
851 db->depth = 0;
852 memset (&db->state, 0, sizeof(db->state));
853 db->state[db->depth].tree = db->tree;
854 db->key = NULL;
856 return cj_curl_perform (db, db->curl);
857 } /* }}} int cj_read */
859 void module_register (void)
860 {
861 plugin_register_complex_config ("curl_json", cj_config);
862 } /* void module_register */
864 /* vim: set sw=2 sts=2 et fdm=marker : */