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 "configfile.h"
27 #include "utils_curl_stats.h"
28 #include "utils_llist.h"
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <libxml/xpath.h>
33 #include <libxml/xpathInternals.h>
35 #include <curl/curl.h>
37 #define CX_DEFAULT_HOST "localhost"
40 * Private data structures
42 struct cx_values_s /* {{{ */
44 char path[DATA_MAX_NAME_LEN];
47 typedef struct cx_values_s cx_values_t;
50 struct cx_xpath_s /* {{{ */
56 char *instance_prefix;
61 typedef struct cx_xpath_s cx_xpath_t;
64 struct cx_namespace_s /* {{{ */
69 typedef struct cx_namespace_s cx_namespace_t;
87 struct curl_slist *headers;
90 cx_namespace_t *namespaces;
91 size_t namespaces_num;
94 char curl_errbuf[CURL_ERROR_SIZE];
99 llist_t *list; /* list of xpath blocks */
101 typedef struct cx_s cx_t; /* }}} */
106 static size_t cx_curl_callback (void *buf, /* {{{ */
107 size_t size, size_t nmemb, void *user_data)
109 size_t len = size * nmemb;
115 ERROR ("curl_xml plugin: cx_curl_callback: "
116 "user_data pointer is NULL.");
123 if ((db->buffer_fill + len) >= db->buffer_size)
127 temp = realloc (db->buffer,
128 db->buffer_fill + len + 1);
131 ERROR ("curl_xml plugin: realloc failed.");
135 db->buffer_size = db->buffer_fill + len + 1;
138 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
139 db->buffer_fill += len;
140 db->buffer[db->buffer_fill] = 0;
143 } /* }}} size_t cx_curl_callback */
145 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
152 sfree (xpath->instance_prefix);
153 sfree (xpath->instance);
154 sfree (xpath->values);
156 } /* }}} void cx_xpath_free */
158 static void cx_list_free (llist_t *list) /* {{{ */
162 le = llist_head (list);
170 cx_xpath_free (le->value);
175 llist_destroy (list);
176 } /* }}} void cx_list_free */
178 static void cx_free (void *arg) /* {{{ */
182 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
189 if (db->curl != NULL)
190 curl_easy_cleanup (db->curl);
193 if (db->list != NULL)
194 cx_list_free (db->list);
197 sfree (db->instance);
203 sfree (db->credentials);
205 sfree (db->post_body);
206 curl_slist_free_all (db->headers);
207 curl_stats_destroy (db->stats);
209 for (size_t i = 0; i < db->namespaces_num; i++)
211 sfree (db->namespaces[i].prefix);
212 sfree (db->namespaces[i].url);
214 sfree (db->namespaces);
217 } /* }}} void cx_free */
219 static const char *cx_host (cx_t *db) /* {{{ */
221 if (db->host == NULL)
226 static int cx_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
229 struct curl_slist *temp = NULL;
230 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
232 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
236 temp = curl_slist_append(*dest, ci->values[0].value.string);
243 } /* }}} int cx_config_append_string */
245 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
249 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
253 if (ds->ds_num != xpath->values_len)
255 WARNING ("curl_xml plugin: DataSet `%s' requires %zu values, but config talks about %zu",
256 xpath->type, ds->ds_num, xpath->values_len);
261 } /* }}} cx_check_type */
263 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
266 xmlXPathObjectPtr xpath_obj;
268 /* XXX: When to free this? */
269 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
270 if (xpath_obj == NULL)
272 WARNING ("curl_xml plugin: "
273 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
278 } /* }}} cx_evaluate_xpath */
280 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
282 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
283 node->type == XML_ELEMENT_NODE)
286 WARNING ("curl_xml plugin: "
287 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
289 } /* }}} cx_if_not_text_node */
291 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
293 const data_set_t *ds, value_list_t *vl, int index)
295 xmlXPathObjectPtr values_node_obj;
296 xmlNodeSetPtr values_node;
300 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
301 if (values_node_obj == NULL)
302 return (-1); /* Error already logged. */
304 values_node = values_node_obj->nodesetval;
305 tmp_size = (values_node) ? values_node->nodeNr : 0;
309 WARNING ("curl_xml plugin: "
310 "relative xpath expression \"%s\" doesn't match any of the nodes. "
311 "Skipping...", xpath->values[index].path);
312 xmlXPathFreeObject (values_node_obj);
318 WARNING ("curl_xml plugin: "
319 "relative xpath expression \"%s\" is expected to return "
320 "only one node. Skipping...", xpath->values[index].path);
321 xmlXPathFreeObject (values_node_obj);
325 /* ignoring the element if other than textnode/attribute*/
326 if (cx_if_not_text_node(values_node->nodeTab[0]))
328 WARNING ("curl_xml plugin: "
329 "relative xpath expression \"%s\" is expected to return "
330 "only text/attribute node which is not the case. Skipping...",
331 xpath->values[index].path);
332 xmlXPathFreeObject (values_node_obj);
336 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
337 switch (ds->ds[index].type)
339 case DS_TYPE_COUNTER:
340 vl->values[index].counter = (counter_t) strtoull (node_value,
341 /* endptr = */ NULL, /* base = */ 0);
344 vl->values[index].derive = (derive_t) strtoll (node_value,
345 /* endptr = */ NULL, /* base = */ 0);
347 case DS_TYPE_ABSOLUTE:
348 vl->values[index].absolute = (absolute_t) strtoull (node_value,
349 /* endptr = */ NULL, /* base = */ 0);
352 vl->values[index].gauge = (gauge_t) strtod (node_value,
353 /* endptr = */ NULL);
357 xmlXPathFreeObject (values_node_obj);
360 /* We have reached here which means that
361 * we have got something to work */
363 } /* }}} int cx_handle_single_value_xpath */
365 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
367 const data_set_t *ds, value_list_t *vl)
369 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++)
379 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
381 return (-1); /* An error has been printed. */
382 } /* for (i = 0; i < xpath->values_len; i++) */
384 plugin_dispatch_values (vl);
388 } /* }}} int cx_handle_all_value_xpaths */
390 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
391 cx_xpath_t *xpath, value_list_t *vl,
394 xmlXPathObjectPtr instance_node_obj = NULL;
395 xmlNodeSetPtr instance_node = NULL;
397 memset (vl->type_instance, 0, sizeof (vl->type_instance));
399 /* If the base xpath returns more than one block, the result is assumed to be
400 * a table. The `Instance' option is not optional in this case. Check for the
401 * condition and inform the user. */
402 if (is_table && (xpath->instance == NULL))
404 WARNING ("curl_xml plugin: "
405 "Base-XPath %s is a table (more than one result was returned), "
406 "but no instance-XPath has been defined.",
411 /* instance has to be an xpath expression */
412 if (xpath->instance != NULL)
416 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
417 if (instance_node_obj == NULL)
418 return (-1); /* error is logged already */
420 instance_node = instance_node_obj->nodesetval;
421 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
425 WARNING ("curl_xml plugin: "
426 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
427 "any of the nodes. Skipping the node.", xpath->instance);
428 xmlXPathFreeObject (instance_node_obj);
434 WARNING ("curl_xml plugin: "
435 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
436 "to return only one text node. Skipping the node.", xpath->instance);
437 xmlXPathFreeObject (instance_node_obj);
441 /* ignoring the element if other than textnode/attribute */
442 if (cx_if_not_text_node(instance_node->nodeTab[0]))
444 WARNING ("curl_xml plugin: "
445 "relative xpath expression \"%s\" is expected to return only text node "
446 "which is not the case. Skipping the node.", xpath->instance);
447 xmlXPathFreeObject (instance_node_obj);
450 } /* if (xpath->instance != NULL) */
452 if (xpath->instance_prefix != NULL)
454 if (instance_node != NULL)
456 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
457 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
458 xpath->instance_prefix, node_value);
462 sstrncpy (vl->type_instance, xpath->instance_prefix,
463 sizeof (vl->type_instance));
467 /* If instance_prefix and instance_node are NULL, then
468 * don't set the type_instance */
469 if (instance_node != NULL)
471 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
472 sstrncpy (vl->type_instance, node_value, sizeof (vl->type_instance));
477 /* Free `instance_node_obj' this late, because `instance_node' points to
478 * somewhere inside this structure. */
479 xmlXPathFreeObject (instance_node_obj);
482 } /* }}} int cx_handle_instance_xpath */
484 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
486 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
487 char *base_xpath, cx_xpath_t *xpath)
491 xmlXPathObjectPtr base_node_obj = NULL;
492 xmlNodeSetPtr base_nodes = NULL;
494 value_list_t vl = VALUE_LIST_INIT;
496 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
497 if (base_node_obj == NULL)
498 return -1; /* error is logged already */
500 base_nodes = base_node_obj->nodesetval;
501 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
503 if (total_nodes == 0)
505 ERROR ("curl_xml plugin: "
506 "xpath expression \"%s\" doesn't match any of the nodes. "
507 "Skipping the xpath block...", base_xpath);
508 xmlXPathFreeObject (base_node_obj);
512 /* If base_xpath returned multiple results, then */
513 /* Instance in the xpath block is required */
514 if (total_nodes > 1 && xpath->instance == NULL)
516 ERROR ("curl_xml plugin: "
517 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
518 "returned multiple results. Skipping the xpath block...", base_xpath);
522 /* set the values for the value_list */
523 vl.values_len = ds->ds_num;
524 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
525 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
526 sstrncpy (vl.host, host, sizeof (vl.host));
527 if (plugin_instance != NULL)
528 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
530 for (int i = 0; i < total_nodes; i++)
534 xpath_ctx->node = base_nodes->nodeTab[i];
536 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
537 /* is_table = */ (total_nodes > 1));
539 continue; /* An error has already been reported. */
541 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
543 continue; /* An error has been logged. */
544 } /* for (i = 0; i < total_nodes; i++) */
546 /* free up the allocated memory */
547 xmlXPathFreeObject (base_node_obj);
550 } /* }}} cx_handle_base_xpath */
552 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
553 xmlXPathContextPtr xpath_ctx, cx_t *db)
556 const data_set_t *ds;
561 le = llist_head (db->list);
565 xpath = (cx_xpath_t *) le->value;
566 ds = plugin_get_ds (xpath->type);
568 if ( (cx_check_type(ds, xpath) == 0) &&
569 (cx_handle_base_xpath(db->instance, cx_host (db),
570 xpath_ctx, ds, le->key, xpath) == 0) )
571 status = 0; /* we got atleast one success */
574 } /* while (le != NULL) */
577 } /* }}} cx_handle_parsed_xml */
579 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
583 xmlXPathContextPtr xpath_ctx;
586 doc = xmlParseDoc(xml);
589 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
593 xpath_ctx = xmlXPathNewContext(doc);
594 if(xpath_ctx == NULL)
596 ERROR ("curl_xml plugin: Failed to create the xml context");
601 for (size_t i = 0; i < db->namespaces_num; i++)
603 cx_namespace_t const *ns = db->namespaces + i;
604 status = xmlXPathRegisterNs (xpath_ctx,
605 BAD_CAST ns->prefix, BAD_CAST ns->url);
608 ERROR ("curl_xml plugin: "
609 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
610 ns->prefix, ns->url);
611 xmlXPathFreeContext(xpath_ctx);
617 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
619 xmlXPathFreeContext(xpath_ctx);
622 } /* }}} cx_parse_stats_xml */
624 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
633 status = curl_easy_perform (curl);
634 if (status != CURLE_OK)
636 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
637 status, db->curl_errbuf, url);
640 if (db->stats != NULL)
641 curl_stats_dispatch (db->stats, db->curl, cx_host (db), "curl_xml", db->instance);
643 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
644 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
646 /* The response code is zero if a non-HTTP transport was used. */
647 if ((rc != 0) && (rc != 200))
649 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
656 status = cx_parse_stats_xml(BAD_CAST ptr, db);
660 } /* }}} int cx_curl_perform */
662 static int cx_read (user_data_t *ud) /* {{{ */
666 if ((ud == NULL) || (ud->data == NULL))
668 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
672 db = (cx_t *) ud->data;
674 return cx_curl_perform (db, db->curl);
675 } /* }}} int cx_read */
677 /* Configuration handling functions {{{ */
679 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
682 if (ci->values_num < 1)
684 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
688 for (int i = 0; i < ci->values_num; i++)
689 if (ci->values[i].type != OCONFIG_TYPE_STRING)
691 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
695 sfree (xpath->values);
697 xpath->values_len = 0;
698 xpath->values = malloc (sizeof (cx_values_t) * ci->values_num);
699 if (xpath->values == NULL)
701 xpath->values_len = (size_t) ci->values_num;
703 /* populate cx_values_t structure */
704 for (int i = 0; i < ci->values_num; i++)
706 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
707 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
711 } /* }}} cx_config_add_values */
713 static int cx_config_add_xpath (cx_t *db, oconfig_item_t *ci) /* {{{ */
720 xpath = calloc (1, sizeof (*xpath));
723 ERROR ("curl_xml plugin: calloc failed.");
727 status = cf_util_get_string (ci, &xpath->path);
730 cx_xpath_free (xpath);
734 /* error out if xpath->path is an empty string */
735 if (strlen (xpath->path) == 0)
737 ERROR ("curl_xml plugin: invalid xpath. "
738 "xpath value can't be an empty string");
739 cx_xpath_free (xpath);
744 for (int i = 0; i < ci->children_num; i++)
746 oconfig_item_t *child = ci->children + i;
748 if (strcasecmp ("Type", child->key) == 0)
749 status = cf_util_get_string (child, &xpath->type);
750 else if (strcasecmp ("InstancePrefix", child->key) == 0)
751 status = cf_util_get_string (child, &xpath->instance_prefix);
752 else if (strcasecmp ("InstanceFrom", child->key) == 0)
753 status = cf_util_get_string (child, &xpath->instance);
754 else if (strcasecmp ("ValuesFrom", child->key) == 0)
755 status = cx_config_add_values ("ValuesFrom", xpath, child);
758 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
764 } /* for (i = 0; i < ci->children_num; i++) */
768 cx_xpath_free (xpath);
772 if (xpath->type == NULL)
774 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
775 cx_xpath_free (xpath);
779 if (db->list == NULL)
781 db->list = llist_create();
782 if (db->list == NULL)
784 ERROR ("curl_xml plugin: list creation failed.");
785 cx_xpath_free (xpath);
790 name = strdup (xpath->path);
793 ERROR ("curl_xml plugin: strdup failed.");
794 cx_xpath_free (xpath);
798 le = llentry_create (name, xpath);
801 ERROR ("curl_xml plugin: llentry_create failed.");
802 cx_xpath_free (xpath);
807 llist_append (db->list, le);
809 } /* }}} int cx_config_add_xpath */
811 static int cx_config_add_namespace (cx_t *db, /* {{{ */
816 if ((ci->values_num != 2)
817 || (ci->values[0].type != OCONFIG_TYPE_STRING)
818 || (ci->values[1].type != OCONFIG_TYPE_STRING))
820 WARNING ("curl_xml plugin: The `Namespace' option "
821 "needs exactly two string arguments.");
825 ns = realloc (db->namespaces, sizeof (*db->namespaces)
826 * (db->namespaces_num + 1));
829 ERROR ("curl_xml plugin: realloc failed.");
833 ns = db->namespaces + db->namespaces_num;
834 memset (ns, 0, sizeof (*ns));
836 ns->prefix = strdup (ci->values[0].value.string);
837 ns->url = strdup (ci->values[1].value.string);
839 if ((ns->prefix == NULL) || (ns->url == NULL))
843 ERROR ("curl_xml plugin: strdup failed.");
847 db->namespaces_num++;
849 } /* }}} int cx_config_add_namespace */
851 /* Initialize db->curl */
852 static int cx_init_curl (cx_t *db) /* {{{ */
854 db->curl = curl_easy_init ();
855 if (db->curl == NULL)
857 ERROR ("curl_xml plugin: curl_easy_init failed.");
861 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
862 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
863 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
864 curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
865 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
866 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
867 curl_easy_setopt (db->curl, CURLOPT_FOLLOWLOCATION, 1L);
868 curl_easy_setopt (db->curl, CURLOPT_MAXREDIRS, 50L);
870 if (db->user != NULL)
872 #ifdef HAVE_CURLOPT_USERNAME
873 curl_easy_setopt (db->curl, CURLOPT_USERNAME, db->user);
874 curl_easy_setopt (db->curl, CURLOPT_PASSWORD,
875 (db->pass == NULL) ? "" : db->pass);
877 size_t credentials_size;
879 credentials_size = strlen (db->user) + 2;
880 if (db->pass != NULL)
881 credentials_size += strlen (db->pass);
883 db->credentials = malloc (credentials_size);
884 if (db->credentials == NULL)
886 ERROR ("curl_xml plugin: malloc failed.");
890 ssnprintf (db->credentials, credentials_size, "%s:%s",
891 db->user, (db->pass == NULL) ? "" : db->pass);
892 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
896 curl_easy_setopt (db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
899 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
900 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
901 db->verify_host ? 2L : 0L);
902 if (db->cacert != NULL)
903 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
904 if (db->headers != NULL)
905 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
906 if (db->post_body != NULL)
907 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
909 #ifdef HAVE_CURLOPT_TIMEOUT_MS
910 if (db->timeout >= 0)
911 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout);
913 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
917 } /* }}} int cx_init_curl */
919 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
924 if ((ci->values_num != 1)
925 || (ci->values[0].type != OCONFIG_TYPE_STRING))
927 WARNING ("curl_xml plugin: The `URL' block "
928 "needs exactly one string argument.");
932 db = calloc (1, sizeof (*db));
935 ERROR ("curl_xml plugin: calloc failed.");
941 if (strcasecmp ("URL", ci->key) == 0)
943 status = cf_util_get_string (ci, &db->url);
952 ERROR ("curl_xml plugin: cx_config: "
953 "Invalid key: %s", ci->key);
958 /* Fill the `cx_t' structure.. */
959 for (int i = 0; i < ci->children_num; i++)
961 oconfig_item_t *child = ci->children + i;
963 if (strcasecmp ("Instance", child->key) == 0)
964 status = cf_util_get_string (child, &db->instance);
965 else if (strcasecmp ("Host", child->key) == 0)
966 status = cf_util_get_string (child, &db->host);
967 else if (strcasecmp ("User", child->key) == 0)
968 status = cf_util_get_string (child, &db->user);
969 else if (strcasecmp ("Password", child->key) == 0)
970 status = cf_util_get_string (child, &db->pass);
971 else if (strcasecmp ("Digest", child->key) == 0)
972 status = cf_util_get_boolean (child, &db->digest);
973 else if (strcasecmp ("VerifyPeer", child->key) == 0)
974 status = cf_util_get_boolean (child, &db->verify_peer);
975 else if (strcasecmp ("VerifyHost", child->key) == 0)
976 status = cf_util_get_boolean (child, &db->verify_host);
977 else if (strcasecmp ("CACert", child->key) == 0)
978 status = cf_util_get_string (child, &db->cacert);
979 else if (strcasecmp ("xpath", child->key) == 0)
980 status = cx_config_add_xpath (db, child);
981 else if (strcasecmp ("Header", child->key) == 0)
982 status = cx_config_append_string ("Header", &db->headers, child);
983 else if (strcasecmp ("Post", child->key) == 0)
984 status = cf_util_get_string (child, &db->post_body);
985 else if (strcasecmp ("Namespace", child->key) == 0)
986 status = cx_config_add_namespace (db, child);
987 else if (strcasecmp ("Timeout", child->key) == 0)
988 status = cf_util_get_int (child, &db->timeout);
989 else if (strcasecmp ("Statistics", child->key) == 0)
991 db->stats = curl_stats_from_config (child);
992 if (db->stats == NULL)
997 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1007 if (db->list == NULL)
1009 WARNING ("curl_xml plugin: No (valid) `Key' block "
1010 "within `URL' block `%s'.", db->url);
1014 status = cx_init_curl (db);
1017 /* If all went well, register this database for reading */
1020 user_data_t ud = { 0 };
1023 if (db->instance == NULL)
1024 db->instance = strdup("default");
1026 DEBUG ("curl_xml plugin: Registering new read callback: %s",
1029 ud.data = (void *) db;
1030 ud.free_func = cx_free;
1032 cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url);
1033 plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read,
1034 /* interval = */ 0, &ud);
1044 } /* }}} int cx_config_add_url */
1046 /* }}} End of configuration handling functions */
1048 static int cx_config (oconfig_item_t *ci) /* {{{ */
1057 for (int i = 0; i < ci->children_num; i++)
1059 oconfig_item_t *child = ci->children + i;
1061 if (strcasecmp ("URL", child->key) == 0)
1063 status = cx_config_add_url (child);
1071 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1076 if ((success == 0) && (errors > 0))
1078 ERROR ("curl_xml plugin: All statements failed.");
1083 } /* }}} int cx_config */
1085 static int cx_init (void) /* {{{ */
1087 /* Call this while collectd is still single-threaded to avoid
1088 * initialization issues in libgcrypt. */
1089 curl_global_init (CURL_GLOBAL_SSL);
1091 } /* }}} int cx_init */
1093 void module_register (void)
1095 plugin_register_complex_config ("curl_xml", cx_config);
1096 plugin_register_init ("curl_xml", cx_init);
1097 } /* void module_register */
1099 /* vim: set sw=2 sts=2 et fdm=marker : */