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"
29 #include "utils_complain.h"
31 #include <curl/curl.h>
32 #include <yajl/yajl_parse.h>
33 #if HAVE_YAJL_YAJL_VERSION_H
34 # include <yajl/yajl_version.h>
37 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
38 # define HAVE_YAJL_V2 1
41 #define CJ_DEFAULT_HOST "localhost"
42 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
43 #define CJ_IS_KEY(key) ((key)->magic == CJ_KEY_MAGIC)
45 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
48 typedef struct cj_key_s cj_key_t;
49 struct cj_key_s /* {{{ */
72 char curl_errbuf[CURL_ERROR_SIZE];
83 char name[DATA_MAX_NAME_LEN];
84 } state[YAJL_MAX_DEPTH];
86 typedef struct cj_s cj_t; /* }}} */
89 typedef size_t yajl_len_t;
91 typedef unsigned int yajl_len_t;
94 static int cj_read (user_data_t *ud);
95 static int cj_curl_perform (cj_t *db, CURL *curl);
96 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
98 static size_t cj_curl_callback (void *buf, /* {{{ */
99 size_t size, size_t nmemb, void *user_data)
114 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
115 if (status == yajl_status_ok)
118 else if (status == yajl_status_insufficient_data)
122 if (status != yajl_status_ok)
125 yajl_get_error(db->yajl, /* verbose = */ 1,
126 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
127 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
128 yajl_free_error(db->yajl, msg);
129 return (0); /* abort write callback */
133 } /* }}} size_t cj_curl_callback */
135 static int cj_get_type (cj_key_t *key)
137 const data_set_t *ds;
139 ds = plugin_get_ds (key->type);
142 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
144 assert (key->type != NULL);
145 if (strcmp (type, key->type) != 0)
147 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
149 sstrncpy (type, key->type, sizeof (type));
154 else if (ds->ds_num > 1)
156 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
158 c_complain_once (LOG_WARNING, &complaint,
159 "curl_json plugin: The type \"%s\" has more than one data source. "
160 "This is currently not supported. I will return the type of the "
161 "first data source, but this will likely lead to problems later on.",
165 return ds->ds[0].type;
169 #define CJ_CB_ABORT 0
170 #define CJ_CB_CONTINUE 1
172 /* "number" may not be null terminated, so copy it into a buffer before
174 static int cj_cb_number (void *ctx,
175 const char *number, yajl_len_t number_len)
177 char buffer[number_len + 1];
179 cj_t *db = (cj_t *)ctx;
180 cj_key_t *key = db->state[db->depth].key;
185 if ((key == NULL) || !CJ_IS_KEY (key))
186 return (CJ_CB_CONTINUE);
188 memcpy (buffer, number, number_len);
189 buffer[sizeof (buffer) - 1] = 0;
191 type = cj_get_type (key);
192 status = parse_value (buffer, &vt, type);
195 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
196 return (CJ_CB_CONTINUE);
199 cj_submit (db, key, &vt);
200 return (CJ_CB_CONTINUE);
201 } /* int cj_cb_number */
203 static int cj_cb_map_key (void *ctx, const unsigned char *val,
206 cj_t *db = (cj_t *)ctx;
209 tree = db->state[db->depth-1].tree;
216 name = db->state[db->depth].name;
217 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
218 sstrncpy (name, (char *)val, len+1);
220 if (c_avl_get (tree, name, (void *) &value) == 0)
221 db->state[db->depth].key = value;
222 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
223 db->state[db->depth].key = value;
225 db->state[db->depth].key = NULL;
228 return (CJ_CB_CONTINUE);
231 static int cj_cb_string (void *ctx, const unsigned char *val,
234 cj_t *db = (cj_t *)ctx;
237 /* Create a null-terminated version of the string. */
238 memcpy (str, val, len);
241 /* No configuration for this string -> simply return. */
242 if (db->state[db->depth].key == NULL)
243 return (CJ_CB_CONTINUE);
245 if (!CJ_IS_KEY (db->state[db->depth].key))
247 NOTICE ("curl_json plugin: Found string \"%s\", but the configuration "
248 "expects a map here.", str);
249 return (CJ_CB_CONTINUE);
252 /* Handle the string as if it was a number. */
253 return (cj_cb_number (ctx, (const char *) val, len));
254 } /* int cj_cb_string */
256 static int cj_cb_start (void *ctx)
258 cj_t *db = (cj_t *)ctx;
259 if (++db->depth >= YAJL_MAX_DEPTH)
261 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
262 return (CJ_CB_ABORT);
264 return (CJ_CB_CONTINUE);
267 static int cj_cb_end (void *ctx)
269 cj_t *db = (cj_t *)ctx;
270 db->state[db->depth].tree = NULL;
272 return (CJ_CB_CONTINUE);
275 static int cj_cb_start_map (void *ctx)
277 return cj_cb_start (ctx);
280 static int cj_cb_end_map (void *ctx)
282 return cj_cb_end (ctx);
285 static int cj_cb_start_array (void * ctx)
287 return cj_cb_start (ctx);
290 static int cj_cb_end_array (void * ctx)
292 return cj_cb_end (ctx);
295 static yajl_callbacks ycallbacks = {
309 /* end yajl callbacks */
311 static void cj_key_free (cj_key_t *key) /* {{{ */
318 sfree (key->instance);
321 } /* }}} void cj_key_free */
323 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
328 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
330 cj_key_t *key = (cj_key_t *)value;
335 cj_tree_free ((c_avl_tree_t *)value);
340 c_avl_destroy (tree);
341 } /* }}} void cj_tree_free */
343 static void cj_free (void *arg) /* {{{ */
347 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
354 if (db->curl != NULL)
355 curl_easy_cleanup (db->curl);
358 if (db->tree != NULL)
359 cj_tree_free (db->tree);
362 sfree (db->instance);
368 sfree (db->credentials);
372 } /* }}} void cj_free */
374 /* Configuration handling functions {{{ */
376 static c_avl_tree_t *cj_avl_create(void)
378 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
381 static int cj_config_add_key (cj_t *db, /* {{{ */
388 if ((ci->values_num != 1)
389 || (ci->values[0].type != OCONFIG_TYPE_STRING))
391 WARNING ("curl_json plugin: The `Key' block "
392 "needs exactly one string argument.");
396 key = (cj_key_t *) malloc (sizeof (*key));
399 ERROR ("curl_json plugin: malloc failed.");
402 memset (key, 0, sizeof (*key));
403 key->magic = CJ_KEY_MAGIC;
405 if (strcasecmp ("Key", ci->key) == 0)
407 status = cf_util_get_string (ci, &key->path);
416 ERROR ("curl_json plugin: cj_config: "
417 "Invalid key: %s", ci->key);
422 for (i = 0; i < ci->children_num; i++)
424 oconfig_item_t *child = ci->children + i;
426 if (strcasecmp ("Type", child->key) == 0)
427 status = cf_util_get_string (child, &key->type);
428 else if (strcasecmp ("Instance", child->key) == 0)
429 status = cf_util_get_string (child, &key->instance);
432 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
438 } /* for (i = 0; i < ci->children_num; i++) */
442 if (key->type == NULL)
444 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
449 } /* while (status == 0) */
451 /* store path in a tree that will match the json map structure, example:
452 * "httpd/requests/count",
453 * "httpd/requests/current" ->
454 * { "httpd": { "requests": { "count": $key, "current": $key } } }
463 if (db->tree == NULL)
464 db->tree = cj_avl_create();
483 sstrncpy (ent, name, len+1);
485 if (c_avl_get (tree, ent, (void *) &value) != 0)
487 value = cj_avl_create ();
488 c_avl_insert (tree, strdup (ent), value);
497 c_avl_insert (tree, strdup(name), key);
500 ERROR ("curl_json plugin: invalid key: %s", key->path);
506 } /* }}} int cj_config_add_key */
508 static int cj_init_curl (cj_t *db) /* {{{ */
510 db->curl = curl_easy_init ();
511 if (db->curl == NULL)
513 ERROR ("curl_json plugin: curl_easy_init failed.");
517 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
518 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
519 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
520 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
521 PACKAGE_NAME"/"PACKAGE_VERSION);
522 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
523 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
525 if (db->user != NULL)
527 size_t credentials_size;
529 credentials_size = strlen (db->user) + 2;
530 if (db->pass != NULL)
531 credentials_size += strlen (db->pass);
533 db->credentials = (char *) malloc (credentials_size);
534 if (db->credentials == NULL)
536 ERROR ("curl_json plugin: malloc failed.");
540 ssnprintf (db->credentials, credentials_size, "%s:%s",
541 db->user, (db->pass == NULL) ? "" : db->pass);
542 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
545 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
546 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
547 db->verify_host ? 2L : 0L);
548 if (db->cacert != NULL)
549 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
552 } /* }}} int cj_init_curl */
554 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
560 if ((ci->values_num != 1)
561 || (ci->values[0].type != OCONFIG_TYPE_STRING))
563 WARNING ("curl_json plugin: The `URL' block "
564 "needs exactly one string argument.");
568 db = (cj_t *) malloc (sizeof (*db));
571 ERROR ("curl_json plugin: malloc failed.");
574 memset (db, 0, sizeof (*db));
576 if (strcasecmp ("URL", ci->key) == 0)
578 status = cf_util_get_string (ci, &db->url);
587 ERROR ("curl_json plugin: cj_config: "
588 "Invalid key: %s", ci->key);
592 /* Fill the `cj_t' structure.. */
593 for (i = 0; i < ci->children_num; i++)
595 oconfig_item_t *child = ci->children + i;
597 if (strcasecmp ("Instance", child->key) == 0)
598 status = cf_util_get_string (child, &db->instance);
599 else if (strcasecmp ("Host", child->key) == 0)
600 status = cf_util_get_string (child, &db->host);
601 else if (strcasecmp ("User", child->key) == 0)
602 status = cf_util_get_string (child, &db->user);
603 else if (strcasecmp ("Password", child->key) == 0)
604 status = cf_util_get_string (child, &db->pass);
605 else if (strcasecmp ("VerifyPeer", child->key) == 0)
606 status = cf_util_get_boolean (child, &db->verify_peer);
607 else if (strcasecmp ("VerifyHost", child->key) == 0)
608 status = cf_util_get_boolean (child, &db->verify_host);
609 else if (strcasecmp ("CACert", child->key) == 0)
610 status = cf_util_get_string (child, &db->cacert);
611 else if (strcasecmp ("Key", child->key) == 0)
612 status = cj_config_add_key (db, child);
615 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
625 if (db->tree == NULL)
627 WARNING ("curl_json plugin: No (valid) `Key' block "
628 "within `URL' block `%s'.", db->url);
632 status = cj_init_curl (db);
635 /* If all went well, register this database for reading */
639 char cb_name[DATA_MAX_NAME_LEN];
641 if (db->instance == NULL)
642 db->instance = strdup("default");
644 DEBUG ("curl_json plugin: Registering new read callback: %s",
647 memset (&ud, 0, sizeof (ud));
648 ud.data = (void *) db;
649 ud.free_func = cj_free;
651 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
652 db->instance, db->url);
654 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
655 /* interval = */ NULL, &ud);
665 /* }}} int cj_config_add_database */
667 static int cj_config (oconfig_item_t *ci) /* {{{ */
677 for (i = 0; i < ci->children_num; i++)
679 oconfig_item_t *child = ci->children + i;
681 if (strcasecmp ("URL", child->key) == 0)
683 status = cj_config_add_url (child);
691 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
696 if ((success == 0) && (errors > 0))
698 ERROR ("curl_json plugin: All statements failed.");
703 } /* }}} int cj_config */
705 /* }}} End of configuration handling functions */
707 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
709 value_list_t vl = VALUE_LIST_INIT;
715 if ((db->host == NULL)
716 || (strcmp ("", db->host) == 0)
717 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
722 if (key->instance == NULL)
724 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
725 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
727 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
728 db->state[db->depth-1].name, db->state[db->depth].name);
731 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
733 sstrncpy (vl.host, host, sizeof (vl.host));
734 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
735 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
736 sstrncpy (vl.type, key->type, sizeof (vl.type));
738 plugin_dispatch_values (&vl);
739 } /* }}} int cj_submit */
741 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
746 yajl_handle yprev = db->yajl;
748 db->yajl = yajl_alloc (&ycallbacks,
750 /* alloc funcs = */ NULL,
752 /* alloc funcs = */ NULL, NULL,
754 /* context = */ (void *)db);
755 if (db->yajl == NULL)
757 ERROR ("curl_json plugin: yajl_alloc failed.");
763 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
765 status = curl_easy_perform (curl);
766 if (status != CURLE_OK)
768 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
769 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
770 yajl_free (db->yajl);
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);
788 status = yajl_complete_parse(db->yajl);
790 status = yajl_parse_complete(db->yajl);
792 if (status != yajl_status_ok)
794 unsigned char *errmsg;
796 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
797 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
798 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
800 yajl_free_error (db->yajl, errmsg);
801 yajl_free (db->yajl);
806 yajl_free (db->yajl);
809 } /* }}} int cj_curl_perform */
811 static int cj_read (user_data_t *ud) /* {{{ */
815 if ((ud == NULL) || (ud->data == NULL))
817 ERROR ("curl_json plugin: cj_read: Invalid user data.");
821 db = (cj_t *) ud->data;
824 memset (&db->state, 0, sizeof(db->state));
825 db->state[db->depth].tree = db->tree;
828 return cj_curl_perform (db, db->curl);
829 } /* }}} int cj_read */
831 void module_register (void)
833 plugin_register_complex_config ("curl_json", cj_config);
834 } /* void module_register */
836 /* vim: set sw=2 sts=2 et fdm=marker : */