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 /* {{{ */
70 struct curl_slist *headers;
74 char curl_errbuf[CURL_ERROR_SIZE];
87 char name[DATA_MAX_NAME_LEN];
88 } state[YAJL_MAX_DEPTH];
90 typedef struct cj_s cj_t; /* }}} */
93 typedef size_t yajl_len_t;
95 typedef unsigned int yajl_len_t;
98 static int cj_read (user_data_t *ud);
99 static int cj_curl_perform (cj_t *db, CURL *curl);
100 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
102 static size_t cj_curl_callback (void *buf, /* {{{ */
103 size_t size, size_t nmemb, void *user_data)
118 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
119 if (status == yajl_status_ok)
122 else if (status == yajl_status_insufficient_data)
126 if (status != yajl_status_ok)
129 yajl_get_error(db->yajl, /* verbose = */ 1,
130 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
131 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
132 yajl_free_error(db->yajl, msg);
133 return (0); /* abort write callback */
137 } /* }}} size_t cj_curl_callback */
139 static int cj_get_type (cj_key_t *key)
141 const data_set_t *ds;
143 ds = plugin_get_ds (key->type);
146 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
148 assert (key->type != NULL);
149 if (strcmp (type, key->type) != 0)
151 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
153 sstrncpy (type, key->type, sizeof (type));
158 else if (ds->ds_num > 1)
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.",
169 return ds->ds[0].type;
172 static int cj_cb_map_key (void *ctx, const unsigned char *val,
175 static void cj_cb_inc_array_index (void * ctx, _Bool ignore)
177 cj_t *db = (cj_t *)ctx;
179 if (db->state[db->depth].in_array) {
181 db->state[db->depth].index++;
183 char name[DATA_MAX_NAME_LEN];
184 cj_cb_map_key (ctx, (unsigned char *)name,
185 ssnprintf (name, sizeof (name),
186 "%d", db->state[db->depth].index++));
192 #define CJ_CB_ABORT 0
193 #define CJ_CB_CONTINUE 1
195 static int cj_cb_boolean (void * ctx, int boolVal)
197 cj_cb_inc_array_index (ctx, 1);
198 return (CJ_CB_CONTINUE);
201 static int cj_cb_null (void * ctx)
203 cj_cb_inc_array_index (ctx, 1);
204 return (CJ_CB_CONTINUE);
207 /* "number" may not be null terminated, so copy it into a buffer before
209 static int cj_cb_number (void *ctx,
210 const char *number, yajl_len_t number_len)
212 char buffer[number_len + 1];
214 cj_t *db = (cj_t *)ctx;
215 cj_key_t *key = db->state[db->depth].key;
220 if ((key == NULL) || !CJ_IS_KEY (key)) {
221 cj_cb_inc_array_index (ctx, 1);
222 return (CJ_CB_CONTINUE);
224 cj_cb_inc_array_index (ctx, 0);
226 memcpy (buffer, number, number_len);
227 buffer[sizeof (buffer) - 1] = 0;
229 type = cj_get_type (key);
230 status = parse_value (buffer, &vt, type);
233 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
234 return (CJ_CB_CONTINUE);
237 cj_submit (db, key, &vt);
238 return (CJ_CB_CONTINUE);
239 } /* int cj_cb_number */
241 static int cj_cb_map_key (void *ctx, const unsigned char *val,
244 cj_t *db = (cj_t *)ctx;
247 tree = db->state[db->depth-1].tree;
254 name = db->state[db->depth].name;
255 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
256 sstrncpy (name, (char *)val, len+1);
258 if (c_avl_get (tree, name, (void *) &value) == 0)
259 db->state[db->depth].key = value;
260 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
261 db->state[db->depth].key = value;
263 db->state[db->depth].key = NULL;
266 return (CJ_CB_CONTINUE);
269 static int cj_cb_string (void *ctx, const unsigned char *val,
272 cj_t *db = (cj_t *)ctx;
275 cj_cb_inc_array_index (ctx, 1);
276 return (CJ_CB_CONTINUE);
278 /* Create a null-terminated version of the string. */
279 memcpy (str, val, len);
282 /* No configuration for this string -> simply return. */
283 if (db->state[db->depth].key == NULL)
284 return (CJ_CB_CONTINUE);
286 if (!CJ_IS_KEY (db->state[db->depth].key))
288 NOTICE ("curl_json plugin: Found string \"%s\", but the configuration "
289 "expects a map here.", str);
290 return (CJ_CB_CONTINUE);
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)
299 cj_t *db = (cj_t *)ctx;
300 if (++db->depth >= YAJL_MAX_DEPTH)
302 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
303 return (CJ_CB_ABORT);
305 return (CJ_CB_CONTINUE);
308 static int cj_cb_end (void *ctx)
310 cj_t *db = (cj_t *)ctx;
311 db->state[db->depth].tree = NULL;
313 return (CJ_CB_CONTINUE);
316 static int cj_cb_start_map (void *ctx)
318 cj_cb_inc_array_index (ctx, 0);
319 return cj_cb_start (ctx);
322 static int cj_cb_end_map (void *ctx)
324 return cj_cb_end (ctx);
327 static int cj_cb_start_array (void * ctx)
329 cj_t *db = (cj_t *)ctx;
330 cj_cb_inc_array_index (ctx, 0);
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;
335 return cj_cb_start (ctx);
338 static int cj_cb_end_array (void * ctx)
340 cj_t *db = (cj_t *)ctx;
341 db->state[db->depth].in_array = 0;
342 return cj_cb_end (ctx);
345 static yajl_callbacks ycallbacks = {
346 cj_cb_null, /* null */
347 cj_cb_boolean, /* boolean */
359 /* end yajl callbacks */
361 static void cj_key_free (cj_key_t *key) /* {{{ */
368 sfree (key->instance);
371 } /* }}} void cj_key_free */
373 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
378 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
380 cj_key_t *key = (cj_key_t *)value;
385 cj_tree_free ((c_avl_tree_t *)value);
390 c_avl_destroy (tree);
391 } /* }}} void cj_tree_free */
393 static void cj_free (void *arg) /* {{{ */
397 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
404 if (db->curl != NULL)
405 curl_easy_cleanup (db->curl);
408 if (db->tree != NULL)
409 cj_tree_free (db->tree);
412 sfree (db->instance);
418 sfree (db->credentials);
420 sfree (db->post_body);
421 curl_slist_free_all (db->headers);
424 } /* }}} void cj_free */
426 /* Configuration handling functions {{{ */
428 static c_avl_tree_t *cj_avl_create(void)
430 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
433 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
436 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
438 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
442 *dest = curl_slist_append(*dest, ci->values[0].value.string);
447 } /* }}} int cj_config_append_string */
449 static int cj_config_add_key (cj_t *db, /* {{{ */
456 if ((ci->values_num != 1)
457 || (ci->values[0].type != OCONFIG_TYPE_STRING))
459 WARNING ("curl_json plugin: The `Key' block "
460 "needs exactly one string argument.");
464 key = (cj_key_t *) malloc (sizeof (*key));
467 ERROR ("curl_json plugin: malloc failed.");
470 memset (key, 0, sizeof (*key));
471 key->magic = CJ_KEY_MAGIC;
473 if (strcasecmp ("Key", ci->key) == 0)
475 status = cf_util_get_string (ci, &key->path);
484 ERROR ("curl_json plugin: cj_config: "
485 "Invalid key: %s", ci->key);
490 for (i = 0; i < ci->children_num; i++)
492 oconfig_item_t *child = ci->children + i;
494 if (strcasecmp ("Type", child->key) == 0)
495 status = cf_util_get_string (child, &key->type);
496 else if (strcasecmp ("Instance", child->key) == 0)
497 status = cf_util_get_string (child, &key->instance);
500 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
506 } /* for (i = 0; i < ci->children_num; i++) */
510 if (key->type == NULL)
512 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
517 } /* while (status == 0) */
519 /* store path in a tree that will match the json map structure, example:
520 * "httpd/requests/count",
521 * "httpd/requests/current" ->
522 * { "httpd": { "requests": { "count": $key, "current": $key } } }
531 if (db->tree == NULL)
532 db->tree = cj_avl_create();
551 sstrncpy (ent, name, len+1);
553 if (c_avl_get (tree, ent, (void *) &value) != 0)
555 value = cj_avl_create ();
556 c_avl_insert (tree, strdup (ent), value);
565 c_avl_insert (tree, strdup(name), key);
568 ERROR ("curl_json plugin: invalid key: %s", key->path);
574 } /* }}} int cj_config_add_key */
576 static int cj_init_curl (cj_t *db) /* {{{ */
578 db->curl = curl_easy_init ();
579 if (db->curl == NULL)
581 ERROR ("curl_json plugin: curl_easy_init failed.");
585 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
586 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
587 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
588 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
589 PACKAGE_NAME"/"PACKAGE_VERSION);
590 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
591 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
593 if (db->user != NULL)
595 size_t credentials_size;
597 credentials_size = strlen (db->user) + 2;
598 if (db->pass != NULL)
599 credentials_size += strlen (db->pass);
601 db->credentials = (char *) malloc (credentials_size);
602 if (db->credentials == NULL)
604 ERROR ("curl_json plugin: malloc failed.");
608 ssnprintf (db->credentials, credentials_size, "%s:%s",
609 db->user, (db->pass == NULL) ? "" : db->pass);
610 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
613 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
614 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
615 db->verify_host ? 2L : 0L);
616 if (db->cacert != NULL)
617 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
618 if (db->headers != NULL)
619 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
620 if (db->post_body != NULL)
621 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
624 } /* }}} int cj_init_curl */
626 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
632 if ((ci->values_num != 1)
633 || (ci->values[0].type != OCONFIG_TYPE_STRING))
635 WARNING ("curl_json plugin: The `URL' block "
636 "needs exactly one string argument.");
640 db = (cj_t *) malloc (sizeof (*db));
643 ERROR ("curl_json plugin: malloc failed.");
646 memset (db, 0, sizeof (*db));
648 if (strcasecmp ("URL", ci->key) == 0)
650 status = cf_util_get_string (ci, &db->url);
659 ERROR ("curl_json plugin: cj_config: "
660 "Invalid key: %s", ci->key);
664 /* Fill the `cj_t' structure.. */
665 for (i = 0; i < ci->children_num; i++)
667 oconfig_item_t *child = ci->children + i;
669 if (strcasecmp ("Instance", child->key) == 0)
670 status = cf_util_get_string (child, &db->instance);
671 else if (strcasecmp ("Host", child->key) == 0)
672 status = cf_util_get_string (child, &db->host);
673 else if (strcasecmp ("User", child->key) == 0)
674 status = cf_util_get_string (child, &db->user);
675 else if (strcasecmp ("Password", child->key) == 0)
676 status = cf_util_get_string (child, &db->pass);
677 else if (strcasecmp ("VerifyPeer", child->key) == 0)
678 status = cf_util_get_boolean (child, &db->verify_peer);
679 else if (strcasecmp ("VerifyHost", child->key) == 0)
680 status = cf_util_get_boolean (child, &db->verify_host);
681 else if (strcasecmp ("CACert", child->key) == 0)
682 status = cf_util_get_string (child, &db->cacert);
683 else if (strcasecmp ("Header", child->key) == 0)
684 status = cj_config_append_string ("Header", &db->headers, child);
685 else if (strcasecmp ("Post", child->key) == 0)
686 status = cf_util_get_string (child, &db->post_body);
687 else if (strcasecmp ("Key", child->key) == 0)
688 status = cj_config_add_key (db, child);
691 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
701 if (db->tree == NULL)
703 WARNING ("curl_json plugin: No (valid) `Key' block "
704 "within `URL' block `%s'.", db->url);
708 status = cj_init_curl (db);
711 /* If all went well, register this database for reading */
715 char cb_name[DATA_MAX_NAME_LEN];
717 if (db->instance == NULL)
718 db->instance = strdup("default");
720 DEBUG ("curl_json plugin: Registering new read callback: %s",
723 memset (&ud, 0, sizeof (ud));
724 ud.data = (void *) db;
725 ud.free_func = cj_free;
727 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
728 db->instance, db->url);
730 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
731 /* interval = */ NULL, &ud);
741 /* }}} int cj_config_add_database */
743 static int cj_config (oconfig_item_t *ci) /* {{{ */
753 for (i = 0; i < ci->children_num; i++)
755 oconfig_item_t *child = ci->children + i;
757 if (strcasecmp ("URL", child->key) == 0)
759 status = cj_config_add_url (child);
767 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
772 if ((success == 0) && (errors > 0))
774 ERROR ("curl_json plugin: All statements failed.");
779 } /* }}} int cj_config */
781 /* }}} End of configuration handling functions */
783 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
785 value_list_t vl = VALUE_LIST_INIT;
791 if ((db->host == NULL)
792 || (strcmp ("", db->host) == 0)
793 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
798 if (key->instance == NULL)
800 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
801 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
803 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
804 db->state[db->depth-1].name, db->state[db->depth].name);
807 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
809 sstrncpy (vl.host, host, sizeof (vl.host));
810 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
811 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
812 sstrncpy (vl.type, key->type, sizeof (vl.type));
814 plugin_dispatch_values (&vl);
815 } /* }}} int cj_submit */
817 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
822 yajl_handle yprev = db->yajl;
824 db->yajl = yajl_alloc (&ycallbacks,
826 /* alloc funcs = */ NULL,
828 /* alloc funcs = */ NULL, NULL,
830 /* context = */ (void *)db);
831 if (db->yajl == NULL)
833 ERROR ("curl_json plugin: yajl_alloc failed.");
839 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
841 status = curl_easy_perform (curl);
842 if (status != CURLE_OK)
844 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
845 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
846 yajl_free (db->yajl);
851 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
853 /* The response code is zero if a non-HTTP transport was used. */
854 if ((rc != 0) && (rc != 200))
856 ERROR ("curl_json plugin: curl_easy_perform failed with "
857 "response code %ld (%s)", rc, url);
858 yajl_free (db->yajl);
864 status = yajl_complete_parse(db->yajl);
866 status = yajl_parse_complete(db->yajl);
868 if (status != yajl_status_ok)
870 unsigned char *errmsg;
872 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
873 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
874 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
876 yajl_free_error (db->yajl, errmsg);
877 yajl_free (db->yajl);
882 yajl_free (db->yajl);
885 } /* }}} int cj_curl_perform */
887 static int cj_read (user_data_t *ud) /* {{{ */
891 if ((ud == NULL) || (ud->data == NULL))
893 ERROR ("curl_json plugin: cj_read: Invalid user data.");
897 db = (cj_t *) ud->data;
900 memset (&db->state, 0, sizeof(db->state));
901 db->state[db->depth].tree = db->tree;
904 return cj_curl_perform (db, db->curl);
905 } /* }}} int cj_read */
907 void module_register (void)
909 plugin_register_complex_config ("curl_json", cj_config);
910 } /* void module_register */
912 /* vim: set sw=2 sts=2 et fdm=marker : */