Debian Codegraph @

src/curl_jolokia.c

raw
1 /**
2 * collectd - src/curl_jolokia.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 * Wilfried dothebart Goesgens <dothebart at citadel.org>
23 **/
24
25 #include "collectd.h"
26
27 #include "configfile.h"
28 #include "plugin.h"
29 #include "utils/common/common.h"
30 #include "utils_complain.h"
31 #include "utils_llist.h"
32
33 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <sys/un.h>
36
37 #include <curl/curl.h>
38
39 #include <yajl/yajl_parse.h>
40 #if HAVE_YAJL_YAJL_VERSION_H
41 #include <yajl/yajl_version.h>
42 #endif
43
44 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
45 #define HAVE_YAJL_V2 1
46 #endif
47
48 #define CJO_DEFAULT_HOST "localhost"
49
50 struct cjo_attribute_s /* {{{ */
51 {
52 char *attribute_name;
53 char *attribute_match;
54 size_t attribute_match_len;
55 char *type;
56 };
57 typedef struct cjo_attribute_s cjo_attribute_t;
58 /* }}} */
59
60 struct cjo_bean_s /* {{{ */
61 {
62 char *bean_name;
63 char *mbean_match;
64 char *mbean_namespace;
65 size_t mbean_match_len;
66 llist_t *attributes; /* list of cjo_attribute_t */
67 };
68 typedef struct cjo_bean_s cjo_bean_t;
69 /* }}} */
70
71 typedef enum { eNone = 0, eValue, eMBean } cjo_expect_token;
72
73 /* {{{ */
74 struct cjo_attribute_values_s {
75 const char *json_value;
76 size_t json_value_len;
77 const char *json_name;
78 size_t json_name_len;
79 };
80 typedef struct cjo_attribute_values_s cjo_attribute_values_t;
81 /* }}} */
82
83 struct cjo_membuffer_s {
84 char *buffer;
85 size_t size;
86 size_t used;
87 };
88
89 typedef struct cjo_membuffer_s cjo_membuffer_t;
90
91 struct cjo_s /* {{{ */
92 {
93 char *instance;
94 char *host;
95
96 char *sock;
97
98 char *url;
99 char *user;
100 char *pass;
101 char *credentials;
102
103 const cjo_bean_t *match_this_bean;
104 cjo_attribute_values_t *curr_attribute_value; /* points to the pool below. */
105 cjo_attribute_values_t *attributepool;
106 int attribute_pool_used;
107
108 const char *json_key;
109 size_t json_key_len;
110
111 cjo_expect_token expect;
112
113 _Bool verify_peer;
114 _Bool verify_host;
115 char *cacert;
116 struct curl_slist *headers;
117 char *post_body;
118 size_t post_body_len;
119 _Bool expect_escapes;
120 cdtime_t interval;
121 int timeout;
122
123 CURL *curl;
124 char curl_errbuf[CURL_ERROR_SIZE];
125 const unsigned char *start, *end;
126 cjo_membuffer_t replybuffer;
127 cjo_membuffer_t itembuffer;
128
129 size_t poolsize;
130 char *pool;
131 const char *poolmax;
132 char *pool_ptr;
133
134 yajl_handle yajl;
135 llist_t *bean_configs;
136 int max_attribute_count;
137 int max_value_len;
138 };
139 typedef struct cjo_s cjo_t; /* }}} */
140
141 #if HAVE_YAJL_V2
142 typedef size_t yajl_len_t;
143 #else
144 typedef unsigned int yajl_len_t;
145 #endif
146
147 static void cjo_init_buffer(cjo_membuffer_t *buf, size_t initial_size) {
148 buf->buffer = calloc(1, initial_size);
149 buf->used = 0;
150 buf->size = initial_size;
151 }
152
153 static void cjo_append_buffer(cjo_membuffer_t *buf, const char *append,
154 size_t appendlen) {
155 if (buf->used + appendlen + 1 > buf->size) {
156 size_t newsize = buf->size * 2;
157 char *newbuf = calloc(1, newsize);
158 memcpy(newbuf, buf->buffer, buf->used);
159 free(buf->buffer);
160 buf->buffer = newbuf;
161 buf->size = newsize;
162 }
163 memcpy(buf->buffer + buf->used, append, appendlen);
164 buf->used += appendlen;
165 }
166
167 static void cjo_release_buffercontent(cjo_membuffer_t *buf) {
168 sfree(buf->buffer);
169 buf->size = 0;
170 buf->used = 0;
171 }
172
173 static void cjo_remember_value(cjo_membuffer_t *should_be_in_here,
174 cjo_membuffer_t *buffer_here_if_not,
175 const char *ptr, size_t len,
176 const char **SetThisOne, size_t *SetThisLen) {
177 if (!((ptr > should_be_in_here->buffer) &&
178 (ptr < should_be_in_here->buffer + should_be_in_here->used))) {
179 char *tptr = buffer_here_if_not->buffer + buffer_here_if_not->used;
180 cjo_append_buffer(buffer_here_if_not, ptr, len);
181 ptr = tptr;
182 }
183
184 *SetThisOne = ptr;
185 *SetThisLen = len;
186 }
187
188 /*
189 * llist lookup methods
190 */
191 static int cjo_cfg_compare_attribute(llentry_t *match, void *vdb) {
192 cjo_t *db = (cjo_t *)vdb;
193 cjo_attribute_t *pcfg = (cjo_attribute_t *)match->value;
194
195 if (pcfg->attribute_match_len != db->curr_attribute_value->json_name_len)
196 return -1;
197 else
198 return strncmp(pcfg->attribute_match, db->curr_attribute_value->json_name,
199 db->curr_attribute_value->json_name_len);
200 }
201 static int cjo_cfg_compare_bean(llentry_t *match, void *vdb) {
202 cjo_t *db = (cjo_t *)vdb;
203 cjo_bean_t *pbeancfg = (cjo_bean_t *)match->value;
204
205 if (pbeancfg->mbean_match_len != db->json_key_len)
206 return -1;
207 return strncmp(pbeancfg->mbean_match, db->json_key, db->json_key_len);
208 }
209
210 /*
211 * Aligning data with Configs & Sending it
212 */
213
214 static int cjo_get_type(cjo_attribute_t *attribute) {
215 if (attribute->type != NULL) {
216 return -EINVAL;
217 }
218
219 const data_set_t *ds = plugin_get_ds(attribute->type);
220 if (ds == NULL) {
221 static char type[DATA_MAX_NAME_LEN] = "/--invalid--/";
222
223 if (strcmp(type, attribute->type) != 0) {
224 ERROR("curl_jolokia plugin: Unable to look up DS type \"%s\".",
225 attribute->type);
226 sstrncpy(type, attribute->type, sizeof(type));
227 }
228
229 return -1;
230 } else if (ds->ds_num > 1) {
231 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
232
233 c_complain_once(
234 LOG_WARNING, &complaint,
235 "curl_jolokia plugin: The type \"%s\" has more than one data source. "
236 "This is currently not supported. I will return the type of the "
237 "first data source, but this will likely lead to problems later on.",
238 attribute->type);
239 }
240
241 return ds->ds[0].type;
242 }
243
244 static void cjo_submit(cjo_t *db) /* {{{ */
245 {
246 if ((db->match_this_bean == NULL) || (db->attribute_pool_used == 0))
247 return; /* nothing to do yet. */
248
249 for (int i = 0; i < db->attribute_pool_used; i++) {
250 value_list_t vl = VALUE_LIST_INIT;
251 cjo_attribute_values_t *CAV = db->curr_attribute_value =
252 &db->attributepool[i];
253 llentry_t *cfgptr = llist_search_custom(db->match_this_bean->attributes,
254 cjo_cfg_compare_attribute, db);
255 if (cfgptr == NULL) {
256 char attribute[CAV->json_name_len + 1];
257
258 memcpy(attribute, CAV->json_name, CAV->json_name_len);
259 attribute[CAV->json_name_len] = '\0';
260 ERROR("curl_jolokia plugin: failed to locate attribute [%s:\"%s\"]",
261 db->match_this_bean->bean_name, attribute);
262
263 continue; /* we don't know this! */
264 }
265 cjo_attribute_t *curr_attribute = cfgptr->value;
266
267 /* Create a null-terminated version of the string. */
268 int ds_type = cjo_get_type(curr_attribute);
269 if (ds_type < 0) {
270 char attribute[CAV->json_name_len + 1];
271
272 memcpy(attribute, CAV->json_name, CAV->json_name_len);
273 attribute[CAV->json_name_len] = '\0';
274
275 ERROR("curl_jolokia plugin: failed to map type for [%s:%s:%s]",
276 db->match_this_bean->bean_name, attribute, curr_attribute->type);
277 continue;
278 }
279
280 value_t ret_value = {0};
281
282 char buffer[db->max_value_len + 1];
283 memcpy(buffer, CAV->json_value, CAV->json_value_len);
284
285 buffer[CAV->json_value_len] = '\0';
286
287 if (parse_value(buffer, &ret_value, ds_type) != 0) {
288 char attribute[CAV->json_name_len + 1];
289
290 memcpy(attribute, CAV->json_name, CAV->json_name_len);
291 attribute[CAV->json_name_len] = '\0';
292
293 WARNING("curl_jolokia plugin: Unable to parse number: [%s:%s:\"%s\"]",
294 db->match_this_bean->bean_name, attribute, buffer);
295 continue;
296 }
297
298 vl.values = &ret_value;
299 vl.values_len = 1;
300
301 char *host;
302 if ((db->host == NULL) || (strcmp("", db->host) == 0) ||
303 (strcmp(CJO_DEFAULT_HOST, db->host) == 0))
304 host = "";
305 else
306 host = db->host;
307
308 sstrncpy(vl.type_instance, curr_attribute->attribute_name,
309 sizeof(vl.type_instance));
310 sstrncpy(vl.plugin_instance, db->match_this_bean->bean_name,
311 sizeof(vl.type_instance));
312
313 sstrncpy(vl.host, host, sizeof(vl.host));
314 if (db->match_this_bean->mbean_namespace == NULL)
315 sstrncpy(vl.plugin, "jolokia", sizeof(vl.plugin));
316 else
317 sstrncpy(vl.plugin, db->match_this_bean->mbean_namespace,
318 sizeof(vl.plugin));
319 sstrncpy(vl.type, curr_attribute->type, sizeof(vl.type));
320
321 plugin_dispatch_values(&vl);
322 }
323
324 /* flush values */
325 db->match_this_bean = NULL;
326 db->attribute_pool_used = 0;
327
328 memset(db->attributepool, 0,
329 sizeof(*db->attributepool) * db->max_attribute_count);
330
331 } /* }}} int cjo_submit */
332
333 /* yajl callbacks */
334 #define CJO_CB_ABORT 0
335 #define CJO_CB_CONTINUE 1
336
337 static int cjo_cb_string(void *ctx, const unsigned char *val, yajl_len_t len) {
338 cjo_t *db = (cjo_t *)ctx;
339
340 switch (db->expect) {
341 case eValue:
342 cjo_remember_value(&db->replybuffer, &db->itembuffer, (const char *)val,
343 len, &db->curr_attribute_value->json_value,
344 &db->curr_attribute_value->json_value_len);
345
346 if (len > db->max_value_len)
347 db->max_value_len = len;
348
349 break;
350 case eMBean:
351 db->json_key = (const char *)val;
352 db->json_key_len = len;
353
354 llentry_t *cfgptr =
355 llist_search_custom(db->bean_configs, cjo_cfg_compare_bean, db);
356 if (cfgptr != NULL) {
357 db->match_this_bean = cfgptr->value;
358 cjo_submit(db);
359 }
360 db->expect = eNone;
361 break;
362 case eNone:
363 break; /* Don't care... */
364 }
365
366 /* Handle the string as if it was a number. */
367 return CJO_CB_CONTINUE;
368 } /* int cjo_cb_string */
369
370 static int cjo_cb_number(void *ctx, const char *number, yajl_len_t number_len) {
371 cjo_t *db = (cjo_t *)ctx;
372
373 switch (db->expect) {
374 case eValue:
375 cjo_remember_value(&db->replybuffer, &db->itembuffer, number, number_len,
376 &db->curr_attribute_value->json_value,
377 &db->curr_attribute_value->json_value_len);
378
379 if (number_len > db->max_value_len)
380 db->max_value_len = number_len;
381 break;
382 case eMBean:
383 case eNone:
384 db->expect = eNone;
385 break; /* Don't care... */
386 }
387
388 /* are we complete yet? */
389
390 return CJO_CB_CONTINUE;
391 } /* int cjo_cb_number */
392
393 static int cjo_cb_map_key(void *ctx, unsigned char const *in_name,
394 yajl_len_t in_name_len) {
395 cjo_t *db = (cjo_t *)ctx;
396
397 if ((in_name_len == 5) && !strncmp((const char *)in_name, "value", 5)) {
398 db->expect = eValue;
399 } else if ((in_name_len == 5) &&
400 !strncmp((const char *)in_name, "mbean", 5)) {
401 db->expect = eMBean;
402 } else if (db->expect == eValue) {
403 if (db->attribute_pool_used <= db->max_attribute_count) {
404 db->curr_attribute_value = &db->attributepool[db->attribute_pool_used];
405 db->attribute_pool_used++;
406 } else {
407 char buffer[in_name_len + 1];
408
409 memcpy(buffer, in_name, in_name_len);
410 buffer[in_name_len] = '\0';
411 ERROR("curl_jolokia plugin: attribute pool[%d/%d] [%s] exhausted! We may "
412 "loose values!",
413 db->attribute_pool_used, db->max_attribute_count, buffer);
414 }
415 cjo_remember_value(&db->replybuffer, &db->itembuffer, (const char *)in_name,
416 in_name_len, &db->curr_attribute_value->json_name,
417 &db->curr_attribute_value->json_name_len);
418 }
419 return CJO_CB_CONTINUE;
420 }
421
422 static int cjo_cb_end_map(void *ctx) {
423 cjo_t *db = (cjo_t *)ctx;
424
425 if (db->expect == eValue) {
426 cjo_submit(db);
427 db->expect = eNone;
428 }
429 return CJO_CB_CONTINUE;
430 }
431
432 static yajl_callbacks ycallbacks = {
433 NULL, /* null */
434 NULL, /* boolean */
435 NULL, /* integer */
436 NULL, /* double */
437 cjo_cb_number, cjo_cb_string, NULL, cjo_cb_map_key,
438 cjo_cb_end_map, NULL, NULL};
439
440 /*
441 *------------------------------------------------------------------------------
442 * cURL Handling
443 */
444 static size_t cjo_curl_callback(void *buf, /* {{{ */
445 size_t size, size_t nmemb, void *user_data) {
446 size_t len = size * nmemb;
447
448 if (len <= 0)
449 return len;
450
451 cjo_t *db = user_data;
452 if (db == NULL)
453 return 0;
454
455 cjo_append_buffer(&db->replybuffer, (char *)buf, len);
456
457 return len;
458 } /* }}} size_t cjo_curl_callback */
459
460 static int cjo_init_curl(cjo_t *db) /* {{{ */
461 {
462 db->curl = curl_easy_init();
463 if (db->curl == NULL) {
464 ERROR("curl_jolokia plugin: curl_easy_init failed.");
465 return -1;
466 }
467
468 curl_easy_setopt(db->curl, CURLOPT_NOSIGNAL, 1L);
469 curl_easy_setopt(db->curl, CURLOPT_WRITEFUNCTION, cjo_curl_callback);
470 curl_easy_setopt(db->curl, CURLOPT_WRITEDATA, db);
471 curl_easy_setopt(db->curl, CURLOPT_USERAGENT,
472 PACKAGE_NAME "/" PACKAGE_VERSION);
473 curl_easy_setopt(db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
474 curl_easy_setopt(db->curl, CURLOPT_URL, db->url);
475
476 if (db->user != NULL) {
477 size_t credentials_size;
478
479 credentials_size = strlen(db->user) + 2;
480 if (db->pass != NULL)
481 credentials_size += strlen(db->pass);
482
483 db->credentials = (char *)calloc(1, credentials_size);
484 if (db->credentials == NULL) {
485 ERROR("curl_jolokia plugin: calloc failed.");
486 return -1;
487 }
488
489 snprintf(db->credentials, credentials_size, "%s:%s", db->user,
490 (db->pass == NULL) ? "" : db->pass);
491 curl_easy_setopt(db->curl, CURLOPT_USERPWD, db->credentials);
492 }
493
494 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYPEER, (long)db->verify_peer);
495 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYHOST, db->verify_host ? 2L : 0L);
496 if (db->cacert != NULL)
497 curl_easy_setopt(db->curl, CURLOPT_CAINFO, db->cacert);
498 if (db->headers != NULL)
499 curl_easy_setopt(db->curl, CURLOPT_HTTPHEADER, db->headers);
500 if (db->post_body != NULL)
501 curl_easy_setopt(db->curl, CURLOPT_POSTFIELDS, db->post_body);
502
503 #ifdef HAVE_CURLOPT_TIMEOUT_MS
504 if (db->timeout >= 0)
505 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS, (long)db->timeout);
506 else if (db->interval > 0)
507 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
508 (long)CDTIME_T_TO_MS(db->interval));
509 else
510 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
511 (long)CDTIME_T_TO_MS(plugin_get_interval()));
512 #endif
513 return 0;
514 } /* }}} int cjo_init_curl */
515
516 static int cjo_sock_perform(cjo_t *db) /* {{{ */
517 {
518 char errbuf[1024];
519 struct sockaddr_un sa_unix = {
520 .sun_family = AF_UNIX,
521 };
522 sstrncpy(sa_unix.sun_path, db->sock, sizeof(sa_unix.sun_path));
523
524 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
525 if (fd < 0)
526 return -1;
527 if (connect(fd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)) < 0) {
528 ERROR("curl_jolokia plugin: connect(%s) failed: %s",
529 (db->sock != NULL) ? db->sock : "<null>",
530 sstrerror(errno, errbuf, sizeof(errbuf)));
531 close(fd);
532 return -1;
533 }
534
535 ssize_t red;
536 do {
537 unsigned char buffer[4096];
538 red = read(fd, buffer, sizeof(buffer));
539 if (red < 0) {
540 ERROR("curl_jolokia plugin: read(%s) failed: %s",
541 (db->sock != NULL) ? db->sock : "<null>",
542 sstrerror(errno, errbuf, sizeof(errbuf)));
543 close(fd);
544 return -1;
545 }
546 if (!cjo_curl_callback(buffer, red, 1, db))
547 break;
548 } while (red > 0);
549 close(fd);
550 return 0;
551 } /* }}} int cjo_sock_perform */
552
553 static int cjo_curl_perform(cjo_t *db) /* {{{ */
554 {
555
556 char *url = NULL;
557
558 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
559
560 size_t len = db->post_body_len;
561 if (len < 4096)
562 len = 4096;
563
564 cjo_init_buffer(&db->replybuffer, len * 4);
565 cjo_init_buffer(&db->itembuffer, len);
566
567 int status = curl_easy_perform(db->curl);
568 if (status != CURLE_OK) {
569 ERROR(
570 "curl_jolokia plugin: curl_easy_perform failed with status %i: %s (%s)",
571 status, db->curl_errbuf, (url != NULL) ? url : "<null>");
572 cjo_release_buffercontent(&db->replybuffer);
573 return -1;
574 }
575
576 long rc;
577 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
578
579 /* The response code is zero if a non-HTTP transport was used. */
580 if ((rc != 0) && (rc != 200)) {
581 ERROR("curl_jolokia plugin: curl_easy_perform failed with "
582 "response code %ld (%s)",
583 rc, url);
584 cjo_release_buffercontent(&db->replybuffer);
585 return -1;
586 }
587
588 yajl_status json_status = yajl_parse(
589 db->yajl, (unsigned char *)db->replybuffer.buffer, db->replybuffer.used);
590
591 if (json_status != yajl_status_ok) {
592 unsigned char *msg =
593 yajl_get_error(db->yajl, /* verbose = */ 1,
594 /* jsonText = */ (unsigned char *)db->replybuffer.buffer,
595 db->replybuffer.used);
596 ERROR("curl_jolokia plugin: yajl_parse failed: %s", msg);
597 yajl_free_error(db->yajl, msg);
598 }
599
600 cjo_release_buffercontent(&db->replybuffer);
601 cjo_release_buffercontent(&db->itembuffer);
602 return 0;
603 } /* }}} int cjo_curl_perform */
604
605 static int cjo_perform(cjo_t *db) /* {{{ */
606 {
607 yajl_handle yprev = db->yajl;
608
609 db->yajl = yajl_alloc(&ycallbacks,
610 #if HAVE_YAJL_V2
611 /* alloc funcs = */ NULL,
612 #else
613 /* alloc funcs = */ NULL, NULL,
614 #endif
615 /* context = */ (void *)db);
616 if (db->yajl == NULL) {
617 ERROR("curl_jolokia plugin: yajl_alloc failed.");
618 db->yajl = yprev;
619 return -1;
620 }
621
622 int status;
623 if (db->url)
624 status = cjo_curl_perform(db);
625 else
626 status = cjo_sock_perform(db);
627 if (status < 0) {
628 yajl_free(db->yajl);
629 db->yajl = yprev;
630 return -1;
631 }
632
633 #if HAVE_YAJL_V2
634 status = yajl_complete_parse(db->yajl);
635 #else
636 status = yajl_parse_complete(db->yajl);
637 #endif
638 if (status != yajl_status_ok) {
639 unsigned char *errmsg;
640
641 errmsg = yajl_get_error(db->yajl, /* verbose = */ 0,
642 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
643 ERROR("curl_jolokia plugin: yajl_parse_complete failed: %s",
644 (char *)errmsg);
645 yajl_free_error(db->yajl, errmsg);
646 yajl_free(db->yajl);
647 db->yajl = yprev;
648 return -1;
649 }
650
651 yajl_free(db->yajl);
652 db->yajl = yprev;
653 return 0;
654 } /* }}} int cjo_perform */
655
656 static int cjo_read(user_data_t *ud) /* {{{ */
657 {
658 if ((ud == NULL) || (ud->data == NULL)) {
659 ERROR("curl_jolokia plugin: cjo_read: Invalid user data.");
660 return -1;
661 }
662
663 return cjo_perform((cjo_t *)ud->data);
664 } /* }}} int cjo_read */
665 /* end cURL callbacks */
666
667 /*----------------------------------------------------------------------------*/
668 /* Configuration handling functions {{{ */
669
670 static int cjo_config_append_string(const char *name,
671 struct curl_slist **dest, /* {{{ */
672 oconfig_item_t *ci) {
673 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
674 WARNING("curl_jolokia plugin: `%s' needs exactly one string argument.",
675 name);
676 return -1;
677 }
678
679 *dest = curl_slist_append(*dest, ci->values[0].value.string);
680 if (*dest == NULL)
681 return -1;
682
683 return 0;
684 } /* }}} int cjo_config_append_string */
685
686 void destroy_attribute_config(cjo_attribute_t *attribute) {
687 sfree(attribute->attribute_name);
688 sfree(attribute->attribute_match);
689 sfree(attribute->type);
690 sfree(attribute);
691 }
692
693 int cjo_get_attribute(cjo_bean_t *bean, oconfig_item_t *ci) {
694 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
695 ERROR("curl_jolokia plugin: The `Attribute' block "
696 "needs exactly one string argument.");
697
698 return -1;
699 }
700 if (strcasecmp("AttributeName", ci->key)) {
701 ERROR("curl_jolokia plugin: cjo_config_add_attribute: "
702 "Invalid key: %s",
703 ci->key);
704 return -1;
705 }
706
707 cjo_attribute_t *attribute =
708 (cjo_attribute_t *)calloc(1, sizeof(cjo_attribute_t));
709 if (attribute == NULL) {
710 ERROR("curl_jolokia plugin: calloc of bean attribute failed.");
711
712 return -1;
713 }
714 memset(attribute, 0, sizeof(*attribute));
715
716 int status = cf_util_get_string(ci, &attribute->attribute_name);
717 if (status != 0) {
718 ERROR("curl_jolokia plugin: failed to get attribute name.");
719
720 destroy_attribute_config(attribute);
721 return status;
722 }
723
724 status = 0;
725 for (int i = 0; i < ci->children_num; i++) {
726 oconfig_item_t *child = ci->children + i;
727
728 if (strcasecmp("Attribute", child->key) == 0) {
729 status = cf_util_get_string(child, &attribute->attribute_match);
730 } else if (strcasecmp("Type", child->key) == 0) {
731 status = cf_util_get_string(child, &attribute->type);
732 } else {
733 ERROR("curl_jolokia plugin: Option `%s' not allowed in Bean Attribute.",
734 child->key);
735 status = -1;
736 }
737
738 if (status != 0)
739 break;
740 } /* for (i = 0; i < ci->children_num; i++) */
741
742 if (status == 0) {
743 if ((attribute->attribute_name == NULL) ||
744 (attribute->attribute_match == NULL) || (attribute->type == NULL)) {
745 ERROR("curl_jolokia plugin: some attribute property is missing..");
746 status = -1;
747 }
748 }
749 if (status >= 0) {
750 attribute->attribute_match_len = strlen(attribute->attribute_match);
751 llentry_t *listentry = llentry_create(NULL, attribute);
752 llist_append(bean->attributes, listentry);
753 } else {
754 destroy_attribute_config(attribute);
755 }
756 return status;
757 }
758
759 void destroy_bean_config(cjo_bean_t *bean) {
760 if (bean == NULL)
761 return;
762
763 for (llentry_t *e = llist_head(bean->attributes); e != NULL; e = e->next) {
764 cjo_attribute_t *attribute = (cjo_attribute_t *)e->value;
765 e->value = NULL;
766
767 destroy_attribute_config(attribute);
768 }
769
770 llist_destroy(bean->attributes);
771 sfree(bean->bean_name);
772 sfree(bean->mbean_match);
773 sfree(bean->mbean_namespace);
774 sfree(bean);
775 }
776
777 static int cjo_config_add_bean(cjo_t *db, /* {{{ */
778 oconfig_item_t *ci) {
779 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
780 ERROR("curl_jolokia plugin: The `BeanName' block "
781 "needs exactly one string argument.");
782
783 return -1;
784 }
785 if (strcasecmp("BeanName", ci->key)) {
786 ERROR("curl_jolokia plugin: cjo_config_add_bean: "
787 "Invalid key: %s",
788 ci->key);
789 return -1;
790 }
791
792 cjo_bean_t *bean = (cjo_bean_t *)calloc(1, sizeof(cjo_bean_t));
793 if (bean == NULL) {
794 ERROR("curl_jolokia plugin: calloc of bean property failed.");
795
796 return -1;
797 }
798 memset(bean, 0, sizeof(*bean));
799 bean->attributes = llist_create();
800 if (bean->attributes == NULL) {
801 ERROR("curl_jolokia plugin: calloc of bean property failed.");
802
803 destroy_bean_config(bean);
804 return -1;
805 }
806
807 int status = cf_util_get_string(ci, &bean->bean_name);
808 if (status != 0) {
809 ERROR("curl_jolokia plugin: fetching of bean name failed.");
810
811 destroy_bean_config(bean);
812 return status;
813 }
814
815 status = 0;
816 for (int i = 0; i < ci->children_num; i++) {
817 oconfig_item_t *child = ci->children + i;
818
819 if (strcasecmp("MBean", child->key) == 0) {
820 status = cf_util_get_string(child, &bean->mbean_match);
821 } else if (strcasecmp("BeanNameSpace", child->key) == 0) {
822 status = cf_util_get_string(child, &bean->mbean_namespace);
823 } else if (strcasecmp("AttributeName", child->key) == 0) {
824 status = cjo_get_attribute(bean, child);
825 } else {
826 ERROR("curl_jolokia plugin: Option `%s' not allowed in Bean.",
827 child->key);
828 status = -1;
829 }
830
831 if (status != 0)
832 break;
833 } /* for (i = 0; i < ci->children_num; i++) */
834
835 if (status == 0) {
836 if ((bean->bean_name == NULL) || (bean->mbean_match == NULL) ||
837 (bean->attributes == NULL) || (llist_size(bean->attributes) == 0)) {
838 ERROR("curl_jolokia plugin: some bean property is invalid..");
839 status = -1;
840 }
841 }
842 if (status >= 0) {
843 int count;
844
845 count = llist_size(bean->attributes);
846 if (count > db->max_attribute_count)
847 db->max_attribute_count = count;
848
849 bean->mbean_match_len = strlen(bean->mbean_match);
850 if (db->bean_configs == NULL)
851 db->bean_configs = llist_create();
852
853 llist_append(db->bean_configs, llentry_create(NULL, bean));
854 } else {
855 destroy_bean_config(bean);
856 }
857
858 return status;
859 } /* }}} int cjo_config_add_key */
860
861 static void cjo_destroy_bean_configs(llist_t *bean_configs) {
862 llentry_t *e;
863
864 if (bean_configs == NULL)
865 return;
866
867 for (e = llist_head(bean_configs); e != NULL; e = e->next) {
868 cjo_bean_t *bean = (cjo_bean_t *)e->value;
869 e->value = NULL;
870
871 destroy_bean_config(bean);
872 }
873
874 llist_destroy(bean_configs);
875 }
876 static void cjo_free(void *arg) /* {{{ */
877 {
878 cjo_t *db;
879
880 DEBUG("curl_jolokia plugin: cjo_free (arg = %p);", arg);
881
882 db = (cjo_t *)arg;
883
884 if (db == NULL)
885 return;
886
887 if (db->curl != NULL)
888 curl_easy_cleanup(db->curl);
889 db->curl = NULL;
890
891 cjo_destroy_bean_configs(db->bean_configs);
892 db->bean_configs = NULL;
893 sfree(db->instance);
894 sfree(db->host);
895
896 sfree(db->sock);
897
898 sfree(db->url);
899 sfree(db->user);
900 sfree(db->pass);
901 sfree(db->credentials);
902 sfree(db->cacert);
903 sfree(db->post_body);
904 sfree(db->attributepool);
905
906 curl_slist_free_all(db->headers);
907 /// todo: freeme cjo_attribute_values_t *attributepool;
908
909 sfree(db);
910 } /* }}} void cjo_free */
911
912 static int cjo_config_add_url(oconfig_item_t *ci) /* {{{ */
913 {
914
915 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
916 ERROR("curl_jolokia plugin: The `URL' block "
917 "needs exactly one string argument.");
918 return -1;
919 }
920
921 cjo_t *db = (cjo_t *)calloc(1, sizeof(cjo_t));
922 if (db == NULL) {
923 ERROR("curl_jolokia plugin: calloc failed.");
924 return -1;
925 }
926 memset(db, 0, sizeof(*db));
927
928 int status = 0;
929 if (strcasecmp("URL", ci->key) == 0)
930 status = cf_util_get_string(ci, &db->url);
931 else if (strcasecmp("Sock", ci->key) == 0)
932 status = cf_util_get_string(ci, &db->sock);
933 else {
934 ERROR("curl_jolokia plugin: cjo_config: "
935 "Invalid key: %s",
936 ci->key);
937 return -1;
938 }
939 if (status != 0) {
940 sfree(db);
941 return status;
942 }
943
944 /* Fill the `cjo_t' structure.. */
945 for (int i = 0; i < ci->children_num; i++) {
946 oconfig_item_t *child = ci->children + i;
947
948 if (strcasecmp("Instance", child->key) == 0)
949 status = cf_util_get_string(child, &db->instance);
950 else if (strcasecmp("Host", child->key) == 0)
951 status = cf_util_get_string(child, &db->host);
952 else if (strcasecmp("MaxReadAttributes", child->key) == 0)
953 status = cf_util_get_int(child, &db->max_attribute_count);
954 else if (db->url && strcasecmp("User", child->key) == 0)
955 status = cf_util_get_string(child, &db->user);
956 else if (db->url && strcasecmp("Password", child->key) == 0)
957 status = cf_util_get_string(child, &db->pass);
958 else if (db->url && strcasecmp("VerifyPeer", child->key) == 0)
959 status = cf_util_get_boolean(child, &db->verify_peer);
960 else if (db->url && strcasecmp("VerifyHost", child->key) == 0)
961 status = cf_util_get_boolean(child, &db->verify_host);
962 else if (db->url && strcasecmp("CACert", child->key) == 0)
963 status = cf_util_get_string(child, &db->cacert);
964 else if (db->url && strcasecmp("Header", child->key) == 0)
965 status = cjo_config_append_string("Header", &db->headers, child);
966 else if (db->url && strcasecmp("Post", child->key) == 0) {
967 status = cf_util_get_string(child, &db->post_body);
968 db->post_body_len = strlen(db->post_body);
969 db->expect_escapes = (strchr(db->post_body, '\\') != NULL) ||
970 (strchr(db->post_body, '/') != NULL);
971 } else if (strcasecmp("BeanName", child->key) == 0)
972 status = cjo_config_add_bean(db, child);
973 else {
974 ERROR("curl_jolokia plugin: Option `%s' not allowed here.", child->key);
975
976 status = -1;
977 }
978
979 if (status != 0)
980 break;
981 }
982
983 if (status == 0) {
984 if (status == 0 && db->url)
985 status = cjo_init_curl(db);
986 }
987
988 /* If all went well, register this database for reading */
989 if (status == 0) {
990 char cb_name[DATA_MAX_NAME_LEN];
991
992 if (db->instance == NULL)
993 db->instance = strdup("default");
994
995 DEBUG("curl_jolokia plugin: Registering new read callback: %s",
996 db->instance);
997
998 user_data_t ud = {
999 .data = (void *)db,
1000 .free_func = cjo_free,
1001 };
1002
1003 snprintf(cb_name, sizeof(cb_name), "curl_jolokia-%s-%s", db->instance,
1004 db->url ? db->url : db->sock);
1005
1006 db->attributepool =
1007 calloc(db->max_attribute_count + 1, sizeof(*db->attributepool));
1008
1009 plugin_register_complex_read(/* group = */ NULL, cb_name, cjo_read,
1010 /* interval = */ db->interval, &ud);
1011 } else {
1012 ERROR("curl_jolokia plugin: Failed to load URL");
1013
1014 cjo_free(db);
1015 return -1;
1016 }
1017
1018 return 0;
1019 }
1020 /* }}} int cjo_config_add_database */
1021
1022 static int cjo_config(oconfig_item_t *ci) /* {{{ */
1023 {
1024 int success = 0;
1025 int errors = 0;
1026
1027 for (int i = 0; i < ci->children_num; i++) {
1028 oconfig_item_t *child = ci->children + i;
1029
1030 if (strcasecmp("Sock", child->key) == 0 ||
1031 strcasecmp("URL", child->key) == 0) {
1032 int status = cjo_config_add_url(child);
1033 if (status == 0)
1034 success++;
1035 else
1036 errors++;
1037 } else {
1038 WARNING("curl_jolokia plugin: Option `%s' not allowed here.", child->key);
1039 errors++;
1040 }
1041 }
1042
1043 if ((success == 0) && (errors > 0)) {
1044 ERROR("curl_jolokia plugin: All statements failed.");
1045 return -1;
1046 }
1047
1048 return 0;
1049 } /* }}} int cjo_config */
1050
1051 /* }}} End of configuration handling functions */
1052
1053 void module_register(void) {
1054 plugin_register_complex_config("curl_jolokia", cjo_config);
1055 } /* void module_register */
1056
1057 /* vim: set sw=2 sts=2 et fdm=marker : */