3ad80a224fcd1b7b07a25592f96e2f693eb7c8ff
1 /**
2 * collectd - src/curl_json.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2010 Florian octo Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Doug MacEachern <dougm at hyperic.com>
21 * Florian octo Forster <octo at verplant.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
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
36 #define CJ_ANY "*"
37 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
39 struct cj_key_s;
40 typedef struct cj_key_s cj_key_t;
41 struct cj_key_s /* {{{ */
42 {
43 char *path;
44 char *type;
45 char *instance;
46 unsigned long magic;
47 };
48 /* }}} */
50 struct cj_s /* {{{ */
51 {
52 char *instance;
53 char *host;
55 char *url;
56 char *user;
57 char *pass;
58 char *credentials;
59 _Bool verify_peer;
60 _Bool verify_host;
61 char *cacert;
63 CURL *curl;
64 char curl_errbuf[CURL_ERROR_SIZE];
66 yajl_handle yajl;
67 c_avl_tree_t *tree;
68 cj_key_t *key;
69 int depth;
70 struct {
71 union {
72 c_avl_tree_t *tree;
73 cj_key_t *key;
74 };
75 char name[DATA_MAX_NAME_LEN];
76 } state[YAJL_MAX_DEPTH];
77 };
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)
86 {
87 cj_t *db;
88 size_t len;
89 yajl_status status;
91 len = size * nmemb;
93 if (len <= 0)
94 return (len);
96 db = user_data;
97 if (db == NULL)
98 return (0);
100 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
101 if ((status != yajl_status_ok)
102 && (status != yajl_status_insufficient_data))
103 {
104 unsigned char *msg =
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 */
110 }
112 return (len);
113 } /* }}} size_t cj_curl_callback */
115 static int cj_get_type (cj_key_t *key)
116 {
117 const data_set_t *ds;
119 ds = plugin_get_ds (key->type);
120 if (ds == NULL)
121 return -1; /* let plugin_write do the complaining */
122 else
123 return ds->ds[0].type; /* XXX support ds->ds_len > 1 */
124 }
126 /* yajl callbacks */
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
131 * parsing. */
132 static int cj_cb_number (void *ctx,
133 const char *number, unsigned int number_len)
134 {
135 char buffer[number_len + 1];
137 cj_t *db = (cj_t *)ctx;
138 cj_key_t *key = db->state[db->depth].key;
139 char *endptr;
140 value_t vt;
141 int type;
143 if (key == NULL)
144 return (CJ_CB_CONTINUE);
146 memcpy (buffer, number, number_len);
147 buffer[sizeof (buffer) - 1] = 0;
149 type = cj_get_type (key);
151 endptr = NULL;
152 errno = 0;
154 if (type == DS_TYPE_COUNTER)
155 vt.counter = (counter_t) strtoull (buffer, &endptr, /* base = */ 0);
156 else if (type == DS_TYPE_GAUGE)
157 vt.gauge = (gauge_t) strtod (buffer, &endptr);
158 else if (type == DS_TYPE_DERIVE)
159 vt.derive = (derive_t) strtoll (buffer, &endptr, /* base = */ 0);
160 else if (type == DS_TYPE_ABSOLUTE)
161 vt.absolute = (absolute_t) strtoull (buffer, &endptr, /* base = */ 0);
162 else
163 {
164 ERROR ("curl_json plugin: Unknown data source type: \"%s\"", key->type);
165 return (CJ_CB_ABORT);
166 }
168 if ((endptr == &buffer[0]) || (errno != 0))
169 {
170 NOTICE ("curl_json plugin: Overflow while parsing number. "
171 "Ignoring this value.");
172 return (CJ_CB_CONTINUE);
173 }
175 cj_submit (db, key, &vt);
176 return (CJ_CB_CONTINUE);
177 } /* int cj_cb_number */
179 static int cj_cb_map_key (void *ctx, const unsigned char *val,
180 unsigned int len)
181 {
182 cj_t *db = (cj_t *)ctx;
183 c_avl_tree_t *tree;
185 tree = db->state[db->depth-1].tree;
187 if (tree != NULL)
188 {
189 cj_key_t *value;
190 char *name;
192 name = db->state[db->depth].name;
193 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
194 sstrncpy (name, (char *)val, len+1);
196 if (c_avl_get (tree, name, (void *) &value) == 0)
197 db->state[db->depth].key = value;
198 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
199 db->state[db->depth].key = value;
200 else
201 db->state[db->depth].key = NULL;
202 }
204 return (CJ_CB_CONTINUE);
205 }
207 static int cj_cb_string (void *ctx, const unsigned char *val,
208 unsigned int len)
209 {
210 cj_t *db = (cj_t *)ctx;
211 c_avl_tree_t *tree;
212 char *ptr;
214 if (db->depth != 1) /* e.g. _all_dbs */
215 return (CJ_CB_CONTINUE);
217 cj_cb_map_key (ctx, val, len); /* same logic */
219 tree = db->state[db->depth].tree;
221 if ((tree != NULL) && (ptr = rindex (db->url, '/')))
222 {
223 char url[PATH_MAX];
224 CURL *curl;
226 /* url =~ s,[^/]+$,$name, */
227 len = (ptr - db->url) + 1;
228 ptr = url;
229 sstrncpy (ptr, db->url, sizeof (url));
230 sstrncpy (ptr + len, db->state[db->depth].name, sizeof (url) - len);
232 curl = curl_easy_duphandle (db->curl);
233 curl_easy_setopt (curl, CURLOPT_URL, url);
234 cj_curl_perform (db, curl);
235 curl_easy_cleanup (curl);
236 }
237 return (CJ_CB_CONTINUE);
238 }
240 static int cj_cb_start (void *ctx)
241 {
242 cj_t *db = (cj_t *)ctx;
243 if (++db->depth >= YAJL_MAX_DEPTH)
244 {
245 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
246 return (CJ_CB_ABORT);
247 }
248 return (CJ_CB_CONTINUE);
249 }
251 static int cj_cb_end (void *ctx)
252 {
253 cj_t *db = (cj_t *)ctx;
254 db->state[db->depth].tree = NULL;
255 --db->depth;
256 return (CJ_CB_CONTINUE);
257 }
259 static int cj_cb_start_map (void *ctx)
260 {
261 return cj_cb_start (ctx);
262 }
264 static int cj_cb_end_map (void *ctx)
265 {
266 return cj_cb_end (ctx);
267 }
269 static int cj_cb_start_array (void * ctx)
270 {
271 return cj_cb_start (ctx);
272 }
274 static int cj_cb_end_array (void * ctx)
275 {
276 return cj_cb_start (ctx);
277 }
279 static yajl_callbacks ycallbacks = {
280 NULL, /* null */
281 NULL, /* boolean */
282 NULL, /* integer */
283 NULL, /* double */
284 cj_cb_number,
285 cj_cb_string,
286 cj_cb_start_map,
287 cj_cb_map_key,
288 cj_cb_end_map,
289 cj_cb_start_array,
290 cj_cb_end_array
291 };
293 /* end yajl callbacks */
295 static void cj_key_free (cj_key_t *key) /* {{{ */
296 {
297 if (key == NULL)
298 return;
300 sfree (key->path);
301 sfree (key->type);
302 sfree (key->instance);
304 sfree (key);
305 } /* }}} void cj_key_free */
307 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
308 {
309 char *name;
310 void *value;
312 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
313 {
314 cj_key_t *key = (cj_key_t *)value;
316 if (CJ_IS_KEY(key))
317 cj_key_free (key);
318 else
319 cj_tree_free ((c_avl_tree_t *)value);
321 sfree (name);
322 }
324 c_avl_destroy (tree);
325 } /* }}} void cj_tree_free */
327 static void cj_free (void *arg) /* {{{ */
328 {
329 cj_t *db;
331 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
333 db = (cj_t *) arg;
335 if (db == NULL)
336 return;
338 if (db->curl != NULL)
339 curl_easy_cleanup (db->curl);
340 db->curl = NULL;
342 if (db->tree != NULL)
343 cj_tree_free (db->tree);
344 db->tree = NULL;
346 sfree (db->instance);
347 sfree (db->host);
349 sfree (db->url);
350 sfree (db->user);
351 sfree (db->pass);
352 sfree (db->credentials);
353 sfree (db->cacert);
355 sfree (db);
356 } /* }}} void cj_free */
358 /* Configuration handling functions {{{ */
360 static c_avl_tree_t *cj_avl_create(void)
361 {
362 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
363 }
365 static int cj_config_add_key (cj_t *db, /* {{{ */
366 oconfig_item_t *ci)
367 {
368 cj_key_t *key;
369 int status;
370 int i;
372 if ((ci->values_num != 1)
373 || (ci->values[0].type != OCONFIG_TYPE_STRING))
374 {
375 WARNING ("curl_json plugin: The `Key' block "
376 "needs exactly one string argument.");
377 return (-1);
378 }
380 key = (cj_key_t *) malloc (sizeof (*key));
381 if (key == NULL)
382 {
383 ERROR ("curl_json plugin: malloc failed.");
384 return (-1);
385 }
386 memset (key, 0, sizeof (*key));
387 key->magic = CJ_KEY_MAGIC;
389 if (strcasecmp ("Key", ci->key) == 0)
390 {
391 status = cf_util_get_string (ci, &key->path);
392 if (status != 0)
393 {
394 sfree (key);
395 return (status);
396 }
397 }
398 else
399 {
400 ERROR ("curl_json plugin: cj_config: "
401 "Invalid key: %s", ci->key);
402 return (-1);
403 }
405 status = 0;
406 for (i = 0; i < ci->children_num; i++)
407 {
408 oconfig_item_t *child = ci->children + i;
410 if (strcasecmp ("Type", child->key) == 0)
411 status = cf_util_get_string (child, &key->type);
412 else if (strcasecmp ("Instance", child->key) == 0)
413 status = cf_util_get_string (child, &key->instance);
414 else
415 {
416 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
417 status = -1;
418 }
420 if (status != 0)
421 break;
422 } /* for (i = 0; i < ci->children_num; i++) */
424 while (status == 0)
425 {
426 if (key->type == NULL)
427 {
428 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
429 status = -1;
430 }
432 break;
433 } /* while (status == 0) */
435 /* store path in a tree that will match the json map structure, example:
436 * "httpd/requests/count",
437 * "httpd/requests/current" ->
438 * { "httpd": { "requests": { "count": $key, "current": $key } } }
439 */
440 if (status == 0)
441 {
442 char *ptr;
443 char *name;
444 char ent[PATH_MAX];
445 c_avl_tree_t *tree;
447 if (db->tree == NULL)
448 db->tree = cj_avl_create();
450 tree = db->tree;
451 name = key->path;
452 ptr = key->path;
453 if (*ptr == '/')
454 ++ptr;
456 name = ptr;
457 while (*ptr)
458 {
459 if (*ptr == '/')
460 {
461 c_avl_tree_t *value;
462 int len;
464 len = ptr-name;
465 if (len == 0)
466 break;
467 sstrncpy (ent, name, len+1);
469 if (c_avl_get (tree, ent, (void *) &value) != 0)
470 {
471 value = cj_avl_create ();
472 c_avl_insert (tree, strdup (ent), value);
473 }
475 tree = value;
476 name = ptr+1;
477 }
478 ++ptr;
479 }
480 if (*name)
481 c_avl_insert (tree, strdup(name), key);
482 else
483 {
484 ERROR ("curl_json plugin: invalid key: %s", key->path);
485 status = -1;
486 }
487 }
489 return (status);
490 } /* }}} int cj_config_add_key */
492 static int cj_init_curl (cj_t *db) /* {{{ */
493 {
494 db->curl = curl_easy_init ();
495 if (db->curl == NULL)
496 {
497 ERROR ("curl_json plugin: curl_easy_init failed.");
498 return (-1);
499 }
501 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
502 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
503 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
504 PACKAGE_NAME"/"PACKAGE_VERSION);
505 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
506 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
508 if (db->user != NULL)
509 {
510 size_t credentials_size;
512 credentials_size = strlen (db->user) + 2;
513 if (db->pass != NULL)
514 credentials_size += strlen (db->pass);
516 db->credentials = (char *) malloc (credentials_size);
517 if (db->credentials == NULL)
518 {
519 ERROR ("curl_json plugin: malloc failed.");
520 return (-1);
521 }
523 ssnprintf (db->credentials, credentials_size, "%s:%s",
524 db->user, (db->pass == NULL) ? "" : db->pass);
525 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
526 }
528 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (int) db->verify_peer);
529 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
530 (int) (db->verify_host ? 2 : 0));
531 if (db->cacert != NULL)
532 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
534 return (0);
535 } /* }}} int cj_init_curl */
537 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
538 {
539 cj_t *db;
540 int status = 0;
541 int i;
543 if ((ci->values_num != 1)
544 || (ci->values[0].type != OCONFIG_TYPE_STRING))
545 {
546 WARNING ("curl_json plugin: The `URL' block "
547 "needs exactly one string argument.");
548 return (-1);
549 }
551 db = (cj_t *) malloc (sizeof (*db));
552 if (db == NULL)
553 {
554 ERROR ("curl_json plugin: malloc failed.");
555 return (-1);
556 }
557 memset (db, 0, sizeof (*db));
559 if (strcasecmp ("URL", ci->key) == 0)
560 {
561 status = cf_util_get_string (ci, &db->url);
562 if (status != 0)
563 {
564 sfree (db);
565 return (status);
566 }
567 }
568 else
569 {
570 ERROR ("curl_json plugin: cj_config: "
571 "Invalid key: %s", ci->key);
572 return (-1);
573 }
575 /* Fill the `cj_t' structure.. */
576 for (i = 0; i < ci->children_num; i++)
577 {
578 oconfig_item_t *child = ci->children + i;
580 if (strcasecmp ("Instance", child->key) == 0)
581 status = cf_util_get_string (child, &db->instance);
582 else if (strcasecmp ("Host", child->key) == 0)
583 status = cf_util_get_string (child, &db->host);
584 else if (strcasecmp ("User", child->key) == 0)
585 status = cf_util_get_string (child, &db->user);
586 else if (strcasecmp ("Password", child->key) == 0)
587 status = cf_util_get_string (child, &db->pass);
588 else if (strcasecmp ("VerifyPeer", child->key) == 0)
589 status = cf_util_get_boolean (child, &db->verify_peer);
590 else if (strcasecmp ("VerifyHost", child->key) == 0)
591 status = cf_util_get_boolean (child, &db->verify_host);
592 else if (strcasecmp ("CACert", child->key) == 0)
593 status = cf_util_get_string (child, &db->cacert);
594 else if (strcasecmp ("Key", child->key) == 0)
595 status = cj_config_add_key (db, child);
596 else
597 {
598 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
599 status = -1;
600 }
602 if (status != 0)
603 break;
604 }
606 if (status == 0)
607 {
608 if (db->tree == NULL)
609 {
610 WARNING ("curl_json plugin: No (valid) `Key' block "
611 "within `URL' block `%s'.", db->url);
612 status = -1;
613 }
614 if (status == 0)
615 status = cj_init_curl (db);
616 }
618 /* If all went well, register this database for reading */
619 if (status == 0)
620 {
621 user_data_t ud;
622 char cb_name[DATA_MAX_NAME_LEN];
624 if (db->instance == NULL)
625 db->instance = strdup("default");
627 DEBUG ("curl_json plugin: Registering new read callback: %s",
628 db->instance);
630 memset (&ud, 0, sizeof (ud));
631 ud.data = (void *) db;
632 ud.free_func = cj_free;
634 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
635 db->instance, db->url);
637 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
638 /* interval = */ NULL, &ud);
639 }
640 else
641 {
642 cj_free (db);
643 return (-1);
644 }
646 return (0);
647 }
648 /* }}} int cj_config_add_database */
650 static int cj_config (oconfig_item_t *ci) /* {{{ */
651 {
652 int success;
653 int errors;
654 int status;
655 int i;
657 success = 0;
658 errors = 0;
660 for (i = 0; i < ci->children_num; i++)
661 {
662 oconfig_item_t *child = ci->children + i;
664 if (strcasecmp ("URL", child->key) == 0)
665 {
666 status = cj_config_add_url (child);
667 if (status == 0)
668 success++;
669 else
670 errors++;
671 }
672 else
673 {
674 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
675 errors++;
676 }
677 }
679 if ((success == 0) && (errors > 0))
680 {
681 ERROR ("curl_json plugin: All statements failed.");
682 return (-1);
683 }
685 return (0);
686 } /* }}} int cj_config */
688 /* }}} End of configuration handling functions */
690 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
691 {
692 value_list_t vl = VALUE_LIST_INIT;
693 char *host;
695 vl.values = value;
696 vl.values_len = 1;
698 if ((db->host == NULL)
699 || (strcmp ("", db->host) == 0)
700 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
701 host = hostname_g;
702 else
703 host = db->host;
705 if (key->instance == NULL)
706 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
707 db->state[db->depth-1].name, db->state[db->depth].name);
708 else
709 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
711 sstrncpy (vl.host, host, sizeof (vl.host));
712 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
713 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
714 sstrncpy (vl.type, key->type, sizeof (vl.type));
716 plugin_dispatch_values (&vl);
717 } /* }}} int cj_submit */
719 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
720 {
721 int status;
722 long rc;
723 char *url;
724 yajl_handle yprev = db->yajl;
726 db->yajl = yajl_alloc (&ycallbacks, NULL, NULL, (void *)db);
727 if (db->yajl == NULL)
728 {
729 ERROR ("curl_json plugin: yajl_alloc failed.");
730 db->yajl = yprev;
731 return (-1);
732 }
734 status = curl_easy_perform (curl);
735 if (status != 0)
736 {
737 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
738 status, db->curl_errbuf, url);
739 yajl_free (db->yajl);
740 db->yajl = yprev;
741 return (-1);
742 }
744 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
745 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
747 /* The response code is zero if a non-HTTP transport was used. */
748 if ((rc != 0) && (rc != 200))
749 {
750 ERROR ("curl_json plugin: curl_easy_perform failed with "
751 "response code %ld (%s)", rc, url);
752 yajl_free (db->yajl);
753 db->yajl = yprev;
754 return (-1);
755 }
757 status = yajl_parse_complete (db->yajl);
758 if (status != yajl_status_ok)
759 {
760 unsigned char *errmsg;
762 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
763 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
764 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
765 (char *) errmsg);
766 yajl_free_error (db->yajl, errmsg);
767 yajl_free (db->yajl);
768 db->yajl = yprev;
769 return (-1);
770 }
772 yajl_free (db->yajl);
773 db->yajl = yprev;
774 return (0);
775 } /* }}} int cj_curl_perform */
777 static int cj_read (user_data_t *ud) /* {{{ */
778 {
779 cj_t *db;
781 if ((ud == NULL) || (ud->data == NULL))
782 {
783 ERROR ("curl_json plugin: cj_read: Invalid user data.");
784 return (-1);
785 }
787 db = (cj_t *) ud->data;
789 db->depth = 0;
790 memset (&db->state, 0, sizeof(db->state));
791 db->state[db->depth].tree = db->tree;
792 db->key = NULL;
794 return cj_curl_perform (db, db->curl);
795 } /* }}} int cj_read */
797 void module_register (void)
798 {
799 plugin_register_complex_config ("curl_json", cj_config);
800 } /* void module_register */
802 /* vim: set sw=2 sts=2 et fdm=marker : */