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>
26 #include "utils_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;
88 struct curl_slist *headers;
91 cx_namespace_t *namespaces;
92 size_t namespaces_num;
95 char curl_errbuf[CURL_ERROR_SIZE];
100 llist_t *xpath_list; /* list of xpath blocks */
102 typedef struct cx_s cx_t; /* }}} */
107 static size_t cx_curl_callback(void *buf, /* {{{ */
108 size_t size, size_t nmemb, void *user_data) {
109 size_t len = size * nmemb;
111 cx_t *db = user_data;
113 ERROR("curl_xml plugin: cx_curl_callback: "
114 "user_data pointer is NULL.");
121 if ((db->buffer_fill + len) >= db->buffer_size) {
122 char *temp = realloc(db->buffer, db->buffer_fill + len + 1);
124 ERROR("curl_xml plugin: realloc failed.");
128 db->buffer_size = db->buffer_fill + len + 1;
131 memcpy(db->buffer + db->buffer_fill, (char *)buf, len);
132 db->buffer_fill += len;
133 db->buffer[db->buffer_fill] = 0;
136 } /* }}} size_t cx_curl_callback */
138 static void cx_xpath_free(cx_xpath_t *xpath) /* {{{ */
145 sfree(xpath->instance_prefix);
146 sfree(xpath->plugin_instance_from);
147 sfree(xpath->instance);
148 sfree(xpath->values);
150 } /* }}} void cx_xpath_free */
152 static void cx_xpath_list_free(llist_t *list) /* {{{ */
156 le = llist_head(list);
158 llentry_t *le_next = le->next;
160 cx_xpath_free(le->value);
166 } /* }}} void cx_list_free */
168 static void cx_free(void *arg) /* {{{ */
172 DEBUG("curl_xml plugin: cx_free (arg = %p);", arg);
179 if (db->curl != NULL)
180 curl_easy_cleanup(db->curl);
183 if (db->xpath_list != NULL)
184 cx_xpath_list_free(db->xpath_list);
188 sfree(db->plugin_name);
194 sfree(db->credentials);
196 sfree(db->post_body);
197 curl_slist_free_all(db->headers);
198 curl_stats_destroy(db->stats);
200 for (size_t i = 0; i < db->namespaces_num; i++) {
201 sfree(db->namespaces[i].prefix);
202 sfree(db->namespaces[i].url);
204 sfree(db->namespaces);
207 } /* }}} void cx_free */
209 static const char *cx_host(const cx_t *db) /* {{{ */
211 if (db->host == NULL)
216 static int cx_config_append_string(const char *name,
217 struct curl_slist **dest, /* {{{ */
218 oconfig_item_t *ci) {
219 struct curl_slist *temp = NULL;
220 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
221 WARNING("curl_xml plugin: `%s' needs exactly one string argument.", name);
225 temp = curl_slist_append(*dest, ci->values[0].value.string);
232 } /* }}} int cx_config_append_string */
234 static int cx_check_type(const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
237 WARNING("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
241 if (ds->ds_num != xpath->values_len) {
242 WARNING("curl_xml plugin: DataSet `%s' requires %zu values, but config "
244 xpath->type, ds->ds_num, xpath->values_len);
249 } /* }}} cx_check_type */
251 static xmlXPathObjectPtr cx_evaluate_xpath(xmlXPathContextPtr xpath_ctx,
252 char *expr) /* {{{ */
254 xmlXPathObjectPtr xpath_obj =
255 xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
256 if (xpath_obj == NULL) {
257 WARNING("curl_xml plugin: "
258 "Error unable to evaluate xpath expression \"%s\". Skipping...",
264 } /* }}} cx_evaluate_xpath */
266 static int cx_if_not_text_node(xmlNodePtr node) /* {{{ */
268 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
269 node->type == XML_ELEMENT_NODE)
272 WARNING("curl_xml plugin: "
273 "Node \"%s\" doesn't seem to be a text node. Skipping...",
276 } /* }}} cx_if_not_text_node */
278 static char *cx_get_text_node_value(xmlXPathContextPtr xpath_ctx, /* {{{ */
279 char *expr, const char *from_option) {
280 xmlXPathObjectPtr values_node_obj = cx_evaluate_xpath(xpath_ctx, expr);
281 if (values_node_obj == NULL)
282 return NULL; /* Error already logged. */
284 xmlNodeSetPtr values_node = values_node_obj->nodesetval;
285 size_t tmp_size = (values_node) ? values_node->nodeNr : 0;
288 WARNING("curl_xml plugin: "
289 "relative xpath expression \"%s\" %sdoesn't match "
291 expr, (from_option == NULL) ? "" : from_option);
292 xmlXPathFreeObject(values_node_obj);
297 WARNING("curl_xml plugin: "
298 "relative xpath expression \"%s\" %sis expected to return "
299 "only one text node. Skipping the node.",
300 expr, (from_option == NULL) ? "" : from_option);
301 xmlXPathFreeObject(values_node_obj);
305 /* ignoring the element if other than textnode/attribute*/
306 if (cx_if_not_text_node(values_node->nodeTab[0])) {
307 WARNING("curl_xml plugin: "
308 "relative xpath expression \"%s\" %sis expected to return "
309 "only text/attribute node which is not the case. "
310 "Skipping the node.",
311 expr, (from_option == NULL) ? "" : from_option);
312 xmlXPathFreeObject(values_node_obj);
316 char *node_value = (char *)xmlNodeGetContent(values_node->nodeTab[0]);
319 xmlXPathFreeObject(values_node_obj);
322 } /* }}} char * cx_get_text_node_value */
324 static int cx_handle_single_value_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
325 cx_xpath_t *xpath, const data_set_t *ds,
326 value_list_t *vl, int index) {
328 char *node_value = cx_get_text_node_value(
329 xpath_ctx, xpath->values[index].path, "from 'ValuesFrom' ");
331 if (node_value == NULL)
334 switch (ds->ds[index].type) {
335 case DS_TYPE_COUNTER:
336 vl->values[index].counter =
337 (counter_t)strtoull(node_value,
338 /* endptr = */ NULL, /* base = */ 0);
341 vl->values[index].derive =
342 (derive_t)strtoll(node_value,
343 /* endptr = */ NULL, /* base = */ 0);
345 case DS_TYPE_ABSOLUTE:
346 vl->values[index].absolute =
347 (absolute_t)strtoull(node_value,
348 /* endptr = */ NULL, /* base = */ 0);
351 vl->values[index].gauge = (gauge_t)strtod(node_value,
352 /* endptr = */ NULL);
357 /* We have reached here which means that
358 * we have got something to work */
360 } /* }}} int cx_handle_single_value_xpath */
362 static int cx_handle_all_value_xpaths(xmlXPathContextPtr xpath_ctx, /* {{{ */
363 cx_xpath_t *xpath, const data_set_t *ds,
365 value_t values[xpath->values_len];
367 assert(xpath->values_len > 0);
368 assert(xpath->values_len == vl->values_len);
369 assert(xpath->values_len == ds->ds_num);
372 for (size_t i = 0; i < xpath->values_len; i++) {
373 if (cx_handle_single_value_xpath(xpath_ctx, xpath, ds, vl, i) != 0)
374 return -1; /* An error has been printed. */
375 } /* for (i = 0; i < xpath->values_len; i++) */
377 plugin_dispatch_values(vl);
381 } /* }}} int cx_handle_all_value_xpaths */
383 static int cx_handle_instance_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
384 cx_xpath_t *xpath, value_list_t *vl) {
386 /* Handle type instance */
387 if (xpath->instance != NULL) {
388 char *node_value = cx_get_text_node_value(xpath_ctx, xpath->instance,
389 "from 'InstanceFrom' ");
390 if (node_value == NULL)
393 if (xpath->instance_prefix != NULL)
394 snprintf(vl->type_instance, sizeof(vl->type_instance), "%s%s",
395 xpath->instance_prefix, node_value);
397 sstrncpy(vl->type_instance, node_value, sizeof(vl->type_instance));
400 } else if (xpath->instance_prefix != NULL)
401 sstrncpy(vl->type_instance, xpath->instance_prefix,
402 sizeof(vl->type_instance));
404 /* Handle plugin instance */
405 if (xpath->plugin_instance_from != NULL) {
406 char *node_value = cx_get_text_node_value(
407 xpath_ctx, xpath->plugin_instance_from, "from 'PluginInstanceFrom' ");
409 if (node_value == NULL)
412 sstrncpy(vl->plugin_instance, node_value, sizeof(vl->plugin_instance));
417 } /* }}} int cx_handle_instance_xpath */
419 static int cx_handle_xpath(const cx_t *db, /* {{{ */
420 xmlXPathContextPtr xpath_ctx, cx_xpath_t *xpath) {
422 const data_set_t *ds = plugin_get_ds(xpath->type);
423 if (cx_check_type(ds, xpath) != 0)
426 xmlXPathObjectPtr base_node_obj = cx_evaluate_xpath(xpath_ctx, xpath->path);
427 if (base_node_obj == NULL)
428 return -1; /* error is logged already */
430 xmlNodeSetPtr base_nodes = base_node_obj->nodesetval;
431 int total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
433 if (total_nodes == 0) {
434 ERROR("curl_xml plugin: "
435 "xpath expression \"%s\" doesn't match any of the nodes. "
436 "Skipping the xpath block...",
438 xmlXPathFreeObject(base_node_obj);
442 /* If base_xpath returned multiple results, then */
443 /* InstanceFrom or PluginInstanceFrom in the xpath block is required */
444 if (total_nodes > 1 && xpath->instance == NULL &&
445 xpath->plugin_instance_from == NULL) {
446 ERROR("curl_xml plugin: "
447 "InstanceFrom or PluginInstanceFrom is must in xpath block "
448 "since the base xpath expression \"%s\" "
449 "returned multiple results. Skipping the xpath block...",
454 value_list_t vl = VALUE_LIST_INIT;
456 /* set the values for the value_list */
457 vl.values_len = ds->ds_num;
458 sstrncpy(vl.type, xpath->type, sizeof(vl.type));
459 sstrncpy(vl.plugin, (db->plugin_name != NULL) ? db->plugin_name : "curl_xml",
461 sstrncpy(vl.host, cx_host(db), sizeof(vl.host));
463 for (int i = 0; i < total_nodes; i++) {
464 xpath_ctx->node = base_nodes->nodeTab[i];
466 if (db->instance != NULL)
467 sstrncpy(vl.plugin_instance, db->instance, sizeof(vl.plugin_instance));
469 if (cx_handle_instance_xpath(xpath_ctx, xpath, &vl) != 0)
470 continue; /* An error has already been reported. */
472 if (cx_handle_all_value_xpaths(xpath_ctx, xpath, ds, &vl) != 0)
473 continue; /* An error has been logged. */
474 } /* for (i = 0; i < total_nodes; i++) */
476 /* free up the allocated memory */
477 xmlXPathFreeObject(base_node_obj);
480 } /* }}} cx_handle_xpath */
482 static int cx_handle_parsed_xml(cx_t *db, xmlDocPtr doc, /* {{{ */
483 xmlXPathContextPtr xpath_ctx) {
486 llentry_t *le = llist_head(db->xpath_list);
488 cx_xpath_t *xpath = (cx_xpath_t *)le->value;
490 if (cx_handle_xpath(db, xpath_ctx, xpath) == 0)
491 status = 0; /* we got atleast one success */
494 } /* while (le != NULL) */
497 } /* }}} cx_handle_parsed_xml */
499 static int cx_parse_xml(cx_t *db, char *xml) /* {{{ */
502 xmlDocPtr doc = xmlParseDoc(BAD_CAST xml);
504 ERROR("curl_xml plugin: Failed to parse the xml document - %s", xml);
508 xmlXPathContextPtr xpath_ctx = xmlXPathNewContext(doc);
509 if (xpath_ctx == NULL) {
510 ERROR("curl_xml plugin: Failed to create the xml context");
515 for (size_t i = 0; i < db->namespaces_num; i++) {
516 cx_namespace_t const *ns = db->namespaces + i;
518 xmlXPathRegisterNs(xpath_ctx, BAD_CAST ns->prefix, BAD_CAST ns->url);
520 ERROR("curl_xml plugin: "
521 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
522 ns->prefix, ns->url);
523 xmlXPathFreeContext(xpath_ctx);
529 int status = cx_handle_parsed_xml(db, doc, xpath_ctx);
531 xmlXPathFreeContext(xpath_ctx);
534 } /* }}} cx_parse_xml */
536 static int cx_read(user_data_t *ud) /* {{{ */
538 if ((ud == NULL) || (ud->data == NULL)) {
539 ERROR("curl_xml plugin: cx_read: Invalid user data.");
545 cx_t *db = (cx_t *)ud->data;
549 curl_easy_setopt(db->curl, CURLOPT_URL, db->url);
551 int status = curl_easy_perform(db->curl);
552 if (status != CURLE_OK) {
553 ERROR("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
554 status, db->curl_errbuf, db->url);
557 if (db->stats != NULL)
558 curl_stats_dispatch(db->stats, db->curl, cx_host(db), "curl_xml",
561 curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
562 curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
564 /* The response code is zero if a non-HTTP transport was used. */
565 if ((rc != 0) && (rc != 200)) {
567 "curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
572 status = cx_parse_xml(db, db->buffer);
576 } /* }}} int cx_read */
578 /* Configuration handling functions {{{ */
580 static int cx_config_add_values(const char *name, cx_xpath_t *xpath, /* {{{ */
581 oconfig_item_t *ci) {
582 if (ci->values_num < 1) {
583 WARNING("curl_xml plugin: `ValuesFrom' needs at least one argument.");
587 for (int i = 0; i < ci->values_num; i++)
588 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
589 WARNING("curl_xml plugin: `ValuesFrom' needs only string argument.");
593 sfree(xpath->values);
595 xpath->values_len = 0;
596 xpath->values = malloc(sizeof(cx_values_t) * ci->values_num);
597 if (xpath->values == NULL)
599 xpath->values_len = (size_t)ci->values_num;
601 /* populate cx_values_t structure */
602 for (int i = 0; i < ci->values_num; i++) {
603 xpath->values[i].path_len = sizeof(ci->values[i].value.string);
604 sstrncpy(xpath->values[i].path, ci->values[i].value.string,
605 sizeof(xpath->values[i].path));
609 } /* }}} cx_config_add_values */
611 static int cx_config_add_xpath(cx_t *db, oconfig_item_t *ci) /* {{{ */
613 cx_xpath_t *xpath = calloc(1, sizeof(*xpath));
615 ERROR("curl_xml plugin: calloc failed.");
619 int status = cf_util_get_string(ci, &xpath->path);
621 cx_xpath_free(xpath);
625 /* error out if xpath->path is an empty string */
626 if (strlen(xpath->path) == 0) {
627 ERROR("curl_xml plugin: invalid xpath. "
628 "xpath value can't be an empty string");
629 cx_xpath_free(xpath);
634 for (int i = 0; i < ci->children_num; i++) {
635 oconfig_item_t *child = ci->children + i;
637 if (strcasecmp("Type", child->key) == 0)
638 status = cf_util_get_string(child, &xpath->type);
639 else if (strcasecmp("InstancePrefix", child->key) == 0)
640 status = cf_util_get_string(child, &xpath->instance_prefix);
641 else if (strcasecmp("InstanceFrom", child->key) == 0)
642 status = cf_util_get_string(child, &xpath->instance);
643 else if (strcasecmp("PluginInstanceFrom", child->key) == 0)
644 status = cf_util_get_string(child, &xpath->plugin_instance_from);
645 else if (strcasecmp("ValuesFrom", child->key) == 0)
646 status = cx_config_add_values("ValuesFrom", xpath, child);
648 WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
654 } /* for (i = 0; i < ci->children_num; i++) */
657 cx_xpath_free(xpath);
661 if (xpath->type == NULL) {
662 WARNING("curl_xml plugin: `Type' missing in `xpath' block.");
663 cx_xpath_free(xpath);
667 if (xpath->values_len == 0) {
668 WARNING("curl_xml plugin: `ValuesFrom' missing in `xpath' block.");
669 cx_xpath_free(xpath);
673 llentry_t *le = llentry_create(xpath->path, xpath);
675 ERROR("curl_xml plugin: llentry_create failed.");
676 cx_xpath_free(xpath);
680 llist_append(db->xpath_list, le);
682 } /* }}} int cx_config_add_xpath */
684 static int cx_config_add_namespace(cx_t *db, /* {{{ */
685 oconfig_item_t *ci) {
687 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
688 (ci->values[1].type != OCONFIG_TYPE_STRING)) {
689 WARNING("curl_xml plugin: The `Namespace' option "
690 "needs exactly two string arguments.");
694 cx_namespace_t *ns = realloc(
695 db->namespaces, sizeof(*db->namespaces) * (db->namespaces_num + 1));
697 ERROR("curl_xml plugin: realloc failed.");
701 ns = db->namespaces + db->namespaces_num;
702 memset(ns, 0, sizeof(*ns));
704 ns->prefix = strdup(ci->values[0].value.string);
705 ns->url = strdup(ci->values[1].value.string);
707 if ((ns->prefix == NULL) || (ns->url == NULL)) {
710 ERROR("curl_xml plugin: strdup failed.");
714 db->namespaces_num++;
716 } /* }}} int cx_config_add_namespace */
718 /* Initialize db->curl */
719 static int cx_init_curl(cx_t *db) /* {{{ */
721 db->curl = curl_easy_init();
722 if (db->curl == NULL) {
723 ERROR("curl_xml plugin: curl_easy_init failed.");
727 curl_easy_setopt(db->curl, CURLOPT_NOSIGNAL, 1L);
728 curl_easy_setopt(db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
729 curl_easy_setopt(db->curl, CURLOPT_WRITEDATA, db);
730 curl_easy_setopt(db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
731 curl_easy_setopt(db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
732 curl_easy_setopt(db->curl, CURLOPT_FOLLOWLOCATION, 1L);
733 curl_easy_setopt(db->curl, CURLOPT_MAXREDIRS, 50L);
735 if (db->user != NULL) {
736 #ifdef HAVE_CURLOPT_USERNAME
737 curl_easy_setopt(db->curl, CURLOPT_USERNAME, db->user);
738 curl_easy_setopt(db->curl, CURLOPT_PASSWORD,
739 (db->pass == NULL) ? "" : db->pass);
741 size_t credentials_size;
743 credentials_size = strlen(db->user) + 2;
744 if (db->pass != NULL)
745 credentials_size += strlen(db->pass);
747 db->credentials = malloc(credentials_size);
748 if (db->credentials == NULL) {
749 ERROR("curl_xml plugin: malloc failed.");
753 snprintf(db->credentials, credentials_size, "%s:%s", db->user,
754 (db->pass == NULL) ? "" : db->pass);
755 curl_easy_setopt(db->curl, CURLOPT_USERPWD, db->credentials);
759 curl_easy_setopt(db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
762 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
763 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYHOST, db->verify_host ? 2L : 0L);
764 if (db->cacert != NULL)
765 curl_easy_setopt(db->curl, CURLOPT_CAINFO, db->cacert);
766 if (db->headers != NULL)
767 curl_easy_setopt(db->curl, CURLOPT_HTTPHEADER, db->headers);
768 if (db->post_body != NULL)
769 curl_easy_setopt(db->curl, CURLOPT_POSTFIELDS, db->post_body);
771 #ifdef HAVE_CURLOPT_TIMEOUT_MS
772 if (db->timeout >= 0)
773 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS, (long)db->timeout);
775 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
776 (long)CDTIME_T_TO_MS(plugin_get_interval()));
780 } /* }}} int cx_init_curl */
782 static int cx_config_add_url(oconfig_item_t *ci) /* {{{ */
784 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
785 WARNING("curl_xml plugin: The `URL' block "
786 "needs exactly one string argument.");
790 cx_t *db = calloc(1, sizeof(*db));
792 ERROR("curl_xml plugin: calloc failed.");
796 db->instance = strdup("default");
797 if (db->instance == NULL) {
798 ERROR("curl_xml plugin: strdup failed.");
803 db->xpath_list = llist_create();
804 if (db->xpath_list == NULL) {
805 ERROR("curl_xml plugin: list creation failed.");
813 int status = cf_util_get_string(ci, &db->url);
815 llist_destroy(db->xpath_list);
821 /* Fill the `cx_t' structure.. */
822 for (int i = 0; i < ci->children_num; i++) {
823 oconfig_item_t *child = ci->children + i;
825 if (strcasecmp("Instance", child->key) == 0)
826 status = cf_util_get_string(child, &db->instance);
827 else if (strcasecmp("Plugin", child->key) == 0)
828 status = cf_util_get_string(child, &db->plugin_name);
829 else if (strcasecmp("Host", child->key) == 0)
830 status = cf_util_get_string(child, &db->host);
831 else if (strcasecmp("User", child->key) == 0)
832 status = cf_util_get_string(child, &db->user);
833 else if (strcasecmp("Password", child->key) == 0)
834 status = cf_util_get_string(child, &db->pass);
835 else if (strcasecmp("Digest", child->key) == 0)
836 status = cf_util_get_boolean(child, &db->digest);
837 else if (strcasecmp("VerifyPeer", child->key) == 0)
838 status = cf_util_get_boolean(child, &db->verify_peer);
839 else if (strcasecmp("VerifyHost", child->key) == 0)
840 status = cf_util_get_boolean(child, &db->verify_host);
841 else if (strcasecmp("CACert", child->key) == 0)
842 status = cf_util_get_string(child, &db->cacert);
843 else if (strcasecmp("xpath", child->key) == 0)
844 status = cx_config_add_xpath(db, child);
845 else if (strcasecmp("Header", child->key) == 0)
846 status = cx_config_append_string("Header", &db->headers, child);
847 else if (strcasecmp("Post", child->key) == 0)
848 status = cf_util_get_string(child, &db->post_body);
849 else if (strcasecmp("Namespace", child->key) == 0)
850 status = cx_config_add_namespace(db, child);
851 else if (strcasecmp("Timeout", child->key) == 0)
852 status = cf_util_get_int(child, &db->timeout);
853 else if (strcasecmp("Statistics", child->key) == 0) {
854 db->stats = curl_stats_from_config(child);
855 if (db->stats == NULL)
858 WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
871 if (llist_size(db->xpath_list) == 0) {
872 WARNING("curl_xml plugin: No `xpath' block within `URL' block `%s'.",
878 if (cx_init_curl(db) != 0) {
883 /* If all went well, register this database for reading */
884 DEBUG("curl_xml plugin: Registering new read callback: %s", db->instance);
886 char *cb_name = ssnprintf_alloc("curl_xml-%s-%s", db->instance, db->url);
888 plugin_register_complex_read(/* group = */ "curl_xml", cb_name, cx_read,
891 .data = db, .free_func = cx_free,
895 } /* }}} int cx_config_add_url */
897 /* }}} End of configuration handling functions */
899 static int cx_config(oconfig_item_t *ci) /* {{{ */
904 for (int i = 0; i < ci->children_num; i++) {
905 oconfig_item_t *child = ci->children + i;
907 if (strcasecmp("URL", child->key) == 0) {
908 if (cx_config_add_url(child) == 0)
913 WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
918 if ((success == 0) && (errors > 0)) {
919 ERROR("curl_xml plugin: All statements failed.");
924 } /* }}} int cx_config */
926 static int cx_init(void) /* {{{ */
928 /* Call this while collectd is still single-threaded to avoid
929 * initialization issues in libgcrypt. */
930 curl_global_init(CURL_GLOBAL_SSL);
932 } /* }}} int cx_init */
934 void module_register(void) {
935 plugin_register_complex_config("curl_xml", cx_config);
936 plugin_register_init("curl_xml", cx_init);
937 } /* void module_register */