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];
85 char name[DATA_MAX_NAME_LEN];
86 } state[YAJL_MAX_DEPTH];
88 typedef struct cj_s cj_t; /* }}} */
91 typedef size_t yajl_len_t;
93 typedef unsigned int yajl_len_t;
96 static int cj_read (user_data_t *ud);
97 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
99 static size_t cj_curl_callback (void *buf, /* {{{ */
100 size_t size, size_t nmemb, void *user_data)
115 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
116 if (status == yajl_status_ok)
119 else if (status == yajl_status_insufficient_data)
123 if (status != yajl_status_ok)
126 yajl_get_error(db->yajl, /* verbose = */ 1,
127 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
128 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
129 yajl_free_error(db->yajl, msg);
130 return (0); /* abort write callback */
134 } /* }}} size_t cj_curl_callback */
136 static int cj_get_type (cj_key_t *key)
138 const data_set_t *ds;
140 ds = plugin_get_ds (key->type);
143 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
145 assert (key->type != NULL);
146 if (strcmp (type, key->type) != 0)
148 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
150 sstrncpy (type, key->type, sizeof (type));
155 else if (ds->ds_num > 1)
157 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
159 c_complain_once (LOG_WARNING, &complaint,
160 "curl_json plugin: The type \"%s\" has more than one data source. "
161 "This is currently not supported. I will return the type of the "
162 "first data source, but this will likely lead to problems later on.",
166 return ds->ds[0].type;
170 #define CJ_CB_ABORT 0
171 #define CJ_CB_CONTINUE 1
173 /* "number" may not be null terminated, so copy it into a buffer before
175 static int cj_cb_number (void *ctx,
176 const char *number, yajl_len_t number_len)
178 char buffer[number_len + 1];
180 cj_t *db = (cj_t *)ctx;
181 cj_key_t *key = db->state[db->depth].key;
186 if ((key == NULL) || !CJ_IS_KEY (key))
187 return (CJ_CB_CONTINUE);
189 memcpy (buffer, number, number_len);
190 buffer[sizeof (buffer) - 1] = 0;
192 type = cj_get_type (key);
193 status = parse_value (buffer, &vt, type);
196 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
197 return (CJ_CB_CONTINUE);
200 cj_submit (db, key, &vt);
201 return (CJ_CB_CONTINUE);
202 } /* int cj_cb_number */
204 static int cj_cb_map_key (void *ctx, const unsigned char *val,
207 cj_t *db = (cj_t *)ctx;
210 tree = db->state[db->depth-1].tree;
217 name = db->state[db->depth].name;
218 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
219 sstrncpy (name, (char *)val, len+1);
221 if (c_avl_get (tree, name, (void *) &value) == 0)
222 db->state[db->depth].key = value;
223 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
224 db->state[db->depth].key = value;
226 db->state[db->depth].key = NULL;
229 return (CJ_CB_CONTINUE);
232 static int cj_cb_string (void *ctx, const unsigned char *val,
235 cj_t *db = (cj_t *)ctx;
238 /* Create a null-terminated version of the string. */
239 memcpy (str, val, len);
242 /* No configuration for this string -> simply return. */
243 if (db->state[db->depth].key == NULL)
244 return (CJ_CB_CONTINUE);
246 if (!CJ_IS_KEY (db->state[db->depth].key))
248 NOTICE ("curl_json plugin: Found string \"%s\", but the configuration "
249 "expects a map here.", str);
250 return (CJ_CB_CONTINUE);
253 /* Handle the string as if it was a number. */
254 return (cj_cb_number (ctx, (const char *) val, len));
255 } /* int cj_cb_string */
257 static int cj_cb_start (void *ctx)
259 cj_t *db = (cj_t *)ctx;
260 if (++db->depth >= YAJL_MAX_DEPTH)
262 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
263 return (CJ_CB_ABORT);
265 return (CJ_CB_CONTINUE);
268 static int cj_cb_end (void *ctx)
270 cj_t *db = (cj_t *)ctx;
271 db->state[db->depth].tree = NULL;
273 return (CJ_CB_CONTINUE);
276 static int cj_cb_start_map (void *ctx)
278 return cj_cb_start (ctx);
281 static int cj_cb_end_map (void *ctx)
283 return cj_cb_end (ctx);
286 static int cj_cb_start_array (void * ctx)
288 return cj_cb_start (ctx);
291 static int cj_cb_end_array (void * ctx)
293 return cj_cb_end (ctx);
296 static yajl_callbacks ycallbacks = {
310 /* end yajl callbacks */
312 static void cj_key_free (cj_key_t *key) /* {{{ */
319 sfree (key->instance);
322 } /* }}} void cj_key_free */
324 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
329 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
331 cj_key_t *key = (cj_key_t *)value;
336 cj_tree_free ((c_avl_tree_t *)value);
341 c_avl_destroy (tree);
342 } /* }}} void cj_tree_free */
344 static void cj_free (void *arg) /* {{{ */
348 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
355 if (db->curl != NULL)
356 curl_easy_cleanup (db->curl);
359 if (db->tree != NULL)
360 cj_tree_free (db->tree);
363 sfree (db->instance);
369 sfree (db->credentials);
371 sfree (db->post_body);
372 curl_slist_free_all (db->headers);
375 } /* }}} void cj_free */
377 /* Configuration handling functions {{{ */
379 static c_avl_tree_t *cj_avl_create(void)
381 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
384 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
387 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
389 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
393 *dest = curl_slist_append(*dest, ci->values[0].value.string);
398 } /* }}} int cj_config_append_string */
400 static int cj_config_add_key (cj_t *db, /* {{{ */
407 if ((ci->values_num != 1)
408 || (ci->values[0].type != OCONFIG_TYPE_STRING))
410 WARNING ("curl_json plugin: The `Key' block "
411 "needs exactly one string argument.");
415 key = (cj_key_t *) malloc (sizeof (*key));
418 ERROR ("curl_json plugin: malloc failed.");
421 memset (key, 0, sizeof (*key));
422 key->magic = CJ_KEY_MAGIC;
424 if (strcasecmp ("Key", ci->key) == 0)
426 status = cf_util_get_string (ci, &key->path);
435 ERROR ("curl_json plugin: cj_config: "
436 "Invalid key: %s", ci->key);
441 for (i = 0; i < ci->children_num; i++)
443 oconfig_item_t *child = ci->children + i;
445 if (strcasecmp ("Type", child->key) == 0)
446 status = cf_util_get_string (child, &key->type);
447 else if (strcasecmp ("Instance", child->key) == 0)
448 status = cf_util_get_string (child, &key->instance);
451 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
457 } /* for (i = 0; i < ci->children_num; i++) */
461 if (key->type == NULL)
463 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
468 } /* while (status == 0) */
470 /* store path in a tree that will match the json map structure, example:
471 * "httpd/requests/count",
472 * "httpd/requests/current" ->
473 * { "httpd": { "requests": { "count": $key, "current": $key } } }
482 if (db->tree == NULL)
483 db->tree = cj_avl_create();
502 sstrncpy (ent, name, len+1);
504 if (c_avl_get (tree, ent, (void *) &value) != 0)
506 value = cj_avl_create ();
507 c_avl_insert (tree, strdup (ent), value);
516 c_avl_insert (tree, strdup(name), key);
519 ERROR ("curl_json plugin: invalid key: %s", key->path);
525 } /* }}} int cj_config_add_key */
527 static int cj_init_curl (cj_t *db) /* {{{ */
529 db->curl = curl_easy_init ();
530 if (db->curl == NULL)
532 ERROR ("curl_json plugin: curl_easy_init failed.");
536 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
537 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
538 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
539 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
540 PACKAGE_NAME"/"PACKAGE_VERSION);
541 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
542 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
544 if (db->user != NULL)
546 size_t credentials_size;
548 credentials_size = strlen (db->user) + 2;
549 if (db->pass != NULL)
550 credentials_size += strlen (db->pass);
552 db->credentials = (char *) malloc (credentials_size);
553 if (db->credentials == NULL)
555 ERROR ("curl_json plugin: malloc failed.");
559 ssnprintf (db->credentials, credentials_size, "%s:%s",
560 db->user, (db->pass == NULL) ? "" : db->pass);
561 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
564 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
565 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
566 db->verify_host ? 2L : 0L);
567 if (db->cacert != NULL)
568 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
569 if (db->headers != NULL)
570 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
571 if (db->post_body != NULL)
572 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
575 } /* }}} int cj_init_curl */
577 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
583 if ((ci->values_num != 1)
584 || (ci->values[0].type != OCONFIG_TYPE_STRING))
586 WARNING ("curl_json plugin: The `URL' block "
587 "needs exactly one string argument.");
591 db = (cj_t *) malloc (sizeof (*db));
594 ERROR ("curl_json plugin: malloc failed.");
597 memset (db, 0, sizeof (*db));
599 if (strcasecmp ("URL", ci->key) == 0)
601 status = cf_util_get_string (ci, &db->url);
610 ERROR ("curl_json plugin: cj_config: "
611 "Invalid key: %s", ci->key);
615 /* Fill the `cj_t' structure.. */
616 for (i = 0; i < ci->children_num; i++)
618 oconfig_item_t *child = ci->children + i;
620 if (strcasecmp ("Instance", child->key) == 0)
621 status = cf_util_get_string (child, &db->instance);
622 else if (strcasecmp ("Host", child->key) == 0)
623 status = cf_util_get_string (child, &db->host);
624 else if (strcasecmp ("User", child->key) == 0)
625 status = cf_util_get_string (child, &db->user);
626 else if (strcasecmp ("Password", child->key) == 0)
627 status = cf_util_get_string (child, &db->pass);
628 else if (strcasecmp ("VerifyPeer", child->key) == 0)
629 status = cf_util_get_boolean (child, &db->verify_peer);
630 else if (strcasecmp ("VerifyHost", child->key) == 0)
631 status = cf_util_get_boolean (child, &db->verify_host);
632 else if (strcasecmp ("CACert", child->key) == 0)
633 status = cf_util_get_string (child, &db->cacert);
634 else if (strcasecmp ("Header", child->key) == 0)
635 status = cj_config_append_string ("Header", &db->headers, child);
636 else if (strcasecmp ("Post", child->key) == 0)
637 status = cf_util_get_string (child, &db->post_body);
638 else if (strcasecmp ("Key", child->key) == 0)
639 status = cj_config_add_key (db, child);
642 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
652 if (db->tree == NULL)
654 WARNING ("curl_json plugin: No (valid) `Key' block "
655 "within `URL' block `%s'.", db->url);
659 status = cj_init_curl (db);
662 /* If all went well, register this database for reading */
666 char cb_name[DATA_MAX_NAME_LEN];
668 if (db->instance == NULL)
669 db->instance = strdup("default");
671 DEBUG ("curl_json plugin: Registering new read callback: %s",
674 memset (&ud, 0, sizeof (ud));
675 ud.data = (void *) db;
676 ud.free_func = cj_free;
678 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
679 db->instance, db->url);
681 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
682 /* interval = */ NULL, &ud);
692 /* }}} int cj_config_add_database */
694 static int cj_config (oconfig_item_t *ci) /* {{{ */
704 for (i = 0; i < ci->children_num; i++)
706 oconfig_item_t *child = ci->children + i;
708 if (strcasecmp ("URL", child->key) == 0)
710 status = cj_config_add_url (child);
718 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
723 if ((success == 0) && (errors > 0))
725 ERROR ("curl_json plugin: All statements failed.");
730 } /* }}} int cj_config */
732 /* }}} End of configuration handling functions */
734 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
736 value_list_t vl = VALUE_LIST_INIT;
742 if ((db->host == NULL)
743 || (strcmp ("", db->host) == 0)
744 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
749 if (key->instance == NULL)
751 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
752 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
754 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
755 db->state[db->depth-1].name, db->state[db->depth].name);
758 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
760 sstrncpy (vl.host, host, sizeof (vl.host));
761 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
762 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
763 sstrncpy (vl.type, key->type, sizeof (vl.type));
765 plugin_dispatch_values (&vl);
766 } /* }}} int cj_submit */
768 static int cj_curl_perform (cj_t *db) /* {{{ */
774 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
776 status = curl_easy_perform (db->curl);
777 if (status != CURLE_OK)
779 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
780 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
784 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
786 /* The response code is zero if a non-HTTP transport was used. */
787 if ((rc != 0) && (rc != 200))
789 ERROR ("curl_json plugin: curl_easy_perform failed with "
790 "response code %ld (%s)", rc, url);
795 } /* }}} int cj_curl_perform */
797 static int cj_perform (cj_t *db) /* {{{ */
800 yajl_handle yprev = db->yajl;
802 db->yajl = yajl_alloc (&ycallbacks,
804 /* alloc funcs = */ NULL,
806 /* alloc funcs = */ NULL, NULL,
808 /* context = */ (void *)db);
809 if (db->yajl == NULL)
811 ERROR ("curl_json plugin: yajl_alloc failed.");
816 if (cj_curl_perform(db) < 0)
818 yajl_free (db->yajl);
824 status = yajl_complete_parse(db->yajl);
826 status = yajl_parse_complete(db->yajl);
828 if (status != yajl_status_ok)
830 unsigned char *errmsg;
832 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
833 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
834 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
836 yajl_free_error (db->yajl, errmsg);
837 yajl_free (db->yajl);
842 yajl_free (db->yajl);
845 } /* }}} int cj_perform */
847 static int cj_read (user_data_t *ud) /* {{{ */
851 if ((ud == NULL) || (ud->data == NULL))
853 ERROR ("curl_json plugin: cj_read: Invalid user data.");
857 db = (cj_t *) ud->data;
860 memset (&db->state, 0, sizeof(db->state));
861 db->state[db->depth].tree = db->tree;
864 return cj_perform (db);
865 } /* }}} int cj_read */
867 void module_register (void)
869 plugin_register_complex_config ("curl_json", cj_config);
870 } /* void module_register */
872 /* vim: set sw=2 sts=2 et fdm=marker : */