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