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, 1, (unsigned char *)buf, len);
126 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
127 yajl_free_error(db->yajl, msg);
128 return (0); /* abort write callback */
132 } /* }}} size_t cj_curl_callback */
134 static int cj_get_type (cj_key_t *key)
136 const data_set_t *ds;
138 ds = plugin_get_ds (key->type);
141 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
143 assert (key->type != NULL);
144 if (strcmp (type, key->type) != 0)
146 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
148 sstrncpy (type, key->type, sizeof (type));
153 else if (ds->ds_num > 1)
155 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
157 c_complain_once (LOG_WARNING, &complaint,
158 "curl_json plugin: The type \"%s\" has more than one data source. "
159 "This is currently not supported. I will return the type of the "
160 "first data source, but this will likely lead to problems later on.",
164 return ds->ds[0].type;
168 #define CJ_CB_ABORT 0
169 #define CJ_CB_CONTINUE 1
171 /* "number" may not be null terminated, so copy it into a buffer before
173 static int cj_cb_number (void *ctx,
174 const char *number, yajl_len_t number_len)
176 char buffer[number_len + 1];
178 cj_t *db = (cj_t *)ctx;
179 cj_key_t *key = db->state[db->depth].key;
185 return (CJ_CB_CONTINUE);
187 memcpy (buffer, number, number_len);
188 buffer[sizeof (buffer) - 1] = 0;
190 type = cj_get_type (key);
192 return (CJ_CB_CONTINUE);
197 if (type == DS_TYPE_COUNTER)
198 vt.counter = (counter_t) strtoull (buffer, &endptr, /* base = */ 0);
199 else if (type == DS_TYPE_GAUGE)
200 vt.gauge = (gauge_t) strtod (buffer, &endptr);
201 else if (type == DS_TYPE_DERIVE)
202 vt.derive = (derive_t) strtoll (buffer, &endptr, /* base = */ 0);
203 else if (type == DS_TYPE_ABSOLUTE)
204 vt.absolute = (absolute_t) strtoull (buffer, &endptr, /* base = */ 0);
207 ERROR ("curl_json plugin: Unknown data source type: \"%s\"", key->type);
208 return (CJ_CB_ABORT);
211 if ((endptr == &buffer[0]) || (errno != 0))
213 NOTICE ("curl_json plugin: Overflow while parsing number. "
214 "Ignoring this value.");
215 return (CJ_CB_CONTINUE);
218 cj_submit (db, key, &vt);
219 return (CJ_CB_CONTINUE);
220 } /* int cj_cb_number */
222 static int cj_cb_map_key (void *ctx, const unsigned char *val,
225 cj_t *db = (cj_t *)ctx;
228 tree = db->state[db->depth-1].tree;
235 name = db->state[db->depth].name;
236 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
237 sstrncpy (name, (char *)val, len+1);
239 if (c_avl_get (tree, name, (void *) &value) == 0)
240 db->state[db->depth].key = value;
241 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
242 db->state[db->depth].key = value;
244 db->state[db->depth].key = NULL;
247 return (CJ_CB_CONTINUE);
250 static int cj_cb_string (void *ctx, const unsigned char *val,
253 cj_t *db = (cj_t *)ctx;
257 if (db->depth != 1) /* e.g. _all_dbs */
258 return (CJ_CB_CONTINUE);
260 cj_cb_map_key (ctx, val, len); /* same logic */
262 tree = db->state[db->depth].tree;
264 if ((tree != NULL) && (ptr = rindex (db->url, '/')))
269 /* url =~ s,[^/]+$,$name, */
270 len = (ptr - db->url) + 1;
272 sstrncpy (ptr, db->url, sizeof (url));
273 sstrncpy (ptr + len, db->state[db->depth].name, sizeof (url) - len);
275 curl = curl_easy_duphandle (db->curl);
276 curl_easy_setopt (curl, CURLOPT_URL, url);
277 cj_curl_perform (db, curl);
278 curl_easy_cleanup (curl);
280 return (CJ_CB_CONTINUE);
283 static int cj_cb_start (void *ctx)
285 cj_t *db = (cj_t *)ctx;
286 if (++db->depth >= YAJL_MAX_DEPTH)
288 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
289 return (CJ_CB_ABORT);
291 return (CJ_CB_CONTINUE);
294 static int cj_cb_end (void *ctx)
296 cj_t *db = (cj_t *)ctx;
297 db->state[db->depth].tree = NULL;
299 return (CJ_CB_CONTINUE);
302 static int cj_cb_start_map (void *ctx)
304 return cj_cb_start (ctx);
307 static int cj_cb_end_map (void *ctx)
309 return cj_cb_end (ctx);
312 static int cj_cb_start_array (void * ctx)
314 return cj_cb_start (ctx);
317 static int cj_cb_end_array (void * ctx)
319 return cj_cb_end (ctx);
322 static yajl_callbacks ycallbacks = {
336 /* end yajl callbacks */
338 static void cj_key_free (cj_key_t *key) /* {{{ */
345 sfree (key->instance);
348 } /* }}} void cj_key_free */
350 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
355 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
357 cj_key_t *key = (cj_key_t *)value;
362 cj_tree_free ((c_avl_tree_t *)value);
367 c_avl_destroy (tree);
368 } /* }}} void cj_tree_free */
370 static void cj_free (void *arg) /* {{{ */
374 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
381 if (db->curl != NULL)
382 curl_easy_cleanup (db->curl);
385 if (db->tree != NULL)
386 cj_tree_free (db->tree);
389 sfree (db->instance);
395 sfree (db->credentials);
399 } /* }}} void cj_free */
401 /* Configuration handling functions {{{ */
403 static int cj_config_add_string (const char *name, char **dest, /* {{{ */
406 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
408 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
413 *dest = strdup (ci->values[0].value.string);
418 } /* }}} int cj_config_add_string */
420 static int cj_config_set_boolean (const char *name, int *dest, /* {{{ */
423 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
425 WARNING ("curl_json plugin: `%s' needs exactly one boolean argument.", name);
429 *dest = ci->values[0].value.boolean ? 1 : 0;
432 } /* }}} int cj_config_set_boolean */
434 static c_avl_tree_t *cj_avl_create(void)
436 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
439 static int cj_config_add_key (cj_t *db, /* {{{ */
446 if ((ci->values_num != 1)
447 || (ci->values[0].type != OCONFIG_TYPE_STRING))
449 WARNING ("curl_json plugin: The `Key' block "
450 "needs exactly one string argument.");
454 key = (cj_key_t *) malloc (sizeof (*key));
457 ERROR ("curl_json plugin: malloc failed.");
460 memset (key, 0, sizeof (*key));
461 key->magic = CJ_KEY_MAGIC;
463 if (strcasecmp ("Key", ci->key) == 0)
465 status = cj_config_add_string ("Key", &key->path, ci);
474 ERROR ("curl_json plugin: cj_config: "
475 "Invalid key: %s", ci->key);
480 for (i = 0; i < ci->children_num; i++)
482 oconfig_item_t *child = ci->children + i;
484 if (strcasecmp ("Type", child->key) == 0)
485 status = cj_config_add_string ("Type", &key->type, child);
486 else if (strcasecmp ("Instance", child->key) == 0)
487 status = cj_config_add_string ("Instance", &key->instance, child);
490 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
496 } /* for (i = 0; i < ci->children_num; i++) */
500 if (key->type == NULL)
502 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
507 } /* while (status == 0) */
509 /* store path in a tree that will match the json map structure, example:
510 * "httpd/requests/count",
511 * "httpd/requests/current" ->
512 * { "httpd": { "requests": { "count": $key, "current": $key } } }
521 if (db->tree == NULL)
522 db->tree = cj_avl_create();
541 sstrncpy (ent, name, len+1);
543 if (c_avl_get (tree, ent, (void *) &value) != 0)
545 value = cj_avl_create ();
546 c_avl_insert (tree, strdup (ent), value);
555 c_avl_insert (tree, strdup(name), key);
558 ERROR ("curl_json plugin: invalid key: %s", key->path);
564 } /* }}} int cj_config_add_key */
566 static int cj_init_curl (cj_t *db) /* {{{ */
568 db->curl = curl_easy_init ();
569 if (db->curl == NULL)
571 ERROR ("curl_json plugin: curl_easy_init failed.");
575 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
576 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
577 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
578 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
579 PACKAGE_NAME"/"PACKAGE_VERSION);
580 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
581 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
583 if (db->user != NULL)
585 size_t credentials_size;
587 credentials_size = strlen (db->user) + 2;
588 if (db->pass != NULL)
589 credentials_size += strlen (db->pass);
591 db->credentials = (char *) malloc (credentials_size);
592 if (db->credentials == NULL)
594 ERROR ("curl_json plugin: malloc failed.");
598 ssnprintf (db->credentials, credentials_size, "%s:%s",
599 db->user, (db->pass == NULL) ? "" : db->pass);
600 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
603 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
604 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
605 db->verify_host ? 2L : 0L);
606 if (db->cacert != NULL)
607 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
610 } /* }}} int cj_init_curl */
612 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
618 if ((ci->values_num != 1)
619 || (ci->values[0].type != OCONFIG_TYPE_STRING))
621 WARNING ("curl_json plugin: The `URL' block "
622 "needs exactly one string argument.");
626 db = (cj_t *) malloc (sizeof (*db));
629 ERROR ("curl_json plugin: malloc failed.");
632 memset (db, 0, sizeof (*db));
634 if (strcasecmp ("URL", ci->key) == 0)
636 status = cj_config_add_string ("URL", &db->url, ci);
645 ERROR ("curl_json plugin: cj_config: "
646 "Invalid key: %s", ci->key);
650 /* Fill the `cj_t' structure.. */
651 for (i = 0; i < ci->children_num; i++)
653 oconfig_item_t *child = ci->children + i;
655 if (strcasecmp ("Instance", child->key) == 0)
656 status = cj_config_add_string ("Instance", &db->instance, child);
657 else if (strcasecmp ("Host", child->key) == 0)
658 status = cj_config_add_string ("Host", &db->host, child);
659 else if (strcasecmp ("User", child->key) == 0)
660 status = cj_config_add_string ("User", &db->user, child);
661 else if (strcasecmp ("Password", child->key) == 0)
662 status = cj_config_add_string ("Password", &db->pass, child);
663 else if (strcasecmp ("VerifyPeer", child->key) == 0)
664 status = cj_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
665 else if (strcasecmp ("VerifyHost", child->key) == 0)
666 status = cj_config_set_boolean ("VerifyHost", &db->verify_host, child);
667 else if (strcasecmp ("CACert", child->key) == 0)
668 status = cj_config_add_string ("CACert", &db->cacert, child);
669 else if (strcasecmp ("Key", child->key) == 0)
670 status = cj_config_add_key (db, child);
673 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
683 if (db->tree == NULL)
685 WARNING ("curl_json plugin: No (valid) `Key' block "
686 "within `URL' block `%s'.", db->url);
690 status = cj_init_curl (db);
693 /* If all went well, register this database for reading */
697 char cb_name[DATA_MAX_NAME_LEN];
699 if (db->instance == NULL)
700 db->instance = strdup("default");
702 DEBUG ("curl_json plugin: Registering new read callback: %s",
705 memset (&ud, 0, sizeof (ud));
706 ud.data = (void *) db;
707 ud.free_func = cj_free;
709 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
710 db->instance, db->url);
712 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
713 /* interval = */ NULL, &ud);
723 /* }}} int cj_config_add_database */
725 static int cj_config (oconfig_item_t *ci) /* {{{ */
735 for (i = 0; i < ci->children_num; i++)
737 oconfig_item_t *child = ci->children + i;
739 if (strcasecmp ("URL", child->key) == 0)
741 status = cj_config_add_url (child);
749 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
754 if ((success == 0) && (errors > 0))
756 ERROR ("curl_json plugin: All statements failed.");
761 } /* }}} int cj_config */
763 /* }}} End of configuration handling functions */
765 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
767 value_list_t vl = VALUE_LIST_INIT;
773 if ((db->host == NULL)
774 || (strcmp ("", db->host) == 0)
775 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
780 if (key->instance == NULL)
782 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
783 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
785 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
786 db->state[db->depth-1].name, db->state[db->depth].name);
789 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
791 sstrncpy (vl.host, host, sizeof (vl.host));
792 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
793 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
794 sstrncpy (vl.type, key->type, sizeof (vl.type));
796 plugin_dispatch_values (&vl);
797 } /* }}} int cj_submit */
799 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
804 yajl_handle yprev = db->yajl;
806 db->yajl = yajl_alloc (&ycallbacks,
808 /* alloc funcs = */ NULL,
810 /* alloc funcs = */ NULL, NULL,
812 /* context = */ (void *)db);
813 if (db->yajl == NULL)
815 ERROR ("curl_json plugin: yajl_alloc failed.");
820 status = curl_easy_perform (curl);
821 if (status != CURLE_OK)
823 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
824 status, db->curl_errbuf, url);
825 yajl_free (db->yajl);
830 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
831 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
833 /* The response code is zero if a non-HTTP transport was used. */
834 if ((rc != 0) && (rc != 200))
836 ERROR ("curl_json plugin: curl_easy_perform failed with response code %ld (%s)",
838 yajl_free (db->yajl);
844 status = yajl_complete_parse(db->yajl);
846 status = yajl_parse_complete(db->yajl);
848 if (status != yajl_status_ok)
850 ERROR ("curl_json plugin: %s failed with status %i.",
852 "yajl_complete_parse",
854 "yajl_parse_complete",
859 yajl_free (db->yajl);
863 } /* }}} int cj_curl_perform */
865 static int cj_read (user_data_t *ud) /* {{{ */
869 if ((ud == NULL) || (ud->data == NULL))
871 ERROR ("curl_json plugin: cj_read: Invalid user data.");
875 db = (cj_t *) ud->data;
878 memset (&db->state, 0, sizeof(db->state));
879 db->state[db->depth].tree = db->tree;
882 return cj_curl_perform (db, db->curl);
883 } /* }}} int cj_read */
885 static int cj_init (void) /* {{{ */
887 /* Call this while collectd is still single-threaded to avoid
888 * initialization issues in libgcrypt. */
889 curl_global_init (CURL_GLOBAL_SSL);
891 } /* }}} int cj_init */
893 void module_register (void)
895 plugin_register_complex_config ("curl_json", cj_config);
896 plugin_register_init ("curl_json", cj_init);
897 } /* void module_register */
899 /* vim: set sw=2 sts=2 et fdm=marker : */