2 * collectd - src/curl_xml.c
3 * Copyright (C) 2009,2010 Amit Gupta
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Amit Gupta <amit.gupta221 at gmail.com>
25 #include "utils/common/common.h"
26 #include "utils/curl_stats/curl_stats.h"
27 #include "utils_llist.h"
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31 #include <libxml/xpath.h>
32 #include <libxml/xpathInternals.h>
34 #include <curl/curl.h>
36 #define CX_DEFAULT_HOST "localhost"
39 * Private data structures
41 struct cx_values_s /* {{{ */
43 char path[DATA_MAX_NAME_LEN];
46 typedef struct cx_values_s cx_values_t;
49 struct cx_xpath_s /* {{{ */
55 char *instance_prefix;
57 char *plugin_instance_from;
61 typedef struct cx_xpath_s cx_xpath_t;
64 struct cx_namespace_s /* {{{ */
69 typedef struct cx_namespace_s cx_namespace_t;
89 struct curl_slist *headers;
92 cx_namespace_t *namespaces;
93 size_t namespaces_num;
96 char curl_errbuf[CURL_ERROR_SIZE];
101 llist_t *xpath_list; /* list of xpath blocks */
103 typedef struct cx_s cx_t; /* }}} */
108 static size_t cx_curl_callback(void *buf, /* {{{ */
109 size_t size, size_t nmemb, void *user_data) {
110 size_t len = size * nmemb;
112 cx_t *db = user_data;
114 ERROR("curl_xml plugin: cx_curl_callback: "
115 "user_data pointer is NULL.");
122 if ((db->buffer_fill + len) >= db->buffer_size) {
123 char *temp = realloc(db->buffer, db->buffer_fill + len + 1);
125 ERROR("curl_xml plugin: realloc failed.");
129 db->buffer_size = db->buffer_fill + len + 1;
132 memcpy(db->buffer + db->buffer_fill, (char *)buf, len);
133 db->buffer_fill += len;
134 db->buffer[db->buffer_fill] = 0;
137 } /* }}} size_t cx_curl_callback */
139 static void cx_xpath_free(cx_xpath_t *xpath) /* {{{ */
146 sfree(xpath->instance_prefix);
147 sfree(xpath->plugin_instance_from);
148 sfree(xpath->instance);
149 sfree(xpath->values);
151 } /* }}} void cx_xpath_free */
153 static void cx_xpath_list_free(llist_t *list) /* {{{ */
157 le = llist_head(list);
159 llentry_t *le_next = le->next;
161 /* this also frees xpath->path used for le->key */
162 cx_xpath_free(le->value);
168 } /* }}} void cx_xpath_list_free */
170 static void cx_free(void *arg) /* {{{ */
174 DEBUG("curl_xml plugin: cx_free (arg = %p);", arg);
181 if (db->curl != NULL)
182 curl_easy_cleanup(db->curl);
185 if (db->xpath_list != NULL)
186 cx_xpath_list_free(db->xpath_list);
190 sfree(db->plugin_name);
196 sfree(db->credentials);
198 sfree(db->post_body);
199 curl_slist_free_all(db->headers);
200 curl_stats_destroy(db->stats);
202 for (size_t i = 0; i < db->namespaces_num; i++) {
203 sfree(db->namespaces[i].prefix);
204 sfree(db->namespaces[i].url);
206 sfree(db->namespaces);
209 } /* }}} void cx_free */
211 static const char *cx_host(const cx_t *db) /* {{{ */
213 if (db->host == NULL)
218 static int cx_config_append_string(const char *name,
219 struct curl_slist **dest, /* {{{ */
220 oconfig_item_t *ci) {
221 struct curl_slist *temp = NULL;
222 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
223 WARNING("curl_xml plugin: `%s' needs exactly one string argument.", name);
227 temp = curl_slist_append(*dest, ci->values[0].value.string);
234 } /* }}} int cx_config_append_string */
236 static int cx_check_type(const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
239 WARNING("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
243 if (ds->ds_num != xpath->values_len) {
244 WARNING("curl_xml plugin: DataSet `%s' requires %" PRIsz
245 " values, but config talks about %" PRIsz,
246 xpath->type, ds->ds_num, xpath->values_len);
251 } /* }}} cx_check_type */
253 static xmlXPathObjectPtr cx_evaluate_xpath(xmlXPathContextPtr xpath_ctx,
254 char *expr) /* {{{ */
256 xmlXPathObjectPtr xpath_obj =
257 xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
258 if (xpath_obj == NULL) {
259 WARNING("curl_xml plugin: "
260 "Error unable to evaluate xpath expression \"%s\". Skipping...",
266 } /* }}} cx_evaluate_xpath */
268 static int cx_if_not_text_node(xmlNodePtr node) /* {{{ */
270 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
271 node->type == XML_ELEMENT_NODE)
274 WARNING("curl_xml plugin: "
275 "Node \"%s\" doesn't seem to be a text node. Skipping...",
278 } /* }}} cx_if_not_text_node */
281 * Returned value should be freed with xmlFree().
283 static char *cx_get_text_node_value(xmlXPathContextPtr xpath_ctx, /* {{{ */
284 char *expr, const char *from_option) {
285 xmlXPathObjectPtr values_node_obj = cx_evaluate_xpath(xpath_ctx, expr);
286 if (values_node_obj == NULL)
287 return NULL; /* Error already logged. */
289 xmlNodeSetPtr values_node = values_node_obj->nodesetval;
290 size_t tmp_size = (values_node) ? values_node->nodeNr : 0;
293 WARNING("curl_xml plugin: "
294 "relative xpath expression \"%s\" from '%s' doesn't match "
297 xmlXPathFreeObject(values_node_obj);
302 WARNING("curl_xml plugin: "
303 "relative xpath expression \"%s\" from '%s' is expected to return "
304 "only one text node. Skipping the node.",
306 xmlXPathFreeObject(values_node_obj);
310 /* ignoring the element if other than textnode/attribute*/
311 if (cx_if_not_text_node(values_node->nodeTab[0])) {
312 WARNING("curl_xml plugin: "
313 "relative xpath expression \"%s\" from '%s' is expected to return "
314 "only text/attribute node which is not the case. "
315 "Skipping the node.",
317 xmlXPathFreeObject(values_node_obj);
321 char *node_value = (char *)xmlNodeGetContent(values_node->nodeTab[0]);
324 xmlXPathFreeObject(values_node_obj);
327 } /* }}} char * cx_get_text_node_value */
329 static int cx_handle_single_value_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
330 cx_xpath_t *xpath, const data_set_t *ds,
331 value_list_t *vl, int index) {
333 char *node_value = cx_get_text_node_value(
334 xpath_ctx, xpath->values[index].path, "ValuesFrom");
336 if (node_value == NULL)
339 switch (ds->ds[index].type) {
340 case DS_TYPE_COUNTER:
341 vl->values[index].counter =
342 (counter_t)strtoull(node_value,
343 /* endptr = */ NULL, /* base = */ 0);
346 vl->values[index].derive =
347 (derive_t)strtoll(node_value,
348 /* endptr = */ NULL, /* base = */ 0);
350 case DS_TYPE_ABSOLUTE:
351 vl->values[index].absolute =
352 (absolute_t)strtoull(node_value,
353 /* endptr = */ NULL, /* base = */ 0);
356 vl->values[index].gauge = (gauge_t)strtod(node_value,
357 /* endptr = */ NULL);
362 /* We have reached here which means that
363 * we have got something to work */
365 } /* }}} int cx_handle_single_value_xpath */
367 static int cx_handle_all_value_xpaths(xmlXPathContextPtr xpath_ctx, /* {{{ */
368 cx_xpath_t *xpath, const data_set_t *ds,
370 value_t values[xpath->values_len];
372 assert(xpath->values_len > 0);
373 assert(xpath->values_len == vl->values_len);
374 assert(xpath->values_len == ds->ds_num);
377 for (size_t i = 0; i < xpath->values_len; i++) {
378 if (cx_handle_single_value_xpath(xpath_ctx, xpath, ds, vl, i) != 0)
379 return -1; /* An error has been printed. */
380 } /* for (i = 0; i < xpath->values_len; i++) */
382 plugin_dispatch_values(vl);
386 } /* }}} int cx_handle_all_value_xpaths */
388 static int cx_handle_instance_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
389 cx_xpath_t *xpath, value_list_t *vl) {
391 /* Handle type instance */
392 if (xpath->instance != NULL) {
394 cx_get_text_node_value(xpath_ctx, xpath->instance, "InstanceFrom");
395 if (node_value == NULL)
398 if (xpath->instance_prefix != NULL)
399 snprintf(vl->type_instance, sizeof(vl->type_instance), "%s%s",
400 xpath->instance_prefix, node_value);
402 sstrncpy(vl->type_instance, node_value, sizeof(vl->type_instance));
405 } else if (xpath->instance_prefix != NULL)
406 sstrncpy(vl->type_instance, xpath->instance_prefix,
407 sizeof(vl->type_instance));
409 /* Handle plugin instance */
410 if (xpath->plugin_instance_from != NULL) {
411 char *node_value = cx_get_text_node_value(
412 xpath_ctx, xpath->plugin_instance_from, "PluginInstanceFrom");
414 if (node_value == NULL)
417 sstrncpy(vl->plugin_instance, node_value, sizeof(vl->plugin_instance));
422 } /* }}} int cx_handle_instance_xpath */
424 static int cx_handle_xpath(const cx_t *db, /* {{{ */
425 xmlXPathContextPtr xpath_ctx, cx_xpath_t *xpath) {
427 const data_set_t *ds = plugin_get_ds(xpath->type);
428 if (cx_check_type(ds, xpath) != 0)
431 xmlXPathObjectPtr base_node_obj = cx_evaluate_xpath(xpath_ctx, xpath->path);
432 if (base_node_obj == NULL)
433 return -1; /* error is logged already */
435 xmlNodeSetPtr base_nodes = base_node_obj->nodesetval;
436 int total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
438 if (total_nodes == 0) {
439 ERROR("curl_xml plugin: "
440 "xpath expression \"%s\" doesn't match any of the nodes. "
441 "Skipping the xpath block...",
443 xmlXPathFreeObject(base_node_obj);
447 /* If base_xpath returned multiple results, then */
448 /* InstanceFrom or PluginInstanceFrom in the xpath block is required */
449 if (total_nodes > 1 && xpath->instance == NULL &&
450 xpath->plugin_instance_from == NULL) {
451 ERROR("curl_xml plugin: "
452 "InstanceFrom or PluginInstanceFrom is must in xpath block "
453 "since the base xpath expression \"%s\" "
454 "returned multiple results. Skipping the xpath block...",
456 xmlXPathFreeObject(base_node_obj);
460 value_list_t vl = VALUE_LIST_INIT;
462 /* set the values for the value_list */
463 vl.values_len = ds->ds_num;
464 sstrncpy(vl.type, xpath->type, sizeof(vl.type));
465 sstrncpy(vl.plugin, (db->plugin_name != NULL) ? db->plugin_name : "curl_xml",
467 sstrncpy(vl.host, cx_host(db), sizeof(vl.host));
469 for (int i = 0; i < total_nodes; i++) {
470 xpath_ctx->node = base_nodes->nodeTab[i];
472 if (db->instance != NULL)
473 sstrncpy(vl.plugin_instance, db->instance, sizeof(vl.plugin_instance));
475 if (cx_handle_instance_xpath(xpath_ctx, xpath, &vl) != 0)
476 continue; /* An error has already been reported. */
478 if (cx_handle_all_value_xpaths(xpath_ctx, xpath, ds, &vl) != 0)
479 continue; /* An error has been logged. */
480 } /* for (i = 0; i < total_nodes; i++) */
482 /* free up the allocated memory */
483 xmlXPathFreeObject(base_node_obj);
486 } /* }}} cx_handle_xpath */
488 static int cx_handle_parsed_xml(cx_t *db, xmlDocPtr doc, /* {{{ */
489 xmlXPathContextPtr xpath_ctx) {
492 llentry_t *le = llist_head(db->xpath_list);
494 cx_xpath_t *xpath = (cx_xpath_t *)le->value;
496 if (cx_handle_xpath(db, xpath_ctx, xpath) == 0)
497 status = 0; /* we got atleast one success */
500 } /* while (le != NULL) */
503 } /* }}} cx_handle_parsed_xml */
505 static int cx_parse_xml(cx_t *db, char *xml) /* {{{ */
508 xmlDocPtr doc = xmlParseDoc(BAD_CAST xml);
510 ERROR("curl_xml plugin: Failed to parse the xml document - %s", xml);
514 xmlXPathContextPtr xpath_ctx = xmlXPathNewContext(doc);
515 if (xpath_ctx == NULL) {
516 ERROR("curl_xml plugin: Failed to create the xml context");
521 for (size_t i = 0; i < db->namespaces_num; i++) {
522 cx_namespace_t const *ns = db->namespaces + i;
524 xmlXPathRegisterNs(xpath_ctx, BAD_CAST ns->prefix, BAD_CAST ns->url);
526 ERROR("curl_xml plugin: "
527 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
528 ns->prefix, ns->url);
529 xmlXPathFreeContext(xpath_ctx);
535 int status = cx_handle_parsed_xml(db, doc, xpath_ctx);
537 xmlXPathFreeContext(xpath_ctx);
540 } /* }}} cx_parse_xml */
542 static int cx_read(user_data_t *ud) /* {{{ */
544 if ((ud == NULL) || (ud->data == NULL)) {
545 ERROR("curl_xml plugin: cx_read: Invalid user data.");
551 cx_t *db = (cx_t *)ud->data;
555 curl_easy_setopt(db->curl, CURLOPT_URL, db->url);
557 int status = curl_easy_perform(db->curl);
558 if (status != CURLE_OK) {
559 ERROR("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
560 status, db->curl_errbuf, db->url);
563 if (db->stats != NULL)
564 curl_stats_dispatch(db->stats, db->curl, cx_host(db), "curl_xml",
567 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
568 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
570 /* The response code is zero if a non-HTTP transport was used. */
571 if ((rc != 0) && (rc != 200)) {
573 "curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
578 status = cx_parse_xml(db, db->buffer);
582 } /* }}} int cx_read */
584 /* Configuration handling functions {{{ */
586 static int cx_config_add_values(const char *name, cx_xpath_t *xpath, /* {{{ */
587 oconfig_item_t *ci) {
588 if (ci->values_num < 1) {
589 WARNING("curl_xml plugin: `ValuesFrom' needs at least one argument.");
593 for (int i = 0; i < ci->values_num; i++)
594 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
595 WARNING("curl_xml plugin: `ValuesFrom' needs only string argument.");
599 sfree(xpath->values);
601 xpath->values_len = 0;
602 xpath->values = malloc(sizeof(cx_values_t) * ci->values_num);
603 if (xpath->values == NULL)
605 xpath->values_len = (size_t)ci->values_num;
607 /* populate cx_values_t structure */
608 for (int i = 0; i < ci->values_num; i++) {
609 xpath->values[i].path_len = sizeof(ci->values[i].value.string);
610 sstrncpy(xpath->values[i].path, ci->values[i].value.string,
611 sizeof(xpath->values[i].path));
615 } /* }}} cx_config_add_values */
617 static int cx_config_add_xpath(cx_t *db, oconfig_item_t *ci) /* {{{ */
619 cx_xpath_t *xpath = calloc(1, sizeof(*xpath));
621 ERROR("curl_xml plugin: calloc failed.");
625 int status = cf_util_get_string(ci, &xpath->path);
627 cx_xpath_free(xpath);
631 /* error out if xpath->path is an empty string */
632 if (strlen(xpath->path) == 0) {
633 ERROR("curl_xml plugin: invalid xpath. "
634 "xpath value can't be an empty string");
635 cx_xpath_free(xpath);
640 for (int i = 0; i < ci->children_num; i++) {
641 oconfig_item_t *child = ci->children + i;
643 if (strcasecmp("Type", child->key) == 0)
644 status = cf_util_get_string(child, &xpath->type);
645 else if (strcasecmp("InstancePrefix", child->key) == 0)
646 status = cf_util_get_string(child, &xpath->instance_prefix);
647 else if (strcasecmp("InstanceFrom", child->key) == 0)
648 status = cf_util_get_string(child, &xpath->instance);
649 else if (strcasecmp("PluginInstanceFrom", child->key) == 0)
650 status = cf_util_get_string(child, &xpath->plugin_instance_from);
651 else if (strcasecmp("ValuesFrom", child->key) == 0)
652 status = cx_config_add_values("ValuesFrom", xpath, child);
654 WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
660 } /* for (i = 0; i < ci->children_num; i++) */
663 cx_xpath_free(xpath);
667 if (xpath->type == NULL) {
668 WARNING("curl_xml plugin: `Type' missing in `xpath' block.");
669 cx_xpath_free(xpath);
673 if (xpath->values_len == 0) {
674 WARNING("curl_xml plugin: `ValuesFrom' missing in `xpath' block.");
675 cx_xpath_free(xpath);
679 llentry_t *le = llentry_create(xpath->path, xpath);
681 ERROR("curl_xml plugin: llentry_create failed.");
682 cx_xpath_free(xpath);
686 llist_append(db->xpath_list, le);
688 } /* }}} int cx_config_add_xpath */
690 static int cx_config_add_namespace(cx_t *db, /* {{{ */
691 oconfig_item_t *ci) {
693 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
694 (ci->values[1].type != OCONFIG_TYPE_STRING)) {
695 WARNING("curl_xml plugin: The `Namespace' option "
696 "needs exactly two string arguments.");
700 cx_namespace_t *ns = realloc(
701 db->namespaces, sizeof(*db->namespaces) * (db->namespaces_num + 1));
703 ERROR("curl_xml plugin: realloc failed.");
707 ns = db->namespaces + db->namespaces_num;
708 memset(ns, 0, sizeof(*ns));
710 ns->prefix = strdup(ci->values[0].value.string);
711 ns->url = strdup(ci->values[1].value.string);
713 if ((ns->prefix == NULL) || (ns->url == NULL)) {
716 ERROR("curl_xml plugin: strdup failed.");
720 db->namespaces_num++;
722 } /* }}} int cx_config_add_namespace */
724 /* Initialize db->curl */
725 static int cx_init_curl(cx_t *db) /* {{{ */
727 db->curl = curl_easy_init();
728 if (db->curl == NULL) {
729 ERROR("curl_xml plugin: curl_easy_init failed.");
733 curl_easy_setopt(db->curl, CURLOPT_NOSIGNAL, 1L);
734 curl_easy_setopt(db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
735 curl_easy_setopt(db->curl, CURLOPT_WRITEDATA, db);
736 curl_easy_setopt(db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
737 curl_easy_setopt(db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
738 curl_easy_setopt(db->curl, CURLOPT_FOLLOWLOCATION, 1L);
739 curl_easy_setopt(db->curl, CURLOPT_MAXREDIRS, 50L);
740 curl_easy_setopt(db->curl, CURLOPT_IPRESOLVE, db->address_family);
742 if (db->user != NULL) {
743 #ifdef HAVE_CURLOPT_USERNAME
744 curl_easy_setopt(db->curl, CURLOPT_USERNAME, db->user);
745 curl_easy_setopt(db->curl, CURLOPT_PASSWORD,
746 (db->pass == NULL) ? "" : db->pass);
748 size_t credentials_size;
750 credentials_size = strlen(db->user) + 2;
751 if (db->pass != NULL)
752 credentials_size += strlen(db->pass);
754 db->credentials = malloc(credentials_size);
755 if (db->credentials == NULL) {
756 ERROR("curl_xml plugin: malloc failed.");
760 snprintf(db->credentials, credentials_size, "%s:%s", db->user,
761 (db->pass == NULL) ? "" : db->pass);
762 curl_easy_setopt(db->curl, CURLOPT_USERPWD, db->credentials);
766 curl_easy_setopt(db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
769 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
770 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYHOST, db->verify_host ? 2L : 0L);
771 if (db->cacert != NULL)
772 curl_easy_setopt(db->curl, CURLOPT_CAINFO, db->cacert);
773 if (db->headers != NULL)
774 curl_easy_setopt(db->curl, CURLOPT_HTTPHEADER, db->headers);
775 if (db->post_body != NULL)
776 curl_easy_setopt(db->curl, CURLOPT_POSTFIELDS, db->post_body);
778 #ifdef HAVE_CURLOPT_TIMEOUT_MS
779 if (db->timeout >= 0)
780 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS, (long)db->timeout);
782 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
783 (long)CDTIME_T_TO_MS(plugin_get_interval()));
787 } /* }}} int cx_init_curl */
789 static int cx_config_add_url(oconfig_item_t *ci) /* {{{ */
791 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
792 WARNING("curl_xml plugin: The `URL' block "
793 "needs exactly one string argument.");
797 cx_t *db = calloc(1, sizeof(*db));
799 ERROR("curl_xml plugin: calloc failed.");
803 db->instance = strdup("default");
804 if (db->instance == NULL) {
805 ERROR("curl_xml plugin: strdup failed.");
810 db->xpath_list = llist_create();
811 if (db->xpath_list == NULL) {
812 ERROR("curl_xml plugin: list creation failed.");
819 db->address_family = CURL_IPRESOLVE_WHATEVER;
821 int status = cf_util_get_string(ci, &db->url);
823 llist_destroy(db->xpath_list);
829 cdtime_t interval = 0;
831 /* Fill the `cx_t' structure.. */
832 for (int i = 0; i < ci->children_num; i++) {
833 oconfig_item_t *child = ci->children + i;
835 if (strcasecmp("Instance", child->key) == 0)
836 status = cf_util_get_string(child, &db->instance);
837 else if (strcasecmp("Plugin", child->key) == 0)
838 status = cf_util_get_string(child, &db->plugin_name);
839 else if (strcasecmp("Host", child->key) == 0)
840 status = cf_util_get_string(child, &db->host);
841 else if (strcasecmp("User", child->key) == 0)
842 status = cf_util_get_string(child, &db->user);
843 else if (strcasecmp("Password", child->key) == 0)
844 status = cf_util_get_string(child, &db->pass);
845 else if (strcasecmp("Digest", child->key) == 0)
846 status = cf_util_get_boolean(child, &db->digest);
847 else if (strcasecmp("VerifyPeer", child->key) == 0)
848 status = cf_util_get_boolean(child, &db->verify_peer);
849 else if (strcasecmp("VerifyHost", child->key) == 0)
850 status = cf_util_get_boolean(child, &db->verify_host);
851 else if (strcasecmp("CACert", child->key) == 0)
852 status = cf_util_get_string(child, &db->cacert);
853 else if (strcasecmp("xpath", child->key) == 0)
854 status = cx_config_add_xpath(db, child);
855 else if (strcasecmp("Header", child->key) == 0)
856 status = cx_config_append_string("Header", &db->headers, child);
857 else if (strcasecmp("Post", child->key) == 0)
858 status = cf_util_get_string(child, &db->post_body);
859 else if (strcasecmp("Namespace", child->key) == 0)
860 status = cx_config_add_namespace(db, child);
861 else if (strcasecmp("Interval", child->key) == 0)
862 status = cf_util_get_cdtime(child, &interval);
863 else if (strcasecmp("Timeout", child->key) == 0)
864 status = cf_util_get_int(child, &db->timeout);
865 else if (strcasecmp("Statistics", child->key) == 0) {
866 db->stats = curl_stats_from_config(child);
867 if (db->stats == NULL)
869 } else if (strcasecmp("AddressFamily", child->key) == 0) {
871 status = cf_util_get_string(child, &af);
872 if (status != 0 || af == NULL) {
873 WARNING("curl_xml plugin: Cannot parse value of `%s' for URL `%s'.",
874 child->key, db->url);
875 } else if (strcasecmp("any", af) == 0) {
876 db->address_family = CURL_IPRESOLVE_WHATEVER;
877 } else if (strcasecmp("ipv4", af) == 0) {
878 db->address_family = CURL_IPRESOLVE_V4;
879 } else if (strcasecmp("ipv6", af) == 0) {
880 /* If curl supports ipv6, use it. If not, log a warning and
881 * fall back to default - don't set status to non-zero.
883 curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
884 if (curl_info->features & CURL_VERSION_IPV6)
885 db->address_family = CURL_IPRESOLVE_V6;
887 WARNING("curl_xml plugin: IPv6 not supported by this libCURL. "
888 "Using fallback `any'.");
890 WARNING("curl_xml plugin: Unsupported value of `%s' for URL `%s'.",
891 child->key, db->url);
895 WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
908 if (llist_size(db->xpath_list) == 0) {
909 WARNING("curl_xml plugin: No `xpath' block within `URL' block `%s'.",
915 if (cx_init_curl(db) != 0) {
920 /* If all went well, register this database for reading */
921 DEBUG("curl_xml plugin: Registering new read callback: %s", db->instance);
923 char *cb_name = ssnprintf_alloc("curl_xml-%s-%s", db->instance, db->url);
925 plugin_register_complex_read(/* group = */ "curl_xml", cb_name, cx_read,
926 /* interval = */ interval,
928 .data = db, .free_func = cx_free,
932 } /* }}} int cx_config_add_url */
934 /* }}} End of configuration handling functions */
936 static int cx_config(oconfig_item_t *ci) /* {{{ */
941 for (int i = 0; i < ci->children_num; i++) {
942 oconfig_item_t *child = ci->children + i;
944 if (strcasecmp("URL", child->key) == 0) {
945 if (cx_config_add_url(child) == 0)
950 WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
955 if ((success == 0) && (errors > 0)) {
956 ERROR("curl_xml plugin: All statements failed.");
961 } /* }}} int cx_config */
963 static int cx_init(void) /* {{{ */
965 /* Call this while collectd is still single-threaded to avoid
966 * initialization issues in libgcrypt. */
967 curl_global_init(CURL_GLOBAL_SSL);
969 } /* }}} int cx_init */
971 void module_register(void) {
972 plugin_register_complex_config("curl_xml", cx_config);
973 plugin_register_init("curl_xml", cx_init);
974 } /* void module_register */