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