1 /**
2 * collectd - src/curl_json.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2013 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 collectd.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_avltree.h"
29 #include "utils_complain.h"
31 #include <sys/types.h>
32 #include <sys/un.h>
34 #include <curl/curl.h>
36 #include <yajl/yajl_parse.h>
37 #if HAVE_YAJL_YAJL_VERSION_H
38 # include <yajl/yajl_version.h>
39 #endif
41 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
42 # define HAVE_YAJL_V2 1
43 #endif
45 #define CJ_DEFAULT_HOST "localhost"
46 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
47 #define CJ_IS_KEY(key) ((key)->magic == CJ_KEY_MAGIC)
48 #define CJ_ANY "*"
49 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
51 struct cj_key_s;
52 typedef struct cj_key_s cj_key_t;
53 struct cj_key_s /* {{{ */
54 {
55 unsigned long magic;
56 char *path;
57 char *type;
58 char *instance;
59 };
60 /* }}} */
62 struct cj_s /* {{{ */
63 {
64 char *instance;
65 char *host;
67 char *sock;
69 char *url;
70 char *user;
71 char *pass;
72 char *credentials;
73 _Bool digest;
74 _Bool verify_peer;
75 _Bool verify_host;
76 char *cacert;
77 struct curl_slist *headers;
78 char *post_body;
79 cdtime_t interval;
80 int timeout;
82 CURL *curl;
83 char curl_errbuf[CURL_ERROR_SIZE];
85 yajl_handle yajl;
86 c_avl_tree_t *tree;
87 cj_key_t *key;
88 int depth;
89 struct {
90 union {
91 c_avl_tree_t *tree;
92 cj_key_t *key;
93 };
94 _Bool in_array;
95 int index;
96 char name[DATA_MAX_NAME_LEN];
97 } state[YAJL_MAX_DEPTH];
98 };
99 typedef struct cj_s cj_t; /* }}} */
101 #if HAVE_YAJL_V2
102 typedef size_t yajl_len_t;
103 #else
104 typedef unsigned int yajl_len_t;
105 #endif
107 static int cj_read (user_data_t *ud);
108 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value);
110 static size_t cj_curl_callback (void *buf, /* {{{ */
111 size_t size, size_t nmemb, void *user_data)
112 {
113 cj_t *db;
114 size_t len;
115 yajl_status status;
117 len = size * nmemb;
119 if (len <= 0)
120 return (len);
122 db = user_data;
123 if (db == NULL)
124 return (0);
126 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
127 if (status == yajl_status_ok)
128 return (len);
129 #if !HAVE_YAJL_V2
130 else if (status == yajl_status_insufficient_data)
131 return (len);
132 #endif
134 unsigned char *msg = yajl_get_error(db->yajl, /* verbose = */ 1,
135 /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
136 ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
137 yajl_free_error(db->yajl, msg);
138 return (0); /* abort write callback */
139 } /* }}} size_t cj_curl_callback */
141 static int cj_get_type (cj_key_t *key)
142 {
143 const data_set_t *ds;
145 ds = plugin_get_ds (key->type);
146 if (ds == NULL)
147 {
148 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
150 assert (key->type != NULL);
151 if (strcmp (type, key->type) != 0)
152 {
153 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
154 key->type);
155 sstrncpy (type, key->type, sizeof (type));
156 }
158 return -1;
159 }
160 else if (ds->ds_num > 1)
161 {
162 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
164 c_complain_once (LOG_WARNING, &complaint,
165 "curl_json plugin: The type \"%s\" has more than one data source. "
166 "This is currently not supported. I will return the type of the "
167 "first data source, but this will likely lead to problems later on.",
168 key->type);
169 }
171 return ds->ds[0].type;
172 }
174 static int cj_cb_map_key (void *ctx, const unsigned char *val,
175 yajl_len_t len);
177 static void cj_cb_inc_array_index (void *ctx, _Bool update_key)
178 {
179 cj_t *db = (cj_t *)ctx;
181 if (!db->state[db->depth].in_array)
182 return;
184 db->state[db->depth].index++;
186 if (update_key)
187 {
188 char name[DATA_MAX_NAME_LEN];
190 ssnprintf (name, sizeof (name), "%d", db->state[db->depth].index - 1);
192 cj_cb_map_key (ctx, (unsigned char *)name, (yajl_len_t) strlen (name));
193 }
194 }
196 /* yajl callbacks */
197 #define CJ_CB_ABORT 0
198 #define CJ_CB_CONTINUE 1
200 static int cj_cb_boolean (void * ctx, int boolVal)
201 {
202 cj_cb_inc_array_index (ctx, /* update_key = */ 0);
203 return (CJ_CB_CONTINUE);
204 }
206 static int cj_cb_null (void * ctx)
207 {
208 cj_cb_inc_array_index (ctx, /* update_key = */ 0);
209 return (CJ_CB_CONTINUE);
210 }
212 static int cj_cb_number (void *ctx,
213 const char *number, yajl_len_t number_len)
214 {
215 char buffer[number_len + 1];
217 cj_t *db = (cj_t *)ctx;
218 cj_key_t *key = db->state[db->depth].key;
219 value_t vt;
220 int type;
221 int status;
223 /* Create a null-terminated version of the string. */
224 memcpy (buffer, number, number_len);
225 buffer[sizeof (buffer) - 1] = 0;
227 if ((key == NULL) || !CJ_IS_KEY (key)) {
228 if (key != NULL && !db->state[db->depth].in_array/*can be inhomogeneous*/)
229 NOTICE ("curl_json plugin: Found \"%s\", but the configuration expects"
230 " a map.", buffer);
231 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
232 key = db->state[db->depth].key;
233 if (key == NULL) {
234 return (CJ_CB_CONTINUE);
235 }
236 }
237 else
238 {
239 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
240 }
242 type = cj_get_type (key);
243 status = parse_value (buffer, &vt, type);
244 if (status != 0)
245 {
246 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
247 return (CJ_CB_CONTINUE);
248 }
250 cj_submit (db, key, &vt);
251 return (CJ_CB_CONTINUE);
252 } /* int cj_cb_number */
254 /* Queries the key-tree of the parent context for "in_name" and, if found,
255 * updates the "key" field of the current context. Otherwise, "key" is set to
256 * NULL. */
257 static int cj_cb_map_key (void *ctx,
258 unsigned char const *in_name, yajl_len_t in_name_len)
259 {
260 cj_t *db = (cj_t *)ctx;
261 c_avl_tree_t *tree;
263 tree = db->state[db->depth-1].tree;
265 if (tree != NULL)
266 {
267 cj_key_t *value = NULL;
268 char *name;
269 size_t name_len;
271 /* Create a null-terminated version of the name. */
272 name = db->state[db->depth].name;
273 name_len = COUCH_MIN ((size_t) in_name_len,
274 sizeof (db->state[db->depth].name) - 1);
275 memcpy (name, in_name, name_len);
276 name[name_len] = 0;
278 if (c_avl_get (tree, name, (void *) &value) == 0) {
279 if (CJ_IS_KEY((cj_key_t*)value)) {
280 db->state[db->depth].key = value;
281 }
282 else {
283 db->state[db->depth].tree = (c_avl_tree_t*) value;
284 }
285 }
286 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
287 if (CJ_IS_KEY((cj_key_t*)value)) {
288 db->state[db->depth].key = value;
289 }
290 else {
291 db->state[db->depth].tree = (c_avl_tree_t*) value;
292 }
293 else
294 db->state[db->depth].key = NULL;
295 }
297 return (CJ_CB_CONTINUE);
298 }
300 static int cj_cb_string (void *ctx, const unsigned char *val,
301 yajl_len_t len)
302 {
303 /* Handle the string as if it was a number. */
304 return (cj_cb_number (ctx, (const char *) val, len));
305 } /* int cj_cb_string */
307 static int cj_cb_start (void *ctx)
308 {
309 cj_t *db = (cj_t *)ctx;
310 if (++db->depth >= YAJL_MAX_DEPTH)
311 {
312 ERROR ("curl_json plugin: %s depth exceeds max, aborting.",
313 db->url ? db->url : db->sock);
314 return (CJ_CB_ABORT);
315 }
316 return (CJ_CB_CONTINUE);
317 }
319 static int cj_cb_end (void *ctx)
320 {
321 cj_t *db = (cj_t *)ctx;
322 db->state[db->depth].tree = NULL;
323 --db->depth;
324 return (CJ_CB_CONTINUE);
325 }
327 static int cj_cb_start_map (void *ctx)
328 {
329 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
330 return cj_cb_start (ctx);
331 }
333 static int cj_cb_end_map (void *ctx)
334 {
335 return cj_cb_end (ctx);
336 }
338 static int cj_cb_start_array (void * ctx)
339 {
340 cj_t *db = (cj_t *)ctx;
341 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
342 if (db->depth+1 < YAJL_MAX_DEPTH) {
343 db->state[db->depth+1].in_array = 1;
344 db->state[db->depth+1].index = 0;
345 }
346 return cj_cb_start (ctx);
347 }
349 static int cj_cb_end_array (void * ctx)
350 {
351 cj_t *db = (cj_t *)ctx;
352 db->state[db->depth].in_array = 0;
353 return cj_cb_end (ctx);
354 }
356 static yajl_callbacks ycallbacks = {
357 cj_cb_null, /* null */
358 cj_cb_boolean, /* boolean */
359 NULL, /* integer */
360 NULL, /* double */
361 cj_cb_number,
362 cj_cb_string,
363 cj_cb_start_map,
364 cj_cb_map_key,
365 cj_cb_end_map,
366 cj_cb_start_array,
367 cj_cb_end_array
368 };
370 /* end yajl callbacks */
372 static void cj_key_free (cj_key_t *key) /* {{{ */
373 {
374 if (key == NULL)
375 return;
377 sfree (key->path);
378 sfree (key->type);
379 sfree (key->instance);
381 sfree (key);
382 } /* }}} void cj_key_free */
384 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
385 {
386 char *name;
387 void *value;
389 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
390 {
391 cj_key_t *key = (cj_key_t *)value;
393 if (CJ_IS_KEY(key))
394 cj_key_free (key);
395 else
396 cj_tree_free ((c_avl_tree_t *)value);
398 sfree (name);
399 }
401 c_avl_destroy (tree);
402 } /* }}} void cj_tree_free */
404 static void cj_free (void *arg) /* {{{ */
405 {
406 cj_t *db;
408 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
410 db = (cj_t *) arg;
412 if (db == NULL)
413 return;
415 if (db->curl != NULL)
416 curl_easy_cleanup (db->curl);
417 db->curl = NULL;
419 if (db->tree != NULL)
420 cj_tree_free (db->tree);
421 db->tree = NULL;
423 sfree (db->instance);
424 sfree (db->host);
426 sfree (db->sock);
428 sfree (db->url);
429 sfree (db->user);
430 sfree (db->pass);
431 sfree (db->credentials);
432 sfree (db->cacert);
433 sfree (db->post_body);
434 curl_slist_free_all (db->headers);
436 sfree (db);
437 } /* }}} void cj_free */
439 /* Configuration handling functions {{{ */
441 static c_avl_tree_t *cj_avl_create(void)
442 {
443 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
444 }
446 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
447 oconfig_item_t *ci)
448 {
449 struct curl_slist *temp = NULL;
450 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
451 {
452 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
453 return (-1);
454 }
456 temp = curl_slist_append(*dest, ci->values[0].value.string);
457 if (temp == NULL)
458 return (-1);
460 *dest = temp;
462 return (0);
463 } /* }}} int cj_config_append_string */
465 static int cj_config_add_key (cj_t *db, /* {{{ */
466 oconfig_item_t *ci)
467 {
468 cj_key_t *key;
469 int status;
470 int i;
472 if ((ci->values_num != 1)
473 || (ci->values[0].type != OCONFIG_TYPE_STRING))
474 {
475 WARNING ("curl_json plugin: The `Key' block "
476 "needs exactly one string argument.");
477 return (-1);
478 }
480 key = calloc (1, sizeof (*key));
481 if (key == NULL)
482 {
483 ERROR ("curl_json plugin: calloc failed.");
484 return (-1);
485 }
486 key->magic = CJ_KEY_MAGIC;
488 if (strcasecmp ("Key", ci->key) == 0)
489 {
490 status = cf_util_get_string (ci, &key->path);
491 if (status != 0)
492 {
493 sfree (key);
494 return (status);
495 }
496 }
497 else
498 {
499 ERROR ("curl_json plugin: cj_config: "
500 "Invalid key: %s", ci->key);
501 cj_key_free (key);
502 return (-1);
503 }
505 status = 0;
506 for (i = 0; i < ci->children_num; i++)
507 {
508 oconfig_item_t *child = ci->children + i;
510 if (strcasecmp ("Type", child->key) == 0)
511 status = cf_util_get_string (child, &key->type);
512 else if (strcasecmp ("Instance", child->key) == 0)
513 status = cf_util_get_string (child, &key->instance);
514 else
515 {
516 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
517 status = -1;
518 }
520 if (status != 0)
521 break;
522 } /* for (i = 0; i < ci->children_num; i++) */
524 if (status != 0)
525 {
526 cj_key_free (key);
527 return (-1);
528 }
530 if (key->type == NULL)
531 {
532 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
533 cj_key_free (key);
534 return (-1);
535 }
537 /* store path in a tree that will match the json map structure, example:
538 * "httpd/requests/count",
539 * "httpd/requests/current" ->
540 * { "httpd": { "requests": { "count": $key, "current": $key } } }
541 */
542 char *ptr;
543 char *name;
544 c_avl_tree_t *tree;
546 if (db->tree == NULL)
547 db->tree = cj_avl_create();
549 tree = db->tree;
550 ptr = key->path;
551 if (*ptr == '/')
552 ++ptr;
554 name = ptr;
555 while ((ptr = strchr (name, '/')) != NULL)
556 {
557 char ent[PATH_MAX];
558 c_avl_tree_t *value;
559 size_t len;
561 len = ptr - name;
562 if (len == 0)
563 break;
565 len = COUCH_MIN(len, sizeof (ent)-1);
566 sstrncpy (ent, name, len+1);
568 if (c_avl_get (tree, ent, (void *) &value) != 0)
569 {
570 value = cj_avl_create ();
571 c_avl_insert (tree, strdup (ent), value);
572 }
574 tree = value;
575 name = ptr + 1;
576 }
578 if (strlen (name) == 0)
579 {
580 ERROR ("curl_json plugin: invalid key: %s", key->path);
581 cj_key_free (key);
582 return (-1);
583 }
585 c_avl_insert (tree, strdup (name), key);
586 return (status);
587 } /* }}} int cj_config_add_key */
589 static int cj_init_curl (cj_t *db) /* {{{ */
590 {
591 db->curl = curl_easy_init ();
592 if (db->curl == NULL)
593 {
594 ERROR ("curl_json plugin: curl_easy_init failed.");
595 return (-1);
596 }
598 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
599 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
600 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
601 curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
602 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
603 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
604 curl_easy_setopt (db->curl, CURLOPT_FOLLOWLOCATION, 1L);
605 curl_easy_setopt (db->curl, CURLOPT_MAXREDIRS, 50L);
607 if (db->user != NULL)
608 {
609 #ifdef HAVE_CURLOPT_USERNAME
610 curl_easy_setopt (db->curl, CURLOPT_USERNAME, db->user);
611 curl_easy_setopt (db->curl, CURLOPT_PASSWORD,
612 (db->pass == NULL) ? "" : db->pass);
613 #else
614 size_t credentials_size;
616 credentials_size = strlen (db->user) + 2;
617 if (db->pass != NULL)
618 credentials_size += strlen (db->pass);
620 db->credentials = malloc (credentials_size);
621 if (db->credentials == NULL)
622 {
623 ERROR ("curl_json plugin: malloc failed.");
624 return (-1);
625 }
627 ssnprintf (db->credentials, credentials_size, "%s:%s",
628 db->user, (db->pass == NULL) ? "" : db->pass);
629 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
630 #endif
632 if (db->digest)
633 curl_easy_setopt (db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
634 }
636 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
637 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
638 db->verify_host ? 2L : 0L);
639 if (db->cacert != NULL)
640 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
641 if (db->headers != NULL)
642 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
643 if (db->post_body != NULL)
644 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
646 #ifdef HAVE_CURLOPT_TIMEOUT_MS
647 if (db->timeout >= 0)
648 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout);
649 else if (db->interval > 0)
650 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(db->timeout));
651 else
652 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
653 #endif
655 return (0);
656 } /* }}} int cj_init_curl */
658 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
659 {
660 cj_t *db;
661 int status = 0;
662 int i;
664 if ((ci->values_num != 1)
665 || (ci->values[0].type != OCONFIG_TYPE_STRING))
666 {
667 WARNING ("curl_json plugin: The `URL' block "
668 "needs exactly one string argument.");
669 return (-1);
670 }
672 db = calloc (1, sizeof (*db));
673 if (db == NULL)
674 {
675 ERROR ("curl_json plugin: calloc failed.");
676 return (-1);
677 }
679 db->timeout = -1;
681 if (strcasecmp ("URL", ci->key) == 0)
682 status = cf_util_get_string (ci, &db->url);
683 else if (strcasecmp ("Sock", ci->key) == 0)
684 status = cf_util_get_string (ci, &db->sock);
685 else
686 {
687 ERROR ("curl_json plugin: cj_config: "
688 "Invalid key: %s", ci->key);
689 cj_free (db);
690 return (-1);
691 }
692 if (status != 0)
693 {
694 sfree (db);
695 return (status);
696 }
698 /* Fill the `cj_t' structure.. */
699 for (i = 0; i < ci->children_num; i++)
700 {
701 oconfig_item_t *child = ci->children + i;
703 if (strcasecmp ("Instance", child->key) == 0)
704 status = cf_util_get_string (child, &db->instance);
705 else if (strcasecmp ("Host", child->key) == 0)
706 status = cf_util_get_string (child, &db->host);
707 else if (db->url && strcasecmp ("User", child->key) == 0)
708 status = cf_util_get_string (child, &db->user);
709 else if (db->url && strcasecmp ("Password", child->key) == 0)
710 status = cf_util_get_string (child, &db->pass);
711 else if (strcasecmp ("Digest", child->key) == 0)
712 status = cf_util_get_boolean (child, &db->digest);
713 else if (db->url && strcasecmp ("VerifyPeer", child->key) == 0)
714 status = cf_util_get_boolean (child, &db->verify_peer);
715 else if (db->url && strcasecmp ("VerifyHost", child->key) == 0)
716 status = cf_util_get_boolean (child, &db->verify_host);
717 else if (db->url && strcasecmp ("CACert", child->key) == 0)
718 status = cf_util_get_string (child, &db->cacert);
719 else if (db->url && strcasecmp ("Header", child->key) == 0)
720 status = cj_config_append_string ("Header", &db->headers, child);
721 else if (db->url && strcasecmp ("Post", child->key) == 0)
722 status = cf_util_get_string (child, &db->post_body);
723 else if (strcasecmp ("Key", child->key) == 0)
724 status = cj_config_add_key (db, child);
725 else if (strcasecmp ("Interval", child->key) == 0)
726 status = cf_util_get_cdtime(child, &db->interval);
727 else if (strcasecmp ("Timeout", child->key) == 0)
728 status = cf_util_get_int (child, &db->timeout);
729 else
730 {
731 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
732 status = -1;
733 }
735 if (status != 0)
736 break;
737 }
739 if (status == 0)
740 {
741 if (db->tree == NULL)
742 {
743 WARNING ("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".",
744 db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
745 status = -1;
746 }
747 if (status == 0 && db->url)
748 status = cj_init_curl (db);
749 }
751 /* If all went well, register this database for reading */
752 if (status == 0)
753 {
754 user_data_t ud;
755 char *cb_name;
757 if (db->instance == NULL)
758 db->instance = strdup("default");
760 DEBUG ("curl_json plugin: Registering new read callback: %s",
761 db->instance);
763 memset (&ud, 0, sizeof (ud));
764 ud.data = (void *) db;
765 ud.free_func = cj_free;
767 cb_name = ssnprintf_alloc ("curl_json-%s-%s",
768 db->instance, db->url ? db->url : db->sock);
770 plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
771 /* interval = */ db->interval,
772 &ud);
773 sfree (cb_name);
774 }
775 else
776 {
777 cj_free (db);
778 return (-1);
779 }
781 return (0);
782 }
783 /* }}} int cj_config_add_database */
785 static int cj_config (oconfig_item_t *ci) /* {{{ */
786 {
787 int success;
788 int errors;
789 int status;
790 int i;
792 success = 0;
793 errors = 0;
795 for (i = 0; i < ci->children_num; i++)
796 {
797 oconfig_item_t *child = ci->children + i;
799 if (strcasecmp ("Sock", child->key) == 0
800 || strcasecmp ("URL", child->key) == 0)
801 {
802 status = cj_config_add_url (child);
803 if (status == 0)
804 success++;
805 else
806 errors++;
807 }
808 else
809 {
810 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
811 errors++;
812 }
813 }
815 if ((success == 0) && (errors > 0))
816 {
817 ERROR ("curl_json plugin: All statements failed.");
818 return (-1);
819 }
821 return (0);
822 } /* }}} int cj_config */
824 /* }}} End of configuration handling functions */
826 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
827 {
828 value_list_t vl = VALUE_LIST_INIT;
829 char *host;
831 vl.values = value;
832 vl.values_len = 1;
834 if ((db->host == NULL)
835 || (strcmp ("", db->host) == 0)
836 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
837 host = hostname_g;
838 else
839 host = db->host;
841 if (key->instance == NULL)
842 {
843 int i, len = 0;
844 for (i = 0; i < db->depth; i++)
845 len += ssnprintf(vl.type_instance+len, sizeof(vl.type_instance)-len,
846 i ? "-%s" : "%s", db->state[i+1].name);
847 }
848 else
849 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
851 sstrncpy (vl.host, host, sizeof (vl.host));
852 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
853 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
854 sstrncpy (vl.type, key->type, sizeof (vl.type));
856 if (db->interval > 0)
857 vl.interval = db->interval;
859 plugin_dispatch_values (&vl);
860 } /* }}} int cj_submit */
862 static int cj_sock_perform (cj_t *db) /* {{{ */
863 {
864 char errbuf[1024];
865 struct sockaddr_un sa_unix = { 0 };
866 sa_unix.sun_family = AF_UNIX;
867 sstrncpy (sa_unix.sun_path, db->sock, sizeof (sa_unix.sun_path));
869 int fd = socket (AF_UNIX, SOCK_STREAM, 0);
870 if (fd < 0)
871 return (-1);
872 if (connect (fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0)
873 {
874 ERROR ("curl_json plugin: connect(%s) failed: %s",
875 (db->sock != NULL) ? db->sock : "<null>",
876 sstrerror(errno, errbuf, sizeof (errbuf)));
877 close (fd);
878 return (-1);
879 }
881 ssize_t red;
882 do {
883 unsigned char buffer[4096];
884 red = read (fd, buffer, sizeof(buffer));
885 if (red < 0) {
886 ERROR ("curl_json plugin: read(%s) failed: %s",
887 (db->sock != NULL) ? db->sock : "<null>",
888 sstrerror(errno, errbuf, sizeof (errbuf)));
889 close (fd);
890 return (-1);
891 }
892 if (!cj_curl_callback (buffer, red, 1, db))
893 break;
894 } while (red > 0);
895 close (fd);
896 return (0);
897 } /* }}} int cj_sock_perform */
900 static int cj_curl_perform(cj_t *db) /* {{{ */
901 {
902 int status;
903 long rc;
904 char *url;
905 url = db->url;
907 status = curl_easy_perform (db->curl);
908 if (status != CURLE_OK)
909 {
910 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
911 status, db->curl_errbuf, url);
912 return (-1);
913 }
915 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
916 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
918 /* The response code is zero if a non-HTTP transport was used. */
919 if ((rc != 0) && (rc != 200))
920 {
921 ERROR ("curl_json plugin: curl_easy_perform failed with "
922 "response code %ld (%s)", rc, url);
923 return (-1);
924 }
925 return (0);
926 } /* }}} int cj_curl_perform */
928 static int cj_perform (cj_t *db) /* {{{ */
929 {
930 int status;
931 yajl_handle yprev = db->yajl;
933 db->yajl = yajl_alloc (&ycallbacks,
934 #if HAVE_YAJL_V2
935 /* alloc funcs = */ NULL,
936 #else
937 /* alloc funcs = */ NULL, NULL,
938 #endif
939 /* context = */ (void *)db);
940 if (db->yajl == NULL)
941 {
942 ERROR ("curl_json plugin: yajl_alloc failed.");
943 db->yajl = yprev;
944 return (-1);
945 }
947 if (db->url)
948 status = cj_curl_perform (db);
949 else
950 status = cj_sock_perform (db);
951 if (status < 0)
952 {
953 yajl_free (db->yajl);
954 db->yajl = yprev;
955 return (-1);
956 }
958 #if HAVE_YAJL_V2
959 status = yajl_complete_parse(db->yajl);
960 #else
961 status = yajl_parse_complete(db->yajl);
962 #endif
963 if (status != yajl_status_ok)
964 {
965 unsigned char *errmsg;
967 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
968 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
969 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
970 (char *) errmsg);
971 yajl_free_error (db->yajl, errmsg);
972 yajl_free (db->yajl);
973 db->yajl = yprev;
974 return (-1);
975 }
977 yajl_free (db->yajl);
978 db->yajl = yprev;
979 return (0);
980 } /* }}} int cj_perform */
982 static int cj_read (user_data_t *ud) /* {{{ */
983 {
984 cj_t *db;
986 if ((ud == NULL) || (ud->data == NULL))
987 {
988 ERROR ("curl_json plugin: cj_read: Invalid user data.");
989 return (-1);
990 }
992 db = (cj_t *) ud->data;
994 db->depth = 0;
995 memset (&db->state, 0, sizeof(db->state));
996 db->state[db->depth].tree = db->tree;
997 db->key = NULL;
999 return cj_perform (db);
1000 } /* }}} int cj_read */
1002 static int cj_init (void) /* {{{ */
1003 {
1004 /* Call this while collectd is still single-threaded to avoid
1005 * initialization issues in libgcrypt. */
1006 curl_global_init (CURL_GLOBAL_SSL);
1007 return (0);
1008 } /* }}} int cj_init */
1010 void module_register (void)
1011 {
1012 plugin_register_complex_config ("curl_json", cj_config);
1013 plugin_register_init ("curl_json", cj_init);
1014 } /* void module_register */
1016 /* vim: set sw=2 sts=2 et fdm=marker : */