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