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;
84 struct curl_slist *headers;
86 cx_namespace_t *namespaces;
87 size_t namespaces_num;
90 char curl_errbuf[CURL_ERROR_SIZE];
95 llist_t *list; /* list of xpath blocks */
97 typedef struct cx_s cx_t; /* }}} */
102 static size_t cx_curl_callback (void *buf, /* {{{ */
103 size_t size, size_t nmemb, void *user_data)
105 size_t len = size * nmemb;
111 ERROR ("curl_xml plugin: cx_curl_callback: "
112 "user_data pointer is NULL.");
119 if ((db->buffer_fill + len) >= db->buffer_size)
123 temp = (char *) realloc (db->buffer,
124 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->instance);
150 sfree (xpath->values);
152 } /* }}} void cx_xpath_free */
154 static void cx_list_free (llist_t *list) /* {{{ */
158 le = llist_head (list);
166 cx_xpath_free (le->value);
171 llist_destroy (list);
173 } /* }}} void cx_list_free */
175 static void cx_free (void *arg) /* {{{ */
180 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
187 if (db->curl != NULL)
188 curl_easy_cleanup (db->curl);
191 if (db->list != NULL)
192 cx_list_free (db->list);
195 sfree (db->instance);
201 sfree (db->credentials);
203 sfree (db->post_body);
204 curl_slist_free_all (db->headers);
206 for (i = 0; i < db->namespaces_num; i++)
208 sfree (db->namespaces[i].prefix);
209 sfree (db->namespaces[i].url);
211 sfree (db->namespaces);
214 } /* }}} void cx_free */
216 static int cx_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
219 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
221 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
225 *dest = curl_slist_append(*dest, ci->values[0].value.string);
230 } /* }}} int cx_config_append_string */
232 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
236 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
240 if (ds->ds_num != xpath->values_len)
242 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
243 xpath->type, ds->ds_num, xpath->values_len);
248 } /* }}} cx_check_type */
250 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
253 xmlXPathObjectPtr xpath_obj;
255 /* XXX: When to free this? */
256 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
257 if (xpath_obj == NULL)
259 WARNING ("curl_xml plugin: "
260 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
265 } /* }}} cx_evaluate_xpath */
267 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
269 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
270 node->type == XML_ELEMENT_NODE)
273 WARNING ("curl_xml plugin: "
274 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
276 } /* }}} cx_if_not_text_node */
278 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
280 const data_set_t *ds, value_list_t *vl, int index)
282 xmlXPathObjectPtr values_node_obj;
283 xmlNodeSetPtr values_node;
287 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
288 if (values_node_obj == NULL)
289 return (-1); /* Error already logged. */
291 values_node = values_node_obj->nodesetval;
292 tmp_size = (values_node) ? values_node->nodeNr : 0;
296 WARNING ("curl_xml plugin: "
297 "relative xpath expression \"%s\" doesn't match any of the nodes. "
298 "Skipping...", xpath->values[index].path);
299 xmlXPathFreeObject (values_node_obj);
305 WARNING ("curl_xml plugin: "
306 "relative xpath expression \"%s\" is expected to return "
307 "only one node. Skipping...", xpath->values[index].path);
308 xmlXPathFreeObject (values_node_obj);
312 /* ignoring the element if other than textnode/attribute*/
313 if (cx_if_not_text_node(values_node->nodeTab[0]))
315 WARNING ("curl_xml plugin: "
316 "relative xpath expression \"%s\" is expected to return "
317 "only text/attribute node which is not the case. Skipping...",
318 xpath->values[index].path);
319 xmlXPathFreeObject (values_node_obj);
323 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
324 switch (ds->ds[index].type)
326 case DS_TYPE_COUNTER:
327 vl->values[index].counter = (counter_t) strtoull (node_value,
328 /* endptr = */ NULL, /* base = */ 0);
331 vl->values[index].derive = (derive_t) strtoll (node_value,
332 /* endptr = */ NULL, /* base = */ 0);
334 case DS_TYPE_ABSOLUTE:
335 vl->values[index].absolute = (absolute_t) strtoull (node_value,
336 /* endptr = */ NULL, /* base = */ 0);
339 vl->values[index].gauge = (gauge_t) strtod (node_value,
340 /* endptr = */ NULL);
344 xmlXPathFreeObject (values_node_obj);
346 /* We have reached here which means that
347 * we have got something to work */
349 } /* }}} int cx_handle_single_value_xpath */
351 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
353 const data_set_t *ds, value_list_t *vl)
355 value_t values[xpath->values_len];
359 assert (xpath->values_len > 0);
360 assert (xpath->values_len == vl->values_len);
361 assert (xpath->values_len == ds->ds_num);
364 for (i = 0; i < xpath->values_len; i++)
366 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
368 return (-1); /* An error has been printed. */
369 } /* for (i = 0; i < xpath->values_len; i++) */
371 plugin_dispatch_values (vl);
375 } /* }}} int cx_handle_all_value_xpaths */
377 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
378 cx_xpath_t *xpath, value_list_t *vl,
381 xmlXPathObjectPtr instance_node_obj = NULL;
382 xmlNodeSetPtr instance_node = NULL;
384 memset (vl->type_instance, 0, sizeof (vl->type_instance));
386 /* If the base xpath returns more than one block, the result is assumed to be
387 * a table. The `Instance' option is not optional in this case. Check for the
388 * condition and inform the user. */
391 WARNING ("curl_xml plugin: "
392 "Base-XPath %s is a table (more than one result was returned), "
393 "but no instance-XPath has been defined.",
398 /* instance has to be an xpath expression */
399 if (xpath->instance != NULL)
403 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
404 if (instance_node_obj == NULL)
405 return (-1); /* error is logged already */
407 instance_node = instance_node_obj->nodesetval;
408 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
412 WARNING ("curl_xml plugin: "
413 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
414 "any of the nodes. Skipping the node.", xpath->instance);
415 xmlXPathFreeObject (instance_node_obj);
421 WARNING ("curl_xml plugin: "
422 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
423 "to return only one text node. Skipping the node.", xpath->instance);
424 xmlXPathFreeObject (instance_node_obj);
428 /* ignoring the element if other than textnode/attribute */
429 if (cx_if_not_text_node(instance_node->nodeTab[0]))
431 WARNING ("curl_xml plugin: "
432 "relative xpath expression \"%s\" is expected to return only text node "
433 "which is not the case. Skipping the node.", xpath->instance);
434 xmlXPathFreeObject (instance_node_obj);
437 } /* if (xpath->instance != NULL) */
439 if (xpath->instance_prefix != NULL)
441 if (instance_node != NULL)
442 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
443 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
445 sstrncpy (vl->type_instance, xpath->instance_prefix,
446 sizeof (vl->type_instance));
450 /* If instance_prefix and instance_node are NULL, then
451 * don't set the type_instance */
452 if (instance_node != NULL)
453 sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
454 sizeof (vl->type_instance));
457 /* Free `instance_node_obj' this late, because `instance_node' points to
458 * somewhere inside this structure. */
459 xmlXPathFreeObject (instance_node_obj);
462 } /* }}} int cx_handle_instance_xpath */
464 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
466 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
467 char *base_xpath, cx_xpath_t *xpath)
472 xmlXPathObjectPtr base_node_obj = NULL;
473 xmlNodeSetPtr base_nodes = NULL;
475 value_list_t vl = VALUE_LIST_INIT;
477 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
478 if (base_node_obj == NULL)
479 return -1; /* error is logged already */
481 base_nodes = base_node_obj->nodesetval;
482 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
484 if (total_nodes == 0)
486 ERROR ("curl_xml plugin: "
487 "xpath expression \"%s\" doesn't match any of the nodes. "
488 "Skipping the xpath block...", base_xpath);
489 xmlXPathFreeObject (base_node_obj);
493 /* If base_xpath returned multiple results, then */
494 /* Instance in the xpath block is required */
495 if (total_nodes > 1 && xpath->instance == NULL)
497 ERROR ("curl_xml plugin: "
498 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
499 "returned multiple results. Skipping the xpath block...", base_xpath);
503 /* set the values for the value_list */
504 vl.values_len = ds->ds_num;
505 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
506 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
507 sstrncpy (vl.host, (host != NULL) ? host : hostname_g, sizeof (vl.host));
508 if (plugin_instance != NULL)
509 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
511 for (i = 0; i < total_nodes; i++)
515 xpath_ctx->node = base_nodes->nodeTab[i];
517 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
518 /* is_table = */ (total_nodes > 1));
520 continue; /* An error has already been reported. */
522 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
524 continue; /* An error has been logged. */
525 } /* for (i = 0; i < total_nodes; i++) */
527 /* free up the allocated memory */
528 xmlXPathFreeObject (base_node_obj);
531 } /* }}} cx_handle_base_xpath */
533 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
534 xmlXPathContextPtr xpath_ctx, cx_t *db)
537 const data_set_t *ds;
542 le = llist_head (db->list);
546 xpath = (cx_xpath_t *) le->value;
547 ds = plugin_get_ds (xpath->type);
549 if ( (cx_check_type(ds, xpath) == 0) &&
550 (cx_handle_base_xpath(db->instance, db->host,
551 xpath_ctx, ds, le->key, xpath) == 0) )
552 status = 0; /* we got atleast one success */
555 } /* while (le != NULL) */
558 } /* }}} cx_handle_parsed_xml */
560 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
564 xmlXPathContextPtr xpath_ctx;
568 doc = xmlParseDoc(xml);
571 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
575 xpath_ctx = xmlXPathNewContext(doc);
576 if(xpath_ctx == NULL)
578 ERROR ("curl_xml plugin: Failed to create the xml context");
583 for (i = 0; i < db->namespaces_num; i++)
585 cx_namespace_t const *ns = db->namespaces + i;
586 status = xmlXPathRegisterNs (xpath_ctx,
587 BAD_CAST ns->prefix, BAD_CAST ns->url);
590 ERROR ("curl_xml plugin: "
591 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
592 ns->prefix, ns->url);
593 xmlXPathFreeContext(xpath_ctx);
599 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
601 xmlXPathFreeContext(xpath_ctx);
604 } /* }}} cx_parse_stats_xml */
606 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
615 status = curl_easy_perform (curl);
616 if (status != CURLE_OK)
618 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
619 status, db->curl_errbuf, url);
623 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
624 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
626 /* The response code is zero if a non-HTTP transport was used. */
627 if ((rc != 0) && (rc != 200))
629 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
636 status = cx_parse_stats_xml(BAD_CAST ptr, db);
640 } /* }}} int cx_curl_perform */
642 static int cx_read (user_data_t *ud) /* {{{ */
646 if ((ud == NULL) || (ud->data == NULL))
648 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
652 db = (cx_t *) ud->data;
654 return cx_curl_perform (db, db->curl);
655 } /* }}} int cx_read */
657 /* Configuration handling functions {{{ */
659 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
664 if (ci->values_num < 1)
666 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
670 for (i = 0; i < ci->values_num; i++)
671 if (ci->values[i].type != OCONFIG_TYPE_STRING)
673 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
677 sfree (xpath->values);
679 xpath->values_len = 0;
680 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
681 if (xpath->values == NULL)
683 xpath->values_len = ci->values_num;
685 /* populate cx_values_t structure */
686 for (i = 0; i < ci->values_num; i++)
688 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
689 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
693 } /* }}} cx_config_add_values */
695 static int cx_config_add_xpath (cx_t *db, /* {{{ */
702 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
705 ERROR ("curl_xml plugin: malloc failed.");
708 memset (xpath, 0, sizeof (*xpath));
710 status = cf_util_get_string (ci, &xpath->path);
717 /* error out if xpath->path is an empty string */
718 if (*xpath->path == 0)
720 ERROR ("curl_xml plugin: invalid xpath. "
721 "xpath value can't be an empty string");
726 for (i = 0; i < ci->children_num; i++)
728 oconfig_item_t *child = ci->children + i;
730 if (strcasecmp ("Type", child->key) == 0)
731 status = cf_util_get_string (child, &xpath->type);
732 else if (strcasecmp ("InstancePrefix", child->key) == 0)
733 status = cf_util_get_string (child, &xpath->instance_prefix);
734 else if (strcasecmp ("InstanceFrom", child->key) == 0)
735 status = cf_util_get_string (child, &xpath->instance);
736 else if (strcasecmp ("ValuesFrom", child->key) == 0)
737 status = cx_config_add_values ("ValuesFrom", xpath, child);
740 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
746 } /* for (i = 0; i < ci->children_num; i++) */
748 if (status == 0 && xpath->type == NULL)
750 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
759 if (db->list == NULL)
761 db->list = llist_create();
762 if (db->list == NULL)
764 ERROR ("curl_xml plugin: list creation failed.");
769 name = strdup(xpath->path);
772 ERROR ("curl_xml plugin: strdup failed.");
776 le = llentry_create (name, xpath);
779 ERROR ("curl_xml plugin: llentry_create failed.");
783 llist_append (db->list, le);
787 } /* }}} int cx_config_add_xpath */
789 static int cx_config_add_namespace (cx_t *db, /* {{{ */
794 if ((ci->values_num != 2)
795 || (ci->values[0].type != OCONFIG_TYPE_STRING)
796 || (ci->values[1].type != OCONFIG_TYPE_STRING))
798 WARNING ("curl_xml plugin: The `Namespace' option "
799 "needs exactly two string arguments.");
803 ns = realloc (db->namespaces, sizeof (*db->namespaces)
804 * (db->namespaces_num + 1));
807 ERROR ("curl_xml plugin: realloc failed.");
811 ns = db->namespaces + db->namespaces_num;
812 memset (ns, 0, sizeof (*ns));
814 ns->prefix = strdup (ci->values[0].value.string);
815 ns->url = strdup (ci->values[1].value.string);
817 if ((ns->prefix == NULL) || (ns->url == NULL))
821 ERROR ("curl_xml plugin: strdup failed.");
825 db->namespaces_num++;
827 } /* }}} int cx_config_add_namespace */
829 /* Initialize db->curl */
830 static int cx_init_curl (cx_t *db) /* {{{ */
832 db->curl = curl_easy_init ();
833 if (db->curl == NULL)
835 ERROR ("curl_xml plugin: curl_easy_init failed.");
839 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
840 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
841 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
842 curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
843 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
844 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
846 if (db->user != NULL)
848 #ifdef HAVE_CURLOPT_USERNAME
849 curl_easy_setopt (db->curl, CURLOPT_USERNAME, db->user);
850 curl_easy_setopt (db->curl, CURLOPT_PASSWORD,
851 (db->pass == NULL) ? "" : db->pass);
853 size_t credentials_size;
855 credentials_size = strlen (db->user) + 2;
856 if (db->pass != NULL)
857 credentials_size += strlen (db->pass);
859 db->credentials = (char *) malloc (credentials_size);
860 if (db->credentials == NULL)
862 ERROR ("curl_xml plugin: malloc failed.");
866 ssnprintf (db->credentials, credentials_size, "%s:%s",
867 db->user, (db->pass == NULL) ? "" : db->pass);
868 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
872 curl_easy_setopt (db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
875 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
876 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
877 db->verify_host ? 2L : 0L);
878 if (db->cacert != NULL)
879 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
880 if (db->headers != NULL)
881 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
882 if (db->post_body != NULL)
883 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
886 } /* }}} int cx_init_curl */
888 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
894 if ((ci->values_num != 1)
895 || (ci->values[0].type != OCONFIG_TYPE_STRING))
897 WARNING ("curl_xml plugin: The `URL' block "
898 "needs exactly one string argument.");
902 db = (cx_t *) malloc (sizeof (*db));
905 ERROR ("curl_xml plugin: malloc failed.");
908 memset (db, 0, sizeof (*db));
910 if (strcasecmp ("URL", ci->key) == 0)
912 status = cf_util_get_string (ci, &db->url);
921 ERROR ("curl_xml plugin: cx_config: "
922 "Invalid key: %s", ci->key);
926 /* Fill the `cx_t' structure.. */
927 for (i = 0; i < ci->children_num; i++)
929 oconfig_item_t *child = ci->children + i;
931 if (strcasecmp ("Instance", child->key) == 0)
932 status = cf_util_get_string (child, &db->instance);
933 else if (strcasecmp ("Host", child->key) == 0)
934 status = cf_util_get_string (child, &db->host);
935 else if (strcasecmp ("User", child->key) == 0)
936 status = cf_util_get_string (child, &db->user);
937 else if (strcasecmp ("Password", child->key) == 0)
938 status = cf_util_get_string (child, &db->pass);
939 else if (strcasecmp ("Digest", child->key) == 0)
940 status = cf_util_get_boolean (child, &db->digest);
941 else if (strcasecmp ("VerifyPeer", child->key) == 0)
942 status = cf_util_get_boolean (child, &db->verify_peer);
943 else if (strcasecmp ("VerifyHost", child->key) == 0)
944 status = cf_util_get_boolean (child, &db->verify_host);
945 else if (strcasecmp ("CACert", child->key) == 0)
946 status = cf_util_get_string (child, &db->cacert);
947 else if (strcasecmp ("xpath", child->key) == 0)
948 status = cx_config_add_xpath (db, child);
949 else if (strcasecmp ("Header", child->key) == 0)
950 status = cx_config_append_string ("Header", &db->headers, child);
951 else if (strcasecmp ("Post", child->key) == 0)
952 status = cf_util_get_string (child, &db->post_body);
953 else if (strcasecmp ("Namespace", child->key) == 0)
954 status = cx_config_add_namespace (db, child);
957 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
967 if (db->list == NULL)
969 WARNING ("curl_xml plugin: No (valid) `Key' block "
970 "within `URL' block `%s'.", db->url);
974 status = cx_init_curl (db);
977 /* If all went well, register this database for reading */
983 if (db->instance == NULL)
984 db->instance = strdup("default");
986 DEBUG ("curl_xml plugin: Registering new read callback: %s",
989 memset (&ud, 0, sizeof (ud));
990 ud.data = (void *) db;
991 ud.free_func = cx_free;
993 cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url);
994 plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read,
995 /* interval = */ NULL, &ud);
1005 } /* }}} int cx_config_add_url */
1007 /* }}} End of configuration handling functions */
1009 static int cx_config (oconfig_item_t *ci) /* {{{ */
1019 for (i = 0; i < ci->children_num; i++)
1021 oconfig_item_t *child = ci->children + i;
1023 if (strcasecmp ("URL", child->key) == 0)
1025 status = cx_config_add_url (child);
1033 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1038 if ((success == 0) && (errors > 0))
1040 ERROR ("curl_xml plugin: All statements failed.");
1045 } /* }}} int cx_config */
1047 static int cx_init (void) /* {{{ */
1049 /* Call this while collectd is still single-threaded to avoid
1050 * initialization issues in libgcrypt. */
1051 curl_global_init (CURL_GLOBAL_SSL);
1053 } /* }}} int cx_init */
1055 void module_register (void)
1057 plugin_register_complex_config ("curl_xml", cx_config);
1058 plugin_register_init ("curl_xml", cx_init);
1059 } /* void module_register */
1061 /* vim: set sw=2 sts=2 et fdm=marker : */