2 * collectd - src/curl_json.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2010 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 #define CJ_CB_ABORT 0
134 #define CJ_CB_CONTINUE 1
136 /* "number" may not be null terminated, so copy it into a buffer before
138 static int cj_cb_number (void *ctx,
139 const char *number, unsigned int number_len)
141 char buffer[number_len + 1];
143 cj_t *db = (cj_t *)ctx;
144 cj_key_t *key = db->state[db->depth].key;
150 return (CJ_CB_CONTINUE);
152 memcpy (buffer, number, number_len);
153 buffer[sizeof (buffer) - 1] = 0;
155 type = cj_get_type (key);
160 if (type == DS_TYPE_COUNTER)
161 vt.counter = (counter_t) strtoull (buffer, &endptr, /* base = */ 0);
162 else if (type == DS_TYPE_GAUGE)
163 vt.gauge = (gauge_t) strtod (buffer, &endptr);
164 else if (type == DS_TYPE_DERIVE)
165 vt.derive = (derive_t) strtoll (buffer, &endptr, /* base = */ 0);
166 else if (type == DS_TYPE_ABSOLUTE)
167 vt.absolute = (absolute_t) strtoull (buffer, &endptr, /* base = */ 0);
170 ERROR ("curl_json plugin: Unknown data source type: \"%s\"", key->type);
171 return (CJ_CB_ABORT);
174 if ((endptr == &buffer[0]) || (errno != 0))
176 NOTICE ("curl_json plugin: Overflow while parsing number. "
177 "Ignoring this value.");
178 return (CJ_CB_CONTINUE);
181 cj_submit (db, key, &vt);
182 return (CJ_CB_CONTINUE);
183 } /* int cj_cb_number */
185 static int cj_cb_map_key (void *ctx, const unsigned char *val,
188 cj_t *db = (cj_t *)ctx;
191 tree = db->state[db->depth-1].tree;
198 name = db->state[db->depth].name;
199 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
200 sstrncpy (name, (char *)val, len+1);
202 if (c_avl_get (tree, name, (void *) &value) == 0)
203 db->state[db->depth].key = value;
204 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
205 db->state[db->depth].key = value;
207 db->state[db->depth].key = NULL;
210 return (CJ_CB_CONTINUE);
213 static int cj_cb_string (void *ctx, const unsigned char *val,
216 cj_t *db = (cj_t *)ctx;
220 if (db->depth != 1) /* e.g. _all_dbs */
221 return (CJ_CB_CONTINUE);
223 cj_cb_map_key (ctx, val, len); /* same logic */
225 tree = db->state[db->depth].tree;
227 if ((tree != NULL) && (ptr = rindex (db->url, '/')))
232 /* url =~ s,[^/]+$,$name, */
233 len = (ptr - db->url) + 1;
235 sstrncpy (ptr, db->url, sizeof (url));
236 sstrncpy (ptr + len, db->state[db->depth].name, sizeof (url) - len);
238 curl = curl_easy_duphandle (db->curl);
239 curl_easy_setopt (curl, CURLOPT_URL, url);
240 cj_curl_perform (db, curl);
241 curl_easy_cleanup (curl);
243 return (CJ_CB_CONTINUE);
246 static int cj_cb_start (void *ctx)
248 cj_t *db = (cj_t *)ctx;
249 if (++db->depth >= YAJL_MAX_DEPTH)
251 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
252 return (CJ_CB_ABORT);
254 return (CJ_CB_CONTINUE);
257 static int cj_cb_end (void *ctx)
259 cj_t *db = (cj_t *)ctx;
260 db->state[db->depth].tree = NULL;
262 return (CJ_CB_CONTINUE);
265 static int cj_cb_start_map (void *ctx)
267 return cj_cb_start (ctx);
270 static int cj_cb_end_map (void *ctx)
272 return cj_cb_end (ctx);
275 static int cj_cb_start_array (void * ctx)
277 return cj_cb_start (ctx);
280 static int cj_cb_end_array (void * ctx)
282 return cj_cb_start (ctx);
285 static yajl_callbacks ycallbacks = {
299 /* end yajl callbacks */
301 static void cj_key_free (cj_key_t *key) /* {{{ */
308 sfree (key->instance);
311 } /* }}} void cj_key_free */
313 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
318 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
320 cj_key_t *key = (cj_key_t *)value;
325 cj_tree_free ((c_avl_tree_t *)value);
330 c_avl_destroy (tree);
331 } /* }}} void cj_tree_free */
333 static void cj_free (void *arg) /* {{{ */
337 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
344 if (db->curl != NULL)
345 curl_easy_cleanup (db->curl);
348 if (db->tree != NULL)
349 cj_tree_free (db->tree);
352 sfree (db->instance);
358 sfree (db->credentials);
362 } /* }}} void cj_free */
364 /* Configuration handling functions {{{ */
366 static int cj_config_add_string (const char *name, char **dest, /* {{{ */
369 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
371 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
376 *dest = strdup (ci->values[0].value.string);
381 } /* }}} int cj_config_add_string */
383 static int cj_config_set_boolean (const char *name, int *dest, /* {{{ */
386 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
388 WARNING ("curl_json plugin: `%s' needs exactly one boolean argument.", name);
392 *dest = ci->values[0].value.boolean ? 1 : 0;
395 } /* }}} int cj_config_set_boolean */
397 static c_avl_tree_t *cj_avl_create(void)
399 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
402 static int cj_config_add_key (cj_t *db, /* {{{ */
409 if ((ci->values_num != 1)
410 || (ci->values[0].type != OCONFIG_TYPE_STRING))
412 WARNING ("curl_json plugin: The `Key' block "
413 "needs exactly one string argument.");
417 key = (cj_key_t *) malloc (sizeof (*key));
420 ERROR ("curl_json plugin: malloc failed.");
423 memset (key, 0, sizeof (*key));
424 key->magic = CJ_KEY_MAGIC;
426 if (strcasecmp ("Key", ci->key) == 0)
428 status = cj_config_add_string ("Key", &key->path, ci);
437 ERROR ("curl_json plugin: cj_config: "
438 "Invalid key: %s", ci->key);
443 for (i = 0; i < ci->children_num; i++)
445 oconfig_item_t *child = ci->children + i;
447 if (strcasecmp ("Type", child->key) == 0)
448 status = cj_config_add_string ("Type", &key->type, child);
449 else if (strcasecmp ("Instance", child->key) == 0)
450 status = cj_config_add_string ("Instance", &key->instance, child);
453 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
459 } /* for (i = 0; i < ci->children_num; i++) */
463 if (key->type == NULL)
465 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
470 } /* while (status == 0) */
472 /* store path in a tree that will match the json map structure, example:
473 * "httpd/requests/count",
474 * "httpd/requests/current" ->
475 * { "httpd": { "requests": { "count": $key, "current": $key } } }
484 if (db->tree == NULL)
485 db->tree = cj_avl_create();
504 sstrncpy (ent, name, len+1);
506 if (c_avl_get (tree, ent, (void *) &value) != 0)
508 value = cj_avl_create ();
509 c_avl_insert (tree, strdup (ent), value);
518 c_avl_insert (tree, strdup(name), key);
521 ERROR ("curl_json plugin: invalid key: %s", key->path);
527 } /* }}} int cj_config_add_key */
529 static int cj_init_curl (cj_t *db) /* {{{ */
531 db->curl = curl_easy_init ();
532 if (db->curl == NULL)
534 ERROR ("curl_json plugin: curl_easy_init failed.");
538 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
539 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
540 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
541 PACKAGE_NAME"/"PACKAGE_VERSION);
542 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
543 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
545 if (db->user != NULL)
547 size_t credentials_size;
549 credentials_size = strlen (db->user) + 2;
550 if (db->pass != NULL)
551 credentials_size += strlen (db->pass);
553 db->credentials = (char *) malloc (credentials_size);
554 if (db->credentials == NULL)
556 ERROR ("curl_json plugin: malloc failed.");
560 ssnprintf (db->credentials, credentials_size, "%s:%s",
561 db->user, (db->pass == NULL) ? "" : db->pass);
562 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
565 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
566 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
567 db->verify_host ? 2 : 0);
568 if (db->cacert != NULL)
569 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
572 } /* }}} int cj_init_curl */
574 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
580 if ((ci->values_num != 1)
581 || (ci->values[0].type != OCONFIG_TYPE_STRING))
583 WARNING ("curl_json plugin: The `URL' block "
584 "needs exactly one string argument.");
588 db = (cj_t *) malloc (sizeof (*db));
591 ERROR ("curl_json plugin: malloc failed.");
594 memset (db, 0, sizeof (*db));
596 if (strcasecmp ("URL", ci->key) == 0)
598 status = cj_config_add_string ("URL", &db->url, ci);
607 ERROR ("curl_json plugin: cj_config: "
608 "Invalid key: %s", ci->key);
612 /* Fill the `cj_t' structure.. */
613 for (i = 0; i < ci->children_num; i++)
615 oconfig_item_t *child = ci->children + i;
617 if (strcasecmp ("Instance", child->key) == 0)
618 status = cj_config_add_string ("Instance", &db->instance, child);
619 else if (strcasecmp ("Host", child->key) == 0)
620 status = cj_config_add_string ("Host", &db->host, child);
621 else if (strcasecmp ("User", child->key) == 0)
622 status = cj_config_add_string ("User", &db->user, child);
623 else if (strcasecmp ("Password", child->key) == 0)
624 status = cj_config_add_string ("Password", &db->pass, child);
625 else if (strcasecmp ("VerifyPeer", child->key) == 0)
626 status = cj_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
627 else if (strcasecmp ("VerifyHost", child->key) == 0)
628 status = cj_config_set_boolean ("VerifyHost", &db->verify_host, child);
629 else if (strcasecmp ("CACert", child->key) == 0)
630 status = cj_config_add_string ("CACert", &db->cacert, child);
631 else if (strcasecmp ("Key", child->key) == 0)
632 status = cj_config_add_key (db, child);
635 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
645 if (db->tree == NULL)
647 WARNING ("curl_json plugin: No (valid) `Key' block "
648 "within `URL' block `%s'.", db->url);
652 status = cj_init_curl (db);
655 /* If all went well, register this database for reading */
659 char cb_name[DATA_MAX_NAME_LEN];
661 if (db->instance == NULL)
662 db->instance = strdup("default");
664 DEBUG ("curl_json plugin: Registering new read callback: %s",
667 memset (&ud, 0, sizeof (ud));
668 ud.data = (void *) db;
669 ud.free_func = cj_free;
671 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
672 db->instance, db->url);
674 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
675 /* interval = */ NULL, &ud);
685 /* }}} int cj_config_add_database */
687 static int cj_config (oconfig_item_t *ci) /* {{{ */
697 for (i = 0; i < ci->children_num; i++)
699 oconfig_item_t *child = ci->children + i;
701 if (strcasecmp ("URL", child->key) == 0)
703 status = cj_config_add_url (child);
711 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
716 if ((success == 0) && (errors > 0))
718 ERROR ("curl_json plugin: All statements failed.");
723 } /* }}} int cj_config */
725 /* }}} End of configuration handling functions */
727 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
729 value_list_t vl = VALUE_LIST_INIT;
735 if ((db->host == NULL)
736 || (strcmp ("", db->host) == 0)
737 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
742 if (key->instance == NULL)
743 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
744 db->state[db->depth-1].name, db->state[db->depth].name);
746 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
748 sstrncpy (vl.host, host, sizeof (vl.host));
749 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
750 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
751 sstrncpy (vl.type, key->type, sizeof (vl.type));
753 plugin_dispatch_values (&vl);
754 } /* }}} int cj_submit */
756 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
761 yajl_handle yprev = db->yajl;
763 db->yajl = yajl_alloc (&ycallbacks, NULL, NULL, (void *)db);
764 if (db->yajl == NULL)
766 ERROR ("curl_json plugin: yajl_alloc failed.");
770 status = curl_easy_perform (curl);
772 yajl_free (db->yajl);
775 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
776 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
778 /* The response code is zero if a non-HTTP transport was used. */
779 if ((rc != 0) && (rc != 200))
781 ERROR ("curl_json plugin: curl_easy_perform failed with response code %ld (%s)",
788 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
789 status, db->curl_errbuf, url);
794 } /* }}} int cj_curl_perform */
796 static int cj_read (user_data_t *ud) /* {{{ */
800 if ((ud == NULL) || (ud->data == NULL))
802 ERROR ("curl_json plugin: cj_read: Invalid user data.");
806 db = (cj_t *) ud->data;
809 memset (&db->state, 0, sizeof(db->state));
810 db->state[db->depth].tree = db->tree;
813 return cj_curl_perform (db, db->curl);
814 } /* }}} int cj_read */
816 void module_register (void)
818 plugin_register_complex_config ("curl_json", cj_config);
819 } /* void module_register */
821 /* vim: set sw=2 sts=2 et fdm=marker : */