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"
30 #include <curl/curl.h>
31 #include <yajl/yajl_parse.h>
32 #if HAVE_YAJL_YAJL_VERSION_H
33 # include <yajl/yajl_version.h>
36 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
37 # define HAVE_YAJL_V2 1
40 #define CJ_DEFAULT_HOST "localhost"
41 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
42 #define CJ_IS_KEY(key) (key)->magic == CJ_KEY_MAGIC
44 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
47 typedef struct cj_key_s cj_key_t;
48 struct cj_key_s /* {{{ */
71 char curl_errbuf[CURL_ERROR_SIZE];
82 char name[DATA_MAX_NAME_LEN];
83 } state[YAJL_MAX_DEPTH];
85 typedef struct cj_s cj_t; /* }}} */
88 typedef size_t yajl_len_t;
90 typedef unsigned int yajl_len_t;
93 static int cj_read (user_data_t *ud);
94 static int cj_curl_perform (cj_t *db, CURL *curl);
95 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
97 static size_t cj_curl_callback (void *buf, /* {{{ */
98 size_t size, size_t nmemb, void *user_data)
113 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
114 if (status == yajl_status_ok)
117 status = yajl_complete_parse(db->yajl);
119 status = yajl_parse_complete(db->yajl);
124 else if (status == yajl_status_insufficient_data)
128 if (status != yajl_status_ok)
131 yajl_get_error(db->yajl, 1, (unsigned char *)buf, len);
132 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
133 yajl_free_error(db->yajl, msg);
134 return (0); /* abort write callback */
138 } /* }}} size_t cj_curl_callback */
140 static int cj_get_type (cj_key_t *key)
142 const data_set_t *ds;
144 ds = plugin_get_ds (key->type);
146 return -1; /* let plugin_write do the complaining */
148 return ds->ds[0].type; /* XXX support ds->ds_len > 1 */
152 #define CJ_CB_ABORT 0
153 #define CJ_CB_CONTINUE 1
155 /* "number" may not be null terminated, so copy it into a buffer before
157 static int cj_cb_number (void *ctx,
158 const char *number, yajl_len_t number_len)
160 char buffer[number_len + 1];
162 cj_t *db = (cj_t *)ctx;
163 cj_key_t *key = db->state[db->depth].key;
169 return (CJ_CB_CONTINUE);
171 memcpy (buffer, number, number_len);
172 buffer[sizeof (buffer) - 1] = 0;
174 type = cj_get_type (key);
179 if (type == DS_TYPE_COUNTER)
180 vt.counter = (counter_t) strtoull (buffer, &endptr, /* base = */ 0);
181 else if (type == DS_TYPE_GAUGE)
182 vt.gauge = (gauge_t) strtod (buffer, &endptr);
183 else if (type == DS_TYPE_DERIVE)
184 vt.derive = (derive_t) strtoll (buffer, &endptr, /* base = */ 0);
185 else if (type == DS_TYPE_ABSOLUTE)
186 vt.absolute = (absolute_t) strtoull (buffer, &endptr, /* base = */ 0);
189 ERROR ("curl_json plugin: Unknown data source type: \"%s\"", key->type);
190 return (CJ_CB_ABORT);
193 if ((endptr == &buffer[0]) || (errno != 0))
195 NOTICE ("curl_json plugin: Overflow while parsing number. "
196 "Ignoring this value.");
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;
239 if (db->depth != 1) /* e.g. _all_dbs */
240 return (CJ_CB_CONTINUE);
242 cj_cb_map_key (ctx, val, len); /* same logic */
244 tree = db->state[db->depth].tree;
246 if ((tree != NULL) && (ptr = rindex (db->url, '/')))
251 /* url =~ s,[^/]+$,$name, */
252 len = (ptr - db->url) + 1;
254 sstrncpy (ptr, db->url, sizeof (url));
255 sstrncpy (ptr + len, db->state[db->depth].name, sizeof (url) - len);
257 curl = curl_easy_duphandle (db->curl);
258 curl_easy_setopt (curl, CURLOPT_URL, url);
259 cj_curl_perform (db, curl);
260 curl_easy_cleanup (curl);
262 return (CJ_CB_CONTINUE);
265 static int cj_cb_start (void *ctx)
267 cj_t *db = (cj_t *)ctx;
268 if (++db->depth >= YAJL_MAX_DEPTH)
270 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
271 return (CJ_CB_ABORT);
273 return (CJ_CB_CONTINUE);
276 static int cj_cb_end (void *ctx)
278 cj_t *db = (cj_t *)ctx;
279 db->state[db->depth].tree = NULL;
281 return (CJ_CB_CONTINUE);
284 static int cj_cb_start_map (void *ctx)
286 return cj_cb_start (ctx);
289 static int cj_cb_end_map (void *ctx)
291 return cj_cb_end (ctx);
294 static int cj_cb_start_array (void * ctx)
296 return cj_cb_start (ctx);
299 static int cj_cb_end_array (void * ctx)
301 return cj_cb_end (ctx);
304 static yajl_callbacks ycallbacks = {
318 /* end yajl callbacks */
320 static void cj_key_free (cj_key_t *key) /* {{{ */
327 sfree (key->instance);
330 } /* }}} void cj_key_free */
332 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
337 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
339 cj_key_t *key = (cj_key_t *)value;
344 cj_tree_free ((c_avl_tree_t *)value);
349 c_avl_destroy (tree);
350 } /* }}} void cj_tree_free */
352 static void cj_free (void *arg) /* {{{ */
356 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
363 if (db->curl != NULL)
364 curl_easy_cleanup (db->curl);
367 if (db->tree != NULL)
368 cj_tree_free (db->tree);
371 sfree (db->instance);
377 sfree (db->credentials);
381 } /* }}} void cj_free */
383 /* Configuration handling functions {{{ */
385 static int cj_config_add_string (const char *name, char **dest, /* {{{ */
388 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
390 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
395 *dest = strdup (ci->values[0].value.string);
400 } /* }}} int cj_config_add_string */
402 static int cj_config_set_boolean (const char *name, int *dest, /* {{{ */
405 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
407 WARNING ("curl_json plugin: `%s' needs exactly one boolean argument.", name);
411 *dest = ci->values[0].value.boolean ? 1 : 0;
414 } /* }}} int cj_config_set_boolean */
416 static c_avl_tree_t *cj_avl_create(void)
418 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
421 static int cj_config_add_key (cj_t *db, /* {{{ */
428 if ((ci->values_num != 1)
429 || (ci->values[0].type != OCONFIG_TYPE_STRING))
431 WARNING ("curl_json plugin: The `Key' block "
432 "needs exactly one string argument.");
436 key = (cj_key_t *) malloc (sizeof (*key));
439 ERROR ("curl_json plugin: malloc failed.");
442 memset (key, 0, sizeof (*key));
443 key->magic = CJ_KEY_MAGIC;
445 if (strcasecmp ("Key", ci->key) == 0)
447 status = cj_config_add_string ("Key", &key->path, ci);
456 ERROR ("curl_json plugin: cj_config: "
457 "Invalid key: %s", ci->key);
462 for (i = 0; i < ci->children_num; i++)
464 oconfig_item_t *child = ci->children + i;
466 if (strcasecmp ("Type", child->key) == 0)
467 status = cj_config_add_string ("Type", &key->type, child);
468 else if (strcasecmp ("Instance", child->key) == 0)
469 status = cj_config_add_string ("Instance", &key->instance, child);
472 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
478 } /* for (i = 0; i < ci->children_num; i++) */
482 if (key->type == NULL)
484 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
489 } /* while (status == 0) */
491 /* store path in a tree that will match the json map structure, example:
492 * "httpd/requests/count",
493 * "httpd/requests/current" ->
494 * { "httpd": { "requests": { "count": $key, "current": $key } } }
503 if (db->tree == NULL)
504 db->tree = cj_avl_create();
523 sstrncpy (ent, name, len+1);
525 if (c_avl_get (tree, ent, (void *) &value) != 0)
527 value = cj_avl_create ();
528 c_avl_insert (tree, strdup (ent), value);
537 c_avl_insert (tree, strdup(name), key);
540 ERROR ("curl_json plugin: invalid key: %s", key->path);
546 } /* }}} int cj_config_add_key */
548 static int cj_init_curl (cj_t *db) /* {{{ */
550 db->curl = curl_easy_init ();
551 if (db->curl == NULL)
553 ERROR ("curl_json plugin: curl_easy_init failed.");
557 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1);
558 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
559 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
560 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
561 PACKAGE_NAME"/"PACKAGE_VERSION);
562 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
563 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
565 if (db->user != NULL)
567 size_t credentials_size;
569 credentials_size = strlen (db->user) + 2;
570 if (db->pass != NULL)
571 credentials_size += strlen (db->pass);
573 db->credentials = (char *) malloc (credentials_size);
574 if (db->credentials == NULL)
576 ERROR ("curl_json plugin: malloc failed.");
580 ssnprintf (db->credentials, credentials_size, "%s:%s",
581 db->user, (db->pass == NULL) ? "" : db->pass);
582 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
585 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
586 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
587 db->verify_host ? 2 : 0);
588 if (db->cacert != NULL)
589 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
592 } /* }}} int cj_init_curl */
594 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
600 if ((ci->values_num != 1)
601 || (ci->values[0].type != OCONFIG_TYPE_STRING))
603 WARNING ("curl_json plugin: The `URL' block "
604 "needs exactly one string argument.");
608 db = (cj_t *) malloc (sizeof (*db));
611 ERROR ("curl_json plugin: malloc failed.");
614 memset (db, 0, sizeof (*db));
616 if (strcasecmp ("URL", ci->key) == 0)
618 status = cj_config_add_string ("URL", &db->url, ci);
627 ERROR ("curl_json plugin: cj_config: "
628 "Invalid key: %s", ci->key);
632 /* Fill the `cj_t' structure.. */
633 for (i = 0; i < ci->children_num; i++)
635 oconfig_item_t *child = ci->children + i;
637 if (strcasecmp ("Instance", child->key) == 0)
638 status = cj_config_add_string ("Instance", &db->instance, child);
639 else if (strcasecmp ("Host", child->key) == 0)
640 status = cj_config_add_string ("Host", &db->host, child);
641 else if (strcasecmp ("User", child->key) == 0)
642 status = cj_config_add_string ("User", &db->user, child);
643 else if (strcasecmp ("Password", child->key) == 0)
644 status = cj_config_add_string ("Password", &db->pass, child);
645 else if (strcasecmp ("VerifyPeer", child->key) == 0)
646 status = cj_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
647 else if (strcasecmp ("VerifyHost", child->key) == 0)
648 status = cj_config_set_boolean ("VerifyHost", &db->verify_host, child);
649 else if (strcasecmp ("CACert", child->key) == 0)
650 status = cj_config_add_string ("CACert", &db->cacert, child);
651 else if (strcasecmp ("Key", child->key) == 0)
652 status = cj_config_add_key (db, child);
655 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
665 if (db->tree == NULL)
667 WARNING ("curl_json plugin: No (valid) `Key' block "
668 "within `URL' block `%s'.", db->url);
672 status = cj_init_curl (db);
675 /* If all went well, register this database for reading */
679 char cb_name[DATA_MAX_NAME_LEN];
681 if (db->instance == NULL)
682 db->instance = strdup("default");
684 DEBUG ("curl_json plugin: Registering new read callback: %s",
687 memset (&ud, 0, sizeof (ud));
688 ud.data = (void *) db;
689 ud.free_func = cj_free;
691 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
692 db->instance, db->url);
694 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
695 /* interval = */ NULL, &ud);
705 /* }}} int cj_config_add_database */
707 static int cj_config (oconfig_item_t *ci) /* {{{ */
717 for (i = 0; i < ci->children_num; i++)
719 oconfig_item_t *child = ci->children + i;
721 if (strcasecmp ("URL", child->key) == 0)
723 status = cj_config_add_url (child);
731 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
736 if ((success == 0) && (errors > 0))
738 ERROR ("curl_json plugin: All statements failed.");
743 } /* }}} int cj_config */
745 /* }}} End of configuration handling functions */
747 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
749 value_list_t vl = VALUE_LIST_INIT;
755 if ((db->host == NULL)
756 || (strcmp ("", db->host) == 0)
757 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
762 if (key->instance == NULL)
763 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
764 db->state[db->depth-1].name, db->state[db->depth].name);
766 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
768 sstrncpy (vl.host, host, sizeof (vl.host));
769 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
770 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
771 sstrncpy (vl.type, key->type, sizeof (vl.type));
773 plugin_dispatch_values (&vl);
774 } /* }}} int cj_submit */
776 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
781 yajl_handle yprev = db->yajl;
783 db->yajl = yajl_alloc (&ycallbacks,
785 /* alloc funcs = */ NULL,
787 /* alloc funcs = */ NULL, NULL,
789 /* context = */ (void *)db);
790 if (db->yajl == NULL)
792 ERROR ("curl_json plugin: yajl_alloc failed.");
797 status = curl_easy_perform (curl);
799 yajl_free (db->yajl);
802 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
803 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
805 /* The response code is zero if a non-HTTP transport was used. */
806 if ((rc != 0) && (rc != 200))
808 ERROR ("curl_json plugin: curl_easy_perform failed with response code %ld (%s)",
815 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
816 status, db->curl_errbuf, url);
821 } /* }}} int cj_curl_perform */
823 static int cj_read (user_data_t *ud) /* {{{ */
827 if ((ud == NULL) || (ud->data == NULL))
829 ERROR ("curl_json plugin: cj_read: Invalid user data.");
833 db = (cj_t *) ud->data;
836 memset (&db->state, 0, sizeof(db->state));
837 db->state[db->depth].tree = db->tree;
840 return cj_curl_perform (db, db->curl);
841 } /* }}} int cj_read */
843 void module_register (void)
845 plugin_register_complex_config ("curl_json", cj_config);
846 } /* void module_register */
848 /* vim: set sw=2 sts=2 et fdm=marker : */