2 * collectd - src/curl_json.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2011 Florian octo Forster
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.
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.
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
20 * Doug MacEachern <dougm at hyperic.com>
21 * Florian octo Forster <octo at collectd.org>
27 #include "configfile.h"
28 #include "utils_avltree.h"
29 #include "utils_complain.h"
31 #include <curl/curl.h>
32 #include <yajl/yajl_parse.h>
33 #if HAVE_YAJL_YAJL_VERSION_H
34 # include <yajl/yajl_version.h>
37 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
38 # define HAVE_YAJL_V2 1
41 #define CJ_DEFAULT_HOST "localhost"
42 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
43 #define CJ_IS_KEY(key) ((key)->magic == CJ_KEY_MAGIC)
45 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
48 typedef struct cj_key_s cj_key_t;
49 struct cj_key_s /* {{{ */
72 char curl_errbuf[CURL_ERROR_SIZE];
83 char name[DATA_MAX_NAME_LEN];
84 } state[YAJL_MAX_DEPTH];
86 typedef struct cj_s cj_t; /* }}} */
89 typedef size_t yajl_len_t;
91 typedef unsigned int yajl_len_t;
94 static int cj_read (user_data_t *ud);
95 static int cj_curl_perform (cj_t *db, CURL *curl);
96 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
98 static size_t cj_curl_callback (void *buf, /* {{{ */
99 size_t size, size_t nmemb, void *user_data)
114 status = yajl_parse(db->yajl, (unsigned char *) buf, len);
115 if (status == yajl_status_ok)
118 status = yajl_complete_parse(db->yajl);
120 status = yajl_parse_complete(db->yajl);
125 else if (status == yajl_status_insufficient_data)
129 if (status != yajl_status_ok)
132 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 */
140 } /* }}} size_t cj_curl_callback */
142 static int cj_get_type (cj_key_t *key)
144 const data_set_t *ds;
146 ds = plugin_get_ds (key->type);
149 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
151 assert (key->type != NULL);
152 if (strcmp (type, key->type) != 0)
154 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
156 sstrncpy (type, key->type, sizeof (type));
161 else if (ds->ds_num > 1)
163 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
165 c_complain_once (LOG_WARNING, &complaint,
166 "curl_json plugin: The type \"%s\" has more than one data source. "
167 "This is currently not supported. I will return the type of the "
168 "first data source, but this will likely lead to problems later on.",
172 return ds->ds[0].type;
176 #define CJ_CB_ABORT 0
177 #define CJ_CB_CONTINUE 1
179 /* "number" may not be null terminated, so copy it into a buffer before
181 static int cj_cb_number (void *ctx,
182 const char *number, yajl_len_t number_len)
184 char buffer[number_len + 1];
186 cj_t *db = (cj_t *)ctx;
187 cj_key_t *key = db->state[db->depth].key;
192 if ((key == NULL) || !CJ_IS_KEY (key))
193 return (CJ_CB_CONTINUE);
195 memcpy (buffer, number, number_len);
196 buffer[sizeof (buffer) - 1] = 0;
198 type = cj_get_type (key);
199 status = parse_value (buffer, &vt, type);
202 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
203 return (CJ_CB_CONTINUE);
206 cj_submit (db, key, &vt);
207 return (CJ_CB_CONTINUE);
208 } /* int cj_cb_number */
210 static int cj_cb_map_key (void *ctx, const unsigned char *val,
213 cj_t *db = (cj_t *)ctx;
216 tree = db->state[db->depth-1].tree;
223 name = db->state[db->depth].name;
224 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
225 sstrncpy (name, (char *)val, len+1);
227 if (c_avl_get (tree, name, (void *) &value) == 0)
228 db->state[db->depth].key = value;
229 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
230 db->state[db->depth].key = value;
232 db->state[db->depth].key = NULL;
235 return (CJ_CB_CONTINUE);
238 static int cj_cb_string (void *ctx, const unsigned char *val,
241 cj_t *db = (cj_t *)ctx;
244 /* Create a null-terminated version of the string. */
245 memcpy (str, val, len);
248 /* No configuration for this string -> simply return. */
249 if (db->state[db->depth].key == NULL)
250 return (CJ_CB_CONTINUE);
252 if (!CJ_IS_KEY (db->state[db->depth].key))
254 NOTICE ("curl_json plugin: Found string \"%s\", but the configuration "
255 "expects a map here.", str);
256 return (CJ_CB_CONTINUE);
259 /* Handle the string as if it was a number. */
260 return (cj_cb_number (ctx, (const char *) val, len));
261 } /* int cj_cb_string */
263 static int cj_cb_start (void *ctx)
265 cj_t *db = (cj_t *)ctx;
266 if (++db->depth >= YAJL_MAX_DEPTH)
268 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
269 return (CJ_CB_ABORT);
271 return (CJ_CB_CONTINUE);
274 static int cj_cb_end (void *ctx)
276 cj_t *db = (cj_t *)ctx;
277 db->state[db->depth].tree = NULL;
279 return (CJ_CB_CONTINUE);
282 static int cj_cb_start_map (void *ctx)
284 return cj_cb_start (ctx);
287 static int cj_cb_end_map (void *ctx)
289 return cj_cb_end (ctx);
292 static int cj_cb_start_array (void * ctx)
294 return cj_cb_start (ctx);
297 static int cj_cb_end_array (void * ctx)
299 return cj_cb_end (ctx);
302 static yajl_callbacks ycallbacks = {
316 /* end yajl callbacks */
318 static void cj_key_free (cj_key_t *key) /* {{{ */
325 sfree (key->instance);
328 } /* }}} void cj_key_free */
330 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
335 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
337 cj_key_t *key = (cj_key_t *)value;
342 cj_tree_free ((c_avl_tree_t *)value);
347 c_avl_destroy (tree);
348 } /* }}} void cj_tree_free */
350 static void cj_free (void *arg) /* {{{ */
354 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
361 if (db->curl != NULL)
362 curl_easy_cleanup (db->curl);
365 if (db->tree != NULL)
366 cj_tree_free (db->tree);
369 sfree (db->instance);
375 sfree (db->credentials);
379 } /* }}} void cj_free */
381 /* Configuration handling functions {{{ */
383 static c_avl_tree_t *cj_avl_create(void)
385 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
388 static int cj_config_add_key (cj_t *db, /* {{{ */
395 if ((ci->values_num != 1)
396 || (ci->values[0].type != OCONFIG_TYPE_STRING))
398 WARNING ("curl_json plugin: The `Key' block "
399 "needs exactly one string argument.");
403 key = (cj_key_t *) malloc (sizeof (*key));
406 ERROR ("curl_json plugin: malloc failed.");
409 memset (key, 0, sizeof (*key));
410 key->magic = CJ_KEY_MAGIC;
412 if (strcasecmp ("Key", ci->key) == 0)
414 status = cf_util_get_string (ci, &key->path);
423 ERROR ("curl_json plugin: cj_config: "
424 "Invalid key: %s", ci->key);
429 for (i = 0; i < ci->children_num; i++)
431 oconfig_item_t *child = ci->children + i;
433 if (strcasecmp ("Type", child->key) == 0)
434 status = cf_util_get_string (child, &key->type);
435 else if (strcasecmp ("Instance", child->key) == 0)
436 status = cf_util_get_string (child, &key->instance);
439 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
445 } /* for (i = 0; i < ci->children_num; i++) */
449 if (key->type == NULL)
451 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
456 } /* while (status == 0) */
458 /* store path in a tree that will match the json map structure, example:
459 * "httpd/requests/count",
460 * "httpd/requests/current" ->
461 * { "httpd": { "requests": { "count": $key, "current": $key } } }
470 if (db->tree == NULL)
471 db->tree = cj_avl_create();
490 sstrncpy (ent, name, len+1);
492 if (c_avl_get (tree, ent, (void *) &value) != 0)
494 value = cj_avl_create ();
495 c_avl_insert (tree, strdup (ent), value);
504 c_avl_insert (tree, strdup(name), key);
507 ERROR ("curl_json plugin: invalid key: %s", key->path);
513 } /* }}} int cj_config_add_key */
515 static int cj_init_curl (cj_t *db) /* {{{ */
517 db->curl = curl_easy_init ();
518 if (db->curl == NULL)
520 ERROR ("curl_json plugin: curl_easy_init failed.");
524 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1);
525 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
526 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
527 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
528 PACKAGE_NAME"/"PACKAGE_VERSION);
529 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
530 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
532 if (db->user != NULL)
534 size_t credentials_size;
536 credentials_size = strlen (db->user) + 2;
537 if (db->pass != NULL)
538 credentials_size += strlen (db->pass);
540 db->credentials = (char *) malloc (credentials_size);
541 if (db->credentials == NULL)
543 ERROR ("curl_json plugin: malloc failed.");
547 ssnprintf (db->credentials, credentials_size, "%s:%s",
548 db->user, (db->pass == NULL) ? "" : db->pass);
549 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
552 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (int) db->verify_peer);
553 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
554 (int) (db->verify_host ? 2 : 0));
555 if (db->cacert != NULL)
556 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
559 } /* }}} int cj_init_curl */
561 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
567 if ((ci->values_num != 1)
568 || (ci->values[0].type != OCONFIG_TYPE_STRING))
570 WARNING ("curl_json plugin: The `URL' block "
571 "needs exactly one string argument.");
575 db = (cj_t *) malloc (sizeof (*db));
578 ERROR ("curl_json plugin: malloc failed.");
581 memset (db, 0, sizeof (*db));
583 if (strcasecmp ("URL", ci->key) == 0)
585 status = cf_util_get_string (ci, &db->url);
594 ERROR ("curl_json plugin: cj_config: "
595 "Invalid key: %s", ci->key);
599 /* Fill the `cj_t' structure.. */
600 for (i = 0; i < ci->children_num; i++)
602 oconfig_item_t *child = ci->children + i;
604 if (strcasecmp ("Instance", child->key) == 0)
605 status = cf_util_get_string (child, &db->instance);
606 else if (strcasecmp ("Host", child->key) == 0)
607 status = cf_util_get_string (child, &db->host);
608 else if (strcasecmp ("User", child->key) == 0)
609 status = cf_util_get_string (child, &db->user);
610 else if (strcasecmp ("Password", child->key) == 0)
611 status = cf_util_get_string (child, &db->pass);
612 else if (strcasecmp ("VerifyPeer", child->key) == 0)
613 status = cf_util_get_boolean (child, &db->verify_peer);
614 else if (strcasecmp ("VerifyHost", child->key) == 0)
615 status = cf_util_get_boolean (child, &db->verify_host);
616 else if (strcasecmp ("CACert", child->key) == 0)
617 status = cf_util_get_string (child, &db->cacert);
618 else if (strcasecmp ("Key", child->key) == 0)
619 status = cj_config_add_key (db, child);
622 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
632 if (db->tree == NULL)
634 WARNING ("curl_json plugin: No (valid) `Key' block "
635 "within `URL' block `%s'.", db->url);
639 status = cj_init_curl (db);
642 /* If all went well, register this database for reading */
646 char cb_name[DATA_MAX_NAME_LEN];
648 if (db->instance == NULL)
649 db->instance = strdup("default");
651 DEBUG ("curl_json plugin: Registering new read callback: %s",
654 memset (&ud, 0, sizeof (ud));
655 ud.data = (void *) db;
656 ud.free_func = cj_free;
658 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
659 db->instance, db->url);
661 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
662 /* interval = */ NULL, &ud);
672 /* }}} int cj_config_add_database */
674 static int cj_config (oconfig_item_t *ci) /* {{{ */
684 for (i = 0; i < ci->children_num; i++)
686 oconfig_item_t *child = ci->children + i;
688 if (strcasecmp ("URL", child->key) == 0)
690 status = cj_config_add_url (child);
698 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
703 if ((success == 0) && (errors > 0))
705 ERROR ("curl_json plugin: All statements failed.");
710 } /* }}} int cj_config */
712 /* }}} End of configuration handling functions */
714 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
716 value_list_t vl = VALUE_LIST_INIT;
722 if ((db->host == NULL)
723 || (strcmp ("", db->host) == 0)
724 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
729 if (key->instance == NULL)
731 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
732 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
734 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
735 db->state[db->depth-1].name, db->state[db->depth].name);
738 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
740 sstrncpy (vl.host, host, sizeof (vl.host));
741 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
742 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
743 sstrncpy (vl.type, key->type, sizeof (vl.type));
745 plugin_dispatch_values (&vl);
746 } /* }}} int cj_submit */
748 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
753 yajl_handle yprev = db->yajl;
755 db->yajl = yajl_alloc (&ycallbacks,
757 /* alloc funcs = */ NULL,
759 /* alloc funcs = */ NULL, NULL,
761 /* context = */ (void *)db);
762 if (db->yajl == NULL)
764 ERROR ("curl_json plugin: yajl_alloc failed.");
770 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
772 status = curl_easy_perform (curl);
775 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
776 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
777 yajl_free (db->yajl);
782 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
784 /* The response code is zero if a non-HTTP transport was used. */
785 if ((rc != 0) && (rc != 200))
787 ERROR ("curl_json plugin: curl_easy_perform failed with "
788 "response code %ld (%s)", rc, url);
789 yajl_free (db->yajl);
795 status = yajl_complete_parse(db->yajl);
797 status = yajl_parse_complete(db->yajl);
799 if (status != yajl_status_ok)
801 unsigned char *errmsg;
803 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
804 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
805 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
807 yajl_free_error (db->yajl, errmsg);
808 yajl_free (db->yajl);
813 yajl_free (db->yajl);
816 } /* }}} int cj_curl_perform */
818 static int cj_read (user_data_t *ud) /* {{{ */
822 if ((ud == NULL) || (ud->data == NULL))
824 ERROR ("curl_json plugin: cj_read: Invalid user data.");
828 db = (cj_t *) ud->data;
831 memset (&db->state, 0, sizeof(db->state));
832 db->state[db->depth].tree = db->tree;
835 return cj_curl_perform (db, db->curl);
836 } /* }}} int cj_read */
838 void module_register (void)
840 plugin_register_complex_config ("curl_json", cj_config);
841 } /* void module_register */
843 /* vim: set sw=2 sts=2 et fdm=marker : */