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