253ca27d9fda5ffafdfdd9f059b600b7b3be2b10
[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.", db->url ? db->url : db->sock);
270     return (CJ_CB_ABORT);
271   }
272   return (CJ_CB_CONTINUE);
273 }
274
275 static int cj_cb_end (void *ctx)
276 {
277   cj_t *db = (cj_t *)ctx;
278   db->state[db->depth].tree = NULL;
279   --db->depth;
280   return (CJ_CB_CONTINUE);
281 }
282
283 static int cj_cb_start_map (void *ctx)
284 {
285   return cj_cb_start (ctx);
286 }
287
288 static int cj_cb_end_map (void *ctx)
289 {
290   return cj_cb_end (ctx);
291 }
292
293 static int cj_cb_start_array (void * ctx)
294 {
295   return cj_cb_start (ctx);
296 }
297
298 static int cj_cb_end_array (void * ctx)
299 {
300   return cj_cb_end (ctx);
301 }
302
303 static yajl_callbacks ycallbacks = {
304   NULL, /* null */
305   NULL, /* boolean */
306   NULL, /* integer */
307   NULL, /* double */
308   cj_cb_number,
309   cj_cb_string,
310   cj_cb_start_map,
311   cj_cb_map_key,
312   cj_cb_end_map,
313   cj_cb_start_array,
314   cj_cb_end_array
315 };
316
317 /* end yajl callbacks */
318
319 static void cj_key_free (cj_key_t *key) /* {{{ */
320 {
321   if (key == NULL)
322     return;
323
324   sfree (key->path);
325   sfree (key->type);
326   sfree (key->instance);
327
328   sfree (key);
329 } /* }}} void cj_key_free */
330
331 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
332 {
333   char *name;
334   void *value;
335
336   while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
337   {
338     cj_key_t *key = (cj_key_t *)value;
339
340     if (CJ_IS_KEY(key))
341       cj_key_free (key);
342     else
343       cj_tree_free ((c_avl_tree_t *)value);
344
345     sfree (name);
346   }
347
348   c_avl_destroy (tree);
349 } /* }}} void cj_tree_free */
350
351 static void cj_free (void *arg) /* {{{ */
352 {
353   cj_t *db;
354
355   DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
356
357   db = (cj_t *) arg;
358
359   if (db == NULL)
360     return;
361
362   if (db->curl != NULL)
363     curl_easy_cleanup (db->curl);
364   db->curl = NULL;
365
366   if (db->tree != NULL)
367     cj_tree_free (db->tree);
368   db->tree = NULL;
369
370   sfree (db->instance);
371   sfree (db->host);
372
373   sfree (db->url);
374   sfree (db->user);
375   sfree (db->pass);
376   sfree (db->credentials);
377   sfree (db->cacert);
378   sfree (db->post_body);
379   curl_slist_free_all (db->headers);
380
381   sfree (db);
382 } /* }}} void cj_free */
383
384 /* Configuration handling functions {{{ */
385
386 static c_avl_tree_t *cj_avl_create(void)
387 {
388   return c_avl_create ((int (*) (const void *, const void *)) strcmp);
389 }
390
391 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
392     oconfig_item_t *ci)
393 {
394   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
395   {
396     WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
397     return (-1);
398   }
399
400   *dest = curl_slist_append(*dest, ci->values[0].value.string);
401   if (*dest == NULL)
402     return (-1);
403
404   return (0);
405 } /* }}} int cj_config_append_string */
406
407 static int cj_config_add_key (cj_t *db, /* {{{ */
408                                    oconfig_item_t *ci)
409 {
410   cj_key_t *key;
411   int status;
412   int i;
413
414   if ((ci->values_num != 1)
415       || (ci->values[0].type != OCONFIG_TYPE_STRING))
416   {
417     WARNING ("curl_json plugin: The `Key' block "
418              "needs exactly one string argument.");
419     return (-1);
420   }
421
422   key = (cj_key_t *) malloc (sizeof (*key));
423   if (key == NULL)
424   {
425     ERROR ("curl_json plugin: malloc failed.");
426     return (-1);
427   }
428   memset (key, 0, sizeof (*key));
429   key->magic = CJ_KEY_MAGIC;
430
431   if (strcasecmp ("Key", ci->key) == 0)
432   {
433     status = cf_util_get_string (ci, &key->path);
434     if (status != 0)
435     {
436       sfree (key);
437       return (status);
438     }
439   }
440   else
441   {
442     ERROR ("curl_json plugin: cj_config: "
443            "Invalid key: %s", ci->key);
444     return (-1);
445   }
446
447   status = 0;
448   for (i = 0; i < ci->children_num; i++)
449   {
450     oconfig_item_t *child = ci->children + i;
451
452     if (strcasecmp ("Type", child->key) == 0)
453       status = cf_util_get_string (child, &key->type);
454     else if (strcasecmp ("Instance", child->key) == 0)
455       status = cf_util_get_string (child, &key->instance);
456     else
457     {
458       WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
459       status = -1;
460     }
461
462     if (status != 0)
463       break;
464   } /* for (i = 0; i < ci->children_num; i++) */
465
466   while (status == 0)
467   {
468     if (key->type == NULL)
469     {
470       WARNING ("curl_json plugin: `Type' missing in `Key' block.");
471       status = -1;
472     }
473
474     break;
475   } /* while (status == 0) */
476
477   /* store path in a tree that will match the json map structure, example:
478    * "httpd/requests/count",
479    * "httpd/requests/current" ->
480    * { "httpd": { "requests": { "count": $key, "current": $key } } }
481    */
482   if (status == 0)
483   {
484     char *ptr;
485     char *name;
486     char ent[PATH_MAX];
487     c_avl_tree_t *tree;
488
489     if (db->tree == NULL)
490       db->tree = cj_avl_create();
491
492     tree = db->tree;
493     name = key->path;
494     ptr = key->path;
495     if (*ptr == '/')
496       ++ptr;
497
498     name = ptr;
499     while (*ptr)
500     {
501       if (*ptr == '/')
502       {
503         c_avl_tree_t *value;
504         int len;
505
506         len = ptr-name;
507         if (len == 0)
508           break;
509         sstrncpy (ent, name, len+1);
510
511         if (c_avl_get (tree, ent, (void *) &value) != 0)
512         {
513           value = cj_avl_create ();
514           c_avl_insert (tree, strdup (ent), value);
515         }
516
517         tree = value;
518         name = ptr+1;
519       }
520       ++ptr;
521     }
522     if (*name)
523       c_avl_insert (tree, strdup(name), key);
524     else
525     {
526       ERROR ("curl_json plugin: invalid key: %s", key->path);
527       status = -1;
528     }
529   }
530
531   return (status);
532 } /* }}} int cj_config_add_key */
533
534 static int cj_init_curl (cj_t *db) /* {{{ */
535 {
536   db->curl = curl_easy_init ();
537   if (db->curl == NULL)
538   {
539     ERROR ("curl_json plugin: curl_easy_init failed.");
540     return (-1);
541   }
542
543   curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
544   curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
545   curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
546   curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
547                     PACKAGE_NAME"/"PACKAGE_VERSION);
548   curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
549   curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
550
551   if (db->user != NULL)
552   {
553     size_t credentials_size;
554
555     credentials_size = strlen (db->user) + 2;
556     if (db->pass != NULL)
557       credentials_size += strlen (db->pass);
558
559     db->credentials = (char *) malloc (credentials_size);
560     if (db->credentials == NULL)
561     {
562       ERROR ("curl_json plugin: malloc failed.");
563       return (-1);
564     }
565
566     ssnprintf (db->credentials, credentials_size, "%s:%s",
567                db->user, (db->pass == NULL) ? "" : db->pass);
568     curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
569   }
570
571   curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
572   curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
573                     db->verify_host ? 2L : 0L);
574   if (db->cacert != NULL)
575     curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
576   if (db->headers != NULL)
577     curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
578   if (db->post_body != NULL)
579     curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
580
581   return (0);
582 } /* }}} int cj_init_curl */
583
584 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
585 {
586   cj_t *db;
587   int status = 0;
588   int i;
589
590   if ((ci->values_num != 1)
591       || (ci->values[0].type != OCONFIG_TYPE_STRING))
592   {
593     WARNING ("curl_json plugin: The `URL' block "
594              "needs exactly one string argument.");
595     return (-1);
596   }
597
598   db = (cj_t *) malloc (sizeof (*db));
599   if (db == NULL)
600   {
601     ERROR ("curl_json plugin: malloc failed.");
602     return (-1);
603   }
604   memset (db, 0, sizeof (*db));
605
606   if (strcasecmp ("URL", ci->key) == 0)
607     status = cf_util_get_string (ci, &db->url);
608   else if (strcasecmp ("Sock", ci->key) == 0)
609     status = cf_util_get_string (ci, &db->sock);
610   else
611   {
612     ERROR ("curl_json plugin: cj_config: "
613            "Invalid key: %s", ci->key);
614     return (-1);
615   }
616   if (status != 0)
617   {
618     sfree (db);
619     return (status);
620   }
621
622   /* Fill the `cj_t' structure.. */
623   for (i = 0; i < ci->children_num; i++)
624   {
625     oconfig_item_t *child = ci->children + i;
626
627     if (strcasecmp ("Instance", child->key) == 0)
628       status = cf_util_get_string (child, &db->instance);
629     else if (strcasecmp ("Host", child->key) == 0)
630       status = cf_util_get_string (child, &db->host);
631     else if (db->url && strcasecmp ("User", child->key) == 0)
632       status = cf_util_get_string (child, &db->user);
633     else if (db->url && strcasecmp ("Password", child->key) == 0)
634       status = cf_util_get_string (child, &db->pass);
635     else if (db->url && strcasecmp ("VerifyPeer", child->key) == 0)
636       status = cf_util_get_boolean (child, &db->verify_peer);
637     else if (db->url && strcasecmp ("VerifyHost", child->key) == 0)
638       status = cf_util_get_boolean (child, &db->verify_host);
639     else if (db->url && strcasecmp ("CACert", child->key) == 0)
640       status = cf_util_get_string (child, &db->cacert);
641     else if (db->url && strcasecmp ("Header", child->key) == 0)
642       status = cj_config_append_string ("Header", &db->headers, child);
643     else if (db->url && strcasecmp ("Post", child->key) == 0)
644       status = cf_util_get_string (child, &db->post_body);
645     else if (strcasecmp ("Key", child->key) == 0)
646       status = cj_config_add_key (db, child);
647     else
648     {
649       WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
650       status = -1;
651     }
652
653     if (status != 0)
654       break;
655   }
656
657   if (status == 0)
658   {
659     if (db->tree == NULL)
660     {
661       WARNING ("curl_json plugin: No (valid) `Key' block "
662                "within `%s' block `%s'.", db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
663       status = -1;
664     }
665     if (status == 0 && db->url)
666       status = cj_init_curl (db);
667   }
668
669   /* If all went well, register this database for reading */
670   if (status == 0)
671   {
672     user_data_t ud;
673     char cb_name[DATA_MAX_NAME_LEN];
674
675     if (db->instance == NULL)
676       db->instance = strdup("default");
677
678     DEBUG ("curl_json plugin: Registering new read callback: %s",
679            db->instance);
680
681     memset (&ud, 0, sizeof (ud));
682     ud.data = (void *) db;
683     ud.free_func = cj_free;
684
685     ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
686                db->instance, db->url ? db->url : db->sock);
687
688     plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
689                                   /* interval = */ NULL, &ud);
690   }
691   else
692   {
693     cj_free (db);
694     return (-1);
695   }
696
697   return (0);
698 }
699  /* }}} int cj_config_add_database */
700
701 static int cj_config (oconfig_item_t *ci) /* {{{ */
702 {
703   int success;
704   int errors;
705   int status;
706   int i;
707
708   success = 0;
709   errors = 0;
710
711   for (i = 0; i < ci->children_num; i++)
712   {
713     oconfig_item_t *child = ci->children + i;
714
715     if (strcasecmp ("Sock", child->key) == 0 || strcasecmp ("URL", child->key) == 0)
716     {
717       status = cj_config_add_url (child);
718       if (status == 0)
719         success++;
720       else
721         errors++;
722     }
723     else
724     {
725       WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
726       errors++;
727     }
728   }
729
730   if ((success == 0) && (errors > 0))
731   {
732     ERROR ("curl_json plugin: All statements failed.");
733     return (-1);
734   }
735
736   return (0);
737 } /* }}} int cj_config */
738
739 /* }}} End of configuration handling functions */
740
741 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
742 {
743   value_list_t vl = VALUE_LIST_INIT;
744   char *host;
745
746   vl.values     = value;
747   vl.values_len = 1;
748
749   if ((db->host == NULL)
750       || (strcmp ("", db->host) == 0)
751       || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
752     host = hostname_g;
753   else
754     host = db->host;
755
756   if (key->instance == NULL)
757   {
758     if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
759       sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
760     else
761       ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
762           db->state[db->depth-1].name, db->state[db->depth].name);
763   }
764   else
765     sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
766
767   sstrncpy (vl.host, host, sizeof (vl.host));
768   sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
769   sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
770   sstrncpy (vl.type, key->type, sizeof (vl.type));
771
772   plugin_dispatch_values (&vl);
773 } /* }}} int cj_submit */
774
775 static int cj_sock_perform (cj_t *db) /* {{{ */
776 {
777   char errbuf[1024];
778   struct sockaddr_un sa_unix = {};
779   sa_unix.sun_family = AF_UNIX;
780   sstrncpy (sa_unix.sun_path, db->sock, sizeof (sa_unix.sun_path));
781
782   int fd = socket (AF_UNIX, SOCK_STREAM, 0);
783   if (fd < 0)
784     return (-1);
785   if (connect (fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0)
786   {
787     ERROR ("curl_json plugin: connect(%s) failed: %s",
788            (db->sock != NULL) ? db->sock : "<null>",
789            sstrerror(errno, errbuf, sizeof (errbuf)));
790     close (fd);
791     return (-1);
792   }
793
794   ssize_t red;
795   do {
796     unsigned char buffer[4096];
797     red = read (fd, buffer, sizeof(buffer));
798     if (red < 0) {
799         ERROR ("curl_json plugin: read(%s) failed: %s",
800                (db->sock != NULL) ? db->sock : "<null>",
801                sstrerror(errno, errbuf, sizeof (errbuf)));
802         close (fd);
803         return (-1);
804     }
805     if (!cj_curl_callback (buffer, red, 1, db))
806         break;
807   } while (red > 0);
808   close (fd);
809   return (0);
810 } /* }}} int cj_sock_perform */
811
812
813 static int cj_curl_perform(cj_t *db) /* {{{ */
814 {
815   int status;
816   long rc;
817   char *url;
818   url = NULL;
819   curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
820
821   status = curl_easy_perform (db->curl);
822   if (status != CURLE_OK)
823   {
824     ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
825            status, db->curl_errbuf, (url != NULL) ? url : "<null>");
826     return (-1);
827   }
828
829   curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
830
831   /* The response code is zero if a non-HTTP transport was used. */
832   if ((rc != 0) && (rc != 200))
833   {
834     ERROR ("curl_json plugin: curl_easy_perform failed with "
835         "response code %ld (%s)", rc, url);
836     return (-1);
837   }
838   return (0);
839 } /* }}} int cj_curl_perform */
840
841 static int cj_perform (cj_t *db) /* {{{ */
842 {
843   int status;
844   yajl_handle yprev = db->yajl;
845
846   db->yajl = yajl_alloc (&ycallbacks,
847 #if HAVE_YAJL_V2
848       /* alloc funcs = */ NULL,
849 #else
850       /* alloc funcs = */ NULL, NULL,
851 #endif
852       /* context = */ (void *)db);
853   if (db->yajl == NULL)
854   {
855     ERROR ("curl_json plugin: yajl_alloc failed.");
856     db->yajl = yprev;
857     return (-1);
858   }
859
860   if (db->url ? cj_curl_perform (db) < 0 : cj_sock_perform (db) < 0)
861   {
862     yajl_free (db->yajl);
863     db->yajl = yprev;
864     return (-1);
865   }
866
867 #if HAVE_YAJL_V2
868     status = yajl_complete_parse(db->yajl);
869 #else
870     status = yajl_parse_complete(db->yajl);
871 #endif
872   if (status != yajl_status_ok)
873   {
874     unsigned char *errmsg;
875
876     errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
877         /* jsonText = */ NULL, /* jsonTextLen = */ 0);
878     ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
879         (char *) errmsg);
880     yajl_free_error (db->yajl, errmsg);
881     yajl_free (db->yajl);
882     db->yajl = yprev;
883     return (-1);
884   }
885
886   yajl_free (db->yajl);
887   db->yajl = yprev;
888   return (0);
889 } /* }}} int cj_perform */
890
891 static int cj_read (user_data_t *ud) /* {{{ */
892 {
893   cj_t *db;
894
895   if ((ud == NULL) || (ud->data == NULL))
896   {
897     ERROR ("curl_json plugin: cj_read: Invalid user data.");
898     return (-1);
899   }
900
901   db = (cj_t *) ud->data;
902
903   db->depth = 0;
904   memset (&db->state, 0, sizeof(db->state));
905   db->state[db->depth].tree = db->tree;
906   db->key = NULL;
907
908   return cj_perform (db);
909 } /* }}} int cj_read */
910
911 void module_register (void)
912 {
913   plugin_register_complex_config ("curl_json", cj_config);
914 } /* void module_register */
915
916 /* vim: set sw=2 sts=2 et fdm=marker : */