5875feaef1c334b40aa56e2827c021713a0e5ac9
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/socket.h>
32 #include <sys/types.h>
33 #include <sys/un.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>
40 #endif
42 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
43 # define HAVE_YAJL_V2 1
44 #endif
46 #define CJ_DEFAULT_HOST "localhost"
47 #define CJ_KEY_MAGIC 0x43484b59UL /* CHKY */
48 #define CJ_IS_KEY(key) ((key)->magic == CJ_KEY_MAGIC)
49 #define CJ_ANY "*"
50 #define COUCH_MIN(x,y) ((x) < (y) ? (x) : (y))
52 struct cj_key_s;
53 typedef struct cj_key_s cj_key_t;
54 struct cj_key_s /* {{{ */
55 {
56 unsigned long magic;
57 char *path;
58 char *type;
59 char *instance;
60 };
61 /* }}} */
63 struct cj_s /* {{{ */
64 {
65 char *instance;
66 char *host;
68 char *sock;
70 char *url;
71 char *user;
72 char *pass;
73 char *credentials;
74 _Bool verify_peer;
75 _Bool verify_host;
76 char *cacert;
77 struct curl_slist *headers;
78 char *post_body;
80 CURL *curl;
81 char curl_errbuf[CURL_ERROR_SIZE];
83 yajl_handle yajl;
84 c_avl_tree_t *tree;
85 cj_key_t *key;
86 int depth;
87 struct {
88 union {
89 c_avl_tree_t *tree;
90 cj_key_t *key;
91 };
92 _Bool in_array;
93 int index;
94 char name[DATA_MAX_NAME_LEN];
95 } state[YAJL_MAX_DEPTH];
96 };
97 typedef struct cj_s cj_t; /* }}} */
99 #if HAVE_YAJL_V2
100 typedef size_t yajl_len_t;
101 #else
102 typedef unsigned int yajl_len_t;
103 #endif
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)
110 {
111 cj_t *db;
112 size_t len;
113 yajl_status status;
115 len = size * nmemb;
117 if (len <= 0)
118 return (len);
120 db = user_data;
121 if (db == NULL)
122 return (0);
124 status = yajl_parse(db->yajl, (unsigned char *)buf, len);
125 if (status == yajl_status_ok)
126 return (len);
127 #if !HAVE_YAJL_V2
128 else if (status == yajl_status_insufficient_data)
129 return (len);
130 #endif
132 if (status != yajl_status_ok)
133 {
134 unsigned char *msg =
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 */
140 }
142 return (len);
143 } /* }}} size_t cj_curl_callback */
145 static int cj_get_type (cj_key_t *key)
146 {
147 const data_set_t *ds;
149 ds = plugin_get_ds (key->type);
150 if (ds == NULL)
151 {
152 static char type[DATA_MAX_NAME_LEN] = "!!!invalid!!!";
154 assert (key->type != NULL);
155 if (strcmp (type, key->type) != 0)
156 {
157 ERROR ("curl_json plugin: Unable to look up DS type \"%s\".",
158 key->type);
159 sstrncpy (type, key->type, sizeof (type));
160 }
162 return -1;
163 }
164 else if (ds->ds_num > 1)
165 {
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.",
172 key->type);
173 }
175 return ds->ds[0].type;
176 }
178 static int cj_cb_map_key (void *ctx, const unsigned char *val,
179 yajl_len_t len);
181 static void cj_cb_inc_array_index (void *ctx, _Bool update_key)
182 {
183 cj_t *db = (cj_t *)ctx;
185 if (!db->state[db->depth].in_array)
186 return;
188 db->state[db->depth].index++;
190 if (update_key)
191 {
192 char name[DATA_MAX_NAME_LEN];
194 ssnprintf (name, sizeof (name), "%d", db->state[db->depth].index - 1);
196 cj_cb_map_key (ctx, (unsigned char *)name, (yajl_len_t) strlen (name));
197 }
198 }
200 /* yajl callbacks */
201 #define CJ_CB_ABORT 0
202 #define CJ_CB_CONTINUE 1
204 static int cj_cb_boolean (void * ctx, int boolVal)
205 {
206 cj_cb_inc_array_index (ctx, /* update_key = */ 0);
207 return (CJ_CB_CONTINUE);
208 }
210 static int cj_cb_null (void * ctx)
211 {
212 cj_cb_inc_array_index (ctx, /* update_key = */ 0);
213 return (CJ_CB_CONTINUE);
214 }
216 static int cj_cb_number (void *ctx,
217 const char *number, yajl_len_t number_len)
218 {
219 char buffer[number_len + 1];
221 cj_t *db = (cj_t *)ctx;
222 cj_key_t *key = db->state[db->depth].key;
223 value_t vt;
224 int type;
225 int status;
227 /* Create a null-terminated version of the string. */
228 memcpy (buffer, number, number_len);
229 buffer[sizeof (buffer) - 1] = 0;
231 if ((key == NULL) || !CJ_IS_KEY (key)) {
232 if (key != NULL)
233 NOTICE ("curl_json plugin: Found \"%s\", but the configuration expects"
234 " a map.", buffer);
235 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
236 key = db->state[db->depth].key;
237 if (key == NULL) {
238 return (CJ_CB_CONTINUE);
239 }
240 }
241 else
242 {
243 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
244 }
246 type = cj_get_type (key);
247 status = parse_value (buffer, &vt, type);
248 if (status != 0)
249 {
250 NOTICE ("curl_json plugin: Unable to parse number: \"%s\"", buffer);
251 return (CJ_CB_CONTINUE);
252 }
254 cj_submit (db, key, &vt);
255 return (CJ_CB_CONTINUE);
256 } /* int cj_cb_number */
258 /* Queries the key-tree of the parent context for "in_name" and, if found,
259 * updates the "key" field of the current context. Otherwise, "key" is set to
260 * NULL. */
261 static int cj_cb_map_key (void *ctx,
262 unsigned char const *in_name, yajl_len_t in_name_len)
263 {
264 cj_t *db = (cj_t *)ctx;
265 c_avl_tree_t *tree;
267 tree = db->state[db->depth-1].tree;
269 if (tree != NULL)
270 {
271 cj_key_t *value = NULL;
272 char *name;
273 size_t name_len;
275 /* Create a null-terminated version of the name. */
276 name = db->state[db->depth].name;
277 name_len = COUCH_MIN ((size_t) in_name_len,
278 sizeof (db->state[db->depth].name) - 1);
279 memcpy (name, in_name, name_len);
280 name[name_len] = 0;
282 if (c_avl_get (tree, name, (void *) &value) == 0) {
283 if (CJ_IS_KEY((cj_key_t*)value)) {
284 db->state[db->depth].key = value;
285 }
286 else {
287 db->state[db->depth].tree = (c_avl_tree_t*) value;
288 }
289 }
290 else if (c_avl_get (tree, CJ_ANY, (void *) &value) == 0)
291 if (CJ_IS_KEY((cj_key_t*)value)) {
292 db->state[db->depth].key = value;
293 }
294 else {
295 db->state[db->depth].tree = (c_avl_tree_t*) value;
296 }
297 else
298 db->state[db->depth].key = NULL;
299 }
301 return (CJ_CB_CONTINUE);
302 }
304 static int cj_cb_string (void *ctx, const unsigned char *val,
305 yajl_len_t len)
306 {
307 /* Handle the string as if it was a number. */
308 return (cj_cb_number (ctx, (const char *) val, len));
309 } /* int cj_cb_string */
311 static int cj_cb_start (void *ctx)
312 {
313 cj_t *db = (cj_t *)ctx;
314 if (++db->depth >= YAJL_MAX_DEPTH)
315 {
316 ERROR ("curl_json plugin: %s depth exceeds max, aborting.",
317 db->url ? db->url : db->sock);
318 return (CJ_CB_ABORT);
319 }
320 return (CJ_CB_CONTINUE);
321 }
323 static int cj_cb_end (void *ctx)
324 {
325 cj_t *db = (cj_t *)ctx;
326 db->state[db->depth].tree = NULL;
327 --db->depth;
328 return (CJ_CB_CONTINUE);
329 }
331 static int cj_cb_start_map (void *ctx)
332 {
333 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
334 return cj_cb_start (ctx);
335 }
337 static int cj_cb_end_map (void *ctx)
338 {
339 return cj_cb_end (ctx);
340 }
342 static int cj_cb_start_array (void * ctx)
343 {
344 cj_t *db = (cj_t *)ctx;
345 cj_cb_inc_array_index (ctx, /* update_key = */ 1);
346 if (db->depth+1 < YAJL_MAX_DEPTH) {
347 db->state[db->depth+1].in_array = 1;
348 db->state[db->depth+1].index = 0;
349 }
350 return cj_cb_start (ctx);
351 }
353 static int cj_cb_end_array (void * ctx)
354 {
355 cj_t *db = (cj_t *)ctx;
356 db->state[db->depth].in_array = 0;
357 return cj_cb_end (ctx);
358 }
360 static yajl_callbacks ycallbacks = {
361 cj_cb_null, /* null */
362 cj_cb_boolean, /* boolean */
363 NULL, /* integer */
364 NULL, /* double */
365 cj_cb_number,
366 cj_cb_string,
367 cj_cb_start_map,
368 cj_cb_map_key,
369 cj_cb_end_map,
370 cj_cb_start_array,
371 cj_cb_end_array
372 };
374 /* end yajl callbacks */
376 static void cj_key_free (cj_key_t *key) /* {{{ */
377 {
378 if (key == NULL)
379 return;
381 sfree (key->path);
382 sfree (key->type);
383 sfree (key->instance);
385 sfree (key);
386 } /* }}} void cj_key_free */
388 static void cj_tree_free (c_avl_tree_t *tree) /* {{{ */
389 {
390 char *name;
391 void *value;
393 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
394 {
395 cj_key_t *key = (cj_key_t *)value;
397 if (CJ_IS_KEY(key))
398 cj_key_free (key);
399 else
400 cj_tree_free ((c_avl_tree_t *)value);
402 sfree (name);
403 }
405 c_avl_destroy (tree);
406 } /* }}} void cj_tree_free */
408 static void cj_free (void *arg) /* {{{ */
409 {
410 cj_t *db;
412 DEBUG ("curl_json plugin: cj_free (arg = %p);", arg);
414 db = (cj_t *) arg;
416 if (db == NULL)
417 return;
419 if (db->curl != NULL)
420 curl_easy_cleanup (db->curl);
421 db->curl = NULL;
423 if (db->tree != NULL)
424 cj_tree_free (db->tree);
425 db->tree = NULL;
427 sfree (db->instance);
428 sfree (db->host);
430 sfree (db->sock);
432 sfree (db->url);
433 sfree (db->user);
434 sfree (db->pass);
435 sfree (db->credentials);
436 sfree (db->cacert);
437 sfree (db->post_body);
438 curl_slist_free_all (db->headers);
440 sfree (db);
441 } /* }}} void cj_free */
443 /* Configuration handling functions {{{ */
445 static c_avl_tree_t *cj_avl_create(void)
446 {
447 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
448 }
450 static int cj_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
451 oconfig_item_t *ci)
452 {
453 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
454 {
455 WARNING ("curl_json plugin: `%s' needs exactly one string argument.", name);
456 return (-1);
457 }
459 *dest = curl_slist_append(*dest, ci->values[0].value.string);
460 if (*dest == NULL)
461 return (-1);
463 return (0);
464 } /* }}} int cj_config_append_string */
466 static int cj_config_add_key (cj_t *db, /* {{{ */
467 oconfig_item_t *ci)
468 {
469 cj_key_t *key;
470 int status;
471 int i;
473 if ((ci->values_num != 1)
474 || (ci->values[0].type != OCONFIG_TYPE_STRING))
475 {
476 WARNING ("curl_json plugin: The `Key' block "
477 "needs exactly one string argument.");
478 return (-1);
479 }
481 key = (cj_key_t *) malloc (sizeof (*key));
482 if (key == NULL)
483 {
484 ERROR ("curl_json plugin: malloc failed.");
485 return (-1);
486 }
487 memset (key, 0, sizeof (*key));
488 key->magic = CJ_KEY_MAGIC;
490 if (strcasecmp ("Key", ci->key) == 0)
491 {
492 status = cf_util_get_string (ci, &key->path);
493 if (status != 0)
494 {
495 sfree (key);
496 return (status);
497 }
498 }
499 else
500 {
501 ERROR ("curl_json plugin: cj_config: "
502 "Invalid key: %s", ci->key);
503 cj_key_free (key);
504 return (-1);
505 }
507 status = 0;
508 for (i = 0; i < ci->children_num; i++)
509 {
510 oconfig_item_t *child = ci->children + i;
512 if (strcasecmp ("Type", child->key) == 0)
513 status = cf_util_get_string (child, &key->type);
514 else if (strcasecmp ("Instance", child->key) == 0)
515 status = cf_util_get_string (child, &key->instance);
516 else
517 {
518 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
519 status = -1;
520 }
522 if (status != 0)
523 break;
524 } /* for (i = 0; i < ci->children_num; i++) */
526 if (status != 0)
527 {
528 cj_key_free (key);
529 return (-1);
530 }
532 if (key->type == NULL)
533 {
534 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
535 cj_key_free (key);
536 return (-1);
537 }
539 /* store path in a tree that will match the json map structure, example:
540 * "httpd/requests/count",
541 * "httpd/requests/current" ->
542 * { "httpd": { "requests": { "count": $key, "current": $key } } }
543 */
544 char *ptr;
545 char *name;
546 c_avl_tree_t *tree;
548 if (db->tree == NULL)
549 db->tree = cj_avl_create();
551 tree = db->tree;
552 ptr = key->path;
553 if (*ptr == '/')
554 ++ptr;
556 name = ptr;
557 while ((ptr = strchr (name, '/')) != NULL)
558 {
559 char ent[PATH_MAX];
560 c_avl_tree_t *value;
561 size_t len;
563 len = ptr - name;
564 if (len == 0)
565 break;
567 len = COUCH_MIN(len, sizeof (ent)-1);
568 sstrncpy (ent, name, len+1);
570 if (c_avl_get (tree, ent, (void *) &value) != 0)
571 {
572 value = cj_avl_create ();
573 c_avl_insert (tree, strdup (ent), value);
574 }
576 tree = value;
577 name = ptr + 1;
578 }
580 if (strlen (name) == 0)
581 {
582 ERROR ("curl_json plugin: invalid key: %s", key->path);
583 cj_key_free (key);
584 return (-1);
585 }
587 c_avl_insert (tree, strdup (name), key);
588 return (status);
589 } /* }}} int cj_config_add_key */
591 static int cj_init_curl (cj_t *db) /* {{{ */
592 {
593 db->curl = curl_easy_init ();
594 if (db->curl == NULL)
595 {
596 ERROR ("curl_json plugin: curl_easy_init failed.");
597 return (-1);
598 }
600 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
601 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
602 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
603 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
604 PACKAGE_NAME"/"PACKAGE_VERSION);
605 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
606 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
608 if (db->user != NULL)
609 {
610 size_t credentials_size;
612 credentials_size = strlen (db->user) + 2;
613 if (db->pass != NULL)
614 credentials_size += strlen (db->pass);
616 db->credentials = (char *) malloc (credentials_size);
617 if (db->credentials == NULL)
618 {
619 ERROR ("curl_json plugin: malloc failed.");
620 return (-1);
621 }
623 ssnprintf (db->credentials, credentials_size, "%s:%s",
624 db->user, (db->pass == NULL) ? "" : db->pass);
625 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
626 }
628 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
629 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
630 db->verify_host ? 2L : 0L);
631 if (db->cacert != NULL)
632 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
633 if (db->headers != NULL)
634 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
635 if (db->post_body != NULL)
636 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
638 return (0);
639 } /* }}} int cj_init_curl */
641 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
642 {
643 cj_t *db;
644 int status = 0;
645 int i;
647 if ((ci->values_num != 1)
648 || (ci->values[0].type != OCONFIG_TYPE_STRING))
649 {
650 WARNING ("curl_json plugin: The `URL' block "
651 "needs exactly one string argument.");
652 return (-1);
653 }
655 db = (cj_t *) malloc (sizeof (*db));
656 if (db == NULL)
657 {
658 ERROR ("curl_json plugin: malloc failed.");
659 return (-1);
660 }
661 memset (db, 0, sizeof (*db));
663 if (strcasecmp ("URL", ci->key) == 0)
664 status = cf_util_get_string (ci, &db->url);
665 else if (strcasecmp ("Sock", ci->key) == 0)
666 status = cf_util_get_string (ci, &db->sock);
667 else
668 {
669 ERROR ("curl_json plugin: cj_config: "
670 "Invalid key: %s", ci->key);
671 cj_free (db);
672 return (-1);
673 }
674 if (status != 0)
675 {
676 sfree (db);
677 return (status);
678 }
680 /* Fill the `cj_t' structure.. */
681 for (i = 0; i < ci->children_num; i++)
682 {
683 oconfig_item_t *child = ci->children + i;
685 if (strcasecmp ("Instance", child->key) == 0)
686 status = cf_util_get_string (child, &db->instance);
687 else if (strcasecmp ("Host", child->key) == 0)
688 status = cf_util_get_string (child, &db->host);
689 else if (db->url && strcasecmp ("User", child->key) == 0)
690 status = cf_util_get_string (child, &db->user);
691 else if (db->url && strcasecmp ("Password", child->key) == 0)
692 status = cf_util_get_string (child, &db->pass);
693 else if (db->url && strcasecmp ("VerifyPeer", child->key) == 0)
694 status = cf_util_get_boolean (child, &db->verify_peer);
695 else if (db->url && strcasecmp ("VerifyHost", child->key) == 0)
696 status = cf_util_get_boolean (child, &db->verify_host);
697 else if (db->url && strcasecmp ("CACert", child->key) == 0)
698 status = cf_util_get_string (child, &db->cacert);
699 else if (db->url && strcasecmp ("Header", child->key) == 0)
700 status = cj_config_append_string ("Header", &db->headers, child);
701 else if (db->url && strcasecmp ("Post", child->key) == 0)
702 status = cf_util_get_string (child, &db->post_body);
703 else if (strcasecmp ("Key", child->key) == 0)
704 status = cj_config_add_key (db, child);
705 else
706 {
707 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
708 status = -1;
709 }
711 if (status != 0)
712 break;
713 }
715 if (status == 0)
716 {
717 if (db->tree == NULL)
718 {
719 WARNING ("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".",
720 db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
721 status = -1;
722 }
723 if (status == 0 && db->url)
724 status = cj_init_curl (db);
725 }
727 /* If all went well, register this database for reading */
728 if (status == 0)
729 {
730 user_data_t ud;
731 char *cb_name;
733 if (db->instance == NULL)
734 db->instance = strdup("default");
736 DEBUG ("curl_json plugin: Registering new read callback: %s",
737 db->instance);
739 memset (&ud, 0, sizeof (ud));
740 ud.data = (void *) db;
741 ud.free_func = cj_free;
743 cb_name = ssnprintf_alloc ("curl_json-%s-%s",
744 db->instance, db->url ? db->url : db->sock);
746 plugin_register_complex_read (/* group = */ "curl_json", cb_name, cj_read,
747 /* interval = */ NULL, &ud);
748 sfree (cb_name);
749 }
750 else
751 {
752 cj_free (db);
753 return (-1);
754 }
756 return (0);
757 }
758 /* }}} int cj_config_add_database */
760 static int cj_config (oconfig_item_t *ci) /* {{{ */
761 {
762 int success;
763 int errors;
764 int status;
765 int i;
767 success = 0;
768 errors = 0;
770 for (i = 0; i < ci->children_num; i++)
771 {
772 oconfig_item_t *child = ci->children + i;
774 if (strcasecmp ("Sock", child->key) == 0
775 || strcasecmp ("URL", child->key) == 0)
776 {
777 status = cj_config_add_url (child);
778 if (status == 0)
779 success++;
780 else
781 errors++;
782 }
783 else
784 {
785 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
786 errors++;
787 }
788 }
790 if ((success == 0) && (errors > 0))
791 {
792 ERROR ("curl_json plugin: All statements failed.");
793 return (-1);
794 }
796 return (0);
797 } /* }}} int cj_config */
799 /* }}} End of configuration handling functions */
801 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
802 {
803 value_list_t vl = VALUE_LIST_INIT;
804 char *host;
806 vl.values = value;
807 vl.values_len = 1;
809 if ((db->host == NULL)
810 || (strcmp ("", db->host) == 0)
811 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
812 host = hostname_g;
813 else
814 host = db->host;
816 if (key->instance == NULL)
817 {
818 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
819 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
820 else
821 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
822 db->state[db->depth-1].name, db->state[db->depth].name);
823 }
824 else
825 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
827 sstrncpy (vl.host, host, sizeof (vl.host));
828 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
829 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
830 sstrncpy (vl.type, key->type, sizeof (vl.type));
832 plugin_dispatch_values (&vl);
833 } /* }}} int cj_submit */
835 static int cj_sock_perform (cj_t *db) /* {{{ */
836 {
837 char errbuf[1024];
838 struct sockaddr_un sa_unix = {};
839 sa_unix.sun_family = AF_UNIX;
840 sstrncpy (sa_unix.sun_path, db->sock, sizeof (sa_unix.sun_path));
842 int fd = socket (AF_UNIX, SOCK_STREAM, 0);
843 if (fd < 0)
844 return (-1);
845 if (connect (fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0)
846 {
847 ERROR ("curl_json plugin: connect(%s) failed: %s",
848 (db->sock != NULL) ? db->sock : "<null>",
849 sstrerror(errno, errbuf, sizeof (errbuf)));
850 close (fd);
851 return (-1);
852 }
854 ssize_t red;
855 do {
856 unsigned char buffer[4096];
857 red = read (fd, buffer, sizeof(buffer));
858 if (red < 0) {
859 ERROR ("curl_json plugin: read(%s) failed: %s",
860 (db->sock != NULL) ? db->sock : "<null>",
861 sstrerror(errno, errbuf, sizeof (errbuf)));
862 close (fd);
863 return (-1);
864 }
865 if (!cj_curl_callback (buffer, red, 1, db))
866 break;
867 } while (red > 0);
868 close (fd);
869 return (0);
870 } /* }}} int cj_sock_perform */
873 static int cj_curl_perform(cj_t *db) /* {{{ */
874 {
875 int status;
876 long rc;
877 char *url;
878 url = db->url;
880 status = curl_easy_perform (db->curl);
881 if (status != CURLE_OK)
882 {
883 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
884 status, db->curl_errbuf, url);
885 return (-1);
886 }
888 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
889 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
891 /* The response code is zero if a non-HTTP transport was used. */
892 if ((rc != 0) && (rc != 200))
893 {
894 ERROR ("curl_json plugin: curl_easy_perform failed with "
895 "response code %ld (%s)", rc, url);
896 return (-1);
897 }
898 return (0);
899 } /* }}} int cj_curl_perform */
901 static int cj_perform (cj_t *db) /* {{{ */
902 {
903 int status;
904 yajl_handle yprev = db->yajl;
906 db->yajl = yajl_alloc (&ycallbacks,
907 #if HAVE_YAJL_V2
908 /* alloc funcs = */ NULL,
909 #else
910 /* alloc funcs = */ NULL, NULL,
911 #endif
912 /* context = */ (void *)db);
913 if (db->yajl == NULL)
914 {
915 ERROR ("curl_json plugin: yajl_alloc failed.");
916 db->yajl = yprev;
917 return (-1);
918 }
920 if (db->url)
921 status = cj_curl_perform (db);
922 else
923 status = cj_sock_perform (db);
924 if (status < 0)
925 {
926 yajl_free (db->yajl);
927 db->yajl = yprev;
928 return (-1);
929 }
931 #if HAVE_YAJL_V2
932 status = yajl_complete_parse(db->yajl);
933 #else
934 status = yajl_parse_complete(db->yajl);
935 #endif
936 if (status != yajl_status_ok)
937 {
938 unsigned char *errmsg;
940 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
941 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
942 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
943 (char *) errmsg);
944 yajl_free_error (db->yajl, errmsg);
945 yajl_free (db->yajl);
946 db->yajl = yprev;
947 return (-1);
948 }
950 yajl_free (db->yajl);
951 db->yajl = yprev;
952 return (0);
953 } /* }}} int cj_perform */
955 static int cj_read (user_data_t *ud) /* {{{ */
956 {
957 cj_t *db;
959 if ((ud == NULL) || (ud->data == NULL))
960 {
961 ERROR ("curl_json plugin: cj_read: Invalid user data.");
962 return (-1);
963 }
965 db = (cj_t *) ud->data;
967 db->depth = 0;
968 memset (&db->state, 0, sizeof(db->state));
969 db->state[db->depth].tree = db->tree;
970 db->key = NULL;
972 return cj_perform (db);
973 } /* }}} int cj_read */
975 static int cj_init (void) /* {{{ */
976 {
977 /* Call this while collectd is still single-threaded to avoid
978 * initialization issues in libgcrypt. */
979 curl_global_init (CURL_GLOBAL_SSL);
980 return (0);
981 } /* }}} int cj_init */
983 void module_register (void)
984 {
985 plugin_register_complex_config ("curl_json", cj_config);
986 plugin_register_init ("curl_json", cj_init);
987 } /* void module_register */
989 /* vim: set sw=2 sts=2 et fdm=marker : */