curl_json plugin: add an example of array access to the documented uWSGI example
[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 <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/un.h>
34
35 #include <curl/curl.h>
36
37 #include <yajl/yajl_parse.h>
38 #if HAVE_YAJL_YAJL_VERSION_H
39 # include <yajl/yajl_version.h>
40 #endif
41
42 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
43 # define HAVE_YAJL_V2 1
44 #endif
45
46 #define CJ_DEFAULT_HOST "localhost"
47 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
48 #define CJ_IS_KEY(key) ((key)->magic == CJ_KEY_MAGIC)
49 #define CJ_ANY "*"
50 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
51
52 struct cj_key_s;
53 typedef struct cj_key_s cj_key_t;
54 struct cj_key_s /* {{{ */
55 {
56   char *path;
57   char *type;
58   char *instance;
59   unsigned long magic;
60 };
61 /* }}} */
62
63 struct cj_s /* {{{ */
64 {
65   char *instance;
66   char *host;
67
68   char *sock;
69
70   char *url;
71   char *user;
72   char *pass;
73   char *credentials;
74   _Bool verify_peer;
75   _Bool verify_host;
76   char *cacert;
77   struct curl_slist *headers;
78   char *post_body;
79
80   CURL *curl;
81   char curl_errbuf[CURL_ERROR_SIZE];
82
83   yajl_handle yajl;
84   c_avl_tree_t *tree;
85   cj_key_t *key;
86   int depth;
87   struct {
88     union {
89       c_avl_tree_t *tree;
90       cj_key_t *key;
91     };
92     char name[DATA_MAX_NAME_LEN];
93   } state[YAJL_MAX_DEPTH];
94 };
95 typedef struct cj_s cj_t; /* }}} */
96
97 #if HAVE_YAJL_V2
98 typedef size_t yajl_len_t;
99 #else
100 typedef unsigned int yajl_len_t;
101 #endif
102
103 static int cj_read (user_data_t *ud);
104 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
105
106 static size_t cj_curl_callback (void *buf, /* {{{ */
107     size_t size, size_t nmemb, void *user_data)
108 {
109   cj_t *db;
110   size_t len;
111   yajl_status status;
112
113   len = size * nmemb;
114
115   if (len <= 0)
116     return (len);
117
118   db = user_data;
119   if (db == NULL)
120     return (0);
121
122   status = yajl_parse(db->yajl, (unsigned char *)buf, len);
123   if (status == yajl_status_ok)
124     return (len);
125 #if !HAVE_YAJL_V2
126   else if (status == yajl_status_insufficient_data)
127     return (len);
128 #endif
129
130   if (status != yajl_status_ok)
131   {
132     unsigned char *msg =
133       yajl_get_error(db->yajl, /* verbose = */ 1,
134           /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
135     ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
136     yajl_free_error(db->yajl, msg);
137     return (0); /* abort write callback */
138   }
139
140   return (len);
141 } /* }}} size_t cj_curl_callback */
142
143 static int cj_get_type (cj_key_t *key)
144 {
145   const data_set_t *ds;
146
147   ds = plugin_get_ds (key->type);
148   if (ds == NULL)
149   {
150     static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
151
152     assert (key->type != NULL);
153     if (strcmp (type, key->type) != 0)
154     {
155       ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
156           key->type);
157       sstrncpy (type, key->type, sizeof (type));
158     }
159
160     return -1;
161   }
162   else if (ds->ds_num > 1)
163   {
164     static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
165
166     c_complain_once (LOG_WARNING, &complaint,
167         "curl_json plugin: The type \"%s\" has more than one data source. "
168         "This is currently not supported. I will return the type of the "
169         "first data source, but this will likely lead to problems later on.",
170         key->type);
171   }
172
173   return ds->ds[0].type;
174 }
175
176 /* yajl callbacks */
177 #define CJ_CB_ABORT    0
178 #define CJ_CB_CONTINUE 1
179
180 /* "number" may not be null terminated, so copy it into a buffer before
181  * parsing. */
182 static int cj_cb_number (void *ctx,
183     const char *number, yajl_len_t number_len)
184 {
185   char buffer[number_len + 1];
186
187   cj_t *db = (cj_t *)ctx;
188   cj_key_t *key = db->state[db->depth].key;
189   value_t vt;
190   int type;
191   int status;
192
193   if ((key == NULL) || !CJ_IS_KEY (key))
194     return (CJ_CB_CONTINUE);
195
196   memcpy (buffer, number, number_len);
197   buffer[sizeof (buffer) - 1] = 0;
198
199   type = cj_get_type (key);
200   status = parse_value (buffer, &vt, type);
201   if (status != 0)
202   {
203     NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
204     return (CJ_CB_CONTINUE);
205   }
206
207   cj_submit (db, key, &vt);
208   return (CJ_CB_CONTINUE);
209 } /* int cj_cb_number */
210
211 static int cj_cb_map_key (void *ctx, const unsigned char *val,
212     yajl_len_t len)
213 {
214   cj_t *db = (cj_t *)ctx;
215   c_avl_tree_t *tree;
216
217   tree = db->state[db->depth-1].tree;
218
219   if (tree != NULL)
220   {
221     cj_key_t *value;
222     char *name;
223
224     name = db->state[db->depth].name;
225     len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
226     sstrncpy (name, (char *)val, len+1);
227
228     if (c_avl_get (tree, name, (void *) &value) == 0)
229       db->state[db->depth].key = value;
230     else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
231       db->state[db->depth].key = value;
232     else
233       db->state[db->depth].key = NULL;
234   }
235
236   return (CJ_CB_CONTINUE);
237 }
238
239 static int cj_cb_string (void *ctx, const unsigned char *val,
240     yajl_len_t len)
241 {
242   cj_t *db = (cj_t *)ctx;
243   char str[len + 1];
244
245   /* Create a null-terminated version of the string. */
246   memcpy (str, val, len);
247   str[len] = 0;
248
249   /* No configuration for this string -> simply return. */
250   if (db->state[db->depth].key == NULL)
251     return (CJ_CB_CONTINUE);
252
253   if (!CJ_IS_KEY (db->state[db->depth].key))
254   {
255     NOTICE ("curl_json plugin: Found string \"%s\", but the configuration "
256         "expects a map here.", str);
257     return (CJ_CB_CONTINUE);
258   }
259
260   /* Handle the string as if it was a number. */
261   return (cj_cb_number (ctx, (const char *) val, len));
262 } /* int cj_cb_string */
263
264 static int cj_cb_start (void *ctx)
265 {
266   cj_t *db = (cj_t *)ctx;
267   if (++db->depth >= YAJL_MAX_DEPTH)
268   {
269     ERROR ("curl_json plugin: %s depth exceeds max, aborting.",
270            db->url ? db->url : db->sock);
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->sock);
375
376   sfree (db->url);
377   sfree (db->user);
378   sfree (db->pass);
379   sfree (db->credentials);
380   sfree (db->cacert);
381   sfree (db->post_body);
382   curl_slist_free_all (db->headers);
383
384   sfree (db);
385 } /* }}} void cj_free */
386
387 /* Configuration handling functions {{{ */
388
389 static c_avl_tree_t *cj_avl_create(void)
390 {
391   return c_avl_create ((int (*) (const void *, const void *)) strcmp);
392 }
393
394 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
395     oconfig_item_t *ci)
396 {
397   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
398   {
399     WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
400     return (-1);
401   }
402
403   *dest = curl_slist_append(*dest, ci->values[0].value.string);
404   if (*dest == NULL)
405     return (-1);
406
407   return (0);
408 } /* }}} int cj_config_append_string */
409
410 static int cj_config_add_key (cj_t *db, /* {{{ */
411                                    oconfig_item_t *ci)
412 {
413   cj_key_t *key;
414   int status;
415   int i;
416
417   if ((ci->values_num != 1)
418       || (ci->values[0].type != OCONFIG_TYPE_STRING))
419   {
420     WARNING ("curl_json plugin: The `Key' block "
421              "needs exactly one string argument.");
422     return (-1);
423   }
424
425   key = (cj_key_t *) malloc (sizeof (*key));
426   if (key == NULL)
427   {
428     ERROR ("curl_json plugin: malloc failed.");
429     return (-1);
430   }
431   memset (key, 0, sizeof (*key));
432   key->magic = CJ_KEY_MAGIC;
433
434   if (strcasecmp ("Key", ci->key) == 0)
435   {
436     status = cf_util_get_string (ci, &key->path);
437     if (status != 0)
438     {
439       sfree (key);
440       return (status);
441     }
442   }
443   else
444   {
445     ERROR ("curl_json plugin: cj_config: "
446            "Invalid key: %s", ci->key);
447     return (-1);
448   }
449
450   status = 0;
451   for (i = 0; i < ci->children_num; i++)
452   {
453     oconfig_item_t *child = ci->children + i;
454
455     if (strcasecmp ("Type", child->key) == 0)
456       status = cf_util_get_string (child, &key->type);
457     else if (strcasecmp ("Instance", child->key) == 0)
458       status = cf_util_get_string (child, &key->instance);
459     else
460     {
461       WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
462       status = -1;
463     }
464
465     if (status != 0)
466       break;
467   } /* for (i = 0; i < ci->children_num; i++) */
468
469   while (status == 0)
470   {
471     if (key->type == NULL)
472     {
473       WARNING ("curl_json plugin: `Type' missing in `Key' block.");
474       status = -1;
475     }
476
477     break;
478   } /* while (status == 0) */
479
480   /* store path in a tree that will match the json map structure, example:
481    * "httpd/requests/count",
482    * "httpd/requests/current" ->
483    * { "httpd": { "requests": { "count": $key, "current": $key } } }
484    */
485   if (status == 0)
486   {
487     char *ptr;
488     char *name;
489     char ent[PATH_MAX];
490     c_avl_tree_t *tree;
491
492     if (db->tree == NULL)
493       db->tree = cj_avl_create();
494
495     tree = db->tree;
496     name = key->path;
497     ptr = key->path;
498     if (*ptr == '/')
499       ++ptr;
500
501     name = ptr;
502     while (*ptr)
503     {
504       if (*ptr == '/')
505       {
506         c_avl_tree_t *value;
507         int len;
508
509         len = ptr-name;
510         if (len == 0)
511           break;
512         sstrncpy (ent, name, len+1);
513
514         if (c_avl_get (tree, ent, (void *) &value) != 0)
515         {
516           value = cj_avl_create ();
517           c_avl_insert (tree, strdup (ent), value);
518         }
519
520         tree = value;
521         name = ptr+1;
522       }
523       ++ptr;
524     }
525     if (*name)
526       c_avl_insert (tree, strdup(name), key);
527     else
528     {
529       ERROR ("curl_json plugin: invalid key: %s", key->path);
530       status = -1;
531     }
532   }
533
534   return (status);
535 } /* }}} int cj_config_add_key */
536
537 static int cj_init_curl (cj_t *db) /* {{{ */
538 {
539   db->curl = curl_easy_init ();
540   if (db->curl == NULL)
541   {
542     ERROR ("curl_json plugin: curl_easy_init failed.");
543     return (-1);
544   }
545
546   curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
547   curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
548   curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
549   curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
550                     PACKAGE_NAME"/"PACKAGE_VERSION);
551   curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
552   curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
553
554   if (db->user != NULL)
555   {
556     size_t credentials_size;
557
558     credentials_size = strlen (db->user) + 2;
559     if (db->pass != NULL)
560       credentials_size += strlen (db->pass);
561
562     db->credentials = (char *) malloc (credentials_size);
563     if (db->credentials == NULL)
564     {
565       ERROR ("curl_json plugin: malloc failed.");
566       return (-1);
567     }
568
569     ssnprintf (db->credentials, credentials_size, "%s:%s",
570                db->user, (db->pass == NULL) ? "" : db->pass);
571     curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
572   }
573
574   curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
575   curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
576                     db->verify_host ? 2L : 0L);
577   if (db->cacert != NULL)
578     curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
579   if (db->headers != NULL)
580     curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
581   if (db->post_body != NULL)
582     curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
583
584   return (0);
585 } /* }}} int cj_init_curl */
586
587 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
588 {
589   cj_t *db;
590   int status = 0;
591   int i;
592
593   if ((ci->values_num != 1)
594       || (ci->values[0].type != OCONFIG_TYPE_STRING))
595   {
596     WARNING ("curl_json plugin: The `URL' block "
597              "needs exactly one string argument.");
598     return (-1);
599   }
600
601   db = (cj_t *) malloc (sizeof (*db));
602   if (db == NULL)
603   {
604     ERROR ("curl_json plugin: malloc failed.");
605     return (-1);
606   }
607   memset (db, 0, sizeof (*db));
608
609   if (strcasecmp ("URL", ci->key) == 0)
610     status = cf_util_get_string (ci, &db->url);
611   else if (strcasecmp ("Sock", ci->key) == 0)
612     status = cf_util_get_string (ci, &db->sock);
613   else
614   {
615     ERROR ("curl_json plugin: cj_config: "
616            "Invalid key: %s", ci->key);
617     return (-1);
618   }
619   if (status != 0)
620   {
621     sfree (db);
622     return (status);
623   }
624
625   /* Fill the `cj_t' structure.. */
626   for (i = 0; i < ci->children_num; i++)
627   {
628     oconfig_item_t *child = ci->children + i;
629
630     if (strcasecmp ("Instance", child->key) == 0)
631       status = cf_util_get_string (child, &db->instance);
632     else if (strcasecmp ("Host", child->key) == 0)
633       status = cf_util_get_string (child, &db->host);
634     else if (db->url && strcasecmp ("User", child->key) == 0)
635       status = cf_util_get_string (child, &db->user);
636     else if (db->url && strcasecmp ("Password", child->key) == 0)
637       status = cf_util_get_string (child, &db->pass);
638     else if (db->url && strcasecmp ("VerifyPeer", child->key) == 0)
639       status = cf_util_get_boolean (child, &db->verify_peer);
640     else if (db->url && strcasecmp ("VerifyHost", child->key) == 0)
641       status = cf_util_get_boolean (child, &db->verify_host);
642     else if (db->url && strcasecmp ("CACert", child->key) == 0)
643       status = cf_util_get_string (child, &db->cacert);
644     else if (db->url && strcasecmp ("Header", child->key) == 0)
645       status = cj_config_append_string ("Header", &db->headers, child);
646     else if (db->url && strcasecmp ("Post", child->key) == 0)
647       status = cf_util_get_string (child, &db->post_body);
648     else if (strcasecmp ("Key", child->key) == 0)
649       status = cj_config_add_key (db, child);
650     else
651     {
652       WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
653       status = -1;
654     }
655
656     if (status != 0)
657       break;
658   }
659
660   if (status == 0)
661   {
662     if (db->tree == NULL)
663     {
664       WARNING ("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".",
665                db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
666       status = -1;
667     }
668     if (status == 0 && db->url)
669       status = cj_init_curl (db);
670   }
671
672   /* If all went well, register this database for reading */
673   if (status == 0)
674   {
675     user_data_t ud;
676     char cb_name[DATA_MAX_NAME_LEN];
677
678     if (db->instance == NULL)
679       db->instance = strdup("default");
680
681     DEBUG ("curl_json plugin: Registering new read callback: %s",
682            db->instance);
683
684     memset (&ud, 0, sizeof (ud));
685     ud.data = (void *) db;
686     ud.free_func = cj_free;
687
688     ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
689                db->instance, db->url ? db->url : db->sock);
690
691     plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
692                                   /* interval = */ NULL, &ud);
693   }
694   else
695   {
696     cj_free (db);
697     return (-1);
698   }
699
700   return (0);
701 }
702  /* }}} int cj_config_add_database */
703
704 static int cj_config (oconfig_item_t *ci) /* {{{ */
705 {
706   int success;
707   int errors;
708   int status;
709   int i;
710
711   success = 0;
712   errors = 0;
713
714   for (i = 0; i < ci->children_num; i++)
715   {
716     oconfig_item_t *child = ci->children + i;
717
718     if (strcasecmp ("Sock", child->key) == 0
719         || strcasecmp ("URL", child->key) == 0)
720     {
721       status = cj_config_add_url (child);
722       if (status == 0)
723         success++;
724       else
725         errors++;
726     }
727     else
728     {
729       WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
730       errors++;
731     }
732   }
733
734   if ((success == 0) && (errors > 0))
735   {
736     ERROR ("curl_json plugin: All statements failed.");
737     return (-1);
738   }
739
740   return (0);
741 } /* }}} int cj_config */
742
743 /* }}} End of configuration handling functions */
744
745 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
746 {
747   value_list_t vl = VALUE_LIST_INIT;
748   char *host;
749
750   vl.values     = value;
751   vl.values_len = 1;
752
753   if ((db->host == NULL)
754       || (strcmp ("", db->host) == 0)
755       || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
756     host = hostname_g;
757   else
758     host = db->host;
759
760   if (key->instance == NULL)
761   {
762     if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
763       sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
764     else
765       ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
766           db->state[db->depth-1].name, db->state[db->depth].name);
767   }
768   else
769     sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
770
771   sstrncpy (vl.host, host, sizeof (vl.host));
772   sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
773   sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
774   sstrncpy (vl.type, key->type, sizeof (vl.type));
775
776   plugin_dispatch_values (&vl);
777 } /* }}} int cj_submit */
778
779 static int cj_sock_perform (cj_t *db) /* {{{ */
780 {
781   char errbuf[1024];
782   struct sockaddr_un sa_unix = {};
783   sa_unix.sun_family = AF_UNIX;
784   sstrncpy (sa_unix.sun_path, db->sock, sizeof (sa_unix.sun_path));
785
786   int fd = socket (AF_UNIX, SOCK_STREAM, 0);
787   if (fd < 0)
788     return (-1);
789   if (connect (fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0)
790   {
791     ERROR ("curl_json plugin: connect(%s) failed: %s",
792            (db->sock != NULL) ? db->sock : "<null>",
793            sstrerror(errno, errbuf, sizeof (errbuf)));
794     close (fd);
795     return (-1);
796   }
797
798   ssize_t red;
799   do {
800     unsigned char buffer[4096];
801     red = read (fd, buffer, sizeof(buffer));
802     if (red < 0) {
803         ERROR ("curl_json plugin: read(%s) failed: %s",
804                (db->sock != NULL) ? db->sock : "<null>",
805                sstrerror(errno, errbuf, sizeof (errbuf)));
806         close (fd);
807         return (-1);
808     }
809     if (!cj_curl_callback (buffer, red, 1, db))
810         break;
811   } while (red > 0);
812   close (fd);
813   return (0);
814 } /* }}} int cj_sock_perform */
815
816
817 static int cj_curl_perform(cj_t *db) /* {{{ */
818 {
819   int status;
820   long rc;
821   char *url;
822   url = NULL;
823   curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
824
825   status = curl_easy_perform (db->curl);
826   if (status != CURLE_OK)
827   {
828     ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
829            status, db->curl_errbuf, (url != NULL) ? url : "<null>");
830     return (-1);
831   }
832
833   curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
834
835   /* The response code is zero if a non-HTTP transport was used. */
836   if ((rc != 0) && (rc != 200))
837   {
838     ERROR ("curl_json plugin: curl_easy_perform failed with "
839         "response code %ld (%s)", rc, url);
840     return (-1);
841   }
842   return (0);
843 } /* }}} int cj_curl_perform */
844
845 static int cj_perform (cj_t *db) /* {{{ */
846 {
847   int status;
848   yajl_handle yprev = db->yajl;
849
850   db->yajl = yajl_alloc (&ycallbacks,
851 #if HAVE_YAJL_V2
852       /* alloc funcs = */ NULL,
853 #else
854       /* alloc funcs = */ NULL, NULL,
855 #endif
856       /* context = */ (void *)db);
857   if (db->yajl == NULL)
858   {
859     ERROR ("curl_json plugin: yajl_alloc failed.");
860     db->yajl = yprev;
861     return (-1);
862   }
863
864   if (db->url)
865     status = cj_curl_perform (db);
866   else
867     status = cj_sock_perform (db);
868   if (status < 0)
869   {
870     yajl_free (db->yajl);
871     db->yajl = yprev;
872     return (-1);
873   }
874
875 #if HAVE_YAJL_V2
876     status = yajl_complete_parse(db->yajl);
877 #else
878     status = yajl_parse_complete(db->yajl);
879 #endif
880   if (status != yajl_status_ok)
881   {
882     unsigned char *errmsg;
883
884     errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
885         /* jsonText = */ NULL, /* jsonTextLen = */ 0);
886     ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
887         (char *) errmsg);
888     yajl_free_error (db->yajl, errmsg);
889     yajl_free (db->yajl);
890     db->yajl = yprev;
891     return (-1);
892   }
893
894   yajl_free (db->yajl);
895   db->yajl = yprev;
896   return (0);
897 } /* }}} int cj_perform */
898
899 static int cj_read (user_data_t *ud) /* {{{ */
900 {
901   cj_t *db;
902
903   if ((ud == NULL) || (ud->data == NULL))
904   {
905     ERROR ("curl_json plugin: cj_read: Invalid user data.");
906     return (-1);
907   }
908
909   db = (cj_t *) ud->data;
910
911   db->depth = 0;
912   memset (&db->state, 0, sizeof(db->state));
913   db->state[db->depth].tree = db->tree;
914   db->key = NULL;
915
916   return cj_perform (db);
917 } /* }}} int cj_read */
918
919 void module_register (void)
920 {
921   plugin_register_complex_config ("curl_json", cj_config);
922 } /* void module_register */
923
924 /* vim: set sw=2 sts=2 et fdm=marker : */