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 *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;
114 ERROR("curl_xml plugin: cx_curl_callback: "
115 "user_data pointer is NULL.");
122 if ((db->buffer_fill + len) >= db->buffer_size) {
125 temp = realloc(db->buffer, db->buffer_fill + len + 1);
127 ERROR("curl_xml plugin: realloc failed.");
131 db->buffer_size = db->buffer_fill + len + 1;
134 memcpy(db->buffer + db->buffer_fill, (char *)buf, len);
135 db->buffer_fill += len;
136 db->buffer[db->buffer_fill] = 0;
139 } /* }}} size_t cx_curl_callback */
141 static void cx_xpath_free(cx_xpath_t *xpath) /* {{{ */
148 sfree(xpath->instance_prefix);
149 sfree(xpath->plugin_instance_from);
150 sfree(xpath->instance);
151 sfree(xpath->values);
153 } /* }}} void cx_xpath_free */
155 static void cx_list_free(llist_t *list) /* {{{ */
159 le = llist_head(list);
166 cx_xpath_free(le->value);
172 } /* }}} void cx_list_free */
174 static void cx_free(void *arg) /* {{{ */
178 DEBUG("curl_xml plugin: cx_free (arg = %p);", arg);
185 if (db->curl != NULL)
186 curl_easy_cleanup(db->curl);
189 if (db->list != NULL)
190 cx_list_free(db->list);
194 sfree(db->plugin_name);
200 sfree(db->credentials);
202 sfree(db->post_body);
203 curl_slist_free_all(db->headers);
204 curl_stats_destroy(db->stats);
206 for (size_t i = 0; i < db->namespaces_num; i++) {
207 sfree(db->namespaces[i].prefix);
208 sfree(db->namespaces[i].url);
210 sfree(db->namespaces);
213 } /* }}} void cx_free */
215 static const char *cx_host(const cx_t *db) /* {{{ */
217 if (db->host == NULL)
222 static int cx_config_append_string(const char *name,
223 struct curl_slist **dest, /* {{{ */
224 oconfig_item_t *ci) {
225 struct curl_slist *temp = NULL;
226 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
227 WARNING("curl_xml plugin: `%s' needs exactly one string argument.", name);
231 temp = curl_slist_append(*dest, ci->values[0].value.string);
238 } /* }}} int cx_config_append_string */
240 static int cx_check_type(const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
243 WARNING("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
247 if (ds->ds_num != xpath->values_len) {
248 WARNING("curl_xml plugin: DataSet `%s' requires %zu values, but config "
250 xpath->type, ds->ds_num, xpath->values_len);
255 } /* }}} cx_check_type */
257 static xmlXPathObjectPtr
258 cx_evaluate_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
260 xmlXPathObjectPtr xpath_obj;
262 /* XXX: When to free this? */
263 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
264 if (xpath_obj == NULL) {
265 WARNING("curl_xml plugin: "
266 "Error unable to evaluate xpath expression \"%s\". Skipping...",
272 } /* }}} cx_evaluate_xpath */
274 static int cx_if_not_text_node(xmlNodePtr node) /* {{{ */
276 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
277 node->type == XML_ELEMENT_NODE)
280 WARNING("curl_xml plugin: "
281 "Node \"%s\" doesn't seem to be a text node. Skipping...",
284 } /* }}} cx_if_not_text_node */
286 static int cx_handle_single_value_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
287 cx_xpath_t *xpath, const data_set_t *ds,
288 value_list_t *vl, int index) {
289 xmlXPathObjectPtr values_node_obj;
290 xmlNodeSetPtr values_node;
295 cx_evaluate_xpath(xpath_ctx, BAD_CAST xpath->values[index].path);
296 if (values_node_obj == NULL)
297 return -1; /* Error already logged. */
299 values_node = values_node_obj->nodesetval;
300 tmp_size = (values_node) ? values_node->nodeNr : 0;
303 WARNING("curl_xml plugin: "
304 "relative xpath expression \"%s\" doesn't match any of the nodes. "
306 xpath->values[index].path);
307 xmlXPathFreeObject(values_node_obj);
312 WARNING("curl_xml plugin: "
313 "relative xpath expression \"%s\" is expected to return "
314 "only one node. Skipping...",
315 xpath->values[index].path);
316 xmlXPathFreeObject(values_node_obj);
320 /* ignoring the element if other than textnode/attribute*/
321 if (cx_if_not_text_node(values_node->nodeTab[0])) {
322 WARNING("curl_xml plugin: "
323 "relative xpath expression \"%s\" is expected to return "
324 "only text/attribute node which is not the case. Skipping...",
325 xpath->values[index].path);
326 xmlXPathFreeObject(values_node_obj);
330 node_value = (char *)xmlNodeGetContent(values_node->nodeTab[0]);
331 switch (ds->ds[index].type) {
332 case DS_TYPE_COUNTER:
333 vl->values[index].counter =
334 (counter_t)strtoull(node_value,
335 /* endptr = */ NULL, /* base = */ 0);
338 vl->values[index].derive =
339 (derive_t)strtoll(node_value,
340 /* endptr = */ NULL, /* base = */ 0);
342 case DS_TYPE_ABSOLUTE:
343 vl->values[index].absolute =
344 (absolute_t)strtoull(node_value,
345 /* endptr = */ NULL, /* base = */ 0);
348 vl->values[index].gauge = (gauge_t)strtod(node_value,
349 /* endptr = */ NULL);
353 xmlXPathFreeObject(values_node_obj);
356 /* We have reached here which means that
357 * we have got something to work */
359 } /* }}} int cx_handle_single_value_xpath */
361 static int cx_handle_all_value_xpaths(xmlXPathContextPtr xpath_ctx, /* {{{ */
362 cx_xpath_t *xpath, const data_set_t *ds,
364 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 status = cx_handle_single_value_xpath(xpath_ctx, xpath, ds, vl, i);
375 return -1; /* An error has been printed. */
376 } /* for (i = 0; i < xpath->values_len; i++) */
378 plugin_dispatch_values(vl);
382 } /* }}} int cx_handle_all_value_xpaths */
384 static int cx_handle_instance_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
385 cx_xpath_t *xpath, value_list_t *vl) {
387 xmlXPathObjectPtr instance_node_obj = NULL;
388 xmlNodeSetPtr instance_node = NULL;
390 memset(vl->type_instance, 0, sizeof(vl->type_instance));
392 /* instance has to be an xpath expression */
393 if (xpath->instance != NULL) {
396 instance_node_obj = cx_evaluate_xpath(xpath_ctx, BAD_CAST xpath->instance);
397 if (instance_node_obj == NULL)
398 return -1; /* error is logged already */
400 instance_node = instance_node_obj->nodesetval;
401 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
406 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
407 "any of the nodes. Skipping the node.",
409 xmlXPathFreeObject(instance_node_obj);
414 WARNING("curl_xml plugin: "
415 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
416 "to return only one text node. Skipping the node.",
418 xmlXPathFreeObject(instance_node_obj);
422 /* ignoring the element if other than textnode/attribute */
423 if (cx_if_not_text_node(instance_node->nodeTab[0])) {
424 WARNING("curl_xml plugin: "
425 "relative xpath expression \"%s\" is expected to return only "
427 "which is not the case. Skipping the node.",
429 xmlXPathFreeObject(instance_node_obj);
432 } /* if (xpath->instance != NULL) */
434 if (xpath->instance_prefix != NULL) {
435 if (instance_node != NULL) {
436 char *node_value = (char *)xmlNodeGetContent(instance_node->nodeTab[0]);
437 snprintf(vl->type_instance, sizeof(vl->type_instance), "%s%s",
438 xpath->instance_prefix, node_value);
441 sstrncpy(vl->type_instance, xpath->instance_prefix,
442 sizeof(vl->type_instance));
444 /* If instance_prefix and instance_node are NULL, then
445 * don't set the type_instance */
446 if (instance_node != NULL) {
447 char *node_value = (char *)xmlNodeGetContent(instance_node->nodeTab[0]);
448 sstrncpy(vl->type_instance, node_value, sizeof(vl->type_instance));
453 /* Free `instance_node_obj' this late, because `instance_node' points to
454 * somewhere inside this structure. */
455 xmlXPathFreeObject(instance_node_obj);
457 /* Part 2, handle PluginInstanceFrom */
458 instance_node_obj = NULL;
459 instance_node = NULL;
461 /* plugin_instance_from has to be an xpath expression */
462 if (xpath->plugin_instance_from != NULL) {
465 instance_node_obj = cx_evaluate_xpath(xpath_ctx,
466 BAD_CAST xpath->plugin_instance_from);
467 if (instance_node_obj == NULL)
468 return -1; /* error is already logged */
470 instance_node = instance_node_obj->nodesetval;
471 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
474 WARNING("curl_xml plugin: "
475 "relative xpath expression for 'PluginInstanceFrom' \"%s\" "
476 "doesn't match any of the nodes. Skipping the node.",
477 xpath->plugin_instance_from);
478 xmlXPathFreeObject(instance_node_obj);
483 WARNING("curl_xml plugin: "
484 "relative xpath expression for 'PluginInstanceFrom' \"%s\" "
485 "is expected to return only one text node. Skipping the node.",
486 xpath->plugin_instance_from);
487 xmlXPathFreeObject(instance_node_obj);
491 /* ignoring the element if other than textnode/attribute */
492 if (cx_if_not_text_node(instance_node->nodeTab[0])) {
493 WARNING("curl_xml plugin: "
494 "relative xpath expression \"%s\" is expected to return only "
495 "text node which is not the case. Skipping the node.",
496 xpath->plugin_instance_from);
497 xmlXPathFreeObject(instance_node_obj);
501 if (instance_node != NULL) {
502 char *node_value = (char *)xmlNodeGetContent(instance_node->nodeTab[0]);
503 sstrncpy (vl->plugin_instance, node_value, sizeof(vl->plugin_instance));
507 /* Free `instance_node_obj' this late, because `instance_node' points to
508 * somewhere inside this structure. */
509 xmlXPathFreeObject(instance_node_obj);
510 } /* if (xpath->plugin_instance_from != NULL) */
513 } /* }}} int cx_handle_instance_xpath */
515 static int cx_handle_base_xpath(const cx_t *db, /* {{{ */
516 xmlXPathContextPtr xpath_ctx,
517 const data_set_t *ds, char *base_xpath,
521 xmlXPathObjectPtr base_node_obj = NULL;
522 xmlNodeSetPtr base_nodes = NULL;
524 value_list_t vl = VALUE_LIST_INIT;
526 base_node_obj = cx_evaluate_xpath(xpath_ctx, BAD_CAST base_xpath);
527 if (base_node_obj == NULL)
528 return -1; /* error is logged already */
530 base_nodes = base_node_obj->nodesetval;
531 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
533 if (total_nodes == 0) {
534 ERROR("curl_xml plugin: "
535 "xpath expression \"%s\" doesn't match any of the nodes. "
536 "Skipping the xpath block...",
538 xmlXPathFreeObject(base_node_obj);
542 /* If base_xpath returned multiple results, then */
543 /* InstanceFrom or PluginInstanceFrom in the xpath block is required */
544 if (total_nodes > 1 && xpath->instance == NULL
545 && xpath->plugin_instance_from == NULL) {
546 ERROR("curl_xml plugin: "
547 "InstanceFrom or PluginInstanceFrom is must in xpath block "
548 "since the base xpath expression \"%s\" "
549 "returned multiple results. Skipping the xpath block...",
554 /* set the values for the value_list */
555 vl.values_len = ds->ds_num;
556 sstrncpy(vl.type, xpath->type, sizeof(vl.type));
557 sstrncpy(vl.plugin, (db->plugin_name != NULL) ? db->plugin_name : "curl_xml",
559 sstrncpy(vl.host, cx_host(db), sizeof(vl.host));
561 for (int i = 0; i < total_nodes; i++) {
564 xpath_ctx->node = base_nodes->nodeTab[i];
566 if (db->instance != NULL)
567 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
569 status = cx_handle_instance_xpath(xpath_ctx, xpath, &vl);
571 continue; /* An error has already been reported. */
573 status = cx_handle_all_value_xpaths(xpath_ctx, xpath, ds, &vl);
575 continue; /* An error has been logged. */
576 } /* for (i = 0; i < total_nodes; i++) */
578 /* free up the allocated memory */
579 xmlXPathFreeObject(base_node_obj);
582 } /* }}} cx_handle_base_xpath */
584 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
585 xmlXPathContextPtr xpath_ctx, cx_t *db) {
587 const data_set_t *ds;
591 le = llist_head(db->list);
594 xpath = (cx_xpath_t *)le->value;
595 ds = plugin_get_ds(xpath->type);
597 if ((cx_check_type(ds, xpath) == 0) &&
598 (cx_handle_base_xpath(db, xpath_ctx, ds, le->key, xpath) == 0))
599 status = 0; /* we got atleast one success */
602 } /* while (le != NULL) */
605 } /* }}} cx_handle_parsed_xml */
607 static int cx_parse_stats_xml(xmlChar *xml, cx_t *db) /* {{{ */
611 xmlXPathContextPtr xpath_ctx;
614 doc = xmlParseDoc(xml);
616 ERROR("curl_xml plugin: Failed to parse the xml document - %s", xml);
620 xpath_ctx = xmlXPathNewContext(doc);
621 if (xpath_ctx == NULL) {
622 ERROR("curl_xml plugin: Failed to create the xml context");
627 for (size_t i = 0; i < db->namespaces_num; i++) {
628 cx_namespace_t const *ns = db->namespaces + i;
630 xmlXPathRegisterNs(xpath_ctx, BAD_CAST ns->prefix, BAD_CAST ns->url);
632 ERROR("curl_xml plugin: "
633 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
634 ns->prefix, ns->url);
635 xmlXPathFreeContext(xpath_ctx);
641 status = cx_handle_parsed_xml(doc, xpath_ctx, db);
643 xmlXPathFreeContext(xpath_ctx);
646 } /* }}} cx_parse_stats_xml */
648 static int cx_curl_perform(cx_t *db, CURL *curl) /* {{{ */
657 curl_easy_setopt(db->curl, CURLOPT_URL, db->url);
659 status = curl_easy_perform(curl);
660 if (status != CURLE_OK) {
661 ERROR("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
662 status, db->curl_errbuf, db->url);
665 if (db->stats != NULL)
666 curl_stats_dispatch(db->stats, db->curl, cx_host(db), "curl_xml",
669 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
670 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
672 /* The response code is zero if a non-HTTP transport was used. */
673 if ((rc != 0) && (rc != 200)) {
675 "curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
682 status = cx_parse_stats_xml(BAD_CAST ptr, db);
686 } /* }}} int cx_curl_perform */
688 static int cx_read(user_data_t *ud) /* {{{ */
692 if ((ud == NULL) || (ud->data == NULL)) {
693 ERROR("curl_xml plugin: cx_read: Invalid user data.");
697 db = (cx_t *)ud->data;
699 return cx_curl_perform(db, db->curl);
700 } /* }}} int cx_read */
702 /* Configuration handling functions {{{ */
704 static int cx_config_add_values(const char *name, cx_xpath_t *xpath, /* {{{ */
705 oconfig_item_t *ci) {
706 if (ci->values_num < 1) {
707 WARNING("curl_xml plugin: `ValuesFrom' needs at least one argument.");
711 for (int i = 0; i < ci->values_num; i++)
712 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
713 WARNING("curl_xml plugin: `ValuesFrom' needs only string argument.");
717 sfree(xpath->values);
719 xpath->values_len = 0;
720 xpath->values = malloc(sizeof(cx_values_t) * ci->values_num);
721 if (xpath->values == NULL)
723 xpath->values_len = (size_t)ci->values_num;
725 /* populate cx_values_t structure */
726 for (int i = 0; i < ci->values_num; i++) {
727 xpath->values[i].path_len = sizeof(ci->values[i].value.string);
728 sstrncpy(xpath->values[i].path, ci->values[i].value.string,
729 sizeof(xpath->values[i].path));
733 } /* }}} cx_config_add_values */
735 static int cx_config_add_xpath(cx_t *db, oconfig_item_t *ci) /* {{{ */
742 xpath = calloc(1, sizeof(*xpath));
744 ERROR("curl_xml plugin: calloc failed.");
748 status = cf_util_get_string(ci, &xpath->path);
750 cx_xpath_free(xpath);
754 /* error out if xpath->path is an empty string */
755 if (strlen(xpath->path) == 0) {
756 ERROR("curl_xml plugin: invalid xpath. "
757 "xpath value can't be an empty string");
758 cx_xpath_free(xpath);
763 for (int i = 0; i < ci->children_num; i++) {
764 oconfig_item_t *child = ci->children + i;
766 if (strcasecmp("Type", child->key) == 0)
767 status = cf_util_get_string(child, &xpath->type);
768 else if (strcasecmp("InstancePrefix", child->key) == 0)
769 status = cf_util_get_string(child, &xpath->instance_prefix);
770 else if (strcasecmp("InstanceFrom", child->key) == 0)
771 status = cf_util_get_string(child, &xpath->instance);
772 else if (strcasecmp("PluginInstanceFrom", child->key) == 0)
773 status = cf_util_get_string(child, &xpath->plugin_instance_from);
774 else if (strcasecmp("ValuesFrom", child->key) == 0)
775 status = cx_config_add_values("ValuesFrom", xpath, child);
777 WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
783 } /* for (i = 0; i < ci->children_num; i++) */
786 cx_xpath_free(xpath);
790 if (xpath->type == NULL) {
791 WARNING("curl_xml plugin: `Type' missing in `xpath' block.");
792 cx_xpath_free(xpath);
796 if (db->list == NULL) {
797 db->list = llist_create();
798 if (db->list == NULL) {
799 ERROR("curl_xml plugin: list creation failed.");
800 cx_xpath_free(xpath);
805 name = strdup(xpath->path);
807 ERROR("curl_xml plugin: strdup failed.");
808 cx_xpath_free(xpath);
812 le = llentry_create(name, xpath);
814 ERROR("curl_xml plugin: llentry_create failed.");
815 cx_xpath_free(xpath);
820 llist_append(db->list, le);
822 } /* }}} int cx_config_add_xpath */
824 static int cx_config_add_namespace(cx_t *db, /* {{{ */
825 oconfig_item_t *ci) {
828 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
829 (ci->values[1].type != OCONFIG_TYPE_STRING)) {
830 WARNING("curl_xml plugin: The `Namespace' option "
831 "needs exactly two string arguments.");
835 ns = realloc(db->namespaces,
836 sizeof(*db->namespaces) * (db->namespaces_num + 1));
838 ERROR("curl_xml plugin: realloc failed.");
842 ns = db->namespaces + db->namespaces_num;
843 memset(ns, 0, sizeof(*ns));
845 ns->prefix = strdup(ci->values[0].value.string);
846 ns->url = strdup(ci->values[1].value.string);
848 if ((ns->prefix == NULL) || (ns->url == NULL)) {
851 ERROR("curl_xml plugin: strdup failed.");
855 db->namespaces_num++;
857 } /* }}} int cx_config_add_namespace */
859 /* Initialize db->curl */
860 static int cx_init_curl(cx_t *db) /* {{{ */
862 db->curl = curl_easy_init();
863 if (db->curl == NULL) {
864 ERROR("curl_xml plugin: curl_easy_init failed.");
868 curl_easy_setopt(db->curl, CURLOPT_NOSIGNAL, 1L);
869 curl_easy_setopt(db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
870 curl_easy_setopt(db->curl, CURLOPT_WRITEDATA, db);
871 curl_easy_setopt(db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
872 curl_easy_setopt(db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
873 curl_easy_setopt(db->curl, CURLOPT_FOLLOWLOCATION, 1L);
874 curl_easy_setopt(db->curl, CURLOPT_MAXREDIRS, 50L);
876 if (db->user != NULL) {
877 #ifdef HAVE_CURLOPT_USERNAME
878 curl_easy_setopt(db->curl, CURLOPT_USERNAME, db->user);
879 curl_easy_setopt(db->curl, CURLOPT_PASSWORD,
880 (db->pass == NULL) ? "" : db->pass);
882 size_t credentials_size;
884 credentials_size = strlen(db->user) + 2;
885 if (db->pass != NULL)
886 credentials_size += strlen(db->pass);
888 db->credentials = malloc(credentials_size);
889 if (db->credentials == NULL) {
890 ERROR("curl_xml plugin: malloc failed.");
894 snprintf(db->credentials, credentials_size, "%s:%s", db->user,
895 (db->pass == NULL) ? "" : db->pass);
896 curl_easy_setopt(db->curl, CURLOPT_USERPWD, db->credentials);
900 curl_easy_setopt(db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
903 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
904 curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYHOST, db->verify_host ? 2L : 0L);
905 if (db->cacert != NULL)
906 curl_easy_setopt(db->curl, CURLOPT_CAINFO, db->cacert);
907 if (db->headers != NULL)
908 curl_easy_setopt(db->curl, CURLOPT_HTTPHEADER, db->headers);
909 if (db->post_body != NULL)
910 curl_easy_setopt(db->curl, CURLOPT_POSTFIELDS, db->post_body);
912 #ifdef HAVE_CURLOPT_TIMEOUT_MS
913 if (db->timeout >= 0)
914 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS, (long)db->timeout);
916 curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
917 (long)CDTIME_T_TO_MS(plugin_get_interval()));
921 } /* }}} int cx_init_curl */
923 static int cx_config_add_url(oconfig_item_t *ci) /* {{{ */
928 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
929 WARNING("curl_xml plugin: The `URL' block "
930 "needs exactly one string argument.");
934 db = calloc(1, sizeof(*db));
936 ERROR("curl_xml plugin: calloc failed.");
942 if (strcasecmp("URL", ci->key) == 0) {
943 status = cf_util_get_string(ci, &db->url);
949 ERROR("curl_xml plugin: cx_config: "
956 /* Fill the `cx_t' structure.. */
957 for (int i = 0; i < ci->children_num; i++) {
958 oconfig_item_t *child = ci->children + i;
960 if (strcasecmp("Instance", child->key) == 0)
961 status = cf_util_get_string(child, &db->instance);
962 else if (strcasecmp("PluginName", child->key) == 0)
963 status = cf_util_get_string(child, &db->plugin_name);
964 else if (strcasecmp("Host", child->key) == 0)
965 status = cf_util_get_string(child, &db->host);
966 else if (strcasecmp("User", child->key) == 0)
967 status = cf_util_get_string(child, &db->user);
968 else if (strcasecmp("Password", child->key) == 0)
969 status = cf_util_get_string(child, &db->pass);
970 else if (strcasecmp("Digest", child->key) == 0)
971 status = cf_util_get_boolean(child, &db->digest);
972 else if (strcasecmp("VerifyPeer", child->key) == 0)
973 status = cf_util_get_boolean(child, &db->verify_peer);
974 else if (strcasecmp("VerifyHost", child->key) == 0)
975 status = cf_util_get_boolean(child, &db->verify_host);
976 else if (strcasecmp("CACert", child->key) == 0)
977 status = cf_util_get_string(child, &db->cacert);
978 else if (strcasecmp("xpath", child->key) == 0)
979 status = cx_config_add_xpath(db, child);
980 else if (strcasecmp("Header", child->key) == 0)
981 status = cx_config_append_string("Header", &db->headers, child);
982 else if (strcasecmp("Post", child->key) == 0)
983 status = cf_util_get_string(child, &db->post_body);
984 else if (strcasecmp("Namespace", child->key) == 0)
985 status = cx_config_add_namespace(db, child);
986 else if (strcasecmp("Timeout", child->key) == 0)
987 status = cf_util_get_int(child, &db->timeout);
988 else if (strcasecmp("Statistics", child->key) == 0) {
989 db->stats = curl_stats_from_config(child);
990 if (db->stats == NULL)
993 WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
1002 if (db->list == NULL) {
1003 WARNING("curl_xml plugin: No (valid) `Key' block "
1004 "within `URL' block `%s'.",
1009 status = cx_init_curl(db);
1012 /* If all went well, register this database for reading */
1016 if (db->instance == NULL)
1017 db->instance = strdup("default");
1019 DEBUG("curl_xml plugin: Registering new read callback: %s", db->instance);
1021 cb_name = ssnprintf_alloc("curl_xml-%s-%s", db->instance, db->url);
1023 plugin_register_complex_read(/* group = */ "curl_xml", cb_name, cx_read,
1026 .data = db, .free_func = cx_free,
1035 } /* }}} int cx_config_add_url */
1037 /* }}} End of configuration handling functions */
1039 static int cx_config(oconfig_item_t *ci) /* {{{ */
1048 for (int i = 0; i < ci->children_num; i++) {
1049 oconfig_item_t *child = ci->children + i;
1051 if (strcasecmp("URL", child->key) == 0) {
1052 status = cx_config_add_url(child);
1058 WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
1063 if ((success == 0) && (errors > 0)) {
1064 ERROR("curl_xml plugin: All statements failed.");
1069 } /* }}} int cx_config */
1071 static int cx_init(void) /* {{{ */
1073 /* Call this while collectd is still single-threaded to avoid
1074 * initialization issues in libgcrypt. */
1075 curl_global_init(CURL_GLOBAL_SSL);
1077 } /* }}} int cx_init */
1079 void module_register(void) {
1080 plugin_register_complex_config("curl_xml", cx_config);
1081 plugin_register_init("curl_xml", cx_init);
1082 } /* void module_register */