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 verify_peer;
75 _Bool verify_host;
76 char *cacert;
77 struct curl_slist *headers;
78 char *post_body;
80 CURL *curl;
81 char curl_errbuf[CURL_ERROR_SIZE];
83 yajl_handle yajl;
84 c_avl_tree_t *tree;
85 cj_key_t *key;
86 int depth;
87 struct {
88 union {
89 c_avl_tree_t *tree;
90 cj_key_t *key;
91 };
92 _Bool in_array;
93 int index;
94 char name[DATA_MAX_NAME_LEN];
95 } state[YAJL_MAX_DEPTH];
96 };
97 typedef struct cj_s cj_t; /* }}} */
99 #if HAVE_YAJL_V2
100 typedef size_t yajl_len_t;
101 #else
102 typedef unsigned int yajl_len_t;
103 #endif
105 static int cj_read (user_data_t *ud);
106 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
108 static size_t cj_curl_callback (void *buf, /* {{{ */
109 size_t size, size_t nmemb, void *user_data)
110 {
111 cj_t *db;
112 size_t len;
113 yajl_status status;
115 len = size * nmemb;
117 if (len <= 0)
118 return (len);
120 db = user_data;
121 if (db == NULL)
122 return (0);
124 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
125 if (status == yajl_status_ok)
126 return (len);
127 #if !HAVE_YAJL_V2
128 else if (status == yajl_status_insufficient_data)
129 return (len);
130 #endif
132 unsigned char *msg = yajl_get_error(db->yajl, /* verbose = */ 1,
133 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
134 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
135 yajl_free_error(db->yajl, msg);
136 return (0); /* abort write callback */
137 } /* }}} size_t cj_curl_callback */
139 static int cj_get_type (cj_key_t *key)
140 {
141 const data_set_t *ds;
143 ds = plugin_get_ds (key->type);
144 if (ds == NULL)
145 {
146 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
148 assert (key->type != NULL);
149 if (strcmp (type, key->type) != 0)
150 {
151 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
152 key->type);
153 sstrncpy (type, key->type, sizeof (type));
154 }
156 return -1;
157 }
158 else if (ds->ds_num > 1)
159 {
160 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
162 c_complain_once (LOG_WARNING, &complaint,
163 "curl_json plugin: The type \"%s\" has more than one data source. "
164 "This is currently not supported. I will return the type of the "
165 "first data source, but this will likely lead to problems later on.",
166 key->type);
167 }
169 return ds->ds[0].type;
170 }
172 static int cj_cb_map_key (void *ctx, const unsigned char *val,
173 yajl_len_t len);
175 static void cj_cb_inc_array_index (void *ctx, _Bool update_key)
176 {
177 cj_t *db = (cj_t *)ctx;
179 if (!db->state[db->depth].in_array)
180 return;
182 db->state[db->depth].index++;
184 if (update_key)
185 {
186 char name[DATA_MAX_NAME_LEN];
188 ssnprintf (name, sizeof (name), "%d", db->state[db->depth].index - 1);
190 cj_cb_map_key (ctx, (unsigned char *)name, (yajl_len_t) strlen (name));
191 }
192 }
194 /* yajl callbacks */
195 #define CJ_CB_ABORT 0
196 #define CJ_CB_CONTINUE 1
198 static int cj_cb_boolean (void * ctx, int boolVal)
199 {
200 cj_cb_inc_array_index (ctx, /* update_key = */ 0);
201 return (CJ_CB_CONTINUE);
202 }
204 static int cj_cb_null (void * ctx)
205 {
206 cj_cb_inc_array_index (ctx, /* update_key = */ 0);
207 return (CJ_CB_CONTINUE);
208 }
210 static int cj_cb_number (void *ctx,
211 const char *number, yajl_len_t number_len)
212 {
213 char buffer[number_len + 1];
215 cj_t *db = (cj_t *)ctx;
216 cj_key_t *key = db->state[db->depth].key;
217 value_t vt;
218 int type;
219 int status;
221 /* Create a null-terminated version of the string. */
222 memcpy (buffer, number, number_len);
223 buffer[sizeof (buffer) - 1] = 0;
225 if ((key == NULL) || !CJ_IS_KEY (key)) {
226 if (key != NULL)
227 NOTICE ("curl_json plugin: Found \"%s\", but the configuration expects"
228 " a map.", buffer);
229 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
230 key = db->state[db->depth].key;
231 if (key == NULL) {
232 return (CJ_CB_CONTINUE);
233 }
234 }
235 else
236 {
237 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
238 }
240 type = cj_get_type (key);
241 status = parse_value (buffer, &vt, type);
242 if (status != 0)
243 {
244 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
245 return (CJ_CB_CONTINUE);
246 }
248 cj_submit (db, key, &vt);
249 return (CJ_CB_CONTINUE);
250 } /* int cj_cb_number */
252 /* Queries the key-tree of the parent context for "in_name" and, if found,
253 * updates the "key" field of the current context. Otherwise, "key" is set to
254 * NULL. */
255 static int cj_cb_map_key (void *ctx,
256 unsigned char const *in_name, yajl_len_t in_name_len)
257 {
258 cj_t *db = (cj_t *)ctx;
259 c_avl_tree_t *tree;
261 tree = db->state[db->depth-1].tree;
263 if (tree != NULL)
264 {
265 cj_key_t *value = NULL;
266 char *name;
267 size_t name_len;
269 /* Create a null-terminated version of the name. */
270 name = db->state[db->depth].name;
271 name_len = COUCH_MIN ((size_t) in_name_len,
272 sizeof (db->state[db->depth].name) - 1);
273 memcpy (name, in_name, name_len);
274 name[name_len] = 0;
276 if (c_avl_get (tree, name, (void *) &value) == 0) {
277 if (CJ_IS_KEY((cj_key_t*)value)) {
278 db->state[db->depth].key = value;
279 }
280 else {
281 db->state[db->depth].tree = (c_avl_tree_t*) value;
282 }
283 }
284 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
285 if (CJ_IS_KEY((cj_key_t*)value)) {
286 db->state[db->depth].key = value;
287 }
288 else {
289 db->state[db->depth].tree = (c_avl_tree_t*) value;
290 }
291 else
292 db->state[db->depth].key = NULL;
293 }
295 return (CJ_CB_CONTINUE);
296 }
298 static int cj_cb_string (void *ctx, const unsigned char *val,
299 yajl_len_t len)
300 {
301 /* Handle the string as if it was a number. */
302 return (cj_cb_number (ctx, (const char *) val, len));
303 } /* int cj_cb_string */
305 static int cj_cb_start (void *ctx)
306 {
307 cj_t *db = (cj_t *)ctx;
308 if (++db->depth >= YAJL_MAX_DEPTH)
309 {
310 ERROR ("curl_json plugin: %s depth exceeds max, aborting.",
311 db->url ? db->url : db->sock);
312 return (CJ_CB_ABORT);
313 }
314 return (CJ_CB_CONTINUE);
315 }
317 static int cj_cb_end (void *ctx)
318 {
319 cj_t *db = (cj_t *)ctx;
320 db->state[db->depth].tree = NULL;
321 --db->depth;
322 return (CJ_CB_CONTINUE);
323 }
325 static int cj_cb_start_map (void *ctx)
326 {
327 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
328 return cj_cb_start (ctx);
329 }
331 static int cj_cb_end_map (void *ctx)
332 {
333 return cj_cb_end (ctx);
334 }
336 static int cj_cb_start_array (void * ctx)
337 {
338 cj_t *db = (cj_t *)ctx;
339 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
340 if (db->depth+1 < YAJL_MAX_DEPTH) {
341 db->state[db->depth+1].in_array = 1;
342 db->state[db->depth+1].index = 0;
343 }
344 return cj_cb_start (ctx);
345 }
347 static int cj_cb_end_array (void * ctx)
348 {
349 cj_t *db = (cj_t *)ctx;
350 db->state[db->depth].in_array = 0;
351 return cj_cb_end (ctx);
352 }
354 static yajl_callbacks ycallbacks = {
355 cj_cb_null, /* null */
356 cj_cb_boolean, /* boolean */
357 NULL, /* integer */
358 NULL, /* double */
359 cj_cb_number,
360 cj_cb_string,
361 cj_cb_start_map,
362 cj_cb_map_key,
363 cj_cb_end_map,
364 cj_cb_start_array,
365 cj_cb_end_array
366 };
368 /* end yajl callbacks */
370 static void cj_key_free (cj_key_t *key) /* {{{ */
371 {
372 if (key == NULL)
373 return;
375 sfree (key->path);
376 sfree (key->type);
377 sfree (key->instance);
379 sfree (key);
380 } /* }}} void cj_key_free */
382 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
383 {
384 char *name;
385 void *value;
387 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
388 {
389 cj_key_t *key = (cj_key_t *)value;
391 if (CJ_IS_KEY(key))
392 cj_key_free (key);
393 else
394 cj_tree_free ((c_avl_tree_t *)value);
396 sfree (name);
397 }
399 c_avl_destroy (tree);
400 } /* }}} void cj_tree_free */
402 static void cj_free (void *arg) /* {{{ */
403 {
404 cj_t *db;
406 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
408 db = (cj_t *) arg;
410 if (db == NULL)
411 return;
413 if (db->curl != NULL)
414 curl_easy_cleanup (db->curl);
415 db->curl = NULL;
417 if (db->tree != NULL)
418 cj_tree_free (db->tree);
419 db->tree = NULL;
421 sfree (db->instance);
422 sfree (db->host);
424 sfree (db->sock);
426 sfree (db->url);
427 sfree (db->user);
428 sfree (db->pass);
429 sfree (db->credentials);
430 sfree (db->cacert);
431 sfree (db->post_body);
432 curl_slist_free_all (db->headers);
434 sfree (db);
435 } /* }}} void cj_free */
437 /* Configuration handling functions {{{ */
439 static c_avl_tree_t *cj_avl_create(void)
440 {
441 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
442 }
444 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
445 oconfig_item_t *ci)
446 {
447 struct curl_slist *temp = NULL;
448 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
449 {
450 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
451 return (-1);
452 }
454 temp = curl_slist_append(*dest, ci->values[0].value.string);
455 if (temp == NULL)
456 return (-1);
458 *dest = temp;
460 return (0);
461 } /* }}} int cj_config_append_string */
463 static int cj_config_add_key (cj_t *db, /* {{{ */
464 oconfig_item_t *ci)
465 {
466 cj_key_t *key;
467 int status;
468 int i;
470 if ((ci->values_num != 1)
471 || (ci->values[0].type != OCONFIG_TYPE_STRING))
472 {
473 WARNING ("curl_json plugin: The `Key' block "
474 "needs exactly one string argument.");
475 return (-1);
476 }
478 key = (cj_key_t *) malloc (sizeof (*key));
479 if (key == NULL)
480 {
481 ERROR ("curl_json plugin: malloc failed.");
482 return (-1);
483 }
484 memset (key, 0, sizeof (*key));
485 key->magic = CJ_KEY_MAGIC;
487 if (strcasecmp ("Key", ci->key) == 0)
488 {
489 status = cf_util_get_string (ci, &key->path);
490 if (status != 0)
491 {
492 sfree (key);
493 return (status);
494 }
495 }
496 else
497 {
498 ERROR ("curl_json plugin: cj_config: "
499 "Invalid key: %s", ci->key);
500 cj_key_free (key);
501 return (-1);
502 }
504 status = 0;
505 for (i = 0; i < ci->children_num; i++)
506 {
507 oconfig_item_t *child = ci->children + i;
509 if (strcasecmp ("Type", child->key) == 0)
510 status = cf_util_get_string (child, &key->type);
511 else if (strcasecmp ("Instance", child->key) == 0)
512 status = cf_util_get_string (child, &key->instance);
513 else
514 {
515 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
516 status = -1;
517 }
519 if (status != 0)
520 break;
521 } /* for (i = 0; i < ci->children_num; i++) */
523 if (status != 0)
524 {
525 cj_key_free (key);
526 return (-1);
527 }
529 if (key->type == NULL)
530 {
531 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
532 cj_key_free (key);
533 return (-1);
534 }
536 /* store path in a tree that will match the json map structure, example:
537 * "httpd/requests/count",
538 * "httpd/requests/current" ->
539 * { "httpd": { "requests": { "count": $key, "current": $key } } }
540 */
541 char *ptr;
542 char *name;
543 c_avl_tree_t *tree;
545 if (db->tree == NULL)
546 db->tree = cj_avl_create();
548 tree = db->tree;
549 ptr = key->path;
550 if (*ptr == '/')
551 ++ptr;
553 name = ptr;
554 while ((ptr = strchr (name, '/')) != NULL)
555 {
556 char ent[PATH_MAX];
557 c_avl_tree_t *value;
558 size_t len;
560 len = ptr - name;
561 if (len == 0)
562 break;
564 len = COUCH_MIN(len, sizeof (ent)-1);
565 sstrncpy (ent, name, len+1);
567 if (c_avl_get (tree, ent, (void *) &value) != 0)
568 {
569 value = cj_avl_create ();
570 c_avl_insert (tree, strdup (ent), value);
571 }
573 tree = value;
574 name = ptr + 1;
575 }
577 if (strlen (name) == 0)
578 {
579 ERROR ("curl_json plugin: invalid key: %s", key->path);
580 cj_key_free (key);
581 return (-1);
582 }
584 c_avl_insert (tree, strdup (name), key);
585 return (status);
586 } /* }}} int cj_config_add_key */
588 static int cj_init_curl (cj_t *db) /* {{{ */
589 {
590 db->curl = curl_easy_init ();
591 if (db->curl == NULL)
592 {
593 ERROR ("curl_json plugin: curl_easy_init failed.");
594 return (-1);
595 }
597 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
598 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
599 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
600 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
601 PACKAGE_NAME"/"PACKAGE_VERSION);
602 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
603 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
605 if (db->user != NULL)
606 {
607 size_t credentials_size;
609 credentials_size = strlen (db->user) + 2;
610 if (db->pass != NULL)
611 credentials_size += strlen (db->pass);
613 db->credentials = (char *) malloc (credentials_size);
614 if (db->credentials == NULL)
615 {
616 ERROR ("curl_json plugin: malloc failed.");
617 return (-1);
618 }
620 ssnprintf (db->credentials, credentials_size, "%s:%s",
621 db->user, (db->pass == NULL) ? "" : db->pass);
622 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
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 cj_free (db);
669 return (-1);
670 }
671 if (status != 0)
672 {
673 sfree (db);
674 return (status);
675 }
677 /* Fill the `cj_t' structure.. */
678 for (i = 0; i < ci->children_num; i++)
679 {
680 oconfig_item_t *child = ci->children + i;
682 if (strcasecmp ("Instance", child->key) == 0)
683 status = cf_util_get_string (child, &db->instance);
684 else if (strcasecmp ("Host", child->key) == 0)
685 status = cf_util_get_string (child, &db->host);
686 else if (db->url && strcasecmp ("User", child->key) == 0)
687 status = cf_util_get_string (child, &db->user);
688 else if (db->url && strcasecmp ("Password", child->key) == 0)
689 status = cf_util_get_string (child, &db->pass);
690 else if (db->url && strcasecmp ("VerifyPeer", child->key) == 0)
691 status = cf_util_get_boolean (child, &db->verify_peer);
692 else if (db->url && strcasecmp ("VerifyHost", child->key) == 0)
693 status = cf_util_get_boolean (child, &db->verify_host);
694 else if (db->url && strcasecmp ("CACert", child->key) == 0)
695 status = cf_util_get_string (child, &db->cacert);
696 else if (db->url && strcasecmp ("Header", child->key) == 0)
697 status = cj_config_append_string ("Header", &db->headers, child);
698 else if (db->url && strcasecmp ("Post", child->key) == 0)
699 status = cf_util_get_string (child, &db->post_body);
700 else if (strcasecmp ("Key", child->key) == 0)
701 status = cj_config_add_key (db, child);
702 else
703 {
704 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
705 status = -1;
706 }
708 if (status != 0)
709 break;
710 }
712 if (status == 0)
713 {
714 if (db->tree == NULL)
715 {
716 WARNING ("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".",
717 db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
718 status = -1;
719 }
720 if (status == 0 && db->url)
721 status = cj_init_curl (db);
722 }
724 /* If all went well, register this database for reading */
725 if (status == 0)
726 {
727 user_data_t ud;
728 char *cb_name;
730 if (db->instance == NULL)
731 db->instance = strdup("default");
733 DEBUG ("curl_json plugin: Registering new read callback: %s",
734 db->instance);
736 memset (&ud, 0, sizeof (ud));
737 ud.data = (void *) db;
738 ud.free_func = cj_free;
740 cb_name = ssnprintf_alloc ("curl_json-%s-%s",
741 db->instance, db->url ? db->url : db->sock);
743 plugin_register_complex_read (/* group = */ "curl_json", cb_name, cj_read,
744 /* interval = */ NULL, &ud);
745 sfree (cb_name);
746 }
747 else
748 {
749 cj_free (db);
750 return (-1);
751 }
753 return (0);
754 }
755 /* }}} int cj_config_add_database */
757 static int cj_config (oconfig_item_t *ci) /* {{{ */
758 {
759 int success;
760 int errors;
761 int status;
762 int i;
764 success = 0;
765 errors = 0;
767 for (i = 0; i < ci->children_num; i++)
768 {
769 oconfig_item_t *child = ci->children + i;
771 if (strcasecmp ("Sock", child->key) == 0
772 || strcasecmp ("URL", child->key) == 0)
773 {
774 status = cj_config_add_url (child);
775 if (status == 0)
776 success++;
777 else
778 errors++;
779 }
780 else
781 {
782 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
783 errors++;
784 }
785 }
787 if ((success == 0) && (errors > 0))
788 {
789 ERROR ("curl_json plugin: All statements failed.");
790 return (-1);
791 }
793 return (0);
794 } /* }}} int cj_config */
796 /* }}} End of configuration handling functions */
798 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
799 {
800 value_list_t vl = VALUE_LIST_INIT;
801 char *host;
803 vl.values = value;
804 vl.values_len = 1;
806 if ((db->host == NULL)
807 || (strcmp ("", db->host) == 0)
808 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
809 host = hostname_g;
810 else
811 host = db->host;
813 if (key->instance == NULL)
814 {
815 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
816 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
817 else
818 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
819 db->state[db->depth-1].name, db->state[db->depth].name);
820 }
821 else
822 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
824 sstrncpy (vl.host, host, sizeof (vl.host));
825 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
826 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
827 sstrncpy (vl.type, key->type, sizeof (vl.type));
829 plugin_dispatch_values (&vl);
830 } /* }}} int cj_submit */
832 static int cj_sock_perform (cj_t *db) /* {{{ */
833 {
834 char errbuf[1024];
835 struct sockaddr_un sa_unix = {};
836 sa_unix.sun_family = AF_UNIX;
837 sstrncpy (sa_unix.sun_path, db->sock, sizeof (sa_unix.sun_path));
839 int fd = socket (AF_UNIX, SOCK_STREAM, 0);
840 if (fd < 0)
841 return (-1);
842 if (connect (fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0)
843 {
844 ERROR ("curl_json plugin: connect(%s) failed: %s",
845 (db->sock != NULL) ? db->sock : "<null>",
846 sstrerror(errno, errbuf, sizeof (errbuf)));
847 close (fd);
848 return (-1);
849 }
851 ssize_t red;
852 do {
853 unsigned char buffer[4096];
854 red = read (fd, buffer, sizeof(buffer));
855 if (red < 0) {
856 ERROR ("curl_json plugin: read(%s) failed: %s",
857 (db->sock != NULL) ? db->sock : "<null>",
858 sstrerror(errno, errbuf, sizeof (errbuf)));
859 close (fd);
860 return (-1);
861 }
862 if (!cj_curl_callback (buffer, red, 1, db))
863 break;
864 } while (red > 0);
865 close (fd);
866 return (0);
867 } /* }}} int cj_sock_perform */
870 static int cj_curl_perform(cj_t *db) /* {{{ */
871 {
872 int status;
873 long rc;
874 char *url;
875 url = db->url;
877 status = curl_easy_perform (db->curl);
878 if (status != CURLE_OK)
879 {
880 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
881 status, db->curl_errbuf, url);
882 return (-1);
883 }
885 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
886 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
888 /* The response code is zero if a non-HTTP transport was used. */
889 if ((rc != 0) && (rc != 200))
890 {
891 ERROR ("curl_json plugin: curl_easy_perform failed with "
892 "response code %ld (%s)", rc, url);
893 return (-1);
894 }
895 return (0);
896 } /* }}} int cj_curl_perform */
898 static int cj_perform (cj_t *db) /* {{{ */
899 {
900 int status;
901 yajl_handle yprev = db->yajl;
903 db->yajl = yajl_alloc (&ycallbacks,
904 #if HAVE_YAJL_V2
905 /* alloc funcs = */ NULL,
906 #else
907 /* alloc funcs = */ NULL, NULL,
908 #endif
909 /* context = */ (void *)db);
910 if (db->yajl == NULL)
911 {
912 ERROR ("curl_json plugin: yajl_alloc failed.");
913 db->yajl = yprev;
914 return (-1);
915 }
917 if (db->url)
918 status = cj_curl_perform (db);
919 else
920 status = cj_sock_perform (db);
921 if (status < 0)
922 {
923 yajl_free (db->yajl);
924 db->yajl = yprev;
925 return (-1);
926 }
928 #if HAVE_YAJL_V2
929 status = yajl_complete_parse(db->yajl);
930 #else
931 status = yajl_parse_complete(db->yajl);
932 #endif
933 if (status != yajl_status_ok)
934 {
935 unsigned char *errmsg;
937 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
938 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
939 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
940 (char *) errmsg);
941 yajl_free_error (db->yajl, errmsg);
942 yajl_free (db->yajl);
943 db->yajl = yprev;
944 return (-1);
945 }
947 yajl_free (db->yajl);
948 db->yajl = yprev;
949 return (0);
950 } /* }}} int cj_perform */
952 static int cj_read (user_data_t *ud) /* {{{ */
953 {
954 cj_t *db;
956 if ((ud == NULL) || (ud->data == NULL))
957 {
958 ERROR ("curl_json plugin: cj_read: Invalid user data.");
959 return (-1);
960 }
962 db = (cj_t *) ud->data;
964 db->depth = 0;
965 memset (&db->state, 0, sizeof(db->state));
966 db->state[db->depth].tree = db->tree;
967 db->key = NULL;
969 return cj_perform (db);
970 } /* }}} int cj_read */
972 static int cj_init (void) /* {{{ */
973 {
974 /* Call this while collectd is still single-threaded to avoid
975 * initialization issues in libgcrypt. */
976 curl_global_init (CURL_GLOBAL_SSL);
977 return (0);
978 } /* }}} int cj_init */
980 void module_register (void)
981 {
982 plugin_register_complex_config ("curl_json", cj_config);
983 plugin_register_init ("curl_json", cj_init);
984 } /* void module_register */
986 /* vim: set sw=2 sts=2 et fdm=marker : */