2 * collectd - src/curl_json.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2009 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 verplant.org>
27 #include "configfile.h"
28 #include "utils_avltree.h"
30 #include <curl/curl.h>
31 #include <yajl/yajl_parse.h>
33 #define CJ_DEFAULT_HOST "localhost"
34 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
35 #define CJ_IS_KEY(key) (key)->magic == CJ_KEY_MAGIC
37 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
40 typedef struct cj_key_s cj_key_t;
41 struct cj_key_s /* {{{ */
64 char curl_errbuf[CURL_ERROR_SIZE];
75 char name[DATA_MAX_NAME_LEN];
76 } state[YAJL_MAX_DEPTH];
78 typedef struct cj_s cj_t; /* }}} */
80 static int cj_read (user_data_t *ud);
81 static int cj_curl_perform (cj_t *db, CURL *curl);
82 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
84 static size_t cj_curl_callback (void *buf, /* {{{ */
85 size_t size, size_t nmemb, void *user_data)
100 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
101 if (status == yajl_status_ok)
103 status = yajl_parse_complete(db->yajl);
106 else if (status == yajl_status_insufficient_data)
109 if (status != yajl_status_ok)
112 yajl_get_error(db->yajl, 1, (unsigned char *)buf, len);
113 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
114 yajl_free_error(db->yajl, msg);
115 return (0); /* abort write callback */
119 } /* }}} size_t cj_curl_callback */
121 static int cj_get_type (cj_key_t *key)
123 const data_set_t *ds;
125 ds = plugin_get_ds (key->type);
127 return -1; /* let plugin_write do the complaining */
129 return ds->ds[0].type; /* XXX support ds->ds_len > 1 */
133 static int cj_cb_integer (void *ctx, long val)
135 cj_t *db = (cj_t *)ctx;
136 cj_key_t *key = db->state[db->depth].key;
143 type = cj_get_type (key);
144 if (type == DS_TYPE_COUNTER)
145 vt.counter = (counter_t) val;
146 else if (type == DS_TYPE_GAUGE)
147 vt.gauge = (gauge_t) val;
148 else if (type == DS_TYPE_DERIVE)
149 vt.derive = (derive_t) val;
150 else if (type == DS_TYPE_ABSOLUTE)
151 vt.absolute = (absolute_t) val;
155 cj_submit (db, key, &vt);
160 static int cj_cb_double (void *ctx, double val)
162 cj_t *db = (cj_t *)ctx;
163 cj_key_t *key = db->state[db->depth].key;
170 type = cj_get_type (key);
171 if (type == DS_TYPE_COUNTER)
172 vt.counter = (counter_t) val;
173 else if (type == DS_TYPE_GAUGE)
174 vt.gauge = (gauge_t) val;
175 else if (type == DS_TYPE_DERIVE)
176 vt.derive = (derive_t) val;
177 else if (type == DS_TYPE_ABSOLUTE)
178 vt.absolute = (absolute_t) val;
182 cj_submit (db, key, &vt);
187 static int cj_cb_map_key (void *ctx, const unsigned char *val,
190 cj_t *db = (cj_t *)ctx;
193 tree = db->state[db->depth-1].tree;
200 name = db->state[db->depth].name;
201 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
202 sstrncpy (name, (char *)val, len+1);
204 if (c_avl_get (tree, name, (void *) &value) == 0)
205 db->state[db->depth].key = value;
206 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
207 db->state[db->depth].key = value;
209 db->state[db->depth].key = NULL;
215 static int cj_cb_string (void *ctx, const unsigned char *val,
218 cj_t *db = (cj_t *)ctx;
222 if (db->depth != 1) /* e.g. _all_dbs */
225 cj_cb_map_key (ctx, val, len); /* same logic */
227 tree = db->state[db->depth].tree;
229 if ((tree != NULL) && (ptr = rindex (db->url, '/')))
234 /* url =~ s,[^/]+$,$name, */
235 len = (ptr - db->url) + 1;
237 sstrncpy (ptr, db->url, sizeof (url));
238 sstrncpy (ptr + len, db->state[db->depth].name, sizeof (url) - len);
240 curl = curl_easy_duphandle (db->curl);
241 curl_easy_setopt (curl, CURLOPT_URL, url);
242 cj_curl_perform (db, curl);
243 curl_easy_cleanup (curl);
248 static int cj_cb_start (void *ctx)
250 cj_t *db = (cj_t *)ctx;
251 if (++db->depth >= YAJL_MAX_DEPTH)
253 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
259 static int cj_cb_end (void *ctx)
261 cj_t *db = (cj_t *)ctx;
262 db->state[db->depth].tree = NULL;
267 static int cj_cb_start_map (void *ctx)
269 return cj_cb_start (ctx);
272 static int cj_cb_end_map (void *ctx)
274 return cj_cb_end (ctx);
277 static int cj_cb_start_array (void * ctx)
279 return cj_cb_start (ctx);
282 static int cj_cb_end_array (void * ctx)
284 return cj_cb_start (ctx);
287 static yajl_callbacks ycallbacks = {
301 /* end yajl callbacks */
303 static void cj_key_free (cj_key_t *key) /* {{{ */
310 sfree (key->instance);
313 } /* }}} void cj_key_free */
315 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
320 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
322 cj_key_t *key = (cj_key_t *)value;
327 cj_tree_free ((c_avl_tree_t *)value);
332 c_avl_destroy (tree);
333 } /* }}} void cj_tree_free */
335 static void cj_free (void *arg) /* {{{ */
339 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
346 if (db->curl != NULL)
347 curl_easy_cleanup (db->curl);
350 if (db->tree != NULL)
351 cj_tree_free (db->tree);
354 sfree (db->instance);
360 sfree (db->credentials);
364 } /* }}} void cj_free */
366 /* Configuration handling functions {{{ */
368 static int cj_config_add_string (const char *name, char **dest, /* {{{ */
371 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
373 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
378 *dest = strdup (ci->values[0].value.string);
383 } /* }}} int cj_config_add_string */
385 static int cj_config_set_boolean (const char *name, int *dest, /* {{{ */
388 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
390 WARNING ("curl_json plugin: `%s' needs exactly one boolean argument.", name);
394 *dest = ci->values[0].value.boolean ? 1 : 0;
397 } /* }}} int cj_config_set_boolean */
399 static c_avl_tree_t *cj_avl_create(void)
401 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
404 static int cj_config_add_key (cj_t *db, /* {{{ */
411 if ((ci->values_num != 1)
412 || (ci->values[0].type != OCONFIG_TYPE_STRING))
414 WARNING ("curl_json plugin: The `Key' block "
415 "needs exactly one string argument.");
419 key = (cj_key_t *) malloc (sizeof (*key));
422 ERROR ("curl_json plugin: malloc failed.");
425 memset (key, 0, sizeof (*key));
426 key->magic = CJ_KEY_MAGIC;
428 if (strcasecmp ("Key", ci->key) == 0)
430 status = cj_config_add_string ("Key", &key->path, ci);
439 ERROR ("curl_json plugin: cj_config: "
440 "Invalid key: %s", ci->key);
445 for (i = 0; i < ci->children_num; i++)
447 oconfig_item_t *child = ci->children + i;
449 if (strcasecmp ("Type", child->key) == 0)
450 status = cj_config_add_string ("Type", &key->type, child);
451 else if (strcasecmp ("Instance", child->key) == 0)
452 status = cj_config_add_string ("Instance", &key->instance, child);
455 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
461 } /* for (i = 0; i < ci->children_num; i++) */
465 if (key->type == NULL)
467 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
472 } /* while (status == 0) */
474 /* store path in a tree that will match the json map structure, example:
475 * "httpd/requests/count",
476 * "httpd/requests/current" ->
477 * { "httpd": { "requests": { "count": $key, "current": $key } } }
486 if (db->tree == NULL)
487 db->tree = cj_avl_create();
506 sstrncpy (ent, name, len+1);
508 if (c_avl_get (tree, ent, (void *) &value) != 0)
510 value = cj_avl_create ();
511 c_avl_insert (tree, strdup (ent), value);
520 c_avl_insert (tree, strdup(name), key);
523 ERROR ("curl_json plugin: invalid key: %s", key->path);
529 } /* }}} int cj_config_add_key */
531 static int cj_init_curl (cj_t *db) /* {{{ */
533 db->curl = curl_easy_init ();
534 if (db->curl == NULL)
536 ERROR ("curl_json plugin: curl_easy_init failed.");
540 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
541 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
542 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
543 PACKAGE_NAME"/"PACKAGE_VERSION);
544 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
545 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
547 if (db->user != NULL)
549 size_t credentials_size;
551 credentials_size = strlen (db->user) + 2;
552 if (db->pass != NULL)
553 credentials_size += strlen (db->pass);
555 db->credentials = (char *) malloc (credentials_size);
556 if (db->credentials == NULL)
558 ERROR ("curl_json plugin: malloc failed.");
562 ssnprintf (db->credentials, credentials_size, "%s:%s",
563 db->user, (db->pass == NULL) ? "" : db->pass);
564 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
567 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
568 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
569 db->verify_host ? 2 : 0);
570 if (db->cacert != NULL)
571 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
574 } /* }}} int cj_init_curl */
576 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
582 if ((ci->values_num != 1)
583 || (ci->values[0].type != OCONFIG_TYPE_STRING))
585 WARNING ("curl_json plugin: The `URL' block "
586 "needs exactly one string argument.");
590 db = (cj_t *) malloc (sizeof (*db));
593 ERROR ("curl_json plugin: malloc failed.");
596 memset (db, 0, sizeof (*db));
598 if (strcasecmp ("URL", ci->key) == 0)
600 status = cj_config_add_string ("URL", &db->url, ci);
609 ERROR ("curl_json plugin: cj_config: "
610 "Invalid key: %s", ci->key);
614 /* Fill the `cj_t' structure.. */
615 for (i = 0; i < ci->children_num; i++)
617 oconfig_item_t *child = ci->children + i;
619 if (strcasecmp ("Instance", child->key) == 0)
620 status = cj_config_add_string ("Instance", &db->instance, child);
621 else if (strcasecmp ("Host", child->key) == 0)
622 status = cj_config_add_string ("Host", &db->host, child);
623 else if (strcasecmp ("User", child->key) == 0)
624 status = cj_config_add_string ("User", &db->user, child);
625 else if (strcasecmp ("Password", child->key) == 0)
626 status = cj_config_add_string ("Password", &db->pass, child);
627 else if (strcasecmp ("VerifyPeer", child->key) == 0)
628 status = cj_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
629 else if (strcasecmp ("VerifyHost", child->key) == 0)
630 status = cj_config_set_boolean ("VerifyHost", &db->verify_host, child);
631 else if (strcasecmp ("CACert", child->key) == 0)
632 status = cj_config_add_string ("CACert", &db->cacert, child);
633 else if (strcasecmp ("Key", child->key) == 0)
634 status = cj_config_add_key (db, child);
637 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
647 if (db->tree == NULL)
649 WARNING ("curl_json plugin: No (valid) `Key' block "
650 "within `URL' block `%s'.", db->url);
654 status = cj_init_curl (db);
657 /* If all went well, register this database for reading */
661 char cb_name[DATA_MAX_NAME_LEN];
663 if (db->instance == NULL)
664 db->instance = strdup("default");
666 DEBUG ("curl_json plugin: Registering new read callback: %s",
669 memset (&ud, 0, sizeof (ud));
670 ud.data = (void *) db;
671 ud.free_func = cj_free;
673 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
674 db->instance, db->url);
676 plugin_register_complex_read (cb_name, cj_read,
677 /* interval = */ NULL, &ud);
687 /* }}} int cj_config_add_database */
689 static int cj_config (oconfig_item_t *ci) /* {{{ */
699 for (i = 0; i < ci->children_num; i++)
701 oconfig_item_t *child = ci->children + i;
703 if (strcasecmp ("URL", child->key) == 0)
705 status = cj_config_add_url (child);
713 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
718 if ((success == 0) && (errors > 0))
720 ERROR ("curl_json plugin: All statements failed.");
725 } /* }}} int cj_config */
727 /* }}} End of configuration handling functions */
729 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
731 value_list_t vl = VALUE_LIST_INIT;
737 if ((db->host == NULL)
738 || (strcmp ("", db->host) == 0)
739 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
744 if (key->instance == NULL)
745 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
746 db->state[db->depth-1].name, db->state[db->depth].name);
748 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
750 sstrncpy (vl.host, host, sizeof (vl.host));
751 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
752 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
753 sstrncpy (vl.type, key->type, sizeof (vl.type));
755 plugin_dispatch_values (&vl);
756 } /* }}} int cj_submit */
758 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
763 yajl_handle yprev = db->yajl;
765 db->yajl = yajl_alloc (&ycallbacks, NULL, NULL, (void *)db);
766 if (db->yajl == NULL)
768 ERROR ("curl_json plugin: yajl_alloc failed.");
772 status = curl_easy_perform (curl);
774 yajl_free (db->yajl);
777 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
778 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
782 ERROR ("curl_json plugin: curl_easy_perform failed with response code %ld (%s)",
789 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
790 status, db->curl_errbuf, url);
795 } /* }}} int cj_curl_perform */
797 static int cj_read (user_data_t *ud) /* {{{ */
801 if ((ud == NULL) || (ud->data == NULL))
803 ERROR ("curl_json plugin: cj_read: Invalid user data.");
807 db = (cj_t *) ud->data;
810 memset (&db->state, 0, sizeof(db->state));
811 db->state[db->depth].tree = db->tree;
814 return cj_curl_perform (db, db->curl);
815 } /* }}} int cj_read */
817 void module_register (void)
819 plugin_register_complex_config ("curl_json", cj_config);
820 } /* void module_register */
822 /* vim: set sw=2 sts=2 et fdm=marker : */