2 * collectd - src/couchdb.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 COUCHDB_DEFAULT_HOST "localhost"
34 #define COUCHDB_KEY_MAGIC 0x43484b59UL /* CHKY */
35 #define COUCHDB_IS_KEY(key) (key)->magic == COUCHDB_KEY_MAGIC
36 #define COUCHDB_ANY "*"
37 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
40 typedef struct couchdb_key_s couchdb_key_t;
41 struct couchdb_key_s /* {{{ */
50 struct couchdb_s /* {{{ */
64 char curl_errbuf[CURL_ERROR_SIZE];
75 char name[DATA_MAX_NAME_LEN];
76 } state[YAJL_MAX_DEPTH];
78 typedef struct couchdb_s couchdb_t; /* }}} */
80 static int couchdb_read (user_data_t *ud);
81 static int couchdb_curl_perform (couchdb_t *db, CURL *curl);
82 static void couchdb_submit (couchdb_t *db, couchdb_key_t *key, value_t *value);
84 static size_t couchdb_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 ("couchdb plugin: yajl_parse failed: %s", msg);
114 yajl_free_error(db->yajl, msg);
115 return (0); /* abort write callback */
119 } /* }}} size_t couchdb_curl_callback */
121 static int couchdb_get_type (couchdb_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 couchcb_integer (void *ctx, long val)
135 couchdb_t *db = (couchdb_t *)ctx;
136 couchdb_key_t *key = db->state[db->depth].key;
143 type = couchdb_get_type (key);
144 if (type == DS_TYPE_COUNTER)
147 vt.gauge = (double)val;
149 couchdb_submit (db, key, &vt);
154 static int couchcb_double (void *ctx, double val)
156 couchdb_t *db = (couchdb_t *)ctx;
157 couchdb_key_t *key = db->state[db->depth].key;
164 type = couchdb_get_type (key);
165 if (type == DS_TYPE_GAUGE)
170 couchdb_submit (db, key, &vt);
175 static int couchcb_map_key (void *ctx, const unsigned char *val,
178 couchdb_t *db = (couchdb_t *)ctx;
181 tree = db->state[db->depth-1].tree;
185 couchdb_key_t *value;
188 name = db->state[db->depth].name;
189 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
190 sstrncpy (name, (char *)val, len+1);
192 if (c_avl_get (tree, name, (void *) &value) == 0)
193 db->state[db->depth].key = value;
194 else if (c_avl_get (tree, COUCHDB_ANY, (void *) &value) == 0)
195 db->state[db->depth].key = value;
197 db->state[db->depth].key = NULL;
203 static int couchcb_string (void *ctx, const unsigned char *val,
206 couchdb_t *db = (couchdb_t *)ctx;
210 if (db->depth != 1) /* e.g. _all_dbs */
213 couchcb_map_key (ctx, val, len); /* same logic */
215 tree = db->state[db->depth].tree;
217 if ((tree != NULL) && (ptr = rindex (db->url, '/')))
222 /* url =~ s,[^/]+$,$name, */
223 len = (ptr - db->url) + 1;
225 sstrncpy (ptr, db->url, sizeof (url));
226 sstrncpy (ptr + len, db->state[db->depth].name, sizeof (url) - len);
228 curl = curl_easy_duphandle (db->curl);
229 curl_easy_setopt (curl, CURLOPT_URL, url);
230 couchdb_curl_perform (db, curl);
231 curl_easy_cleanup (curl);
236 static int couchcb_start (void *ctx)
238 couchdb_t *db = (couchdb_t *)ctx;
239 if (++db->depth >= YAJL_MAX_DEPTH)
241 ERROR ("couchdb plugin: %s depth exceeds max, aborting.", db->url);
247 static int couchcb_end (void *ctx)
249 couchdb_t *db = (couchdb_t *)ctx;
250 db->state[db->depth].tree = NULL;
255 static int couchcb_start_map (void *ctx)
257 return couchcb_start (ctx);
260 static int couchcb_end_map (void *ctx)
262 return couchcb_end (ctx);
265 static int couchcb_start_array (void * ctx)
267 return couchcb_start (ctx);
270 static int couchcb_end_array (void * ctx)
272 return couchcb_start (ctx);
275 static yajl_callbacks ycallbacks = {
289 /* end yajl callbacks */
291 static void couchdb_key_free (couchdb_key_t *key) /* {{{ */
298 sfree (key->instance);
301 } /* }}} void couchdb_key_free */
303 static void couchdb_tree_free (c_avl_tree_t *tree) /* {{{ */
308 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
310 couchdb_key_t *key = (couchdb_key_t *)value;
312 if (COUCHDB_IS_KEY(key))
313 couchdb_key_free (key);
315 couchdb_tree_free ((c_avl_tree_t *)value);
320 c_avl_destroy (tree);
321 } /* }}} void couchdb_tree_free */
323 static void couchdb_free (void *arg) /* {{{ */
327 DEBUG ("couchdb plugin: couchdb_free (arg = %p);", arg);
329 db = (couchdb_t *) arg;
334 if (db->curl != NULL)
335 curl_easy_cleanup (db->curl);
338 if (db->tree != NULL)
339 couchdb_tree_free (db->tree);
342 sfree (db->instance);
348 sfree (db->credentials);
352 } /* }}} void couchdb_free */
354 /* Configuration handling functions {{{ */
356 static int couchdb_config_add_string (const char *name, char **dest, /* {{{ */
359 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
361 WARNING ("couchdb plugin: `%s' needs exactly one string argument.", name);
366 *dest = strdup (ci->values[0].value.string);
371 } /* }}} int couchdb_config_add_string */
373 static int couchdb_config_set_boolean (const char *name, int *dest, /* {{{ */
376 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
378 WARNING ("couchdb plugin: `%s' needs exactly one boolean argument.", name);
382 *dest = ci->values[0].value.boolean ? 1 : 0;
385 } /* }}} int couchdb_config_set_boolean */
387 static c_avl_tree_t *couchdb_avl_create(void)
389 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
392 static int couchdb_config_add_key (couchdb_t *db, /* {{{ */
399 if ((ci->values_num != 1)
400 || (ci->values[0].type != OCONFIG_TYPE_STRING))
402 WARNING ("couchdb plugin: The `Key' block "
403 "needs exactly one string argument.");
407 key = (couchdb_key_t *) malloc (sizeof (*key));
410 ERROR ("couchdb plugin: malloc failed.");
413 memset (key, 0, sizeof (*key));
414 key->magic = COUCHDB_KEY_MAGIC;
416 if (strcasecmp ("Key", ci->key) == 0)
418 status = couchdb_config_add_string ("Key", &key->path, ci);
427 ERROR ("couchdb plugin: couchdb_config: "
428 "Invalid key: %s", ci->key);
433 for (i = 0; i < ci->children_num; i++)
435 oconfig_item_t *child = ci->children + i;
437 if (strcasecmp ("Type", child->key) == 0)
438 status = couchdb_config_add_string ("Type", &key->type, child);
439 else if (strcasecmp ("Instance", child->key) == 0)
440 status = couchdb_config_add_string ("Instance", &key->instance, child);
443 WARNING ("couchdb plugin: Option `%s' not allowed here.", child->key);
449 } /* for (i = 0; i < ci->children_num; i++) */
453 if (key->type == NULL)
455 WARNING ("couchdb plugin: `Type' missing in `Key' block.");
460 } /* while (status == 0) */
462 /* store path in a tree that will match the json map structure, example:
463 * "httpd/requests/count",
464 * "httpd/requests/current" ->
465 * { "httpd": { "requests": { "count": $key, "current": $key } } }
474 if (db->tree == NULL)
475 db->tree = couchdb_avl_create();
494 sstrncpy (ent, name, len+1);
496 if (c_avl_get (tree, ent, (void *) &value) != 0)
498 value = couchdb_avl_create ();
499 c_avl_insert (tree, strdup (ent), value);
508 c_avl_insert (tree, strdup(name), key);
511 ERROR ("couchdb plugin: invalid key: %s", key->path);
517 } /* }}} int couchdb_config_add_key */
519 static int couchdb_init_curl (couchdb_t *db) /* {{{ */
521 db->curl = curl_easy_init ();
522 if (db->curl == NULL)
524 ERROR ("couchdb plugin: curl_easy_init failed.");
528 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, couchdb_curl_callback);
529 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
530 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
531 PACKAGE_NAME"/"PACKAGE_VERSION);
532 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
533 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
535 if (db->user != NULL)
537 size_t credentials_size;
539 credentials_size = strlen (db->user) + 2;
540 if (db->pass != NULL)
541 credentials_size += strlen (db->pass);
543 db->credentials = (char *) malloc (credentials_size);
544 if (db->credentials == NULL)
546 ERROR ("couchdb plugin: malloc failed.");
550 ssnprintf (db->credentials, credentials_size, "%s:%s",
551 db->user, (db->pass == NULL) ? "" : db->pass);
552 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
555 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
556 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
557 db->verify_host ? 2 : 0);
558 if (db->cacert != NULL)
559 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
562 } /* }}} int couchdb_init_curl */
564 static int couchdb_config_add_url (oconfig_item_t *ci) /* {{{ */
570 if ((ci->values_num != 1)
571 || (ci->values[0].type != OCONFIG_TYPE_STRING))
573 WARNING ("couchdb plugin: The `URL' block "
574 "needs exactly one string argument.");
578 db = (couchdb_t *) malloc (sizeof (*db));
581 ERROR ("couchdb plugin: malloc failed.");
584 memset (db, 0, sizeof (*db));
586 if (strcasecmp ("URL", ci->key) == 0)
588 status = couchdb_config_add_string ("URL", &db->url, ci);
597 ERROR ("couchdb plugin: couchdb_config: "
598 "Invalid key: %s", ci->key);
602 /* Fill the `couchdb_t' structure.. */
603 for (i = 0; i < ci->children_num; i++)
605 oconfig_item_t *child = ci->children + i;
607 if (strcasecmp ("Instance", child->key) == 0)
608 status = couchdb_config_add_string ("Instance", &db->instance, child);
609 else if (strcasecmp ("Host", child->key) == 0)
610 status = couchdb_config_add_string ("Host", &db->host, child);
611 else if (strcasecmp ("User", child->key) == 0)
612 status = couchdb_config_add_string ("User", &db->user, child);
613 else if (strcasecmp ("Password", child->key) == 0)
614 status = couchdb_config_add_string ("Password", &db->pass, child);
615 else if (strcasecmp ("VerifyPeer", child->key) == 0)
616 status = couchdb_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
617 else if (strcasecmp ("VerifyHost", child->key) == 0)
618 status = couchdb_config_set_boolean ("VerifyHost", &db->verify_host, child);
619 else if (strcasecmp ("CACert", child->key) == 0)
620 status = couchdb_config_add_string ("CACert", &db->cacert, child);
621 else if (strcasecmp ("Key", child->key) == 0)
622 status = couchdb_config_add_key (db, child);
625 WARNING ("couchdb plugin: Option `%s' not allowed here.", child->key);
635 if (db->tree == NULL)
637 WARNING ("couchdb plugin: No (valid) `Key' block "
638 "within `URL' block `%s'.", db->url);
642 status = couchdb_init_curl (db);
645 /* If all went well, register this database for reading */
649 char cb_name[DATA_MAX_NAME_LEN];
651 if (db->instance == NULL)
652 db->instance = strdup("default");
654 DEBUG ("couchdb plugin: Registering new read callback: %s",
657 memset (&ud, 0, sizeof (ud));
658 ud.data = (void *) db;
659 ud.free_func = couchdb_free;
661 ssnprintf (cb_name, sizeof (cb_name), "couchdb-%s-%s",
662 db->instance, db->url);
664 plugin_register_complex_read (cb_name, couchdb_read,
665 /* interval = */ NULL, &ud);
675 /* }}} int couchdb_config_add_database */
677 static int couchdb_config (oconfig_item_t *ci) /* {{{ */
687 for (i = 0; i < ci->children_num; i++)
689 oconfig_item_t *child = ci->children + i;
691 if (strcasecmp ("URL", child->key) == 0)
693 status = couchdb_config_add_url (child);
701 WARNING ("couchdb plugin: Option `%s' not allowed here.", child->key);
706 if ((success == 0) && (errors > 0))
708 ERROR ("couchdb plugin: All statements failed.");
713 } /* }}} int couchdb_config */
715 /* }}} End of configuration handling functions */
717 static void couchdb_submit (couchdb_t *db, couchdb_key_t *key, value_t *value) /* {{{ */
719 value_list_t vl = VALUE_LIST_INIT;
725 if ((db->host == NULL)
726 || (strcmp ("", db->host) == 0)
727 || (strcmp (COUCHDB_DEFAULT_HOST, db->host) == 0))
732 if (key->instance == NULL)
733 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
734 db->state[db->depth-1].name, db->state[db->depth].name);
736 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
738 sstrncpy (vl.host, host, sizeof (vl.host));
739 sstrncpy (vl.plugin, "couchdb", sizeof (vl.plugin));
740 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
741 sstrncpy (vl.type, key->type, sizeof (vl.type));
743 plugin_dispatch_values (&vl);
744 } /* }}} int couchdb_submit */
746 static int couchdb_curl_perform (couchdb_t *db, CURL *curl) /* {{{ */
751 yajl_handle yprev = db->yajl;
753 db->yajl = yajl_alloc (&ycallbacks, NULL, NULL, (void *)db);
754 if (db->yajl == NULL)
756 ERROR ("couchdb plugin: yajl_alloc failed.");
760 status = curl_easy_perform (curl);
762 yajl_free (db->yajl);
765 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
766 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
770 ERROR ("couchdb plugin: curl_easy_perform failed with response code %ld (%s)",
777 ERROR ("couchdb plugin: curl_easy_perform failed with status %i: %s (%s)",
778 status, db->curl_errbuf, url);
783 } /* }}} int couchdb_curl_perform */
785 static int couchdb_read (user_data_t *ud) /* {{{ */
789 if ((ud == NULL) || (ud->data == NULL))
791 ERROR ("couchdb plugin: couchdb_read: Invalid user data.");
795 db = (couchdb_t *) ud->data;
798 memset (&db->state, 0, sizeof(db->state));
799 db->state[db->depth].tree = db->tree;
802 return couchdb_curl_perform (db, db->curl);
803 } /* }}} int couchdb_read */
805 void module_register (void)
807 plugin_register_complex_config ("couchdb", couchdb_config);
808 } /* void module_register */