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