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