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_llist.h"
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
30 #include <libxml/xpath.h>
31 #include <libxml/xpathInternals.h>
33 #include <curl/curl.h>
35 #define CX_DEFAULT_HOST "localhost"
38 * Private data structures
40 struct cx_values_s /* {{{ */
42 char path[DATA_MAX_NAME_LEN];
45 typedef struct cx_values_s cx_values_t;
48 struct cx_xpath_s /* {{{ */
54 char *instance_prefix;
59 typedef struct cx_xpath_s cx_xpath_t;
62 struct cx_namespace_s /* {{{ */
67 typedef struct cx_namespace_s cx_namespace_t;
85 struct curl_slist *headers;
87 cx_namespace_t *namespaces;
88 size_t namespaces_num;
91 char curl_errbuf[CURL_ERROR_SIZE];
96 llist_t *list; /* list of xpath blocks */
98 typedef struct cx_s cx_t; /* }}} */
103 static size_t cx_curl_callback (void *buf, /* {{{ */
104 size_t size, size_t nmemb, void *user_data)
106 size_t len = size * nmemb;
112 ERROR ("curl_xml plugin: cx_curl_callback: "
113 "user_data pointer is NULL.");
120 if ((db->buffer_fill + len) >= db->buffer_size)
124 temp = (char *) realloc (db->buffer,
125 db->buffer_fill + len + 1);
128 ERROR ("curl_xml plugin: realloc failed.");
132 db->buffer_size = db->buffer_fill + len + 1;
135 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
136 db->buffer_fill += len;
137 db->buffer[db->buffer_fill] = 0;
140 } /* }}} size_t cx_curl_callback */
142 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
149 sfree (xpath->instance_prefix);
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);
167 cx_xpath_free (le->value);
172 llist_destroy (list);
174 } /* }}} void cx_list_free */
176 static void cx_free (void *arg) /* {{{ */
181 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
188 if (db->curl != NULL)
189 curl_easy_cleanup (db->curl);
192 if (db->list != NULL)
193 cx_list_free (db->list);
196 sfree (db->instance);
202 sfree (db->credentials);
204 sfree (db->post_body);
205 curl_slist_free_all (db->headers);
207 for (i = 0; i < db->namespaces_num; i++)
209 sfree (db->namespaces[i].prefix);
210 sfree (db->namespaces[i].url);
212 sfree (db->namespaces);
215 } /* }}} void cx_free */
217 static int cx_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
220 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
222 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
226 *dest = curl_slist_append(*dest, ci->values[0].value.string);
231 } /* }}} int cx_config_append_string */
233 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)
243 WARNING ("curl_xml plugin: DataSet `%s' requires %zu values, but config talks about %zu",
244 xpath->type, ds->ds_num, xpath->values_len);
249 } /* }}} cx_check_type */
251 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
254 xmlXPathObjectPtr xpath_obj;
256 /* XXX: When to free this? */
257 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
258 if (xpath_obj == NULL)
260 WARNING ("curl_xml plugin: "
261 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
266 } /* }}} cx_evaluate_xpath */
268 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
270 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
271 node->type == XML_ELEMENT_NODE)
274 WARNING ("curl_xml plugin: "
275 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
277 } /* }}} cx_if_not_text_node */
279 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
281 const data_set_t *ds, value_list_t *vl, int index)
283 xmlXPathObjectPtr values_node_obj;
284 xmlNodeSetPtr values_node;
288 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
289 if (values_node_obj == NULL)
290 return (-1); /* Error already logged. */
292 values_node = values_node_obj->nodesetval;
293 tmp_size = (values_node) ? values_node->nodeNr : 0;
297 WARNING ("curl_xml plugin: "
298 "relative xpath expression \"%s\" doesn't match any of the nodes. "
299 "Skipping...", xpath->values[index].path);
300 xmlXPathFreeObject (values_node_obj);
306 WARNING ("curl_xml plugin: "
307 "relative xpath expression \"%s\" is expected to return "
308 "only one node. Skipping...", xpath->values[index].path);
309 xmlXPathFreeObject (values_node_obj);
313 /* ignoring the element if other than textnode/attribute*/
314 if (cx_if_not_text_node(values_node->nodeTab[0]))
316 WARNING ("curl_xml plugin: "
317 "relative xpath expression \"%s\" is expected to return "
318 "only text/attribute node which is not the case. Skipping...",
319 xpath->values[index].path);
320 xmlXPathFreeObject (values_node_obj);
324 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
325 switch (ds->ds[index].type)
327 case DS_TYPE_COUNTER:
328 vl->values[index].counter = (counter_t) strtoull (node_value,
329 /* endptr = */ NULL, /* base = */ 0);
332 vl->values[index].derive = (derive_t) strtoll (node_value,
333 /* endptr = */ NULL, /* base = */ 0);
335 case DS_TYPE_ABSOLUTE:
336 vl->values[index].absolute = (absolute_t) strtoull (node_value,
337 /* endptr = */ NULL, /* base = */ 0);
340 vl->values[index].gauge = (gauge_t) strtod (node_value,
341 /* endptr = */ NULL);
345 xmlXPathFreeObject (values_node_obj);
348 /* We have reached here which means that
349 * we have got something to work */
351 } /* }}} int cx_handle_single_value_xpath */
353 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
355 const data_set_t *ds, value_list_t *vl)
357 value_t values[xpath->values_len];
361 assert (xpath->values_len > 0);
362 assert (xpath->values_len == vl->values_len);
363 assert (xpath->values_len == ds->ds_num);
366 for (i = 0; i < xpath->values_len; i++)
368 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
370 return (-1); /* An error has been printed. */
371 } /* for (i = 0; i < xpath->values_len; i++) */
373 plugin_dispatch_values (vl);
377 } /* }}} int cx_handle_all_value_xpaths */
379 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
380 cx_xpath_t *xpath, value_list_t *vl,
383 xmlXPathObjectPtr instance_node_obj = NULL;
384 xmlNodeSetPtr instance_node = NULL;
386 memset (vl->type_instance, 0, sizeof (vl->type_instance));
388 /* If the base xpath returns more than one block, the result is assumed to be
389 * a table. The `Instance' option is not optional in this case. Check for the
390 * condition and inform the user. */
391 if (is_table && (xpath->instance == NULL))
393 WARNING ("curl_xml plugin: "
394 "Base-XPath %s is a table (more than one result was returned), "
395 "but no instance-XPath has been defined.",
400 /* instance has to be an xpath expression */
401 if (xpath->instance != NULL)
405 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
406 if (instance_node_obj == NULL)
407 return (-1); /* error is logged already */
409 instance_node = instance_node_obj->nodesetval;
410 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
414 WARNING ("curl_xml plugin: "
415 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
416 "any of the nodes. Skipping the node.", xpath->instance);
417 xmlXPathFreeObject (instance_node_obj);
423 WARNING ("curl_xml plugin: "
424 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
425 "to return only one text node. Skipping the node.", xpath->instance);
426 xmlXPathFreeObject (instance_node_obj);
430 /* ignoring the element if other than textnode/attribute */
431 if (cx_if_not_text_node(instance_node->nodeTab[0]))
433 WARNING ("curl_xml plugin: "
434 "relative xpath expression \"%s\" is expected to return only text node "
435 "which is not the case. Skipping the node.", xpath->instance);
436 xmlXPathFreeObject (instance_node_obj);
439 } /* if (xpath->instance != NULL) */
441 if (xpath->instance_prefix != NULL)
443 if (instance_node != NULL)
445 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
446 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
447 xpath->instance_prefix, node_value);
451 sstrncpy (vl->type_instance, xpath->instance_prefix,
452 sizeof (vl->type_instance));
456 /* If instance_prefix and instance_node are NULL, then
457 * don't set the type_instance */
458 if (instance_node != NULL)
460 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
461 sstrncpy (vl->type_instance, node_value, sizeof (vl->type_instance));
466 /* Free `instance_node_obj' this late, because `instance_node' points to
467 * somewhere inside this structure. */
468 xmlXPathFreeObject (instance_node_obj);
471 } /* }}} int cx_handle_instance_xpath */
473 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
475 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
476 char *base_xpath, cx_xpath_t *xpath)
481 xmlXPathObjectPtr base_node_obj = NULL;
482 xmlNodeSetPtr base_nodes = NULL;
484 value_list_t vl = VALUE_LIST_INIT;
486 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
487 if (base_node_obj == NULL)
488 return -1; /* error is logged already */
490 base_nodes = base_node_obj->nodesetval;
491 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
493 if (total_nodes == 0)
495 ERROR ("curl_xml plugin: "
496 "xpath expression \"%s\" doesn't match any of the nodes. "
497 "Skipping the xpath block...", base_xpath);
498 xmlXPathFreeObject (base_node_obj);
502 /* If base_xpath returned multiple results, then */
503 /* Instance in the xpath block is required */
504 if (total_nodes > 1 && xpath->instance == NULL)
506 ERROR ("curl_xml plugin: "
507 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
508 "returned multiple results. Skipping the xpath block...", base_xpath);
512 /* set the values for the value_list */
513 vl.values_len = ds->ds_num;
514 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
515 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
516 sstrncpy (vl.host, (host != NULL) ? host : hostname_g, sizeof (vl.host));
517 if (plugin_instance != NULL)
518 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
520 for (i = 0; i < total_nodes; i++)
524 xpath_ctx->node = base_nodes->nodeTab[i];
526 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
527 /* is_table = */ (total_nodes > 1));
529 continue; /* An error has already been reported. */
531 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
533 continue; /* An error has been logged. */
534 } /* for (i = 0; i < total_nodes; i++) */
536 /* free up the allocated memory */
537 xmlXPathFreeObject (base_node_obj);
540 } /* }}} cx_handle_base_xpath */
542 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
543 xmlXPathContextPtr xpath_ctx, cx_t *db)
546 const data_set_t *ds;
551 le = llist_head (db->list);
555 xpath = (cx_xpath_t *) le->value;
556 ds = plugin_get_ds (xpath->type);
558 if ( (cx_check_type(ds, xpath) == 0) &&
559 (cx_handle_base_xpath(db->instance, db->host,
560 xpath_ctx, ds, le->key, xpath) == 0) )
561 status = 0; /* we got atleast one success */
564 } /* while (le != NULL) */
567 } /* }}} cx_handle_parsed_xml */
569 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
573 xmlXPathContextPtr xpath_ctx;
577 doc = xmlParseDoc(xml);
580 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
584 xpath_ctx = xmlXPathNewContext(doc);
585 if(xpath_ctx == NULL)
587 ERROR ("curl_xml plugin: Failed to create the xml context");
592 for (i = 0; i < db->namespaces_num; i++)
594 cx_namespace_t const *ns = db->namespaces + i;
595 status = xmlXPathRegisterNs (xpath_ctx,
596 BAD_CAST ns->prefix, BAD_CAST ns->url);
599 ERROR ("curl_xml plugin: "
600 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
601 ns->prefix, ns->url);
602 xmlXPathFreeContext(xpath_ctx);
608 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
610 xmlXPathFreeContext(xpath_ctx);
613 } /* }}} cx_parse_stats_xml */
615 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
624 status = curl_easy_perform (curl);
625 if (status != CURLE_OK)
627 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
628 status, db->curl_errbuf, url);
632 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
633 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
635 /* The response code is zero if a non-HTTP transport was used. */
636 if ((rc != 0) && (rc != 200))
638 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
645 status = cx_parse_stats_xml(BAD_CAST ptr, db);
649 } /* }}} int cx_curl_perform */
651 static int cx_read (user_data_t *ud) /* {{{ */
655 if ((ud == NULL) || (ud->data == NULL))
657 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
661 db = (cx_t *) ud->data;
663 return cx_curl_perform (db, db->curl);
664 } /* }}} int cx_read */
666 /* Configuration handling functions {{{ */
668 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
673 if (ci->values_num < 1)
675 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
679 for (i = 0; i < ci->values_num; i++)
680 if (ci->values[i].type != OCONFIG_TYPE_STRING)
682 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
686 sfree (xpath->values);
688 xpath->values_len = 0;
689 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
690 if (xpath->values == NULL)
692 xpath->values_len = (size_t) ci->values_num;
694 /* populate cx_values_t structure */
695 for (i = 0; i < ci->values_num; i++)
697 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
698 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
702 } /* }}} cx_config_add_values */
704 static int cx_config_add_xpath (cx_t *db, /* {{{ */
711 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
714 ERROR ("curl_xml plugin: malloc failed.");
717 memset (xpath, 0, sizeof (*xpath));
719 status = cf_util_get_string (ci, &xpath->path);
726 /* error out if xpath->path is an empty string */
727 if (*xpath->path == 0)
729 ERROR ("curl_xml plugin: invalid xpath. "
730 "xpath value can't be an empty string");
735 for (i = 0; i < ci->children_num; i++)
737 oconfig_item_t *child = ci->children + i;
739 if (strcasecmp ("Type", child->key) == 0)
740 status = cf_util_get_string (child, &xpath->type);
741 else if (strcasecmp ("InstancePrefix", child->key) == 0)
742 status = cf_util_get_string (child, &xpath->instance_prefix);
743 else if (strcasecmp ("InstanceFrom", child->key) == 0)
744 status = cf_util_get_string (child, &xpath->instance);
745 else if (strcasecmp ("ValuesFrom", child->key) == 0)
746 status = cx_config_add_values ("ValuesFrom", xpath, child);
749 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
755 } /* for (i = 0; i < ci->children_num; i++) */
757 if (status == 0 && xpath->type == NULL)
759 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
768 if (db->list == NULL)
770 db->list = llist_create();
771 if (db->list == NULL)
773 ERROR ("curl_xml plugin: list creation failed.");
778 name = strdup(xpath->path);
781 ERROR ("curl_xml plugin: strdup failed.");
785 le = llentry_create (name, xpath);
788 ERROR ("curl_xml plugin: llentry_create failed.");
792 llist_append (db->list, le);
796 } /* }}} int cx_config_add_xpath */
798 static int cx_config_add_namespace (cx_t *db, /* {{{ */
803 if ((ci->values_num != 2)
804 || (ci->values[0].type != OCONFIG_TYPE_STRING)
805 || (ci->values[1].type != OCONFIG_TYPE_STRING))
807 WARNING ("curl_xml plugin: The `Namespace' option "
808 "needs exactly two string arguments.");
812 ns = realloc (db->namespaces, sizeof (*db->namespaces)
813 * (db->namespaces_num + 1));
816 ERROR ("curl_xml plugin: realloc failed.");
820 ns = db->namespaces + db->namespaces_num;
821 memset (ns, 0, sizeof (*ns));
823 ns->prefix = strdup (ci->values[0].value.string);
824 ns->url = strdup (ci->values[1].value.string);
826 if ((ns->prefix == NULL) || (ns->url == NULL))
830 ERROR ("curl_xml plugin: strdup failed.");
834 db->namespaces_num++;
836 } /* }}} int cx_config_add_namespace */
838 /* Initialize db->curl */
839 static int cx_init_curl (cx_t *db) /* {{{ */
841 db->curl = curl_easy_init ();
842 if (db->curl == NULL)
844 ERROR ("curl_xml plugin: curl_easy_init failed.");
848 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
849 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
850 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
851 curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
852 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
853 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
854 curl_easy_setopt (db->curl, CURLOPT_FOLLOWLOCATION, 1L);
855 curl_easy_setopt (db->curl, CURLOPT_MAXREDIRS, 50L);
857 if (db->user != NULL)
859 #ifdef HAVE_CURLOPT_USERNAME
860 curl_easy_setopt (db->curl, CURLOPT_USERNAME, db->user);
861 curl_easy_setopt (db->curl, CURLOPT_PASSWORD,
862 (db->pass == NULL) ? "" : db->pass);
864 size_t credentials_size;
866 credentials_size = strlen (db->user) + 2;
867 if (db->pass != NULL)
868 credentials_size += strlen (db->pass);
870 db->credentials = (char *) malloc (credentials_size);
871 if (db->credentials == NULL)
873 ERROR ("curl_xml plugin: malloc failed.");
877 ssnprintf (db->credentials, credentials_size, "%s:%s",
878 db->user, (db->pass == NULL) ? "" : db->pass);
879 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
883 curl_easy_setopt (db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
886 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
887 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
888 db->verify_host ? 2L : 0L);
889 if (db->cacert != NULL)
890 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
891 if (db->headers != NULL)
892 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
893 if (db->post_body != NULL)
894 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
896 #ifdef HAVE_CURLOPT_TIMEOUT_MS
897 if (db->timeout >= 0)
898 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout);
900 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS,
901 CDTIME_T_TO_MS(plugin_get_interval()));
905 } /* }}} int cx_init_curl */
907 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
913 if ((ci->values_num != 1)
914 || (ci->values[0].type != OCONFIG_TYPE_STRING))
916 WARNING ("curl_xml plugin: The `URL' block "
917 "needs exactly one string argument.");
921 db = (cx_t *) malloc (sizeof (*db));
924 ERROR ("curl_xml plugin: malloc failed.");
927 memset (db, 0, sizeof (*db));
931 if (strcasecmp ("URL", ci->key) == 0)
933 status = cf_util_get_string (ci, &db->url);
942 ERROR ("curl_xml plugin: cx_config: "
943 "Invalid key: %s", ci->key);
948 /* Fill the `cx_t' structure.. */
949 for (i = 0; i < ci->children_num; i++)
951 oconfig_item_t *child = ci->children + i;
953 if (strcasecmp ("Instance", child->key) == 0)
954 status = cf_util_get_string (child, &db->instance);
955 else if (strcasecmp ("Host", child->key) == 0)
956 status = cf_util_get_string (child, &db->host);
957 else if (strcasecmp ("User", child->key) == 0)
958 status = cf_util_get_string (child, &db->user);
959 else if (strcasecmp ("Password", child->key) == 0)
960 status = cf_util_get_string (child, &db->pass);
961 else if (strcasecmp ("Digest", child->key) == 0)
962 status = cf_util_get_boolean (child, &db->digest);
963 else if (strcasecmp ("VerifyPeer", child->key) == 0)
964 status = cf_util_get_boolean (child, &db->verify_peer);
965 else if (strcasecmp ("VerifyHost", child->key) == 0)
966 status = cf_util_get_boolean (child, &db->verify_host);
967 else if (strcasecmp ("CACert", child->key) == 0)
968 status = cf_util_get_string (child, &db->cacert);
969 else if (strcasecmp ("xpath", child->key) == 0)
970 status = cx_config_add_xpath (db, child);
971 else if (strcasecmp ("Header", child->key) == 0)
972 status = cx_config_append_string ("Header", &db->headers, child);
973 else if (strcasecmp ("Post", child->key) == 0)
974 status = cf_util_get_string (child, &db->post_body);
975 else if (strcasecmp ("Namespace", child->key) == 0)
976 status = cx_config_add_namespace (db, child);
977 else if (strcasecmp ("Timeout", child->key) == 0)
978 status = cf_util_get_int (child, &db->timeout);
981 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
991 if (db->list == NULL)
993 WARNING ("curl_xml plugin: No (valid) `Key' block "
994 "within `URL' block `%s'.", db->url);
998 status = cx_init_curl (db);
1001 /* If all went well, register this database for reading */
1007 if (db->instance == NULL)
1008 db->instance = strdup("default");
1010 DEBUG ("curl_xml plugin: Registering new read callback: %s",
1013 memset (&ud, 0, sizeof (ud));
1014 ud.data = (void *) db;
1015 ud.free_func = cx_free;
1017 cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url);
1018 plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read,
1019 /* interval = */ 0, &ud);
1029 } /* }}} int cx_config_add_url */
1031 /* }}} End of configuration handling functions */
1033 static int cx_config (oconfig_item_t *ci) /* {{{ */
1043 for (i = 0; i < ci->children_num; i++)
1045 oconfig_item_t *child = ci->children + i;
1047 if (strcasecmp ("URL", child->key) == 0)
1049 status = cx_config_add_url (child);
1057 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1062 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)
1081 plugin_register_complex_config ("curl_xml", cx_config);
1082 plugin_register_init ("curl_xml", cx_init);
1083 } /* void module_register */
1085 /* vim: set sw=2 sts=2 et fdm=marker : */