Fix compile time issues
[collectd.git] / src / curl_xml.c
1 /**
2  * collectd - src/curl_xml.c
3  * Copyright (C) 2009,2010       Amit Gupta
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Amit Gupta <amit.gupta221 at gmail.com>
20  **/
21
22 #include "collectd.h"
23
24 #include "plugin.h"
25 #include "utils/common/common.h"
26 #include "utils/curl_stats/curl_stats.h"
27 #include "utils_llist.h"
28
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31 #include <libxml/xpath.h>
32 #include <libxml/xpathInternals.h>
33
34 #include <curl/curl.h>
35
36 #define CX_DEFAULT_HOST "localhost"
37
38 /*
39  * Private data structures
40  */
41 struct cx_values_s /* {{{ */
42 {
43   char path[DATA_MAX_NAME_LEN];
44   size_t path_len;
45 };
46 typedef struct cx_values_s cx_values_t;
47 /* }}} */
48
49 struct cx_xpath_s /* {{{ */
50 {
51   char *path;
52   char *type;
53   cx_values_t *values;
54   size_t values_len;
55   char *instance_prefix;
56   char *instance;
57   char *plugin_instance_from;
58   int is_table;
59   unsigned long magic;
60 };
61 typedef struct cx_xpath_s cx_xpath_t;
62 /* }}} */
63
64 struct cx_namespace_s /* {{{ */
65 {
66   char *prefix;
67   char *url;
68 };
69 typedef struct cx_namespace_s cx_namespace_t;
70 /* }}} */
71
72 struct cx_s /* {{{ */
73 {
74   char *instance;
75   char *plugin_name;
76   char *host;
77
78   char *url;
79   int address_family;
80   char *user;
81   char *pass;
82   char *credentials;
83   bool digest;
84   bool verify_peer;
85   bool verify_host;
86   char *cacert;
87   char *post_body;
88   int timeout;
89   struct curl_slist *headers;
90   curl_stats_t *stats;
91
92   cx_namespace_t *namespaces;
93   size_t namespaces_num;
94
95   CURL *curl;
96   char curl_errbuf[CURL_ERROR_SIZE];
97   char *buffer;
98   size_t buffer_size;
99   size_t buffer_fill;
100
101   llist_t *xpath_list; /* list of xpath blocks */
102 };
103 typedef struct cx_s cx_t; /* }}} */
104
105 /*
106  * Private functions
107  */
108 static size_t cx_curl_callback(void *buf, /* {{{ */
109                                size_t size, size_t nmemb, void *user_data) {
110   size_t len = size * nmemb;
111
112   cx_t *db = user_data;
113   if (db == NULL) {
114     ERROR("curl_xml plugin: cx_curl_callback: "
115           "user_data pointer is NULL.");
116     return 0;
117   }
118
119   if (len == 0)
120     return len;
121
122   if ((db->buffer_fill + len) >= db->buffer_size) {
123     char *temp = realloc(db->buffer, db->buffer_fill + len + 1);
124     if (temp == NULL) {
125       ERROR("curl_xml plugin: realloc failed.");
126       return 0;
127     }
128     db->buffer = temp;
129     db->buffer_size = db->buffer_fill + len + 1;
130   }
131
132   memcpy(db->buffer + db->buffer_fill, (char *)buf, len);
133   db->buffer_fill += len;
134   db->buffer[db->buffer_fill] = 0;
135
136   return len;
137 } /* }}} size_t cx_curl_callback */
138
139 static void cx_xpath_free(cx_xpath_t *xpath) /* {{{ */
140 {
141   if (xpath == NULL)
142     return;
143
144   sfree(xpath->path);
145   sfree(xpath->type);
146   sfree(xpath->instance_prefix);
147   sfree(xpath->plugin_instance_from);
148   sfree(xpath->instance);
149   sfree(xpath->values);
150   sfree(xpath);
151 } /* }}} void cx_xpath_free */
152
153 static void cx_xpath_list_free(llist_t *list) /* {{{ */
154 {
155   llentry_t *le;
156
157   le = llist_head(list);
158   while (le != NULL) {
159     llentry_t *le_next = le->next;
160
161     /* this also frees xpath->path used for le->key */
162     cx_xpath_free(le->value);
163
164     le = le_next;
165   }
166
167   llist_destroy(list);
168 } /* }}} void cx_xpath_list_free */
169
170 static void cx_free(void *arg) /* {{{ */
171 {
172   cx_t *db;
173
174   DEBUG("curl_xml plugin: cx_free (arg = %p);", arg);
175
176   db = (cx_t *)arg;
177
178   if (db == NULL)
179     return;
180
181   if (db->curl != NULL)
182     curl_easy_cleanup(db->curl);
183   db->curl = NULL;
184
185   if (db->xpath_list != NULL)
186     cx_xpath_list_free(db->xpath_list);
187
188   sfree(db->buffer);
189   sfree(db->instance);
190   sfree(db->plugin_name);
191   sfree(db->host);
192
193   sfree(db->url);
194   sfree(db->user);
195   sfree(db->pass);
196   sfree(db->credentials);
197   sfree(db->cacert);
198   sfree(db->post_body);
199   curl_slist_free_all(db->headers);
200   curl_stats_destroy(db->stats);
201
202   for (size_t i = 0; i < db->namespaces_num; i++) {
203     sfree(db->namespaces[i].prefix);
204     sfree(db->namespaces[i].url);
205   }
206   sfree(db->namespaces);
207
208   sfree(db);
209 } /* }}} void cx_free */
210
211 static const char *cx_host(const cx_t *db) /* {{{ */
212 {
213   if (db->host == NULL)
214     return hostname_g;
215   return db->host;
216 } /* }}} cx_host */
217
218 static int cx_config_append_string(const char *name,
219                                    struct curl_slist **dest, /* {{{ */
220                                    oconfig_item_t *ci) {
221   struct curl_slist *temp = NULL;
222   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
223     WARNING("curl_xml plugin: `%s' needs exactly one string argument.", name);
224     return -1;
225   }
226
227   temp = curl_slist_append(*dest, ci->values[0].value.string);
228   if (temp == NULL)
229     return -1;
230
231   *dest = temp;
232
233   return 0;
234 } /* }}} int cx_config_append_string */
235
236 static int cx_check_type(const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
237 {
238   if (!ds) {
239     WARNING("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
240     return -1;
241   }
242
243   if (ds->ds_num != xpath->values_len) {
244     WARNING("curl_xml plugin: DataSet `%s' requires %" PRIsz
245             " values, but config talks about %" PRIsz,
246             xpath->type, ds->ds_num, xpath->values_len);
247     return -1;
248   }
249
250   return 0;
251 } /* }}} cx_check_type */
252
253 static xmlXPathObjectPtr cx_evaluate_xpath(xmlXPathContextPtr xpath_ctx,
254                                            char *expr) /* {{{ */
255 {
256   xmlXPathObjectPtr xpath_obj =
257       xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
258   if (xpath_obj == NULL) {
259     WARNING("curl_xml plugin: "
260             "Error unable to evaluate xpath expression \"%s\". Skipping...",
261             expr);
262     return NULL;
263   }
264
265   return xpath_obj;
266 } /* }}} cx_evaluate_xpath */
267
268 static int cx_if_not_text_node(xmlNodePtr node) /* {{{ */
269 {
270   if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
271       node->type == XML_ELEMENT_NODE)
272     return 0;
273
274   WARNING("curl_xml plugin: "
275           "Node \"%s\" doesn't seem to be a text node. Skipping...",
276           node->name);
277   return -1;
278 } /* }}} cx_if_not_text_node */
279
280 /*
281  * Returned value should be freed with xmlFree().
282  */
283 static char *cx_get_text_node_value(xmlXPathContextPtr xpath_ctx, /* {{{ */
284                                     char *expr, const char *from_option) {
285   xmlXPathObjectPtr values_node_obj = cx_evaluate_xpath(xpath_ctx, expr);
286   if (values_node_obj == NULL)
287     return NULL; /* Error already logged. */
288
289   xmlNodeSetPtr values_node = values_node_obj->nodesetval;
290   size_t tmp_size = (values_node) ? values_node->nodeNr : 0;
291
292   if (tmp_size == 0) {
293     WARNING("curl_xml plugin: "
294             "relative xpath expression \"%s\" from '%s' doesn't match "
295             "any of the nodes.",
296             expr, from_option);
297     xmlXPathFreeObject(values_node_obj);
298     return NULL;
299   }
300
301   if (tmp_size > 1) {
302     WARNING("curl_xml plugin: "
303             "relative xpath expression \"%s\" from '%s' is expected to return "
304             "only one text node. Skipping the node.",
305             expr, from_option);
306     xmlXPathFreeObject(values_node_obj);
307     return NULL;
308   }
309
310   /* ignoring the element if other than textnode/attribute*/
311   if (cx_if_not_text_node(values_node->nodeTab[0])) {
312     WARNING("curl_xml plugin: "
313             "relative xpath expression \"%s\" from '%s' is expected to return "
314             "only text/attribute node which is not the case. "
315             "Skipping the node.",
316             expr, from_option);
317     xmlXPathFreeObject(values_node_obj);
318     return NULL;
319   }
320
321   char *node_value = (char *)xmlNodeGetContent(values_node->nodeTab[0]);
322
323   /* free up object */
324   xmlXPathFreeObject(values_node_obj);
325
326   return node_value;
327 } /* }}} char * cx_get_text_node_value */
328
329 static int cx_handle_single_value_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
330                                         cx_xpath_t *xpath, const data_set_t *ds,
331                                         value_list_t *vl, int index) {
332
333   char *node_value = cx_get_text_node_value(
334       xpath_ctx, xpath->values[index].path, "ValuesFrom");
335
336   if (node_value == NULL)
337     return -1;
338
339   switch (ds->ds[index].type) {
340   case DS_TYPE_COUNTER:
341     vl->values[index].counter =
342         (counter_t)strtoull(node_value,
343                             /* endptr = */ NULL, /* base = */ 0);
344     break;
345   case DS_TYPE_DERIVE:
346     vl->values[index].derive =
347         (derive_t)strtoll(node_value,
348                           /* endptr = */ NULL, /* base = */ 0);
349     break;
350   case DS_TYPE_ABSOLUTE:
351     vl->values[index].absolute =
352         (absolute_t)strtoull(node_value,
353                              /* endptr = */ NULL, /* base = */ 0);
354     break;
355   case DS_TYPE_GAUGE:
356     vl->values[index].gauge = (gauge_t)strtod(node_value,
357                                               /* endptr = */ NULL);
358   }
359
360   xmlFree(node_value);
361
362   /* We have reached here which means that
363    * we have got something to work */
364   return 0;
365 } /* }}} int cx_handle_single_value_xpath */
366
367 static int cx_handle_all_value_xpaths(xmlXPathContextPtr xpath_ctx, /* {{{ */
368                                       cx_xpath_t *xpath, const data_set_t *ds,
369                                       value_list_t *vl) {
370   value_t values[xpath->values_len];
371
372   assert(xpath->values_len > 0);
373   assert(xpath->values_len == vl->values_len);
374   assert(xpath->values_len == ds->ds_num);
375   vl->values = values;
376
377   for (size_t i = 0; i < xpath->values_len; i++) {
378     if (cx_handle_single_value_xpath(xpath_ctx, xpath, ds, vl, i) != 0)
379       return -1; /* An error has been printed. */
380   }              /* for (i = 0; i < xpath->values_len; i++) */
381
382   plugin_dispatch_values(vl);
383   vl->values = NULL;
384
385   return 0;
386 } /* }}} int cx_handle_all_value_xpaths */
387
388 static int cx_handle_instance_xpath(xmlXPathContextPtr xpath_ctx, /* {{{ */
389                                     cx_xpath_t *xpath, value_list_t *vl) {
390
391   /* Handle type instance */
392   if (xpath->instance != NULL) {
393     char *node_value =
394         cx_get_text_node_value(xpath_ctx, xpath->instance, "InstanceFrom");
395     if (node_value == NULL)
396       return -1;
397
398     if (xpath->instance_prefix != NULL)
399       snprintf(vl->type_instance, sizeof(vl->type_instance), "%s%s",
400                xpath->instance_prefix, node_value);
401     else
402       sstrncpy(vl->type_instance, node_value, sizeof(vl->type_instance));
403
404     xmlFree(node_value);
405   } else if (xpath->instance_prefix != NULL)
406     sstrncpy(vl->type_instance, xpath->instance_prefix,
407              sizeof(vl->type_instance));
408
409   /* Handle plugin instance */
410   if (xpath->plugin_instance_from != NULL) {
411     char *node_value = cx_get_text_node_value(
412         xpath_ctx, xpath->plugin_instance_from, "PluginInstanceFrom");
413
414     if (node_value == NULL)
415       return -1;
416
417     sstrncpy(vl->plugin_instance, node_value, sizeof(vl->plugin_instance));
418     xmlFree(node_value);
419   }
420
421   return 0;
422 } /* }}} int cx_handle_instance_xpath */
423
424 static int cx_handle_xpath(const cx_t *db, /* {{{ */
425                            xmlXPathContextPtr xpath_ctx, cx_xpath_t *xpath) {
426
427   const data_set_t *ds = plugin_get_ds(xpath->type);
428   if (cx_check_type(ds, xpath) != 0)
429     return -1;
430
431   xmlXPathObjectPtr base_node_obj = cx_evaluate_xpath(xpath_ctx, xpath->path);
432   if (base_node_obj == NULL)
433     return -1; /* error is logged already */
434
435   xmlNodeSetPtr base_nodes = base_node_obj->nodesetval;
436   int total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
437
438   if (total_nodes == 0) {
439     ERROR("curl_xml plugin: "
440           "xpath expression \"%s\" doesn't match any of the nodes. "
441           "Skipping the xpath block...",
442           xpath->path);
443     xmlXPathFreeObject(base_node_obj);
444     return -1;
445   }
446
447   /* If base_xpath returned multiple results, then */
448   /* InstanceFrom or PluginInstanceFrom in the xpath block is required */
449   if (total_nodes > 1 && xpath->instance == NULL &&
450       xpath->plugin_instance_from == NULL) {
451     ERROR("curl_xml plugin: "
452           "InstanceFrom or PluginInstanceFrom is must in xpath block "
453           "since the base xpath expression \"%s\" "
454           "returned multiple results. Skipping the xpath block...",
455           xpath->path);
456     xmlXPathFreeObject(base_node_obj);
457     return -1;
458   }
459
460   value_list_t vl = VALUE_LIST_INIT;
461
462   /* set the values for the value_list */
463   vl.values_len = ds->ds_num;
464   sstrncpy(vl.type, xpath->type, sizeof(vl.type));
465   sstrncpy(vl.plugin, (db->plugin_name != NULL) ? db->plugin_name : "curl_xml",
466            sizeof(vl.plugin));
467   sstrncpy(vl.host, cx_host(db), sizeof(vl.host));
468
469   for (int i = 0; i < total_nodes; i++) {
470     xpath_ctx->node = base_nodes->nodeTab[i];
471
472     if (db->instance != NULL)
473       sstrncpy(vl.plugin_instance, db->instance, sizeof(vl.plugin_instance));
474
475     if (cx_handle_instance_xpath(xpath_ctx, xpath, &vl) != 0)
476       continue; /* An error has already been reported. */
477
478     if (cx_handle_all_value_xpaths(xpath_ctx, xpath, ds, &vl) != 0)
479       continue; /* An error has been logged. */
480   }             /* for (i = 0; i < total_nodes; i++) */
481
482   /* free up the allocated memory */
483   xmlXPathFreeObject(base_node_obj);
484
485   return 0;
486 } /* }}} cx_handle_xpath */
487
488 static int cx_handle_parsed_xml(cx_t *db, xmlDocPtr doc, /* {{{ */
489                                 xmlXPathContextPtr xpath_ctx) {
490   int status = -1;
491
492   llentry_t *le = llist_head(db->xpath_list);
493   while (le != NULL) {
494     cx_xpath_t *xpath = (cx_xpath_t *)le->value;
495
496     if (cx_handle_xpath(db, xpath_ctx, xpath) == 0)
497       status = 0; /* we got atleast one success */
498
499     le = le->next;
500   } /* while (le != NULL) */
501
502   return status;
503 } /* }}} cx_handle_parsed_xml */
504
505 static int cx_parse_xml(cx_t *db, char *xml) /* {{{ */
506 {
507   /* Load the XML */
508   xmlDocPtr doc = xmlParseDoc(BAD_CAST xml);
509   if (doc == NULL) {
510     ERROR("curl_xml plugin: Failed to parse the xml document  - %s", xml);
511     return -1;
512   }
513
514   xmlXPathContextPtr xpath_ctx = xmlXPathNewContext(doc);
515   if (xpath_ctx == NULL) {
516     ERROR("curl_xml plugin: Failed to create the xml context");
517     xmlFreeDoc(doc);
518     return -1;
519   }
520
521   for (size_t i = 0; i < db->namespaces_num; i++) {
522     cx_namespace_t const *ns = db->namespaces + i;
523     int status =
524         xmlXPathRegisterNs(xpath_ctx, BAD_CAST ns->prefix, BAD_CAST ns->url);
525     if (status != 0) {
526       ERROR("curl_xml plugin: "
527             "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
528             ns->prefix, ns->url);
529       xmlXPathFreeContext(xpath_ctx);
530       xmlFreeDoc(doc);
531       return status;
532     }
533   }
534
535   int status = cx_handle_parsed_xml(db, doc, xpath_ctx);
536   /* Cleanup */
537   xmlXPathFreeContext(xpath_ctx);
538   xmlFreeDoc(doc);
539   return status;
540 } /* }}} cx_parse_xml */
541
542 static int cx_read(user_data_t *ud) /* {{{ */
543 {
544   if ((ud == NULL) || (ud->data == NULL)) {
545     ERROR("curl_xml plugin: cx_read: Invalid user data.");
546     return -1;
547   }
548
549   long rc;
550   char *url;
551   cx_t *db = (cx_t *)ud->data;
552
553   db->buffer_fill = 0;
554
555   curl_easy_setopt(db->curl, CURLOPT_URL, db->url);
556
557   int status = curl_easy_perform(db->curl);
558   if (status != CURLE_OK) {
559     ERROR("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
560           status, db->curl_errbuf, db->url);
561     return -1;
562   }
563   if (db->stats != NULL)
564     curl_stats_dispatch(db->stats, db->curl, cx_host(db), "curl_xml",
565                         db->instance);
566
567   curl_easy_getinfo(db->curl, CURLINFO_EFFECTIVE_URL, &url);
568   curl_easy_getinfo(db->curl, CURLINFO_RESPONSE_CODE, &rc);
569
570   /* The response code is zero if a non-HTTP transport was used. */
571   if ((rc != 0) && (rc != 200)) {
572     ERROR(
573         "curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
574         rc, url);
575     return -1;
576   }
577
578   status = cx_parse_xml(db, db->buffer);
579   db->buffer_fill = 0;
580
581   return status;
582 } /* }}} int cx_read */
583
584 /* Configuration handling functions {{{ */
585
586 static int cx_config_add_values(const char *name, cx_xpath_t *xpath, /* {{{ */
587                                 oconfig_item_t *ci) {
588   if (ci->values_num < 1) {
589     WARNING("curl_xml plugin: `ValuesFrom' needs at least one argument.");
590     return -1;
591   }
592
593   for (int i = 0; i < ci->values_num; i++)
594     if (ci->values[i].type != OCONFIG_TYPE_STRING) {
595       WARNING("curl_xml plugin: `ValuesFrom' needs only string argument.");
596       return -1;
597     }
598
599   sfree(xpath->values);
600
601   xpath->values_len = 0;
602   xpath->values = malloc(sizeof(cx_values_t) * ci->values_num);
603   if (xpath->values == NULL)
604     return -1;
605   xpath->values_len = (size_t)ci->values_num;
606
607   /* populate cx_values_t structure */
608   for (int i = 0; i < ci->values_num; i++) {
609     xpath->values[i].path_len = sizeof(ci->values[i].value.string);
610     sstrncpy(xpath->values[i].path, ci->values[i].value.string,
611              sizeof(xpath->values[i].path));
612   }
613
614   return 0;
615 } /* }}} cx_config_add_values */
616
617 static int cx_config_add_xpath(cx_t *db, oconfig_item_t *ci) /* {{{ */
618 {
619   cx_xpath_t *xpath = calloc(1, sizeof(*xpath));
620   if (xpath == NULL) {
621     ERROR("curl_xml plugin: calloc failed.");
622     return -1;
623   }
624
625   int status = cf_util_get_string(ci, &xpath->path);
626   if (status != 0) {
627     cx_xpath_free(xpath);
628     return status;
629   }
630
631   /* error out if xpath->path is an empty string */
632   if (strlen(xpath->path) == 0) {
633     ERROR("curl_xml plugin: invalid xpath. "
634           "xpath value can't be an empty string");
635     cx_xpath_free(xpath);
636     return -1;
637   }
638
639   status = 0;
640   for (int i = 0; i < ci->children_num; i++) {
641     oconfig_item_t *child = ci->children + i;
642
643     if (strcasecmp("Type", child->key) == 0)
644       status = cf_util_get_string(child, &xpath->type);
645     else if (strcasecmp("InstancePrefix", child->key) == 0)
646       status = cf_util_get_string(child, &xpath->instance_prefix);
647     else if (strcasecmp("InstanceFrom", child->key) == 0)
648       status = cf_util_get_string(child, &xpath->instance);
649     else if (strcasecmp("PluginInstanceFrom", child->key) == 0)
650       status = cf_util_get_string(child, &xpath->plugin_instance_from);
651     else if (strcasecmp("ValuesFrom", child->key) == 0)
652       status = cx_config_add_values("ValuesFrom", xpath, child);
653     else {
654       WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
655       status = -1;
656     }
657
658     if (status != 0)
659       break;
660   } /* for (i = 0; i < ci->children_num; i++) */
661
662   if (status != 0) {
663     cx_xpath_free(xpath);
664     return status;
665   }
666
667   if (xpath->type == NULL) {
668     WARNING("curl_xml plugin: `Type' missing in `xpath' block.");
669     cx_xpath_free(xpath);
670     return -1;
671   }
672
673   if (xpath->values_len == 0) {
674     WARNING("curl_xml plugin: `ValuesFrom' missing in `xpath' block.");
675     cx_xpath_free(xpath);
676     return -1;
677   }
678
679   llentry_t *le = llentry_create(xpath->path, xpath);
680   if (le == NULL) {
681     ERROR("curl_xml plugin: llentry_create failed.");
682     cx_xpath_free(xpath);
683     return -1;
684   }
685
686   llist_append(db->xpath_list, le);
687   return 0;
688 } /* }}} int cx_config_add_xpath */
689
690 static int cx_config_add_namespace(cx_t *db, /* {{{ */
691                                    oconfig_item_t *ci) {
692
693   if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
694       (ci->values[1].type != OCONFIG_TYPE_STRING)) {
695     WARNING("curl_xml plugin: The `Namespace' option "
696             "needs exactly two string arguments.");
697     return EINVAL;
698   }
699
700   cx_namespace_t *ns = realloc(db->namespaces, sizeof(*db->namespaces) *
701                                                    (db->namespaces_num + 1));
702   if (ns == NULL) {
703     ERROR("curl_xml plugin: realloc failed.");
704     return ENOMEM;
705   }
706   db->namespaces = ns;
707   ns = db->namespaces + db->namespaces_num;
708   memset(ns, 0, sizeof(*ns));
709
710   ns->prefix = strdup(ci->values[0].value.string);
711   ns->url = strdup(ci->values[1].value.string);
712
713   if ((ns->prefix == NULL) || (ns->url == NULL)) {
714     sfree(ns->prefix);
715     sfree(ns->url);
716     ERROR("curl_xml plugin: strdup failed.");
717     return ENOMEM;
718   }
719
720   db->namespaces_num++;
721   return 0;
722 } /* }}} int cx_config_add_namespace */
723
724 /* Initialize db->curl */
725 static int cx_init_curl(cx_t *db) /* {{{ */
726 {
727   db->curl = curl_easy_init();
728   if (db->curl == NULL) {
729     ERROR("curl_xml plugin: curl_easy_init failed.");
730     return -1;
731   }
732
733   curl_easy_setopt(db->curl, CURLOPT_NOSIGNAL, 1L);
734   curl_easy_setopt(db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
735   curl_easy_setopt(db->curl, CURLOPT_WRITEDATA, db);
736   curl_easy_setopt(db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
737   curl_easy_setopt(db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
738   curl_easy_setopt(db->curl, CURLOPT_FOLLOWLOCATION, 1L);
739   curl_easy_setopt(db->curl, CURLOPT_MAXREDIRS, 50L);
740   curl_easy_setopt(db->curl, CURLOPT_IPRESOLVE, db->address_family);
741
742   if (db->user != NULL) {
743 #ifdef HAVE_CURLOPT_USERNAME
744     curl_easy_setopt(db->curl, CURLOPT_USERNAME, db->user);
745     curl_easy_setopt(db->curl, CURLOPT_PASSWORD,
746                      (db->pass == NULL) ? "" : db->pass);
747 #else
748     size_t credentials_size;
749
750     credentials_size = strlen(db->user) + 2;
751     if (db->pass != NULL)
752       credentials_size += strlen(db->pass);
753
754     db->credentials = malloc(credentials_size);
755     if (db->credentials == NULL) {
756       ERROR("curl_xml plugin: malloc failed.");
757       return -1;
758     }
759
760     snprintf(db->credentials, credentials_size, "%s:%s", db->user,
761              (db->pass == NULL) ? "" : db->pass);
762     curl_easy_setopt(db->curl, CURLOPT_USERPWD, db->credentials);
763 #endif
764
765     if (db->digest)
766       curl_easy_setopt(db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
767   }
768
769   curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
770   curl_easy_setopt(db->curl, CURLOPT_SSL_VERIFYHOST, db->verify_host ? 2L : 0L);
771   if (db->cacert != NULL)
772     curl_easy_setopt(db->curl, CURLOPT_CAINFO, db->cacert);
773   if (db->headers != NULL)
774     curl_easy_setopt(db->curl, CURLOPT_HTTPHEADER, db->headers);
775   if (db->post_body != NULL)
776     curl_easy_setopt(db->curl, CURLOPT_POSTFIELDS, db->post_body);
777
778 #ifdef HAVE_CURLOPT_TIMEOUT_MS
779   if (db->timeout >= 0)
780     curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS, (long)db->timeout);
781   else
782     curl_easy_setopt(db->curl, CURLOPT_TIMEOUT_MS,
783                      (long)CDTIME_T_TO_MS(plugin_get_interval()));
784 #endif
785
786   return 0;
787 } /* }}} int cx_init_curl */
788
789 static int cx_config_add_url(oconfig_item_t *ci) /* {{{ */
790 {
791   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
792     WARNING("curl_xml plugin: The `URL' block "
793             "needs exactly one string argument.");
794     return -1;
795   }
796
797   cx_t *db = calloc(1, sizeof(*db));
798   if (db == NULL) {
799     ERROR("curl_xml plugin: calloc failed.");
800     return -1;
801   }
802
803   db->instance = strdup("default");
804   if (db->instance == NULL) {
805     ERROR("curl_xml plugin: strdup failed.");
806     sfree(db);
807     return -1;
808   }
809
810   db->xpath_list = llist_create();
811   if (db->xpath_list == NULL) {
812     ERROR("curl_xml plugin: list creation failed.");
813     sfree(db->instance);
814     sfree(db);
815     return -1;
816   }
817
818   db->timeout = -1;
819   db->address_family = CURL_IPRESOLVE_WHATEVER;
820
821   int status = cf_util_get_string(ci, &db->url);
822   if (status != 0) {
823     llist_destroy(db->xpath_list);
824     sfree(db->instance);
825     sfree(db);
826     return status;
827   }
828
829   cdtime_t interval = 0;
830
831   /* Fill the `cx_t' structure.. */
832   for (int i = 0; i < ci->children_num; i++) {
833     oconfig_item_t *child = ci->children + i;
834
835     if (strcasecmp("Instance", child->key) == 0)
836       status = cf_util_get_string(child, &db->instance);
837     else if (strcasecmp("Plugin", child->key) == 0)
838       status = cf_util_get_string(child, &db->plugin_name);
839     else if (strcasecmp("Host", child->key) == 0)
840       status = cf_util_get_string(child, &db->host);
841     else if (strcasecmp("User", child->key) == 0)
842       status = cf_util_get_string(child, &db->user);
843     else if (strcasecmp("Password", child->key) == 0)
844       status = cf_util_get_string(child, &db->pass);
845     else if (strcasecmp("Digest", child->key) == 0)
846       status = cf_util_get_boolean(child, &db->digest);
847     else if (strcasecmp("VerifyPeer", child->key) == 0)
848       status = cf_util_get_boolean(child, &db->verify_peer);
849     else if (strcasecmp("VerifyHost", child->key) == 0)
850       status = cf_util_get_boolean(child, &db->verify_host);
851     else if (strcasecmp("CACert", child->key) == 0)
852       status = cf_util_get_string(child, &db->cacert);
853     else if (strcasecmp("xpath", child->key) == 0)
854       status = cx_config_add_xpath(db, child);
855     else if (strcasecmp("Header", child->key) == 0)
856       status = cx_config_append_string("Header", &db->headers, child);
857     else if (strcasecmp("Post", child->key) == 0)
858       status = cf_util_get_string(child, &db->post_body);
859     else if (strcasecmp("Namespace", child->key) == 0)
860       status = cx_config_add_namespace(db, child);
861     else if (strcasecmp("Interval", child->key) == 0)
862       status = cf_util_get_cdtime(child, &interval);
863     else if (strcasecmp("Timeout", child->key) == 0)
864       status = cf_util_get_int(child, &db->timeout);
865     else if (strcasecmp("Statistics", child->key) == 0) {
866       db->stats = curl_stats_from_config(child);
867       if (db->stats == NULL)
868         status = -1;
869     } else if (strcasecmp("AddressFamily", child->key) == 0) {
870       char *af = NULL;
871       status = cf_util_get_string(child, &af);
872       if (status != 0 || af == NULL) {
873         WARNING("curl_xml plugin: Cannot parse value of `%s' for URL `%s'.",
874                 child->key, db->url);
875       } else if (strcasecmp("any", af) == 0) {
876         db->address_family = CURL_IPRESOLVE_WHATEVER;
877       } else if (strcasecmp("ipv4", af) == 0) {
878         db->address_family = CURL_IPRESOLVE_V4;
879       } else if (strcasecmp("ipv6", af) == 0) {
880         /* If curl supports ipv6, use it. If not, log a warning and
881          * fall back to default - don't set status to non-zero.
882          */
883         curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
884         if (curl_info->features & CURL_VERSION_IPV6)
885           db->address_family = CURL_IPRESOLVE_V6;
886         else
887           WARNING("curl_xml plugin: IPv6 not supported by this libCURL. "
888                   "Using fallback `any'.");
889       } else {
890         WARNING("curl_xml plugin: Unsupported value of `%s' for URL `%s'.",
891                 child->key, db->url);
892         status = -1;
893       }
894     } else {
895       WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
896       status = -1;
897     }
898
899     if (status != 0)
900       break;
901   }
902
903   if (status != 0) {
904     cx_free(db);
905     return status;
906   }
907
908   if (llist_size(db->xpath_list) == 0) {
909     WARNING("curl_xml plugin: No `xpath' block within `URL' block `%s'.",
910             db->url);
911     cx_free(db);
912     return -1;
913   }
914
915   if (cx_init_curl(db) != 0) {
916     cx_free(db);
917     return -1;
918   }
919
920   /* If all went well, register this database for reading */
921   DEBUG("curl_xml plugin: Registering new read callback: %s", db->instance);
922
923   char *cb_name = ssnprintf_alloc("curl_xml-%s-%s", db->instance, db->url);
924
925   plugin_register_complex_read(/* group = */ "curl_xml", cb_name, cx_read,
926                                /* interval = */ interval,
927                                &(user_data_t){
928                                    .data = db,
929                                    .free_func = cx_free,
930                                });
931   sfree(cb_name);
932   return 0;
933 } /* }}} int cx_config_add_url */
934
935 /* }}} End of configuration handling functions */
936
937 static int cx_config(oconfig_item_t *ci) /* {{{ */
938 {
939   int success = 0;
940   int errors = 0;
941
942   for (int i = 0; i < ci->children_num; i++) {
943     oconfig_item_t *child = ci->children + i;
944
945     if (strcasecmp("URL", child->key) == 0) {
946       if (cx_config_add_url(child) == 0)
947         success++;
948       else
949         errors++;
950     } else {
951       WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
952       errors++;
953     }
954   }
955
956   if ((success == 0) && (errors > 0)) {
957     ERROR("curl_xml plugin: All statements failed.");
958     return -1;
959   }
960
961   return 0;
962 } /* }}} int cx_config */
963
964 static int cx_init(void) /* {{{ */
965 {
966   /* Call this while collectd is still single-threaded to avoid
967    * initialization issues in libgcrypt. */
968   curl_global_init(CURL_GLOBAL_SSL);
969   return 0;
970 } /* }}} int cx_init */
971
972 void module_register(void) {
973   plugin_register_complex_config("curl_xml", cx_config);
974   plugin_register_init("curl_xml", cx_init);
975 } /* void module_register */