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;
143 if ((key == NULL) || !CJ_IS_KEY (key))
144 return (CJ_CB_CONTINUE);
146 memcpy (buffer, number, number_len);
147 buffer[sizeof (buffer) - 1] = 0;
149 type = cj_get_type (key);
150 status = parse_value (buffer, &vt, type);
153 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
154 return (CJ_CB_CONTINUE);
157 cj_submit (db, key, &vt);
158 return (CJ_CB_CONTINUE);
159 } /* int cj_cb_number */
161 static int cj_cb_map_key (void *ctx, const unsigned char *val,
164 cj_t *db = (cj_t *)ctx;
167 tree = db->state[db->depth-1].tree;
174 name = db->state[db->depth].name;
175 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
176 sstrncpy (name, (char *)val, len+1);
178 if (c_avl_get (tree, name, (void *) &value) == 0)
179 db->state[db->depth].key = value;
180 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
181 db->state[db->depth].key = value;
183 db->state[db->depth].key = NULL;
186 return (CJ_CB_CONTINUE);
189 static int cj_cb_string (void *ctx, const unsigned char *val,
192 cj_t *db = (cj_t *)ctx;
195 /* Create a null-terminated version of the string. */
196 memcpy (str, val, len);
199 /* No configuration for this string -> simply return. */
200 if (db->state[db->depth].key == NULL)
201 return (CJ_CB_CONTINUE);
203 if (!CJ_IS_KEY (db->state[db->depth].key))
205 NOTICE ("curl_json plugin: Found string \"%s\", but the configuration "
206 "expects a map here.", str);
207 return (CJ_CB_CONTINUE);
210 /* Handle the string as if it was a number. */
211 return (cj_cb_number (ctx, (const char *) val, len));
212 } /* int cj_cb_string */
214 static int cj_cb_start (void *ctx)
216 cj_t *db = (cj_t *)ctx;
217 if (++db->depth >= YAJL_MAX_DEPTH)
219 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
220 return (CJ_CB_ABORT);
222 return (CJ_CB_CONTINUE);
225 static int cj_cb_end (void *ctx)
227 cj_t *db = (cj_t *)ctx;
228 db->state[db->depth].tree = NULL;
230 return (CJ_CB_CONTINUE);
233 static int cj_cb_start_map (void *ctx)
235 return cj_cb_start (ctx);
238 static int cj_cb_end_map (void *ctx)
240 return cj_cb_end (ctx);
243 static int cj_cb_start_array (void * ctx)
245 return cj_cb_start (ctx);
248 static int cj_cb_end_array (void * ctx)
250 return cj_cb_end (ctx);
253 static yajl_callbacks ycallbacks = {
267 /* end yajl callbacks */
269 static void cj_key_free (cj_key_t *key) /* {{{ */
276 sfree (key->instance);
279 } /* }}} void cj_key_free */
281 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
286 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
288 cj_key_t *key = (cj_key_t *)value;
293 cj_tree_free ((c_avl_tree_t *)value);
298 c_avl_destroy (tree);
299 } /* }}} void cj_tree_free */
301 static void cj_free (void *arg) /* {{{ */
305 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
312 if (db->curl != NULL)
313 curl_easy_cleanup (db->curl);
316 if (db->tree != NULL)
317 cj_tree_free (db->tree);
320 sfree (db->instance);
326 sfree (db->credentials);
330 } /* }}} void cj_free */
332 /* Configuration handling functions {{{ */
334 static c_avl_tree_t *cj_avl_create(void)
336 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
339 static int cj_config_add_key (cj_t *db, /* {{{ */
346 if ((ci->values_num != 1)
347 || (ci->values[0].type != OCONFIG_TYPE_STRING))
349 WARNING ("curl_json plugin: The `Key' block "
350 "needs exactly one string argument.");
354 key = (cj_key_t *) malloc (sizeof (*key));
357 ERROR ("curl_json plugin: malloc failed.");
360 memset (key, 0, sizeof (*key));
361 key->magic = CJ_KEY_MAGIC;
363 if (strcasecmp ("Key", ci->key) == 0)
365 status = cf_util_get_string (ci, &key->path);
374 ERROR ("curl_json plugin: cj_config: "
375 "Invalid key: %s", ci->key);
380 for (i = 0; i < ci->children_num; i++)
382 oconfig_item_t *child = ci->children + i;
384 if (strcasecmp ("Type", child->key) == 0)
385 status = cf_util_get_string (child, &key->type);
386 else if (strcasecmp ("Instance", child->key) == 0)
387 status = cf_util_get_string (child, &key->instance);
390 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
396 } /* for (i = 0; i < ci->children_num; i++) */
400 if (key->type == NULL)
402 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
407 } /* while (status == 0) */
409 /* store path in a tree that will match the json map structure, example:
410 * "httpd/requests/count",
411 * "httpd/requests/current" ->
412 * { "httpd": { "requests": { "count": $key, "current": $key } } }
421 if (db->tree == NULL)
422 db->tree = cj_avl_create();
441 sstrncpy (ent, name, len+1);
443 if (c_avl_get (tree, ent, (void *) &value) != 0)
445 value = cj_avl_create ();
446 c_avl_insert (tree, strdup (ent), value);
455 c_avl_insert (tree, strdup(name), key);
458 ERROR ("curl_json plugin: invalid key: %s", key->path);
464 } /* }}} int cj_config_add_key */
466 static int cj_init_curl (cj_t *db) /* {{{ */
468 db->curl = curl_easy_init ();
469 if (db->curl == NULL)
471 ERROR ("curl_json plugin: curl_easy_init failed.");
475 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
476 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
477 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
478 PACKAGE_NAME"/"PACKAGE_VERSION);
479 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
480 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
482 if (db->user != NULL)
484 size_t credentials_size;
486 credentials_size = strlen (db->user) + 2;
487 if (db->pass != NULL)
488 credentials_size += strlen (db->pass);
490 db->credentials = (char *) malloc (credentials_size);
491 if (db->credentials == NULL)
493 ERROR ("curl_json plugin: malloc failed.");
497 ssnprintf (db->credentials, credentials_size, "%s:%s",
498 db->user, (db->pass == NULL) ? "" : db->pass);
499 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
502 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (int) db->verify_peer);
503 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
504 (int) (db->verify_host ? 2 : 0));
505 if (db->cacert != NULL)
506 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
509 } /* }}} int cj_init_curl */
511 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
517 if ((ci->values_num != 1)
518 || (ci->values[0].type != OCONFIG_TYPE_STRING))
520 WARNING ("curl_json plugin: The `URL' block "
521 "needs exactly one string argument.");
525 db = (cj_t *) malloc (sizeof (*db));
528 ERROR ("curl_json plugin: malloc failed.");
531 memset (db, 0, sizeof (*db));
533 if (strcasecmp ("URL", ci->key) == 0)
535 status = cf_util_get_string (ci, &db->url);
544 ERROR ("curl_json plugin: cj_config: "
545 "Invalid key: %s", ci->key);
549 /* Fill the `cj_t' structure.. */
550 for (i = 0; i < ci->children_num; i++)
552 oconfig_item_t *child = ci->children + i;
554 if (strcasecmp ("Instance", child->key) == 0)
555 status = cf_util_get_string (child, &db->instance);
556 else if (strcasecmp ("Host", child->key) == 0)
557 status = cf_util_get_string (child, &db->host);
558 else if (strcasecmp ("User", child->key) == 0)
559 status = cf_util_get_string (child, &db->user);
560 else if (strcasecmp ("Password", child->key) == 0)
561 status = cf_util_get_string (child, &db->pass);
562 else if (strcasecmp ("VerifyPeer", child->key) == 0)
563 status = cf_util_get_boolean (child, &db->verify_peer);
564 else if (strcasecmp ("VerifyHost", child->key) == 0)
565 status = cf_util_get_boolean (child, &db->verify_host);
566 else if (strcasecmp ("CACert", child->key) == 0)
567 status = cf_util_get_string (child, &db->cacert);
568 else if (strcasecmp ("Key", child->key) == 0)
569 status = cj_config_add_key (db, child);
572 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
582 if (db->tree == NULL)
584 WARNING ("curl_json plugin: No (valid) `Key' block "
585 "within `URL' block `%s'.", db->url);
589 status = cj_init_curl (db);
592 /* If all went well, register this database for reading */
596 char cb_name[DATA_MAX_NAME_LEN];
598 if (db->instance == NULL)
599 db->instance = strdup("default");
601 DEBUG ("curl_json plugin: Registering new read callback: %s",
604 memset (&ud, 0, sizeof (ud));
605 ud.data = (void *) db;
606 ud.free_func = cj_free;
608 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
609 db->instance, db->url);
611 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
612 /* interval = */ NULL, &ud);
622 /* }}} int cj_config_add_database */
624 static int cj_config (oconfig_item_t *ci) /* {{{ */
634 for (i = 0; i < ci->children_num; i++)
636 oconfig_item_t *child = ci->children + i;
638 if (strcasecmp ("URL", child->key) == 0)
640 status = cj_config_add_url (child);
648 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
653 if ((success == 0) && (errors > 0))
655 ERROR ("curl_json plugin: All statements failed.");
660 } /* }}} int cj_config */
662 /* }}} End of configuration handling functions */
664 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
666 value_list_t vl = VALUE_LIST_INIT;
672 if ((db->host == NULL)
673 || (strcmp ("", db->host) == 0)
674 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
679 if (key->instance == NULL)
680 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
681 db->state[db->depth-1].name, db->state[db->depth].name);
683 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
685 sstrncpy (vl.host, host, sizeof (vl.host));
686 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
687 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
688 sstrncpy (vl.type, key->type, sizeof (vl.type));
690 plugin_dispatch_values (&vl);
691 } /* }}} int cj_submit */
693 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
698 yajl_handle yprev = db->yajl;
700 db->yajl = yajl_alloc (&ycallbacks, NULL, NULL, (void *)db);
701 if (db->yajl == NULL)
703 ERROR ("curl_json plugin: yajl_alloc failed.");
709 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
711 status = curl_easy_perform (curl);
714 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
715 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
716 yajl_free (db->yajl);
721 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
723 /* The response code is zero if a non-HTTP transport was used. */
724 if ((rc != 0) && (rc != 200))
726 ERROR ("curl_json plugin: curl_easy_perform failed with "
727 "response code %ld (%s)", rc, url);
728 yajl_free (db->yajl);
733 status = yajl_parse_complete (db->yajl);
734 if (status != yajl_status_ok)
736 unsigned char *errmsg;
738 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
739 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
740 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
742 yajl_free_error (db->yajl, errmsg);
743 yajl_free (db->yajl);
748 yajl_free (db->yajl);
751 } /* }}} int cj_curl_perform */
753 static int cj_read (user_data_t *ud) /* {{{ */
757 if ((ud == NULL) || (ud->data == NULL))
759 ERROR ("curl_json plugin: cj_read: Invalid user data.");
763 db = (cj_t *) ud->data;
766 memset (&db->state, 0, sizeof(db->state));
767 db->state[db->depth].tree = db->tree;
770 return cj_curl_perform (db, db->curl);
771 } /* }}} int cj_read */
773 void module_register (void)
775 plugin_register_complex_config ("curl_json", cj_config);
776 } /* void module_register */
778 /* vim: set sw=2 sts=2 et fdm=marker : */