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 int verify_peer;
60 int 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 {
103 status = yajl_parse_complete(db->yajl);
104 return (len);
105 }
106 else if (status == yajl_status_insufficient_data)
107 return (len);
109 if (status != yajl_status_ok)
110 {
111 unsigned char *msg =
112 yajl_get_error(db->yajl, 1, (unsigned char *)buf, len);
113 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
114 yajl_free_error(db->yajl, msg);
115 return (0); /* abort write callback */
116 }
118 return (len);
119 } /* }}} size_t cj_curl_callback */
121 static int cj_get_type (cj_key_t *key)
122 {
123 const data_set_t *ds;
125 ds = plugin_get_ds (key->type);
126 if (ds == NULL)
127 return -1; /* let plugin_write do the complaining */
128 else
129 return ds->ds[0].type; /* XXX support ds->ds_len > 1 */
130 }
132 /* yajl callbacks */
133 #define CJ_CB_ABORT 0
134 #define CJ_CB_CONTINUE 1
136 /* "number" may not be null terminated, so copy it into a buffer before
137 * parsing. */
138 static int cj_cb_number (void *ctx,
139 const char *number, unsigned int number_len)
140 {
141 char buffer[number_len + 1];
143 cj_t *db = (cj_t *)ctx;
144 cj_key_t *key = db->state[db->depth].key;
145 char *endptr;
146 value_t vt;
147 int type;
149 if (key == NULL)
150 return (CJ_CB_CONTINUE);
152 memcpy (buffer, number, number_len);
153 buffer[sizeof (buffer) - 1] = 0;
155 type = cj_get_type (key);
157 endptr = NULL;
158 errno = 0;
160 if (type == DS_TYPE_COUNTER)
161 vt.counter = (counter_t) strtoull (buffer, &endptr, /* base = */ 0);
162 else if (type == DS_TYPE_GAUGE)
163 vt.gauge = (gauge_t) strtod (buffer, &endptr);
164 else if (type == DS_TYPE_DERIVE)
165 vt.derive = (derive_t) strtoll (buffer, &endptr, /* base = */ 0);
166 else if (type == DS_TYPE_ABSOLUTE)
167 vt.absolute = (absolute_t) strtoull (buffer, &endptr, /* base = */ 0);
168 else
169 {
170 ERROR ("curl_json plugin: Unknown data source type: \"%s\"", key->type);
171 return (CJ_CB_ABORT);
172 }
174 if ((endptr == &buffer[0]) || (errno != 0))
175 {
176 NOTICE ("curl_json plugin: Overflow while parsing number. "
177 "Ignoring this value.");
178 return (CJ_CB_CONTINUE);
179 }
181 cj_submit (db, key, &vt);
182 return (CJ_CB_CONTINUE);
183 } /* int cj_cb_number */
185 static int cj_cb_map_key (void *ctx, const unsigned char *val,
186 unsigned int len)
187 {
188 cj_t *db = (cj_t *)ctx;
189 c_avl_tree_t *tree;
191 tree = db->state[db->depth-1].tree;
193 if (tree != NULL)
194 {
195 cj_key_t *value;
196 char *name;
198 name = db->state[db->depth].name;
199 len = COUCH_MIN(len, sizeof (db->state[db->depth].name)-1);
200 sstrncpy (name, (char *)val, len+1);
202 if (c_avl_get (tree, name, (void *) &value) == 0)
203 db->state[db->depth].key = value;
204 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
205 db->state[db->depth].key = value;
206 else
207 db->state[db->depth].key = NULL;
208 }
210 return (CJ_CB_CONTINUE);
211 }
213 static int cj_cb_string (void *ctx, const unsigned char *val,
214 unsigned int len)
215 {
216 cj_t *db = (cj_t *)ctx;
217 c_avl_tree_t *tree;
218 char *ptr;
220 if (db->depth != 1) /* e.g. _all_dbs */
221 return (CJ_CB_CONTINUE);
223 cj_cb_map_key (ctx, val, len); /* same logic */
225 tree = db->state[db->depth].tree;
227 if ((tree != NULL) && (ptr = rindex (db->url, '/')))
228 {
229 char url[PATH_MAX];
230 CURL *curl;
232 /* url =~ s,[^/]+$,$name, */
233 len = (ptr - db->url) + 1;
234 ptr = url;
235 sstrncpy (ptr, db->url, sizeof (url));
236 sstrncpy (ptr + len, db->state[db->depth].name, sizeof (url) - len);
238 curl = curl_easy_duphandle (db->curl);
239 curl_easy_setopt (curl, CURLOPT_URL, url);
240 cj_curl_perform (db, curl);
241 curl_easy_cleanup (curl);
242 }
243 return (CJ_CB_CONTINUE);
244 }
246 static int cj_cb_start (void *ctx)
247 {
248 cj_t *db = (cj_t *)ctx;
249 if (++db->depth >= YAJL_MAX_DEPTH)
250 {
251 ERROR ("curl_json plugin: %s depth exceeds max, aborting.", db->url);
252 return (CJ_CB_ABORT);
253 }
254 return (CJ_CB_CONTINUE);
255 }
257 static int cj_cb_end (void *ctx)
258 {
259 cj_t *db = (cj_t *)ctx;
260 db->state[db->depth].tree = NULL;
261 --db->depth;
262 return (CJ_CB_CONTINUE);
263 }
265 static int cj_cb_start_map (void *ctx)
266 {
267 return cj_cb_start (ctx);
268 }
270 static int cj_cb_end_map (void *ctx)
271 {
272 return cj_cb_end (ctx);
273 }
275 static int cj_cb_start_array (void * ctx)
276 {
277 return cj_cb_start (ctx);
278 }
280 static int cj_cb_end_array (void * ctx)
281 {
282 return cj_cb_end (ctx);
283 }
285 static yajl_callbacks ycallbacks = {
286 NULL, /* null */
287 NULL, /* boolean */
288 NULL, /* integer */
289 NULL, /* double */
290 cj_cb_number,
291 cj_cb_string,
292 cj_cb_start_map,
293 cj_cb_map_key,
294 cj_cb_end_map,
295 cj_cb_start_array,
296 cj_cb_end_array
297 };
299 /* end yajl callbacks */
301 static void cj_key_free (cj_key_t *key) /* {{{ */
302 {
303 if (key == NULL)
304 return;
306 sfree (key->path);
307 sfree (key->type);
308 sfree (key->instance);
310 sfree (key);
311 } /* }}} void cj_key_free */
313 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
314 {
315 char *name;
316 void *value;
318 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
319 {
320 cj_key_t *key = (cj_key_t *)value;
322 if (CJ_IS_KEY(key))
323 cj_key_free (key);
324 else
325 cj_tree_free ((c_avl_tree_t *)value);
327 sfree (name);
328 }
330 c_avl_destroy (tree);
331 } /* }}} void cj_tree_free */
333 static void cj_free (void *arg) /* {{{ */
334 {
335 cj_t *db;
337 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
339 db = (cj_t *) arg;
341 if (db == NULL)
342 return;
344 if (db->curl != NULL)
345 curl_easy_cleanup (db->curl);
346 db->curl = NULL;
348 if (db->tree != NULL)
349 cj_tree_free (db->tree);
350 db->tree = NULL;
352 sfree (db->instance);
353 sfree (db->host);
355 sfree (db->url);
356 sfree (db->user);
357 sfree (db->pass);
358 sfree (db->credentials);
359 sfree (db->cacert);
361 sfree (db);
362 } /* }}} void cj_free */
364 /* Configuration handling functions {{{ */
366 static int cj_config_add_string (const char *name, char **dest, /* {{{ */
367 oconfig_item_t *ci)
368 {
369 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
370 {
371 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
372 return (-1);
373 }
375 sfree (*dest);
376 *dest = strdup (ci->values[0].value.string);
377 if (*dest == NULL)
378 return (-1);
380 return (0);
381 } /* }}} int cj_config_add_string */
383 static int cj_config_set_boolean (const char *name, int *dest, /* {{{ */
384 oconfig_item_t *ci)
385 {
386 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
387 {
388 WARNING ("curl_json plugin: `%s' needs exactly one boolean argument.", name);
389 return (-1);
390 }
392 *dest = ci->values[0].value.boolean ? 1 : 0;
394 return (0);
395 } /* }}} int cj_config_set_boolean */
397 static c_avl_tree_t *cj_avl_create(void)
398 {
399 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
400 }
402 static int cj_config_add_key (cj_t *db, /* {{{ */
403 oconfig_item_t *ci)
404 {
405 cj_key_t *key;
406 int status;
407 int i;
409 if ((ci->values_num != 1)
410 || (ci->values[0].type != OCONFIG_TYPE_STRING))
411 {
412 WARNING ("curl_json plugin: The `Key' block "
413 "needs exactly one string argument.");
414 return (-1);
415 }
417 key = (cj_key_t *) malloc (sizeof (*key));
418 if (key == NULL)
419 {
420 ERROR ("curl_json plugin: malloc failed.");
421 return (-1);
422 }
423 memset (key, 0, sizeof (*key));
424 key->magic = CJ_KEY_MAGIC;
426 if (strcasecmp ("Key", ci->key) == 0)
427 {
428 status = cj_config_add_string ("Key", &key->path, ci);
429 if (status != 0)
430 {
431 sfree (key);
432 return (status);
433 }
434 }
435 else
436 {
437 ERROR ("curl_json plugin: cj_config: "
438 "Invalid key: %s", ci->key);
439 return (-1);
440 }
442 status = 0;
443 for (i = 0; i < ci->children_num; i++)
444 {
445 oconfig_item_t *child = ci->children + i;
447 if (strcasecmp ("Type", child->key) == 0)
448 status = cj_config_add_string ("Type", &key->type, child);
449 else if (strcasecmp ("Instance", child->key) == 0)
450 status = cj_config_add_string ("Instance", &key->instance, child);
451 else
452 {
453 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
454 status = -1;
455 }
457 if (status != 0)
458 break;
459 } /* for (i = 0; i < ci->children_num; i++) */
461 while (status == 0)
462 {
463 if (key->type == NULL)
464 {
465 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
466 status = -1;
467 }
469 break;
470 } /* while (status == 0) */
472 /* store path in a tree that will match the json map structure, example:
473 * "httpd/requests/count",
474 * "httpd/requests/current" ->
475 * { "httpd": { "requests": { "count": $key, "current": $key } } }
476 */
477 if (status == 0)
478 {
479 char *ptr;
480 char *name;
481 char ent[PATH_MAX];
482 c_avl_tree_t *tree;
484 if (db->tree == NULL)
485 db->tree = cj_avl_create();
487 tree = db->tree;
488 name = key->path;
489 ptr = key->path;
490 if (*ptr == '/')
491 ++ptr;
493 name = ptr;
494 while (*ptr)
495 {
496 if (*ptr == '/')
497 {
498 c_avl_tree_t *value;
499 int len;
501 len = ptr-name;
502 if (len == 0)
503 break;
504 sstrncpy (ent, name, len+1);
506 if (c_avl_get (tree, ent, (void *) &value) != 0)
507 {
508 value = cj_avl_create ();
509 c_avl_insert (tree, strdup (ent), value);
510 }
512 tree = value;
513 name = ptr+1;
514 }
515 ++ptr;
516 }
517 if (*name)
518 c_avl_insert (tree, strdup(name), key);
519 else
520 {
521 ERROR ("curl_json plugin: invalid key: %s", key->path);
522 status = -1;
523 }
524 }
526 return (status);
527 } /* }}} int cj_config_add_key */
529 static int cj_init_curl (cj_t *db) /* {{{ */
530 {
531 db->curl = curl_easy_init ();
532 if (db->curl == NULL)
533 {
534 ERROR ("curl_json plugin: curl_easy_init failed.");
535 return (-1);
536 }
538 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
539 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
540 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
541 PACKAGE_NAME"/"PACKAGE_VERSION);
542 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
543 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
545 if (db->user != NULL)
546 {
547 size_t credentials_size;
549 credentials_size = strlen (db->user) + 2;
550 if (db->pass != NULL)
551 credentials_size += strlen (db->pass);
553 db->credentials = (char *) malloc (credentials_size);
554 if (db->credentials == NULL)
555 {
556 ERROR ("curl_json plugin: malloc failed.");
557 return (-1);
558 }
560 ssnprintf (db->credentials, credentials_size, "%s:%s",
561 db->user, (db->pass == NULL) ? "" : db->pass);
562 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
563 }
565 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
566 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
567 db->verify_host ? 2 : 0);
568 if (db->cacert != NULL)
569 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
571 return (0);
572 } /* }}} int cj_init_curl */
574 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
575 {
576 cj_t *db;
577 int status = 0;
578 int i;
580 if ((ci->values_num != 1)
581 || (ci->values[0].type != OCONFIG_TYPE_STRING))
582 {
583 WARNING ("curl_json plugin: The `URL' block "
584 "needs exactly one string argument.");
585 return (-1);
586 }
588 db = (cj_t *) malloc (sizeof (*db));
589 if (db == NULL)
590 {
591 ERROR ("curl_json plugin: malloc failed.");
592 return (-1);
593 }
594 memset (db, 0, sizeof (*db));
596 if (strcasecmp ("URL", ci->key) == 0)
597 {
598 status = cj_config_add_string ("URL", &db->url, ci);
599 if (status != 0)
600 {
601 sfree (db);
602 return (status);
603 }
604 }
605 else
606 {
607 ERROR ("curl_json plugin: cj_config: "
608 "Invalid key: %s", ci->key);
609 return (-1);
610 }
612 /* Fill the `cj_t' structure.. */
613 for (i = 0; i < ci->children_num; i++)
614 {
615 oconfig_item_t *child = ci->children + i;
617 if (strcasecmp ("Instance", child->key) == 0)
618 status = cj_config_add_string ("Instance", &db->instance, child);
619 else if (strcasecmp ("Host", child->key) == 0)
620 status = cj_config_add_string ("Host", &db->host, child);
621 else if (strcasecmp ("User", child->key) == 0)
622 status = cj_config_add_string ("User", &db->user, child);
623 else if (strcasecmp ("Password", child->key) == 0)
624 status = cj_config_add_string ("Password", &db->pass, child);
625 else if (strcasecmp ("VerifyPeer", child->key) == 0)
626 status = cj_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
627 else if (strcasecmp ("VerifyHost", child->key) == 0)
628 status = cj_config_set_boolean ("VerifyHost", &db->verify_host, child);
629 else if (strcasecmp ("CACert", child->key) == 0)
630 status = cj_config_add_string ("CACert", &db->cacert, child);
631 else if (strcasecmp ("Key", child->key) == 0)
632 status = cj_config_add_key (db, child);
633 else
634 {
635 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
636 status = -1;
637 }
639 if (status != 0)
640 break;
641 }
643 if (status == 0)
644 {
645 if (db->tree == NULL)
646 {
647 WARNING ("curl_json plugin: No (valid) `Key' block "
648 "within `URL' block `%s'.", db->url);
649 status = -1;
650 }
651 if (status == 0)
652 status = cj_init_curl (db);
653 }
655 /* If all went well, register this database for reading */
656 if (status == 0)
657 {
658 user_data_t ud;
659 char cb_name[DATA_MAX_NAME_LEN];
661 if (db->instance == NULL)
662 db->instance = strdup("default");
664 DEBUG ("curl_json plugin: Registering new read callback: %s",
665 db->instance);
667 memset (&ud, 0, sizeof (ud));
668 ud.data = (void *) db;
669 ud.free_func = cj_free;
671 ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
672 db->instance, db->url);
674 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
675 /* interval = */ NULL, &ud);
676 }
677 else
678 {
679 cj_free (db);
680 return (-1);
681 }
683 return (0);
684 }
685 /* }}} int cj_config_add_database */
687 static int cj_config (oconfig_item_t *ci) /* {{{ */
688 {
689 int success;
690 int errors;
691 int status;
692 int i;
694 success = 0;
695 errors = 0;
697 for (i = 0; i < ci->children_num; i++)
698 {
699 oconfig_item_t *child = ci->children + i;
701 if (strcasecmp ("URL", child->key) == 0)
702 {
703 status = cj_config_add_url (child);
704 if (status == 0)
705 success++;
706 else
707 errors++;
708 }
709 else
710 {
711 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
712 errors++;
713 }
714 }
716 if ((success == 0) && (errors > 0))
717 {
718 ERROR ("curl_json plugin: All statements failed.");
719 return (-1);
720 }
722 return (0);
723 } /* }}} int cj_config */
725 /* }}} End of configuration handling functions */
727 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
728 {
729 value_list_t vl = VALUE_LIST_INIT;
730 char *host;
732 vl.values = value;
733 vl.values_len = 1;
735 if ((db->host == NULL)
736 || (strcmp ("", db->host) == 0)
737 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
738 host = hostname_g;
739 else
740 host = db->host;
742 if (key->instance == NULL)
743 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
744 db->state[db->depth-1].name, db->state[db->depth].name);
745 else
746 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
748 sstrncpy (vl.host, host, sizeof (vl.host));
749 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
750 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
751 sstrncpy (vl.type, key->type, sizeof (vl.type));
753 plugin_dispatch_values (&vl);
754 } /* }}} int cj_submit */
756 static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
757 {
758 int status;
759 long rc;
760 char *url;
761 yajl_handle yprev = db->yajl;
763 db->yajl = yajl_alloc (&ycallbacks, NULL, NULL, (void *)db);
764 if (db->yajl == NULL)
765 {
766 ERROR ("curl_json plugin: yajl_alloc failed.");
767 db->yajl = yprev;
768 return (-1);
769 }
771 status = curl_easy_perform (curl);
773 yajl_free (db->yajl);
774 db->yajl = yprev;
776 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
777 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
779 /* The response code is zero if a non-HTTP transport was used. */
780 if ((rc != 0) && (rc != 200))
781 {
782 ERROR ("curl_json plugin: curl_easy_perform failed with response code %ld (%s)",
783 rc, url);
784 return (-1);
785 }
787 if (status != 0)
788 {
789 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
790 status, db->curl_errbuf, url);
791 return (-1);
792 }
794 return (0);
795 } /* }}} int cj_curl_perform */
797 static int cj_read (user_data_t *ud) /* {{{ */
798 {
799 cj_t *db;
801 if ((ud == NULL) || (ud->data == NULL))
802 {
803 ERROR ("curl_json plugin: cj_read: Invalid user data.");
804 return (-1);
805 }
807 db = (cj_t *) ud->data;
809 db->depth = 0;
810 memset (&db->state, 0, sizeof(db->state));
811 db->state[db->depth].tree = db->tree;
812 db->key = NULL;
814 return cj_curl_perform (db, db->curl);
815 } /* }}} int cj_read */
817 void module_register (void)
818 {
819 plugin_register_complex_config ("curl_json", cj_config);
820 } /* void module_register */
822 /* vim: set sw=2 sts=2 et fdm=marker : */