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