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