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 <sys/socket.h>
32 #include <sys/types.h>
35 #include <curl/curl.h>
37 #include <yajl/yajl_parse.h>
38 #if HAVE_YAJL_YAJL_VERSION_H
39 # include <yajl/yajl_version.h>
42 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
43 # define HAVE_YAJL_V2 1
46 #define CJ_DEFAULT_HOST "localhost"
47 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
48 #define CJ_IS_KEY(key) ((key)->magic == CJ_KEY_MAGIC)
50 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
53 typedef struct cj_key_s cj_key_t;
54 struct cj_key_s /* {{{ */
77 struct curl_slist *headers;
81 char curl_errbuf[CURL_ERROR_SIZE];
94 char name[DATA_MAX_NAME_LEN];
95 } state[YAJL_MAX_DEPTH];
97 typedef struct cj_s cj_t; /* }}} */
100 typedef size_t yajl_len_t;
102 typedef unsigned int yajl_len_t;
105 static int cj_read (user_data_t *ud);
106 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
108 static size_t cj_curl_callback (void *buf, /* {{{ */
109 size_t size, size_t nmemb, void *user_data)
124 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
125 if (status == yajl_status_ok)
128 else if (status == yajl_status_insufficient_data)
132 if (status != yajl_status_ok)
135 yajl_get_error(db->yajl, /* verbose = */ 1,
136 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
137 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
138 yajl_free_error(db->yajl, msg);
139 return (0); /* abort write callback */
143 } /* }}} size_t cj_curl_callback */
145 static int cj_get_type (cj_key_t *key)
147 const data_set_t *ds;
149 ds = plugin_get_ds (key->type);
152 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
154 assert (key->type != NULL);
155 if (strcmp (type, key->type) != 0)
157 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
159 sstrncpy (type, key->type, sizeof (type));
164 else if (ds->ds_num > 1)
166 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
168 c_complain_once (LOG_WARNING, &complaint,
169 "curl_json plugin: The type \"%s\" has more than one data source. "
170 "This is currently not supported. I will return the type of the "
171 "first data source, but this will likely lead to problems later on.",
175 return ds->ds[0].type;
178 static int cj_cb_map_key (void *ctx, const unsigned char *val,
181 static void cj_cb_inc_array_index (void * ctx, _Bool ignore)
183 cj_t *db = (cj_t *)ctx;
185 if (db->state[db->depth].in_array) {
187 db->state[db->depth].index++;
189 char name[DATA_MAX_NAME_LEN];
190 cj_cb_map_key (ctx, (unsigned char *)name,
191 ssnprintf (name, sizeof (name),
192 "%d", db->state[db->depth].index++));
198 #define CJ_CB_ABORT 0
199 #define CJ_CB_CONTINUE 1
201 static int cj_cb_boolean (void * ctx, int boolVal)
203 cj_cb_inc_array_index (ctx, 1);
204 return (CJ_CB_CONTINUE);
207 static int cj_cb_null (void * ctx)
209 cj_cb_inc_array_index (ctx, 1);
210 return (CJ_CB_CONTINUE);
213 /* "number" may not be null terminated, so copy it into a buffer before
215 static int cj_cb_number (void *ctx,
216 const char *number, yajl_len_t number_len)
218 char buffer[number_len + 1];
220 cj_t *db = (cj_t *)ctx;
221 cj_key_t *key = db->state[db->depth].key;
226 if ((key == NULL) || !CJ_IS_KEY (key)) {
228 NOTICE ("curl_json plugin: Found \"%.*s\", but the configuration expects"
229 " a map.", (int)number_len > number_len ? 0 : (int)number_len,
231 cj_cb_inc_array_index (ctx, 1);
232 return (CJ_CB_CONTINUE);
234 cj_cb_inc_array_index (ctx, 0);
236 memcpy (buffer, number, number_len);
237 buffer[sizeof (buffer) - 1] = 0;
239 type = cj_get_type (key);
240 status = parse_value (buffer, &vt, type);
243 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
244 return (CJ_CB_CONTINUE);
247 cj_submit (db, key, &vt);
248 return (CJ_CB_CONTINUE);
249 } /* int cj_cb_number */
251 static int cj_cb_map_key (void *ctx, const unsigned char *val,
254 cj_t *db = (cj_t *)ctx;
257 tree = db->state[db->depth-1].tree;
264 name = db->state[db->depth].name;
265 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
266 sstrncpy (name, (char *)val, len+1);
268 if (c_avl_get (tree, name, (void *) &value) == 0)
269 db->state[db->depth].key = value;
270 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
271 db->state[db->depth].key = value;
273 db->state[db->depth].key = NULL;
276 return (CJ_CB_CONTINUE);
279 static int cj_cb_string (void *ctx, const unsigned char *val,
282 /* Handle the string as if it was a number. */
283 return (cj_cb_number (ctx, (const char *) val, len));
284 } /* int cj_cb_string */
286 static int cj_cb_start (void *ctx)
288 cj_t *db = (cj_t *)ctx;
289 if (++db->depth >= YAJL_MAX_DEPTH)
291 ERROR ("curl_json plugin: %s depth exceeds max, aborting.",
292 db->url ? db->url : db->sock);
293 return (CJ_CB_ABORT);
295 return (CJ_CB_CONTINUE);
298 static int cj_cb_end (void *ctx)
300 cj_t *db = (cj_t *)ctx;
301 db->state[db->depth].tree = NULL;
303 return (CJ_CB_CONTINUE);
306 static int cj_cb_start_map (void *ctx)
308 cj_cb_inc_array_index (ctx, 0);
309 return cj_cb_start (ctx);
312 static int cj_cb_end_map (void *ctx)
314 return cj_cb_end (ctx);
317 static int cj_cb_start_array (void * ctx)
319 cj_t *db = (cj_t *)ctx;
320 cj_cb_inc_array_index (ctx, 0);
321 if (db->depth+1 < YAJL_MAX_DEPTH) {
322 db->state[db->depth+1].in_array = 1;
323 db->state[db->depth+1].index = 0;
325 return cj_cb_start (ctx);
328 static int cj_cb_end_array (void * ctx)
330 cj_t *db = (cj_t *)ctx;
331 db->state[db->depth].in_array = 0;
332 return cj_cb_end (ctx);
335 static yajl_callbacks ycallbacks = {
336 cj_cb_null, /* null */
337 cj_cb_boolean, /* boolean */
349 /* end yajl callbacks */
351 static void cj_key_free (cj_key_t *key) /* {{{ */
358 sfree (key->instance);
361 } /* }}} void cj_key_free */
363 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
368 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
370 cj_key_t *key = (cj_key_t *)value;
375 cj_tree_free ((c_avl_tree_t *)value);
380 c_avl_destroy (tree);
381 } /* }}} void cj_tree_free */
383 static void cj_free (void *arg) /* {{{ */
387 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
394 if (db->curl != NULL)
395 curl_easy_cleanup (db->curl);
398 if (db->tree != NULL)
399 cj_tree_free (db->tree);
402 sfree (db->instance);
410 sfree (db->credentials);
412 sfree (db->post_body);
413 curl_slist_free_all (db->headers);
416 } /* }}} void cj_free */
418 /* Configuration handling functions {{{ */
420 static c_avl_tree_t *cj_avl_create(void)
422 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
425 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
428 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
430 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
434 *dest = curl_slist_append(*dest, ci->values[0].value.string);
439 } /* }}} int cj_config_append_string */
441 static int cj_config_add_key (cj_t *db, /* {{{ */
448 if ((ci->values_num != 1)
449 || (ci->values[0].type != OCONFIG_TYPE_STRING))
451 WARNING ("curl_json plugin: The `Key' block "
452 "needs exactly one string argument.");
456 key = (cj_key_t *) malloc (sizeof (*key));
459 ERROR ("curl_json plugin: malloc failed.");
462 memset (key, 0, sizeof (*key));
463 key->magic = CJ_KEY_MAGIC;
465 if (strcasecmp ("Key", ci->key) == 0)
467 status = cf_util_get_string (ci, &key->path);
476 ERROR ("curl_json plugin: cj_config: "
477 "Invalid key: %s", ci->key);
482 for (i = 0; i < ci->children_num; i++)
484 oconfig_item_t *child = ci->children + i;
486 if (strcasecmp ("Type", child->key) == 0)
487 status = cf_util_get_string (child, &key->type);
488 else if (strcasecmp ("Instance", child->key) == 0)
489 status = cf_util_get_string (child, &key->instance);
492 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
498 } /* for (i = 0; i < ci->children_num; i++) */
502 if (key->type == NULL)
504 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
509 } /* while (status == 0) */
511 /* store path in a tree that will match the json map structure, example:
512 * "httpd/requests/count",
513 * "httpd/requests/current" ->
514 * { "httpd": { "requests": { "count": $key, "current": $key } } }
523 if (db->tree == NULL)
524 db->tree = cj_avl_create();
543 sstrncpy (ent, name, len+1);
545 if (c_avl_get (tree, ent, (void *) &value) != 0)
547 value = cj_avl_create ();
548 c_avl_insert (tree, strdup (ent), value);
557 c_avl_insert (tree, strdup(name), key);
560 ERROR ("curl_json plugin: invalid key: %s", key->path);
566 } /* }}} int cj_config_add_key */
568 static int cj_init_curl (cj_t *db) /* {{{ */
570 db->curl = curl_easy_init ();
571 if (db->curl == NULL)
573 ERROR ("curl_json plugin: curl_easy_init failed.");
577 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
578 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
579 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
580 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
581 PACKAGE_NAME"/"PACKAGE_VERSION);
582 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
583 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
585 if (db->user != NULL)
587 size_t credentials_size;
589 credentials_size = strlen (db->user) + 2;
590 if (db->pass != NULL)
591 credentials_size += strlen (db->pass);
593 db->credentials = (char *) malloc (credentials_size);
594 if (db->credentials == NULL)
596 ERROR ("curl_json plugin: malloc failed.");
600 ssnprintf (db->credentials, credentials_size, "%s:%s",
601 db->user, (db->pass == NULL) ? "" : db->pass);
602 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
605 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
606 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
607 db->verify_host ? 2L : 0L);
608 if (db->cacert != NULL)
609 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
610 if (db->headers != NULL)
611 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
612 if (db->post_body != NULL)
613 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
616 } /* }}} int cj_init_curl */
618 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
624 if ((ci->values_num != 1)
625 || (ci->values[0].type != OCONFIG_TYPE_STRING))
627 WARNING ("curl_json plugin: The `URL' block "
628 "needs exactly one string argument.");
632 db = (cj_t *) malloc (sizeof (*db));
635 ERROR ("curl_json plugin: malloc failed.");
638 memset (db, 0, sizeof (*db));
640 if (strcasecmp ("URL", ci->key) == 0)
641 status = cf_util_get_string (ci, &db->url);
642 else if (strcasecmp ("Sock", ci->key) == 0)
643 status = cf_util_get_string (ci, &db->sock);
646 ERROR ("curl_json plugin: cj_config: "
647 "Invalid key: %s", ci->key);
656 /* Fill the `cj_t' structure.. */
657 for (i = 0; i < ci->children_num; i++)
659 oconfig_item_t *child = ci->children + i;
661 if (strcasecmp ("Instance", child->key) == 0)
662 status = cf_util_get_string (child, &db->instance);
663 else if (strcasecmp ("Host", child->key) == 0)
664 status = cf_util_get_string (child, &db->host);
665 else if (db->url && strcasecmp ("User", child->key) == 0)
666 status = cf_util_get_string (child, &db->user);
667 else if (db->url && strcasecmp ("Password", child->key) == 0)
668 status = cf_util_get_string (child, &db->pass);
669 else if (db->url && strcasecmp ("VerifyPeer", child->key) == 0)
670 status = cf_util_get_boolean (child, &db->verify_peer);
671 else if (db->url && strcasecmp ("VerifyHost", child->key) == 0)
672 status = cf_util_get_boolean (child, &db->verify_host);
673 else if (db->url && strcasecmp ("CACert", child->key) == 0)
674 status = cf_util_get_string (child, &db->cacert);
675 else if (db->url && strcasecmp ("Header", child->key) == 0)
676 status = cj_config_append_string ("Header", &db->headers, child);
677 else if (db->url && strcasecmp ("Post", child->key) == 0)
678 status = cf_util_get_string (child, &db->post_body);
679 else if (strcasecmp ("Key", child->key) == 0)
680 status = cj_config_add_key (db, child);
683 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
693 if (db->tree == NULL)
695 WARNING ("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".",
696 db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
699 if (status == 0 && db->url)
700 status = cj_init_curl (db);
703 /* If all went well, register this database for reading */
707 char cb_name[DATA_MAX_NAME_LEN];
709 if (db->instance == NULL)
710 db->instance = strdup("default");
712 DEBUG ("curl_json plugin: Registering new read callback: %s",
715 memset (&ud, 0, sizeof (ud));
716 ud.data = (void *) db;
717 ud.free_func = cj_free;
719 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
720 db->instance, db->url ? db->url : db->sock);
722 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
723 /* interval = */ NULL, &ud);
733 /* }}} int cj_config_add_database */
735 static int cj_config (oconfig_item_t *ci) /* {{{ */
745 for (i = 0; i < ci->children_num; i++)
747 oconfig_item_t *child = ci->children + i;
749 if (strcasecmp ("Sock", child->key) == 0
750 || strcasecmp ("URL", child->key) == 0)
752 status = cj_config_add_url (child);
760 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
765 if ((success == 0) && (errors > 0))
767 ERROR ("curl_json plugin: All statements failed.");
772 } /* }}} int cj_config */
774 /* }}} End of configuration handling functions */
776 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
778 value_list_t vl = VALUE_LIST_INIT;
784 if ((db->host == NULL)
785 || (strcmp ("", db->host) == 0)
786 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
791 if (key->instance == NULL)
793 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
794 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
796 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
797 db->state[db->depth-1].name, db->state[db->depth].name);
800 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
802 sstrncpy (vl.host, host, sizeof (vl.host));
803 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
804 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
805 sstrncpy (vl.type, key->type, sizeof (vl.type));
807 plugin_dispatch_values (&vl);
808 } /* }}} int cj_submit */
810 static int cj_sock_perform (cj_t *db) /* {{{ */
813 struct sockaddr_un sa_unix = {};
814 sa_unix.sun_family = AF_UNIX;
815 sstrncpy (sa_unix.sun_path, db->sock, sizeof (sa_unix.sun_path));
817 int fd = socket (AF_UNIX, SOCK_STREAM, 0);
820 if (connect (fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0)
822 ERROR ("curl_json plugin: connect(%s) failed: %s",
823 (db->sock != NULL) ? db->sock : "<null>",
824 sstrerror(errno, errbuf, sizeof (errbuf)));
831 unsigned char buffer[4096];
832 red = read (fd, buffer, sizeof(buffer));
834 ERROR ("curl_json plugin: read(%s) failed: %s",
835 (db->sock != NULL) ? db->sock : "<null>",
836 sstrerror(errno, errbuf, sizeof (errbuf)));
840 if (!cj_curl_callback (buffer, red, 1, db))
845 } /* }}} int cj_sock_perform */
848 static int cj_curl_perform(cj_t *db) /* {{{ */
854 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
856 status = curl_easy_perform (db->curl);
857 if (status != CURLE_OK)
859 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
860 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
864 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
866 /* The response code is zero if a non-HTTP transport was used. */
867 if ((rc != 0) && (rc != 200))
869 ERROR ("curl_json plugin: curl_easy_perform failed with "
870 "response code %ld (%s)", rc, url);
874 } /* }}} int cj_curl_perform */
876 static int cj_perform (cj_t *db) /* {{{ */
879 yajl_handle yprev = db->yajl;
881 db->yajl = yajl_alloc (&ycallbacks,
883 /* alloc funcs = */ NULL,
885 /* alloc funcs = */ NULL, NULL,
887 /* context = */ (void *)db);
888 if (db->yajl == NULL)
890 ERROR ("curl_json plugin: yajl_alloc failed.");
896 status = cj_curl_perform (db);
898 status = cj_sock_perform (db);
901 yajl_free (db->yajl);
907 status = yajl_complete_parse(db->yajl);
909 status = yajl_parse_complete(db->yajl);
911 if (status != yajl_status_ok)
913 unsigned char *errmsg;
915 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
916 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
917 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
919 yajl_free_error (db->yajl, errmsg);
920 yajl_free (db->yajl);
925 yajl_free (db->yajl);
928 } /* }}} int cj_perform */
930 static int cj_read (user_data_t *ud) /* {{{ */
934 if ((ud == NULL) || (ud->data == NULL))
936 ERROR ("curl_json plugin: cj_read: Invalid user data.");
940 db = (cj_t *) ud->data;
943 memset (&db->state, 0, sizeof(db->state));
944 db->state[db->depth].tree = db->tree;
947 return cj_perform (db);
948 } /* }}} int cj_read */
950 void module_register (void)
952 plugin_register_complex_config ("curl_json", cj_config);
953 } /* void module_register */
955 /* vim: set sw=2 sts=2 et fdm=marker : */