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