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