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 "configfile.h"
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;
60 typedef struct cx_xpath_s cx_xpath_t;
63 struct cx_namespace_s /* {{{ */
68 typedef struct cx_namespace_s cx_namespace_t;
86 struct curl_slist *headers;
89 cx_namespace_t *namespaces;
90 size_t namespaces_num;
93 char curl_errbuf[CURL_ERROR_SIZE];
98 llist_t *list; /* list of xpath blocks */
100 typedef struct cx_s cx_t; /* }}} */
105 static size_t cx_curl_callback (void *buf, /* {{{ */
106 size_t size, size_t nmemb, void *user_data)
108 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)
126 temp = realloc (db->buffer,
127 db->buffer_fill + len + 1);
130 ERROR ("curl_xml plugin: realloc failed.");
134 db->buffer_size = db->buffer_fill + len + 1;
137 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
138 db->buffer_fill += len;
139 db->buffer[db->buffer_fill] = 0;
142 } /* }}} size_t cx_curl_callback */
144 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
151 sfree (xpath->instance_prefix);
152 sfree (xpath->instance);
153 sfree (xpath->values);
155 } /* }}} void cx_xpath_free */
157 static void cx_list_free (llist_t *list) /* {{{ */
161 le = llist_head (list);
169 cx_xpath_free (le->value);
174 llist_destroy (list);
175 } /* }}} void cx_list_free */
177 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 (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];
373 assert (xpath->values_len > 0);
374 assert (xpath->values_len == vl->values_len);
375 assert (xpath->values_len == ds->ds_num);
378 for (i = 0; i < xpath->values_len; i++)
380 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
382 return (-1); /* An error has been printed. */
383 } /* for (i = 0; i < xpath->values_len; i++) */
385 plugin_dispatch_values (vl);
389 } /* }}} int cx_handle_all_value_xpaths */
391 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
392 cx_xpath_t *xpath, value_list_t *vl,
395 xmlXPathObjectPtr instance_node_obj = NULL;
396 xmlNodeSetPtr instance_node = NULL;
398 memset (vl->type_instance, 0, sizeof (vl->type_instance));
400 /* If the base xpath returns more than one block, the result is assumed to be
401 * a table. The `Instance' option is not optional in this case. Check for the
402 * condition and inform the user. */
403 if (is_table && (xpath->instance == NULL))
405 WARNING ("curl_xml plugin: "
406 "Base-XPath %s is a table (more than one result was returned), "
407 "but no instance-XPath has been defined.",
412 /* instance has to be an xpath expression */
413 if (xpath->instance != NULL)
417 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
418 if (instance_node_obj == NULL)
419 return (-1); /* error is logged already */
421 instance_node = instance_node_obj->nodesetval;
422 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
426 WARNING ("curl_xml plugin: "
427 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
428 "any of the nodes. Skipping the node.", xpath->instance);
429 xmlXPathFreeObject (instance_node_obj);
435 WARNING ("curl_xml plugin: "
436 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
437 "to return only one text node. Skipping the node.", xpath->instance);
438 xmlXPathFreeObject (instance_node_obj);
442 /* ignoring the element if other than textnode/attribute */
443 if (cx_if_not_text_node(instance_node->nodeTab[0]))
445 WARNING ("curl_xml plugin: "
446 "relative xpath expression \"%s\" is expected to return only text node "
447 "which is not the case. Skipping the node.", xpath->instance);
448 xmlXPathFreeObject (instance_node_obj);
451 } /* if (xpath->instance != NULL) */
453 if (xpath->instance_prefix != NULL)
455 if (instance_node != NULL)
457 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
458 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
459 xpath->instance_prefix, node_value);
463 sstrncpy (vl->type_instance, xpath->instance_prefix,
464 sizeof (vl->type_instance));
468 /* If instance_prefix and instance_node are NULL, then
469 * don't set the type_instance */
470 if (instance_node != NULL)
472 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
473 sstrncpy (vl->type_instance, node_value, sizeof (vl->type_instance));
478 /* Free `instance_node_obj' this late, because `instance_node' points to
479 * somewhere inside this structure. */
480 xmlXPathFreeObject (instance_node_obj);
483 } /* }}} int cx_handle_instance_xpath */
485 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
487 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
488 char *base_xpath, cx_xpath_t *xpath)
493 xmlXPathObjectPtr base_node_obj = NULL;
494 xmlNodeSetPtr base_nodes = NULL;
496 value_list_t vl = VALUE_LIST_INIT;
498 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
499 if (base_node_obj == NULL)
500 return -1; /* error is logged already */
502 base_nodes = base_node_obj->nodesetval;
503 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
505 if (total_nodes == 0)
507 ERROR ("curl_xml plugin: "
508 "xpath expression \"%s\" doesn't match any of the nodes. "
509 "Skipping the xpath block...", base_xpath);
510 xmlXPathFreeObject (base_node_obj);
514 /* If base_xpath returned multiple results, then */
515 /* Instance in the xpath block is required */
516 if (total_nodes > 1 && xpath->instance == NULL)
518 ERROR ("curl_xml plugin: "
519 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
520 "returned multiple results. Skipping the xpath block...", base_xpath);
524 /* set the values for the value_list */
525 vl.values_len = ds->ds_num;
526 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
527 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
528 sstrncpy (vl.host, host, sizeof (vl.host));
529 if (plugin_instance != NULL)
530 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
532 for (i = 0; i < total_nodes; i++)
536 xpath_ctx->node = base_nodes->nodeTab[i];
538 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
539 /* is_table = */ (total_nodes > 1));
541 continue; /* An error has already been reported. */
543 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
545 continue; /* An error has been logged. */
546 } /* for (i = 0; i < total_nodes; i++) */
548 /* free up the allocated memory */
549 xmlXPathFreeObject (base_node_obj);
552 } /* }}} cx_handle_base_xpath */
554 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
555 xmlXPathContextPtr xpath_ctx, cx_t *db)
558 const data_set_t *ds;
563 le = llist_head (db->list);
567 xpath = (cx_xpath_t *) le->value;
568 ds = plugin_get_ds (xpath->type);
570 if ( (cx_check_type(ds, xpath) == 0) &&
571 (cx_handle_base_xpath(db->instance, cx_host (db),
572 xpath_ctx, ds, le->key, xpath) == 0) )
573 status = 0; /* we got atleast one success */
576 } /* while (le != NULL) */
579 } /* }}} cx_handle_parsed_xml */
581 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
585 xmlXPathContextPtr xpath_ctx;
589 doc = xmlParseDoc(xml);
592 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
596 xpath_ctx = xmlXPathNewContext(doc);
597 if(xpath_ctx == NULL)
599 ERROR ("curl_xml plugin: Failed to create the xml context");
604 for (i = 0; i < db->namespaces_num; i++)
606 cx_namespace_t const *ns = db->namespaces + i;
607 status = xmlXPathRegisterNs (xpath_ctx,
608 BAD_CAST ns->prefix, BAD_CAST ns->url);
611 ERROR ("curl_xml plugin: "
612 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
613 ns->prefix, ns->url);
614 xmlXPathFreeContext(xpath_ctx);
620 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
622 xmlXPathFreeContext(xpath_ctx);
625 } /* }}} cx_parse_stats_xml */
627 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
636 status = curl_easy_perform (curl);
637 if (status != CURLE_OK)
639 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
640 status, db->curl_errbuf, url);
643 if (db->stats != NULL)
644 curl_stats_dispatch (db->stats, db->curl, cx_host (db), "curl_xml", db->instance);
646 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
647 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
649 /* The response code is zero if a non-HTTP transport was used. */
650 if ((rc != 0) && (rc != 200))
652 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
659 status = cx_parse_stats_xml(BAD_CAST ptr, db);
663 } /* }}} int cx_curl_perform */
665 static int cx_read (user_data_t *ud) /* {{{ */
669 if ((ud == NULL) || (ud->data == NULL))
671 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
675 db = (cx_t *) ud->data;
677 return cx_curl_perform (db, db->curl);
678 } /* }}} int cx_read */
680 /* Configuration handling functions {{{ */
682 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
687 if (ci->values_num < 1)
689 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
693 for (i = 0; i < ci->values_num; i++)
694 if (ci->values[i].type != OCONFIG_TYPE_STRING)
696 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
700 sfree (xpath->values);
702 xpath->values_len = 0;
703 xpath->values = malloc (sizeof (cx_values_t) * ci->values_num);
704 if (xpath->values == NULL)
706 xpath->values_len = (size_t) ci->values_num;
708 /* populate cx_values_t structure */
709 for (i = 0; i < ci->values_num; i++)
711 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
712 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
716 } /* }}} cx_config_add_values */
718 static int cx_config_add_xpath (cx_t *db, oconfig_item_t *ci) /* {{{ */
726 xpath = calloc (1, sizeof (*xpath));
729 ERROR ("curl_xml plugin: calloc failed.");
733 status = cf_util_get_string (ci, &xpath->path);
736 cx_xpath_free (xpath);
740 /* error out if xpath->path is an empty string */
741 if (strlen (xpath->path) == 0)
743 ERROR ("curl_xml plugin: invalid xpath. "
744 "xpath value can't be an empty string");
745 cx_xpath_free (xpath);
750 for (i = 0; i < ci->children_num; i++)
752 oconfig_item_t *child = ci->children + i;
754 if (strcasecmp ("Type", child->key) == 0)
755 status = cf_util_get_string (child, &xpath->type);
756 else if (strcasecmp ("InstancePrefix", child->key) == 0)
757 status = cf_util_get_string (child, &xpath->instance_prefix);
758 else if (strcasecmp ("InstanceFrom", child->key) == 0)
759 status = cf_util_get_string (child, &xpath->instance);
760 else if (strcasecmp ("ValuesFrom", child->key) == 0)
761 status = cx_config_add_values ("ValuesFrom", xpath, child);
764 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
770 } /* for (i = 0; i < ci->children_num; i++) */
774 cx_xpath_free (xpath);
778 if (xpath->type == NULL)
780 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
781 cx_xpath_free (xpath);
785 if (db->list == NULL)
787 db->list = llist_create();
788 if (db->list == NULL)
790 ERROR ("curl_xml plugin: list creation failed.");
791 cx_xpath_free (xpath);
796 name = strdup (xpath->path);
799 ERROR ("curl_xml plugin: strdup failed.");
800 cx_xpath_free (xpath);
804 le = llentry_create (name, xpath);
807 ERROR ("curl_xml plugin: llentry_create failed.");
808 cx_xpath_free (xpath);
813 llist_append (db->list, le);
815 } /* }}} int cx_config_add_xpath */
817 static int cx_config_add_namespace (cx_t *db, /* {{{ */
822 if ((ci->values_num != 2)
823 || (ci->values[0].type != OCONFIG_TYPE_STRING)
824 || (ci->values[1].type != OCONFIG_TYPE_STRING))
826 WARNING ("curl_xml plugin: The `Namespace' option "
827 "needs exactly two string arguments.");
831 ns = realloc (db->namespaces, sizeof (*db->namespaces)
832 * (db->namespaces_num + 1));
835 ERROR ("curl_xml plugin: realloc failed.");
839 ns = db->namespaces + db->namespaces_num;
840 memset (ns, 0, sizeof (*ns));
842 ns->prefix = strdup (ci->values[0].value.string);
843 ns->url = strdup (ci->values[1].value.string);
845 if ((ns->prefix == NULL) || (ns->url == NULL))
849 ERROR ("curl_xml plugin: strdup failed.");
853 db->namespaces_num++;
855 } /* }}} int cx_config_add_namespace */
857 /* Initialize db->curl */
858 static int cx_init_curl (cx_t *db) /* {{{ */
860 db->curl = curl_easy_init ();
861 if (db->curl == NULL)
863 ERROR ("curl_xml plugin: curl_easy_init failed.");
867 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
868 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
869 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
870 curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
871 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
872 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
873 curl_easy_setopt (db->curl, CURLOPT_FOLLOWLOCATION, 1L);
874 curl_easy_setopt (db->curl, CURLOPT_MAXREDIRS, 50L);
876 if (db->user != NULL)
878 #ifdef HAVE_CURLOPT_USERNAME
879 curl_easy_setopt (db->curl, CURLOPT_USERNAME, db->user);
880 curl_easy_setopt (db->curl, CURLOPT_PASSWORD,
881 (db->pass == NULL) ? "" : db->pass);
883 size_t credentials_size;
885 credentials_size = strlen (db->user) + 2;
886 if (db->pass != NULL)
887 credentials_size += strlen (db->pass);
889 db->credentials = malloc (credentials_size);
890 if (db->credentials == NULL)
892 ERROR ("curl_xml plugin: malloc failed.");
896 ssnprintf (db->credentials, credentials_size, "%s:%s",
897 db->user, (db->pass == NULL) ? "" : db->pass);
898 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
902 curl_easy_setopt (db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
905 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
906 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
907 db->verify_host ? 2L : 0L);
908 if (db->cacert != NULL)
909 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
910 if (db->headers != NULL)
911 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
912 if (db->post_body != NULL)
913 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
915 #ifdef HAVE_CURLOPT_TIMEOUT_MS
916 if (db->timeout >= 0)
917 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout);
919 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
923 } /* }}} int cx_init_curl */
925 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
931 if ((ci->values_num != 1)
932 || (ci->values[0].type != OCONFIG_TYPE_STRING))
934 WARNING ("curl_xml plugin: The `URL' block "
935 "needs exactly one string argument.");
939 db = calloc (1, sizeof (*db));
942 ERROR ("curl_xml plugin: calloc failed.");
948 if (strcasecmp ("URL", ci->key) == 0)
950 status = cf_util_get_string (ci, &db->url);
959 ERROR ("curl_xml plugin: cx_config: "
960 "Invalid key: %s", ci->key);
965 /* Fill the `cx_t' structure.. */
966 for (i = 0; i < ci->children_num; i++)
968 oconfig_item_t *child = ci->children + i;
970 if (strcasecmp ("Instance", child->key) == 0)
971 status = cf_util_get_string (child, &db->instance);
972 else if (strcasecmp ("Host", child->key) == 0)
973 status = cf_util_get_string (child, &db->host);
974 else if (strcasecmp ("User", child->key) == 0)
975 status = cf_util_get_string (child, &db->user);
976 else if (strcasecmp ("Password", child->key) == 0)
977 status = cf_util_get_string (child, &db->pass);
978 else if (strcasecmp ("Digest", child->key) == 0)
979 status = cf_util_get_boolean (child, &db->digest);
980 else if (strcasecmp ("VerifyPeer", child->key) == 0)
981 status = cf_util_get_boolean (child, &db->verify_peer);
982 else if (strcasecmp ("VerifyHost", child->key) == 0)
983 status = cf_util_get_boolean (child, &db->verify_host);
984 else if (strcasecmp ("CACert", child->key) == 0)
985 status = cf_util_get_string (child, &db->cacert);
986 else if (strcasecmp ("xpath", child->key) == 0)
987 status = cx_config_add_xpath (db, child);
988 else if (strcasecmp ("Header", child->key) == 0)
989 status = cx_config_append_string ("Header", &db->headers, child);
990 else if (strcasecmp ("Post", child->key) == 0)
991 status = cf_util_get_string (child, &db->post_body);
992 else if (strcasecmp ("Namespace", child->key) == 0)
993 status = cx_config_add_namespace (db, child);
994 else if (strcasecmp ("Timeout", child->key) == 0)
995 status = cf_util_get_int (child, &db->timeout);
996 else if (strcasecmp ("Statistics", child->key) == 0)
998 db->stats = curl_stats_from_config (child);
999 if (db->stats == NULL)
1004 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1014 if (db->list == NULL)
1016 WARNING ("curl_xml plugin: No (valid) `Key' block "
1017 "within `URL' block `%s'.", db->url);
1021 status = cx_init_curl (db);
1024 /* If all went well, register this database for reading */
1030 if (db->instance == NULL)
1031 db->instance = strdup("default");
1033 DEBUG ("curl_xml plugin: Registering new read callback: %s",
1036 memset (&ud, 0, sizeof (ud));
1037 ud.data = (void *) db;
1038 ud.free_func = cx_free;
1040 cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url);
1041 plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read,
1042 /* interval = */ 0, &ud);
1052 } /* }}} int cx_config_add_url */
1054 /* }}} End of configuration handling functions */
1056 static int cx_config (oconfig_item_t *ci) /* {{{ */
1066 for (i = 0; i < ci->children_num; i++)
1068 oconfig_item_t *child = ci->children + i;
1070 if (strcasecmp ("URL", child->key) == 0)
1072 status = cx_config_add_url (child);
1080 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1085 if ((success == 0) && (errors > 0))
1087 ERROR ("curl_xml plugin: All statements failed.");
1092 } /* }}} int cx_config */
1094 static int cx_init (void) /* {{{ */
1096 /* Call this while collectd is still single-threaded to avoid
1097 * initialization issues in libgcrypt. */
1098 curl_global_init (CURL_GLOBAL_SSL);
1100 } /* }}} int cx_init */
1102 void module_register (void)
1104 plugin_register_complex_config ("curl_xml", cx_config);
1105 plugin_register_init ("curl_xml", cx_init);
1106 } /* void module_register */
1108 /* vim: set sw=2 sts=2 et fdm=marker : */