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 /* {{{ */
70 struct curl_slist *headers;
74 char curl_errbuf[CURL_ERROR_SIZE];
87 char name[DATA_MAX_NAME_LEN];
88 } state[YAJL_MAX_DEPTH];
90 typedef struct cj_s cj_t; /* }}} */
93 typedef size_t yajl_len_t;
95 typedef unsigned int yajl_len_t;
98 static int cj_read (user_data_t *ud);
99 static int cj_curl_perform (cj_t *db, CURL *curl);
100 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
102 static size_t cj_curl_callback (void *buf, /* {{{ */
103 size_t size, size_t nmemb, void *user_data)
118 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
119 if (status == yajl_status_ok)
122 else if (status == yajl_status_insufficient_data)
126 if (status != yajl_status_ok)
129 yajl_get_error(db->yajl, /* verbose = */ 1,
130 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
131 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
132 yajl_free_error(db->yajl, msg);
133 return (0); /* abort write callback */
137 } /* }}} size_t cj_curl_callback */
139 static int cj_get_type (cj_key_t *key)
141 const data_set_t *ds;
143 ds = plugin_get_ds (key->type);
146 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
148 assert (key->type != NULL);
149 if (strcmp (type, key->type) != 0)
151 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
153 sstrncpy (type, key->type, sizeof (type));
158 else if (ds->ds_num > 1)
160 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
162 c_complain_once (LOG_WARNING, &complaint,
163 "curl_json plugin: The type \"%s\" has more than one data source. "
164 "This is currently not supported. I will return the type of the "
165 "first data source, but this will likely lead to problems later on.",
169 return ds->ds[0].type;
172 static int cj_cb_map_key (void *ctx, const unsigned char *val,
175 static void cj_cb_inc_array_index (void * ctx, _Bool ignore)
177 cj_t *db = (cj_t *)ctx;
179 if (db->state[db->depth].in_array) {
181 db->state[db->depth].index++;
183 char name[DATA_MAX_NAME_LEN];
184 cj_cb_map_key (ctx, (unsigned char *)name,
185 ssnprintf (name, sizeof (name),
186 "%d", db->state[db->depth].index++));
192 #define CJ_CB_ABORT 0
193 #define CJ_CB_CONTINUE 1
195 static int cj_cb_boolean (void * ctx, int boolVal)
197 cj_cb_inc_array_index (ctx, 1);
198 return (CJ_CB_CONTINUE);
201 static int cj_cb_null (void * ctx)
203 cj_cb_inc_array_index (ctx, 1);
204 return (CJ_CB_CONTINUE);
207 /* "number" may not be null terminated, so copy it into a buffer before
209 static int cj_cb_number (void *ctx,
210 const char *number, yajl_len_t number_len)
212 char buffer[number_len + 1];
214 cj_t *db = (cj_t *)ctx;
215 cj_key_t *key = db->state[db->depth].key;
220 if ((key == NULL) || !CJ_IS_KEY (key)) {
222 NOTICE ("curl_json plugin: Found \"%.*s\", but the configuration expects"
223 " a map.", (int)number_len > number_len ? 0 : (int)number_len,
225 cj_cb_inc_array_index (ctx, 1);
226 return (CJ_CB_CONTINUE);
228 cj_cb_inc_array_index (ctx, 0);
230 memcpy (buffer, number, number_len);
231 buffer[sizeof (buffer) - 1] = 0;
233 type = cj_get_type (key);
234 status = parse_value (buffer, &vt, type);
237 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
238 return (CJ_CB_CONTINUE);
241 cj_submit (db, key, &vt);
242 return (CJ_CB_CONTINUE);
243 } /* int cj_cb_number */
245 static int cj_cb_map_key (void *ctx, const unsigned char *val,
248 cj_t *db = (cj_t *)ctx;
251 tree = db->state[db->depth-1].tree;
258 name = db->state[db->depth].name;
259 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
260 sstrncpy (name, (char *)val, len+1);
262 if (c_avl_get (tree, name, (void *) &value) == 0)
263 db->state[db->depth].key = value;
264 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
265 db->state[db->depth].key = value;
267 db->state[db->depth].key = NULL;
270 return (CJ_CB_CONTINUE);
273 static int cj_cb_string (void *ctx, const unsigned char *val,
276 /* Handle the string as if it was a number. */
277 return (cj_cb_number (ctx, (const char *) val, len));
278 } /* int cj_cb_string */
280 static int cj_cb_start (void *ctx)
282 cj_t *db = (cj_t *)ctx;
283 if (++db->depth >= YAJL_MAX_DEPTH)
285 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
286 return (CJ_CB_ABORT);
288 return (CJ_CB_CONTINUE);
291 static int cj_cb_end (void *ctx)
293 cj_t *db = (cj_t *)ctx;
294 db->state[db->depth].tree = NULL;
296 return (CJ_CB_CONTINUE);
299 static int cj_cb_start_map (void *ctx)
301 cj_cb_inc_array_index (ctx, 0);
302 return cj_cb_start (ctx);
305 static int cj_cb_end_map (void *ctx)
307 return cj_cb_end (ctx);
310 static int cj_cb_start_array (void * ctx)
312 cj_t *db = (cj_t *)ctx;
313 cj_cb_inc_array_index (ctx, 0);
314 if (db->depth+1 < YAJL_MAX_DEPTH) {
315 db->state[db->depth+1].in_array = 1;
316 db->state[db->depth+1].index = 0;
318 return cj_cb_start (ctx);
321 static int cj_cb_end_array (void * ctx)
323 cj_t *db = (cj_t *)ctx;
324 db->state[db->depth].in_array = 0;
325 return cj_cb_end (ctx);
328 static yajl_callbacks ycallbacks = {
329 cj_cb_null, /* null */
330 cj_cb_boolean, /* boolean */
342 /* end yajl callbacks */
344 static void cj_key_free (cj_key_t *key) /* {{{ */
351 sfree (key->instance);
354 } /* }}} void cj_key_free */
356 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
361 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
363 cj_key_t *key = (cj_key_t *)value;
368 cj_tree_free ((c_avl_tree_t *)value);
373 c_avl_destroy (tree);
374 } /* }}} void cj_tree_free */
376 static void cj_free (void *arg) /* {{{ */
380 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
387 if (db->curl != NULL)
388 curl_easy_cleanup (db->curl);
391 if (db->tree != NULL)
392 cj_tree_free (db->tree);
395 sfree (db->instance);
401 sfree (db->credentials);
403 sfree (db->post_body);
404 curl_slist_free_all (db->headers);
407 } /* }}} void cj_free */
409 /* Configuration handling functions {{{ */
411 static c_avl_tree_t *cj_avl_create(void)
413 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
416 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
419 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
421 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
425 *dest = curl_slist_append(*dest, ci->values[0].value.string);
430 } /* }}} int cj_config_append_string */
432 static int cj_config_add_key (cj_t *db, /* {{{ */
439 if ((ci->values_num != 1)
440 || (ci->values[0].type != OCONFIG_TYPE_STRING))
442 WARNING ("curl_json plugin: The `Key' block "
443 "needs exactly one string argument.");
447 key = (cj_key_t *) malloc (sizeof (*key));
450 ERROR ("curl_json plugin: malloc failed.");
453 memset (key, 0, sizeof (*key));
454 key->magic = CJ_KEY_MAGIC;
456 if (strcasecmp ("Key", ci->key) == 0)
458 status = cf_util_get_string (ci, &key->path);
467 ERROR ("curl_json plugin: cj_config: "
468 "Invalid key: %s", ci->key);
473 for (i = 0; i < ci->children_num; i++)
475 oconfig_item_t *child = ci->children + i;
477 if (strcasecmp ("Type", child->key) == 0)
478 status = cf_util_get_string (child, &key->type);
479 else if (strcasecmp ("Instance", child->key) == 0)
480 status = cf_util_get_string (child, &key->instance);
483 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
489 } /* for (i = 0; i < ci->children_num; i++) */
493 if (key->type == NULL)
495 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
500 } /* while (status == 0) */
502 /* store path in a tree that will match the json map structure, example:
503 * "httpd/requests/count",
504 * "httpd/requests/current" ->
505 * { "httpd": { "requests": { "count": $key, "current": $key } } }
514 if (db->tree == NULL)
515 db->tree = cj_avl_create();
534 sstrncpy (ent, name, len+1);
536 if (c_avl_get (tree, ent, (void *) &value) != 0)
538 value = cj_avl_create ();
539 c_avl_insert (tree, strdup (ent), value);
548 c_avl_insert (tree, strdup(name), key);
551 ERROR ("curl_json plugin: invalid key: %s", key->path);
557 } /* }}} int cj_config_add_key */
559 static int cj_init_curl (cj_t *db) /* {{{ */
561 db->curl = curl_easy_init ();
562 if (db->curl == NULL)
564 ERROR ("curl_json plugin: curl_easy_init failed.");
568 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
569 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
570 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
571 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
572 PACKAGE_NAME"/"PACKAGE_VERSION);
573 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
574 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
576 if (db->user != NULL)
578 size_t credentials_size;
580 credentials_size = strlen (db->user) + 2;
581 if (db->pass != NULL)
582 credentials_size += strlen (db->pass);
584 db->credentials = (char *) malloc (credentials_size);
585 if (db->credentials == NULL)
587 ERROR ("curl_json plugin: malloc failed.");
591 ssnprintf (db->credentials, credentials_size, "%s:%s",
592 db->user, (db->pass == NULL) ? "" : db->pass);
593 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
596 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
597 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
598 db->verify_host ? 2L : 0L);
599 if (db->cacert != NULL)
600 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
601 if (db->headers != NULL)
602 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
603 if (db->post_body != NULL)
604 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
607 } /* }}} int cj_init_curl */
609 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
615 if ((ci->values_num != 1)
616 || (ci->values[0].type != OCONFIG_TYPE_STRING))
618 WARNING ("curl_json plugin: The `URL' block "
619 "needs exactly one string argument.");
623 db = (cj_t *) malloc (sizeof (*db));
626 ERROR ("curl_json plugin: malloc failed.");
629 memset (db, 0, sizeof (*db));
631 if (strcasecmp ("URL", ci->key) == 0)
633 status = cf_util_get_string (ci, &db->url);
642 ERROR ("curl_json plugin: cj_config: "
643 "Invalid key: %s", ci->key);
647 /* Fill the `cj_t' structure.. */
648 for (i = 0; i < ci->children_num; i++)
650 oconfig_item_t *child = ci->children + i;
652 if (strcasecmp ("Instance", child->key) == 0)
653 status = cf_util_get_string (child, &db->instance);
654 else if (strcasecmp ("Host", child->key) == 0)
655 status = cf_util_get_string (child, &db->host);
656 else if (strcasecmp ("User", child->key) == 0)
657 status = cf_util_get_string (child, &db->user);
658 else if (strcasecmp ("Password", child->key) == 0)
659 status = cf_util_get_string (child, &db->pass);
660 else if (strcasecmp ("VerifyPeer", child->key) == 0)
661 status = cf_util_get_boolean (child, &db->verify_peer);
662 else if (strcasecmp ("VerifyHost", child->key) == 0)
663 status = cf_util_get_boolean (child, &db->verify_host);
664 else if (strcasecmp ("CACert", child->key) == 0)
665 status = cf_util_get_string (child, &db->cacert);
666 else if (strcasecmp ("Header", child->key) == 0)
667 status = cj_config_append_string ("Header", &db->headers, child);
668 else if (strcasecmp ("Post", child->key) == 0)
669 status = cf_util_get_string (child, &db->post_body);
670 else if (strcasecmp ("Key", child->key) == 0)
671 status = cj_config_add_key (db, child);
674 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
684 if (db->tree == NULL)
686 WARNING ("curl_json plugin: No (valid) `Key' block "
687 "within `URL' block `%s'.", db->url);
691 status = cj_init_curl (db);
694 /* If all went well, register this database for reading */
698 char cb_name[DATA_MAX_NAME_LEN];
700 if (db->instance == NULL)
701 db->instance = strdup("default");
703 DEBUG ("curl_json plugin: Registering new read callback: %s",
706 memset (&ud, 0, sizeof (ud));
707 ud.data = (void *) db;
708 ud.free_func = cj_free;
710 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
711 db->instance, db->url);
713 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
714 /* interval = */ NULL, &ud);
724 /* }}} int cj_config_add_database */
726 static int cj_config (oconfig_item_t *ci) /* {{{ */
736 for (i = 0; i < ci->children_num; i++)
738 oconfig_item_t *child = ci->children + i;
740 if (strcasecmp ("URL", child->key) == 0)
742 status = cj_config_add_url (child);
750 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
755 if ((success == 0) && (errors > 0))
757 ERROR ("curl_json plugin: All statements failed.");
762 } /* }}} int cj_config */
764 /* }}} End of configuration handling functions */
766 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
768 value_list_t vl = VALUE_LIST_INIT;
774 if ((db->host == NULL)
775 || (strcmp ("", db->host) == 0)
776 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
781 if (key->instance == NULL)
783 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
784 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
786 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
787 db->state[db->depth-1].name, db->state[db->depth].name);
790 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
792 sstrncpy (vl.host, host, sizeof (vl.host));
793 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
794 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
795 sstrncpy (vl.type, key->type, sizeof (vl.type));
797 plugin_dispatch_values (&vl);
798 } /* }}} int cj_submit */
800 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
805 yajl_handle yprev = db->yajl;
807 db->yajl = yajl_alloc (&ycallbacks,
809 /* alloc funcs = */ NULL,
811 /* alloc funcs = */ NULL, NULL,
813 /* context = */ (void *)db);
814 if (db->yajl == NULL)
816 ERROR ("curl_json plugin: yajl_alloc failed.");
822 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
824 status = curl_easy_perform (curl);
825 if (status != CURLE_OK)
827 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
828 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
829 yajl_free (db->yajl);
834 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
836 /* The response code is zero if a non-HTTP transport was used. */
837 if ((rc != 0) && (rc != 200))
839 ERROR ("curl_json plugin: curl_easy_perform failed with "
840 "response code %ld (%s)", rc, url);
841 yajl_free (db->yajl);
847 status = yajl_complete_parse(db->yajl);
849 status = yajl_parse_complete(db->yajl);
851 if (status != yajl_status_ok)
853 unsigned char *errmsg;
855 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
856 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
857 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
859 yajl_free_error (db->yajl, errmsg);
860 yajl_free (db->yajl);
865 yajl_free (db->yajl);
868 } /* }}} int cj_curl_perform */
870 static int cj_read (user_data_t *ud) /* {{{ */
874 if ((ud == NULL) || (ud->data == NULL))
876 ERROR ("curl_json plugin: cj_read: Invalid user data.");
880 db = (cj_t *) ud->data;
883 memset (&db->state, 0, sizeof(db->state));
884 db->state[db->depth].tree = db->tree;
887 return cj_curl_perform (db, db->curl);
888 } /* }}} int cj_read */
890 void module_register (void)
892 plugin_register_complex_config ("curl_json", cj_config);
893 } /* void module_register */
895 /* vim: set sw=2 sts=2 et fdm=marker : */