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