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)
102 && (status != yajl_status_insufficient_data))
105 yajl_get_error(db->yajl, /* verbose = */ 1,
106 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
107 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
108 yajl_free_error(db->yajl, msg);
109 return (0); /* abort write callback */
113 } /* }}} size_t cj_curl_callback */
115 static int cj_get_type (cj_key_t *key)
117 const data_set_t *ds;
119 ds = plugin_get_ds (key->type);
121 return -1; /* let plugin_write do the complaining */
123 return ds->ds[0].type; /* XXX support ds->ds_len > 1 */
127 #define CJ_CB_ABORT 0
128 #define CJ_CB_CONTINUE 1
130 /* "number" may not be null terminated, so copy it into a buffer before
132 static int cj_cb_number (void *ctx,
133 const char *number, unsigned int number_len)
135 char buffer[number_len + 1];
137 cj_t *db = (cj_t *)ctx;
138 cj_key_t *key = db->state[db->depth].key;
144 return (CJ_CB_CONTINUE);
146 memcpy (buffer, number, number_len);
147 buffer[sizeof (buffer) - 1] = 0;
149 type = cj_get_type (key);
154 if (type == DS_TYPE_COUNTER)
155 vt.counter = (counter_t) strtoull (buffer, &endptr, /* base = */ 0);
156 else if (type == DS_TYPE_GAUGE)
157 vt.gauge = (gauge_t) strtod (buffer, &endptr);
158 else if (type == DS_TYPE_DERIVE)
159 vt.derive = (derive_t) strtoll (buffer, &endptr, /* base = */ 0);
160 else if (type == DS_TYPE_ABSOLUTE)
161 vt.absolute = (absolute_t) strtoull (buffer, &endptr, /* base = */ 0);
164 ERROR ("curl_json plugin: Unknown data source type: \"%s\"", key->type);
165 return (CJ_CB_ABORT);
168 if ((endptr == &buffer[0]) || (errno != 0))
170 NOTICE ("curl_json plugin: Overflow while parsing number. "
171 "Ignoring this value.");
172 return (CJ_CB_CONTINUE);
175 cj_submit (db, key, &vt);
176 return (CJ_CB_CONTINUE);
177 } /* int cj_cb_number */
179 static int cj_cb_map_key (void *ctx, const unsigned char *val,
182 cj_t *db = (cj_t *)ctx;
185 tree = db->state[db->depth-1].tree;
192 name = db->state[db->depth].name;
193 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
194 sstrncpy (name, (char *)val, len+1);
196 if (c_avl_get (tree, name, (void *) &value) == 0)
197 db->state[db->depth].key = value;
198 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
199 db->state[db->depth].key = value;
201 db->state[db->depth].key = NULL;
204 return (CJ_CB_CONTINUE);
207 static int cj_cb_string (void *ctx, const unsigned char *val,
210 cj_t *db = (cj_t *)ctx;
214 if (db->depth != 1) /* e.g. _all_dbs */
215 return (CJ_CB_CONTINUE);
217 cj_cb_map_key (ctx, val, len); /* same logic */
219 tree = db->state[db->depth].tree;
221 if ((tree != NULL) && (ptr = rindex (db->url, '/')))
226 /* url =~ s,[^/]+$,$name, */
227 len = (ptr - db->url) + 1;
229 sstrncpy (ptr, db->url, sizeof (url));
230 sstrncpy (ptr + len, db->state[db->depth].name, sizeof (url) - len);
232 curl = curl_easy_duphandle (db->curl);
233 curl_easy_setopt (curl, CURLOPT_URL, url);
234 cj_curl_perform (db, curl);
235 curl_easy_cleanup (curl);
237 return (CJ_CB_CONTINUE);
240 static int cj_cb_start (void *ctx)
242 cj_t *db = (cj_t *)ctx;
243 if (++db->depth >= YAJL_MAX_DEPTH)
245 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
246 return (CJ_CB_ABORT);
248 return (CJ_CB_CONTINUE);
251 static int cj_cb_end (void *ctx)
253 cj_t *db = (cj_t *)ctx;
254 db->state[db->depth].tree = NULL;
256 return (CJ_CB_CONTINUE);
259 static int cj_cb_start_map (void *ctx)
261 return cj_cb_start (ctx);
264 static int cj_cb_end_map (void *ctx)
266 return cj_cb_end (ctx);
269 static int cj_cb_start_array (void * ctx)
271 return cj_cb_start (ctx);
274 static int cj_cb_end_array (void * ctx)
276 return cj_cb_start (ctx);
279 static yajl_callbacks ycallbacks = {
293 /* end yajl callbacks */
295 static void cj_key_free (cj_key_t *key) /* {{{ */
302 sfree (key->instance);
305 } /* }}} void cj_key_free */
307 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
312 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
314 cj_key_t *key = (cj_key_t *)value;
319 cj_tree_free ((c_avl_tree_t *)value);
324 c_avl_destroy (tree);
325 } /* }}} void cj_tree_free */
327 static void cj_free (void *arg) /* {{{ */
331 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
338 if (db->curl != NULL)
339 curl_easy_cleanup (db->curl);
342 if (db->tree != NULL)
343 cj_tree_free (db->tree);
346 sfree (db->instance);
352 sfree (db->credentials);
356 } /* }}} void cj_free */
358 /* Configuration handling functions {{{ */
360 static int cj_config_add_string (const char *name, char **dest, /* {{{ */
363 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
365 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
370 *dest = strdup (ci->values[0].value.string);
375 } /* }}} int cj_config_add_string */
377 static int cj_config_set_boolean (const char *name, int *dest, /* {{{ */
380 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
382 WARNING ("curl_json plugin: `%s' needs exactly one boolean argument.", name);
386 *dest = ci->values[0].value.boolean ? 1 : 0;
389 } /* }}} int cj_config_set_boolean */
391 static c_avl_tree_t *cj_avl_create(void)
393 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
396 static int cj_config_add_key (cj_t *db, /* {{{ */
403 if ((ci->values_num != 1)
404 || (ci->values[0].type != OCONFIG_TYPE_STRING))
406 WARNING ("curl_json plugin: The `Key' block "
407 "needs exactly one string argument.");
411 key = (cj_key_t *) malloc (sizeof (*key));
414 ERROR ("curl_json plugin: malloc failed.");
417 memset (key, 0, sizeof (*key));
418 key->magic = CJ_KEY_MAGIC;
420 if (strcasecmp ("Key", ci->key) == 0)
422 status = cj_config_add_string ("Key", &key->path, ci);
431 ERROR ("curl_json plugin: cj_config: "
432 "Invalid key: %s", ci->key);
437 for (i = 0; i < ci->children_num; i++)
439 oconfig_item_t *child = ci->children + i;
441 if (strcasecmp ("Type", child->key) == 0)
442 status = cj_config_add_string ("Type", &key->type, child);
443 else if (strcasecmp ("Instance", child->key) == 0)
444 status = cj_config_add_string ("Instance", &key->instance, child);
447 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
453 } /* for (i = 0; i < ci->children_num; i++) */
457 if (key->type == NULL)
459 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
464 } /* while (status == 0) */
466 /* store path in a tree that will match the json map structure, example:
467 * "httpd/requests/count",
468 * "httpd/requests/current" ->
469 * { "httpd": { "requests": { "count": $key, "current": $key } } }
478 if (db->tree == NULL)
479 db->tree = cj_avl_create();
498 sstrncpy (ent, name, len+1);
500 if (c_avl_get (tree, ent, (void *) &value) != 0)
502 value = cj_avl_create ();
503 c_avl_insert (tree, strdup (ent), value);
512 c_avl_insert (tree, strdup(name), key);
515 ERROR ("curl_json plugin: invalid key: %s", key->path);
521 } /* }}} int cj_config_add_key */
523 static int cj_init_curl (cj_t *db) /* {{{ */
525 db->curl = curl_easy_init ();
526 if (db->curl == NULL)
528 ERROR ("curl_json plugin: curl_easy_init failed.");
532 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
533 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
534 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
535 PACKAGE_NAME"/"PACKAGE_VERSION);
536 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
537 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
539 if (db->user != NULL)
541 size_t credentials_size;
543 credentials_size = strlen (db->user) + 2;
544 if (db->pass != NULL)
545 credentials_size += strlen (db->pass);
547 db->credentials = (char *) malloc (credentials_size);
548 if (db->credentials == NULL)
550 ERROR ("curl_json plugin: malloc failed.");
554 ssnprintf (db->credentials, credentials_size, "%s:%s",
555 db->user, (db->pass == NULL) ? "" : db->pass);
556 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
559 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
560 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
561 db->verify_host ? 2 : 0);
562 if (db->cacert != NULL)
563 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
566 } /* }}} int cj_init_curl */
568 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
574 if ((ci->values_num != 1)
575 || (ci->values[0].type != OCONFIG_TYPE_STRING))
577 WARNING ("curl_json plugin: The `URL' block "
578 "needs exactly one string argument.");
582 db = (cj_t *) malloc (sizeof (*db));
585 ERROR ("curl_json plugin: malloc failed.");
588 memset (db, 0, sizeof (*db));
590 if (strcasecmp ("URL", ci->key) == 0)
592 status = cj_config_add_string ("URL", &db->url, ci);
601 ERROR ("curl_json plugin: cj_config: "
602 "Invalid key: %s", ci->key);
606 /* Fill the `cj_t' structure.. */
607 for (i = 0; i < ci->children_num; i++)
609 oconfig_item_t *child = ci->children + i;
611 if (strcasecmp ("Instance", child->key) == 0)
612 status = cj_config_add_string ("Instance", &db->instance, child);
613 else if (strcasecmp ("Host", child->key) == 0)
614 status = cj_config_add_string ("Host", &db->host, child);
615 else if (strcasecmp ("User", child->key) == 0)
616 status = cj_config_add_string ("User", &db->user, child);
617 else if (strcasecmp ("Password", child->key) == 0)
618 status = cj_config_add_string ("Password", &db->pass, child);
619 else if (strcasecmp ("VerifyPeer", child->key) == 0)
620 status = cj_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
621 else if (strcasecmp ("VerifyHost", child->key) == 0)
622 status = cj_config_set_boolean ("VerifyHost", &db->verify_host, child);
623 else if (strcasecmp ("CACert", child->key) == 0)
624 status = cj_config_add_string ("CACert", &db->cacert, child);
625 else if (strcasecmp ("Key", child->key) == 0)
626 status = cj_config_add_key (db, child);
629 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
639 if (db->tree == NULL)
641 WARNING ("curl_json plugin: No (valid) `Key' block "
642 "within `URL' block `%s'.", db->url);
646 status = cj_init_curl (db);
649 /* If all went well, register this database for reading */
653 char cb_name[DATA_MAX_NAME_LEN];
655 if (db->instance == NULL)
656 db->instance = strdup("default");
658 DEBUG ("curl_json plugin: Registering new read callback: %s",
661 memset (&ud, 0, sizeof (ud));
662 ud.data = (void *) db;
663 ud.free_func = cj_free;
665 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
666 db->instance, db->url);
668 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
669 /* interval = */ NULL, &ud);
679 /* }}} int cj_config_add_database */
681 static int cj_config (oconfig_item_t *ci) /* {{{ */
691 for (i = 0; i < ci->children_num; i++)
693 oconfig_item_t *child = ci->children + i;
695 if (strcasecmp ("URL", child->key) == 0)
697 status = cj_config_add_url (child);
705 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
710 if ((success == 0) && (errors > 0))
712 ERROR ("curl_json plugin: All statements failed.");
717 } /* }}} int cj_config */
719 /* }}} End of configuration handling functions */
721 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
723 value_list_t vl = VALUE_LIST_INIT;
729 if ((db->host == NULL)
730 || (strcmp ("", db->host) == 0)
731 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
736 if (key->instance == NULL)
737 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
738 db->state[db->depth-1].name, db->state[db->depth].name);
740 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
742 sstrncpy (vl.host, host, sizeof (vl.host));
743 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
744 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
745 sstrncpy (vl.type, key->type, sizeof (vl.type));
747 plugin_dispatch_values (&vl);
748 } /* }}} int cj_submit */
750 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
755 yajl_handle yprev = db->yajl;
757 db->yajl = yajl_alloc (&ycallbacks, NULL, NULL, (void *)db);
758 if (db->yajl == NULL)
760 ERROR ("curl_json plugin: yajl_alloc failed.");
764 status = curl_easy_perform (curl);
767 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
768 status, db->curl_errbuf, url);
769 yajl_free (db->yajl);
774 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
775 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
777 /* The response code is zero if a non-HTTP transport was used. */
778 if ((rc != 0) && (rc != 200))
780 ERROR ("curl_json plugin: curl_easy_perform failed with "
781 "response code %ld (%s)", rc, url);
782 yajl_free (db->yajl);
787 status = yajl_parse_complete (db->yajl);
788 if (status != yajl_status_ok)
790 unsigned char *errmsg;
792 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
793 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
794 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
796 yajl_free_error (db->yajl, errmsg);
797 yajl_free (db->yajl);
802 yajl_free (db->yajl);
805 } /* }}} int cj_curl_perform */
807 static int cj_read (user_data_t *ud) /* {{{ */
811 if ((ud == NULL) || (ud->data == NULL))
813 ERROR ("curl_json plugin: cj_read: Invalid user data.");
817 db = (cj_t *) ud->data;
820 memset (&db->state, 0, sizeof(db->state));
821 db->state[db->depth].tree = db->tree;
824 return cj_curl_perform (db, db->curl);
825 } /* }}} int cj_read */
827 void module_register (void)
829 plugin_register_complex_config ("curl_json", cj_config);
830 } /* void module_register */
832 /* vim: set sw=2 sts=2 et fdm=marker : */