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