6fdae51d2bf74b29ed4028ee2e77e5c66ca33954
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 while (status == 0)
527 {
528 if (key->type == NULL)
529 {
530 WARNING ("curl_json plugin: `Type' missing in `Key' block.");
531 status = -1;
532 }
534 break;
535 } /* while (status == 0) */
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 if (status == 0)
543 {
544 char *ptr;
545 char *name;
546 char ent[PATH_MAX];
547 c_avl_tree_t *tree;
549 if (db->tree == NULL)
550 db->tree = cj_avl_create();
552 tree = db->tree;
553 ptr = key->path;
554 if (*ptr == '/')
555 ++ptr;
557 name = ptr;
558 while (*ptr)
559 {
560 if (*ptr == '/')
561 {
562 c_avl_tree_t *value;
563 int len;
565 len = ptr-name;
566 if (len == 0)
567 break;
568 len = COUCH_MIN(len, sizeof (ent)-1);
569 sstrncpy (ent, name, len+1);
571 if (c_avl_get (tree, ent, (void *) &value) != 0)
572 {
573 value = cj_avl_create ();
574 c_avl_insert (tree, strdup (ent), value);
575 }
577 tree = value;
578 name = ptr+1;
579 }
580 ++ptr;
581 }
582 if (*name)
583 c_avl_insert (tree, strdup(name), key);
584 else
585 {
586 ERROR ("curl_json plugin: invalid key: %s", key->path);
587 status = -1;
588 }
589 }
591 return (status);
592 } /* }}} int cj_config_add_key */
594 static int cj_init_curl (cj_t *db) /* {{{ */
595 {
596 db->curl = curl_easy_init ();
597 if (db->curl == NULL)
598 {
599 ERROR ("curl_json plugin: curl_easy_init failed.");
600 return (-1);
601 }
603 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
604 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cj_curl_callback);
605 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
606 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
607 PACKAGE_NAME"/"PACKAGE_VERSION);
608 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
609 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
611 if (db->user != NULL)
612 {
613 size_t credentials_size;
615 credentials_size = strlen (db->user) + 2;
616 if (db->pass != NULL)
617 credentials_size += strlen (db->pass);
619 db->credentials = (char *) malloc (credentials_size);
620 if (db->credentials == NULL)
621 {
622 ERROR ("curl_json plugin: malloc failed.");
623 return (-1);
624 }
626 ssnprintf (db->credentials, credentials_size, "%s:%s",
627 db->user, (db->pass == NULL) ? "" : db->pass);
628 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
629 }
631 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, (long) db->verify_peer);
632 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
633 db->verify_host ? 2L : 0L);
634 if (db->cacert != NULL)
635 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
636 if (db->headers != NULL)
637 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
638 if (db->post_body != NULL)
639 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
641 return (0);
642 } /* }}} int cj_init_curl */
644 static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
645 {
646 cj_t *db;
647 int status = 0;
648 int i;
650 if ((ci->values_num != 1)
651 || (ci->values[0].type != OCONFIG_TYPE_STRING))
652 {
653 WARNING ("curl_json plugin: The `URL' block "
654 "needs exactly one string argument.");
655 return (-1);
656 }
658 db = (cj_t *) malloc (sizeof (*db));
659 if (db == NULL)
660 {
661 ERROR ("curl_json plugin: malloc failed.");
662 return (-1);
663 }
664 memset (db, 0, sizeof (*db));
666 if (strcasecmp ("URL", ci->key) == 0)
667 status = cf_util_get_string (ci, &db->url);
668 else if (strcasecmp ("Sock", ci->key) == 0)
669 status = cf_util_get_string (ci, &db->sock);
670 else
671 {
672 ERROR ("curl_json plugin: cj_config: "
673 "Invalid key: %s", ci->key);
674 cj_free (db);
675 return (-1);
676 }
677 if (status != 0)
678 {
679 sfree (db);
680 return (status);
681 }
683 /* Fill the `cj_t' structure.. */
684 for (i = 0; i < ci->children_num; i++)
685 {
686 oconfig_item_t *child = ci->children + i;
688 if (strcasecmp ("Instance", child->key) == 0)
689 status = cf_util_get_string (child, &db->instance);
690 else if (strcasecmp ("Host", child->key) == 0)
691 status = cf_util_get_string (child, &db->host);
692 else if (db->url && strcasecmp ("User", child->key) == 0)
693 status = cf_util_get_string (child, &db->user);
694 else if (db->url && strcasecmp ("Password", child->key) == 0)
695 status = cf_util_get_string (child, &db->pass);
696 else if (db->url && strcasecmp ("VerifyPeer", child->key) == 0)
697 status = cf_util_get_boolean (child, &db->verify_peer);
698 else if (db->url && strcasecmp ("VerifyHost", child->key) == 0)
699 status = cf_util_get_boolean (child, &db->verify_host);
700 else if (db->url && strcasecmp ("CACert", child->key) == 0)
701 status = cf_util_get_string (child, &db->cacert);
702 else if (db->url && strcasecmp ("Header", child->key) == 0)
703 status = cj_config_append_string ("Header", &db->headers, child);
704 else if (db->url && strcasecmp ("Post", child->key) == 0)
705 status = cf_util_get_string (child, &db->post_body);
706 else if (strcasecmp ("Key", child->key) == 0)
707 status = cj_config_add_key (db, child);
708 else
709 {
710 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
711 status = -1;
712 }
714 if (status != 0)
715 break;
716 }
718 if (status == 0)
719 {
720 if (db->tree == NULL)
721 {
722 WARNING ("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".",
723 db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
724 status = -1;
725 }
726 if (status == 0 && db->url)
727 status = cj_init_curl (db);
728 }
730 /* If all went well, register this database for reading */
731 if (status == 0)
732 {
733 user_data_t ud;
734 char *cb_name;
736 if (db->instance == NULL)
737 db->instance = strdup("default");
739 DEBUG ("curl_json plugin: Registering new read callback: %s",
740 db->instance);
742 memset (&ud, 0, sizeof (ud));
743 ud.data = (void *) db;
744 ud.free_func = cj_free;
746 cb_name = ssnprintf_alloc ("curl_json-%s-%s",
747 db->instance, db->url ? db->url : db->sock);
749 plugin_register_complex_read (/* group = */ "curl_json", cb_name, cj_read,
750 /* interval = */ NULL, &ud);
751 sfree (cb_name);
752 }
753 else
754 {
755 cj_free (db);
756 return (-1);
757 }
759 return (0);
760 }
761 /* }}} int cj_config_add_database */
763 static int cj_config (oconfig_item_t *ci) /* {{{ */
764 {
765 int success;
766 int errors;
767 int status;
768 int i;
770 success = 0;
771 errors = 0;
773 for (i = 0; i < ci->children_num; i++)
774 {
775 oconfig_item_t *child = ci->children + i;
777 if (strcasecmp ("Sock", child->key) == 0
778 || strcasecmp ("URL", child->key) == 0)
779 {
780 status = cj_config_add_url (child);
781 if (status == 0)
782 success++;
783 else
784 errors++;
785 }
786 else
787 {
788 WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
789 errors++;
790 }
791 }
793 if ((success == 0) && (errors > 0))
794 {
795 ERROR ("curl_json plugin: All statements failed.");
796 return (-1);
797 }
799 return (0);
800 } /* }}} int cj_config */
802 /* }}} End of configuration handling functions */
804 static void cj_submit (cj_t *db, cj_key_t *key, value_t *value) /* {{{ */
805 {
806 value_list_t vl = VALUE_LIST_INIT;
807 char *host;
809 vl.values = value;
810 vl.values_len = 1;
812 if ((db->host == NULL)
813 || (strcmp ("", db->host) == 0)
814 || (strcmp (CJ_DEFAULT_HOST, db->host) == 0))
815 host = hostname_g;
816 else
817 host = db->host;
819 if (key->instance == NULL)
820 {
821 if ((db->depth == 0) || (strcmp ("", db->state[db->depth-1].name) == 0))
822 sstrncpy (vl.type_instance, db->state[db->depth].name, sizeof (vl.type_instance));
823 else
824 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
825 db->state[db->depth-1].name, db->state[db->depth].name);
826 }
827 else
828 sstrncpy (vl.type_instance, key->instance, sizeof (vl.type_instance));
830 sstrncpy (vl.host, host, sizeof (vl.host));
831 sstrncpy (vl.plugin, "curl_json", sizeof (vl.plugin));
832 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
833 sstrncpy (vl.type, key->type, sizeof (vl.type));
835 plugin_dispatch_values (&vl);
836 } /* }}} int cj_submit */
838 static int cj_sock_perform (cj_t *db) /* {{{ */
839 {
840 char errbuf[1024];
841 struct sockaddr_un sa_unix = {};
842 sa_unix.sun_family = AF_UNIX;
843 sstrncpy (sa_unix.sun_path, db->sock, sizeof (sa_unix.sun_path));
845 int fd = socket (AF_UNIX, SOCK_STREAM, 0);
846 if (fd < 0)
847 return (-1);
848 if (connect (fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0)
849 {
850 ERROR ("curl_json plugin: connect(%s) failed: %s",
851 (db->sock != NULL) ? db->sock : "<null>",
852 sstrerror(errno, errbuf, sizeof (errbuf)));
853 close (fd);
854 return (-1);
855 }
857 ssize_t red;
858 do {
859 unsigned char buffer[4096];
860 red = read (fd, buffer, sizeof(buffer));
861 if (red < 0) {
862 ERROR ("curl_json plugin: read(%s) failed: %s",
863 (db->sock != NULL) ? db->sock : "<null>",
864 sstrerror(errno, errbuf, sizeof (errbuf)));
865 close (fd);
866 return (-1);
867 }
868 if (!cj_curl_callback (buffer, red, 1, db))
869 break;
870 } while (red > 0);
871 close (fd);
872 return (0);
873 } /* }}} int cj_sock_perform */
876 static int cj_curl_perform(cj_t *db) /* {{{ */
877 {
878 int status;
879 long rc;
880 char *url;
881 url = db->url;
883 status = curl_easy_perform (db->curl);
884 if (status != CURLE_OK)
885 {
886 ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
887 status, db->curl_errbuf, url);
888 return (-1);
889 }
891 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
892 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
894 /* The response code is zero if a non-HTTP transport was used. */
895 if ((rc != 0) && (rc != 200))
896 {
897 ERROR ("curl_json plugin: curl_easy_perform failed with "
898 "response code %ld (%s)", rc, url);
899 return (-1);
900 }
901 return (0);
902 } /* }}} int cj_curl_perform */
904 static int cj_perform (cj_t *db) /* {{{ */
905 {
906 int status;
907 yajl_handle yprev = db->yajl;
909 db->yajl = yajl_alloc (&ycallbacks,
910 #if HAVE_YAJL_V2
911 /* alloc funcs = */ NULL,
912 #else
913 /* alloc funcs = */ NULL, NULL,
914 #endif
915 /* context = */ (void *)db);
916 if (db->yajl == NULL)
917 {
918 ERROR ("curl_json plugin: yajl_alloc failed.");
919 db->yajl = yprev;
920 return (-1);
921 }
923 if (db->url)
924 status = cj_curl_perform (db);
925 else
926 status = cj_sock_perform (db);
927 if (status < 0)
928 {
929 yajl_free (db->yajl);
930 db->yajl = yprev;
931 return (-1);
932 }
934 #if HAVE_YAJL_V2
935 status = yajl_complete_parse(db->yajl);
936 #else
937 status = yajl_parse_complete(db->yajl);
938 #endif
939 if (status != yajl_status_ok)
940 {
941 unsigned char *errmsg;
943 errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
944 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
945 ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
946 (char *) errmsg);
947 yajl_free_error (db->yajl, errmsg);
948 yajl_free (db->yajl);
949 db->yajl = yprev;
950 return (-1);
951 }
953 yajl_free (db->yajl);
954 db->yajl = yprev;
955 return (0);
956 } /* }}} int cj_perform */
958 static int cj_read (user_data_t *ud) /* {{{ */
959 {
960 cj_t *db;
962 if ((ud == NULL) || (ud->data == NULL))
963 {
964 ERROR ("curl_json plugin: cj_read: Invalid user data.");
965 return (-1);
966 }
968 db = (cj_t *) ud->data;
970 db->depth = 0;
971 memset (&db->state, 0, sizeof(db->state));
972 db->state[db->depth].tree = db->tree;
973 db->key = NULL;
975 return cj_perform (db);
976 } /* }}} int cj_read */
978 static int cj_init (void) /* {{{ */
979 {
980 /* Call this while collectd is still single-threaded to avoid
981 * initialization issues in libgcrypt. */
982 curl_global_init (CURL_GLOBAL_SSL);
983 return (0);
984 } /* }}} int cj_init */
986 void module_register (void)
987 {
988 plugin_register_complex_config ("curl_json", cj_config);
989 plugin_register_init ("curl_json", cj_init);
990 } /* void module_register */
992 /* vim: set sw=2 sts=2 et fdm=marker : */